Ejemplo n.º 1
0
        private bool parseUserInput(Player i_CurrentPlayer, Player i_OtherPlayer)
        {
            bool      isValidMove = false;
            string    inputMove;
            bool      currentMoveIsJump = false;
            bool      gameIsOver        = false;
            bool      tryingToQuit      = false;
            bool      allowedToQuit     = false;
            bool      newKingWasCreated = false;
            Coin      currentCoin;
            ArrayList allPossibleJumps;

            m_PossibleMoves = new PossibleMoves(m_PlayingBoard);

            if (!this.m_CurrentPlayer.IsComputer)
            {
                inputMove = Console.ReadLine();

                while (!gameIsOver && (!isValidMove || (isValidMove && tryingToQuit)))
                {
                    tryingToQuit  = m_InputValidation.IsTryingToQuit(inputMove);
                    allowedToQuit = MovementValidation.IsAllowedToQuit(m_MatchInformation, i_CurrentPlayer, i_OtherPlayer);
                    if (tryingToQuit && !allowedToQuit)
                    {
                        Console.WriteLine(ErrorMessageGenerator.InvalidQuitMessage());
                    }

                    if (tryingToQuit && allowedToQuit)
                    {
                        if (quitHandler(this.m_CurrentPlayer.Name))
                        {
                            gameIsOver = true;
                        }
                        else
                        {
                            tryingToQuit = false;
                            inputMove    = Console.ReadLine();
                        }
                    }
                    else
                    {
                        isValidMove = m_InputValidation.InputFormatIsValid(inputMove);
                        if (isValidMove)
                        {
                            this.m_CurrentMove = new PlayerMove(inputMove);
                            string errorMessage = MovementValidation.IsLegalMovement(this.m_CurrentMove, m_PlayingBoard);
                            if (!errorMessage.Equals(string.Empty))
                            {
                                isValidMove = false;
                                Console.WriteLine(errorMessage);
                                inputMove = Console.ReadLine();
                            }
                            else
                            {
                                currentCoin = m_PlayingBoard.BoardArray[this.m_CurrentMove.CurrentRowIndex, this.m_CurrentMove.CurrentColIndex];
                                if (!MovementValidation.IsCoinBelongToPlayer(this.m_CurrentPlayer, currentCoin))
                                {
                                    Console.WriteLine(ErrorMessageGenerator.TryingToMoveOpponentsCoin());
                                    inputMove = Console.ReadLine();
                                }
                            }
                        }
                        else
                        {
                            Console.WriteLine(ErrorMessageGenerator.FormatErrorMessage());
                            inputMove = Console.ReadLine();
                        }
                    }
                }

                this.m_CurrentMove = new PlayerMove(inputMove);
            }
            else
            {
                this.m_CurrentMove = RandomMovementGenerator.getRandomMove(m_PossibleMoves.SecondPlayerPossibleMoves);
            }

            Coin[,] boardArray = m_PlayingBoard.BoardArray;
            currentCoin        = m_PlayingBoard.BoardArray[this.m_CurrentMove.CurrentRowIndex, this.m_CurrentMove.CurrentColIndex];

            if (!gameIsOver)
            {
                char currentUserCoinType = currentCoin.Type;

                m_PlayingBoard.MoveCoinInBoard(this.m_CurrentMove);

                if (shouldBeKinged())
                {
                    currentCoin.IsKing = true;
                    newKingWasCreated  = true;
                }

                currentMoveIsJump = currentCoin.IsKing == false?MovementValidation.IsTryingToJump(this.m_CurrentMove, currentUserCoinType) : KingValidation.isTryingToJump_King(this.m_CurrentMove);

                if (currentMoveIsJump)
                {
                    m_PlayingBoard.EatCoin(this.m_CurrentMove);

                    m_PossibleMoves  = new PossibleMoves(m_PlayingBoard);
                    allPossibleJumps = m_PossibleMoves.getAllPossibleJumps(this.m_CurrentMove, currentUserCoinType);

                    while (allPossibleJumps.Count != 0 && !newKingWasCreated)
                    {
                        Ex02.ConsoleUtils.Screen.Clear();
                        m_PlayingBoard.printBoard();

                        if (!this.m_CurrentPlayer.IsComputer)
                        {
                            Console.WriteLine(this.m_CurrentPlayer.Name + ", you can eat more. Please enter another move.");
                            inputMove   = Console.ReadLine();
                            isValidMove = m_InputValidation.InputFormatIsValid(inputMove);
                            while (!isValidMove)
                            {
                                Console.WriteLine(this.m_CurrentPlayer.Name + ", the input is invalid. enter a good move please");
                                inputMove          = Console.ReadLine();
                                isValidMove        = m_InputValidation.InputFormatIsValid(inputMove);
                                this.m_CurrentMove = new PlayerMove(inputMove);
                            }

                            this.m_CurrentMove = new PlayerMove(inputMove);

                            if (!PossibleMoves.isJumpPossible(allPossibleJumps, this.m_CurrentMove))
                            {
                                Console.WriteLine(this.m_CurrentPlayer.Name + ", the Move is not a valid jump. enter a good jump please");
                                isValidMove = false;
                            }
                        }
                        else
                        {
                            this.m_CurrentMove = RandomMovementGenerator.getRandomMove(allPossibleJumps);
                        }

                        m_PlayingBoard.EatCoin(this.m_CurrentMove);
                        m_PlayingBoard.MoveCoinInBoard(this.m_CurrentMove);
                        m_PossibleMoves  = new PossibleMoves(m_PlayingBoard);
                        allPossibleJumps = m_PossibleMoves.getAllPossibleJumps(this.m_CurrentMove, currentUserCoinType);
                    }
                }
            }

            if (shouldBeKinged())
            {
                currentCoin.IsKing = true;
                newKingWasCreated  = true;
            }

            Ex02.ConsoleUtils.Screen.Clear();
            if (!gameIsOver)
            {
                this.m_MatchInformation = new MatchInformation(this.m_FirstPlayer, this.m_SecondPlayer, m_PlayingBoard);
                gameIsOver = this.m_MatchInformation.IsWinnerFound();
                if (gameIsOver)
                {
                    endMatch();
                }
                else
                {
                    m_PlayingBoard.printBoard();
                    Console.WriteLine(this.m_CurrentPlayer.Name + "'s move was: " + this.m_CurrentMove.GetFullMove());
                    Console.WriteLine(i_OtherPlayer.Name + "'s Turn (" + i_OtherPlayer.CoinType + ") :");
                }
            }

            return(gameIsOver);
        }