Ejemplo n.º 1
0
        public static bool IsAllowedToQuit(MatchInformation i_MatchInformation, Player i_CurrentPlayer, Player i_OtherPlayer)
        {
            int currentPlayerPoints = i_MatchInformation.PlayerPoints(i_CurrentPlayer);
            int otherPlayerPoints   = i_MatchInformation.PlayerPoints(i_OtherPlayer);

            return(currentPlayerPoints <= otherPlayerPoints);
        }
Ejemplo n.º 2
0
        public void initializeMatch()
        {
            string numOfPlayers;

            m_InputValidation = new InputValidation();

            this.m_FirstPlayer = new Player(m_InputValidation.getInputNameFromUser(), Constants.k_FirstCoinType);
            m_PlayingBoard     = new Board(int.Parse(m_InputValidation.GetBoardSizeFromUser()));
            numOfPlayers       = m_InputValidation.GetNumOfPlayersFromUser();

            if (int.Parse(numOfPlayers) == 2)
            {
                m_SecondPlayer = new Player(m_InputValidation.getInputNameFromUser(), Constants.k_SecondCoinType);
            }
            else
            {
                m_SecondPlayer = new Player();
            }

            m_CurrentPlayer = m_FirstPlayer;
            m_PlayingBoard.printBoard();
            m_PossibleMoves    = new PossibleMoves(m_PlayingBoard);
            m_MatchInformation = new MatchInformation(m_FirstPlayer, m_SecondPlayer, m_PlayingBoard);

            matchManager(m_PlayingBoard);

            Console.ReadLine();
            Console.WriteLine("Press enter to close terminal");
        }
Ejemplo n.º 3
0
 public GameManager(int i_BoardSize, string i_FirstPlayerName, string i_SecondPlayerName)
 {
     m_FirstPlayer      = new Player(i_FirstPlayerName, 'O');
     m_SecondPlayer     = new Player(i_SecondPlayerName, 'X');
     m_PlayingBoard     = new Board(i_BoardSize);
     m_MatchInformation = new MatchInformation(m_FirstPlayer, m_SecondPlayer, m_PlayingBoard);
     m_CurrentPlayer    = m_FirstPlayer;
     m_PossibleMoves    = new PossibleMoves(m_PlayingBoard);
     m_InputValidation  = new InputValidation();
 }
Ejemplo n.º 4
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);
        }