コード例 #1
0
        public string GetBoardSizeFromUser()
        {
            Console.WriteLine("Please enter a valid board size (6,8,10):");
            string boardSize = Console.ReadLine();

            while (!ValidateBoardSizeInput(boardSize))
            {
                Console.WriteLine(ErrorMessageGenerator.BoardSizeErrorMessage());
                boardSize = Console.ReadLine();
            }

            return(boardSize);
        }
コード例 #2
0
        public static string IsLegalMovement(PlayerMove currentMove, Board i_Board)
        {
            bool   isValidMovement = true;
            Square sourceSquare;
            Square destinationSquare;
            char   currentUserCoinType;
            char   otherUserCoinType;
            bool   tryingToJump;

            Coin[,] boardArray = i_Board.BoardArray;
            sourceSquare       = currentMove.CurrentSquare;
            destinationSquare  = currentMove.NextSquare;
            string errorMessage = string.Empty;

            if (!movementIndexesInRange(currentMove, i_Board.BoardSize))
            {
                isValidMovement = false;
                errorMessage    = ErrorMessageGenerator.OutOfBoundMessage();
            }
            else
            {
                Coin currentCoin = boardArray[sourceSquare.RowIndex, sourceSquare.ColumnIndex];
                if (i_Board.IsEmptyAtSquare(sourceSquare))
                {
                    isValidMovement = false;
                    errorMessage    = ErrorMessageGenerator.NoCoinToMoveMessage();
                }

                if (isValidMovement)
                {
                    currentUserCoinType = currentCoin.Type;
                    otherUserCoinType   = currentUserCoinType == Constants.k_FirstCoinType ? Constants.k_SecondCoinType : Constants.k_FirstCoinType;

                    if (!currentCoin.Type.Equals(currentUserCoinType))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.NoCoinToMoveMessage();
                    }
                    else if (currentCoin.IsKing)
                    {
                        tryingToJump = KingValidation.isTryingToJump_King(currentMove);
                        if (tryingToJump)
                        {
                            isValidMovement = IsValidJump(currentMove, otherUserCoinType, i_Board);
                            if (!isValidMovement)
                            {
                                errorMessage = ErrorMessageGenerator.InvalidJumpMessage();
                            }
                        }
                        else
                        {
                            if (!i_Board.IsEmptyAtSquare(destinationSquare))
                            {
                                isValidMovement = false;
                                errorMessage    = ErrorMessageGenerator.SquareIsBlockedMessage();
                            }
                            else
                            {
                                isValidMovement = KingValidation.isDiagonal_King(currentMove);
                                if (!isValidMovement)
                                {
                                    errorMessage = ErrorMessageGenerator.NotDiagonalMessage();
                                }
                            }
                        }
                    }
                    else if (IsTryingToJump(currentMove, currentUserCoinType))
                    {
                        if (!IsValidJump(currentMove, otherUserCoinType, i_Board))
                        {
                            errorMessage = ErrorMessageGenerator.InvalidJumpMessage();
                        }
                    }
                    else if (!i_Board.IsEmptyAtSquare(destinationSquare))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.SquareIsBlockedMessage();
                    }
                    else if (!IsDiagonal(currentMove, currentUserCoinType))
                    {
                        isValidMovement = false;
                        errorMessage    = ErrorMessageGenerator.NotDiagonalMessage();
                    }
                }
            }

            return(errorMessage);
        }
コード例 #3
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);
        }