예제 #1
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");
        }
예제 #2
0
        public List <PlayerMove> getAllowedMoves(string i_CurrentSquare)
        {
            List <PlayerMove> currentAllowedMoves = new List <PlayerMove>();

            m_PossibleMoves = new PossibleMoves(m_PlayingBoard);
            ArrayList allPossibleJumps;
            Square    currentSquare = new Square(i_CurrentSquare[0], i_CurrentSquare[1]);

            if (m_CurrentPlayer == m_FirstPlayer)
            {
                if (m_ThereAreMoreJumps)
                {
                    allPossibleJumps = m_PossibleMoves.getAllPossibleJumps(currentSquare, m_CurrentPlayer.CoinType);
                    foreach (PlayerMove playerMove in allPossibleJumps)
                    {
                        if (playerMove.CurrentSquare.getSquare() == i_CurrentSquare)
                        {
                            currentAllowedMoves.Add(playerMove);
                        }
                    }
                }
                else
                {
                    foreach (PlayerMove playerMove in m_PossibleMoves.FirstPlayerPossibleMoves)
                    {
                        if (playerMove.CurrentSquare.getSquare() == i_CurrentSquare)
                        {
                            currentAllowedMoves.Add(playerMove);
                        }
                    }
                }
            }
            else
            {
                if (m_ThereAreMoreJumps)
                {
                    allPossibleJumps = m_PossibleMoves.getAllPossibleJumps(currentSquare, m_CurrentPlayer.CoinType);
                    foreach (PlayerMove playerMove in allPossibleJumps)
                    {
                        if (playerMove.CurrentSquare.getSquare() == i_CurrentSquare)
                        {
                            currentAllowedMoves.Add(playerMove);
                        }
                    }
                }
                else
                {
                    foreach (PlayerMove playerMove in m_PossibleMoves.SecondPlayerPossibleMoves)
                    {
                        if (playerMove.CurrentSquare.getSquare() == i_CurrentSquare)
                        {
                            currentAllowedMoves.Add(playerMove);
                        }
                    }
                }
            }

            return(currentAllowedMoves);
        }
예제 #3
0
        private void startAnotherMatch()
        {
            int boardSize = m_PlayingBoard.BoardSize;

            m_PlayingBoard = new Board(boardSize);
            m_PlayingBoard.printBoard();
            m_PossibleMoves = new PossibleMoves(m_PlayingBoard);
            matchManager(m_PlayingBoard);
        }
예제 #4
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();
 }
예제 #5
0
        public void startAnotherMatch()
        {
            m_GameIsOver = false;
            int boardSize = m_PlayingBoard.BoardSize;

            m_PlayingBoard    = new Board(boardSize);
            m_CurrentPlayer   = m_FirstPlayer;
            m_PossibleMoves   = new PossibleMoves(m_PlayingBoard);
            m_InputValidation = new InputValidation();
        }
예제 #6
0
        private void calculatePoints(Board i_PlayingBoard)
        {
            PossibleMoves allPossibleMoves          = new PossibleMoves(i_PlayingBoard);
            ArrayList     firstPlayerPossibleMoves  = allPossibleMoves.FirstPlayerPossibleMoves;
            ArrayList     secondPlayerPossibleMoves = allPossibleMoves.SecondPlayerPossibleMoves;
            int           totalMovesFirstPlayer     = firstPlayerPossibleMoves.Count;
            int           totalMovesSecondPlayer    = secondPlayerPossibleMoves.Count;

            this.m_FirstPlayerCurrentPoints  = calcUserPoints(m_FirstPlayer, i_PlayingBoard);
            this.m_SecondPlayerCurrentPoints = calcUserPoints(m_SecondPlayer, i_PlayingBoard);

            if (totalMovesFirstPlayer == 0 && totalMovesSecondPlayer == 0)
            {
                m_WinnerIsFound = true;
            }
            else if (totalMovesFirstPlayer == 0)
            {
                m_WinnerIsFound = true;
            }
            else if (totalMovesSecondPlayer == 0)
            {
                m_WinnerIsFound = true;
            }
            else
            {
                ArrayList firstUserCoins  = i_PlayingBoard.GetUserCoins(this.m_FirstPlayer.CoinType);
                ArrayList secondUserCoins = i_PlayingBoard.GetUserCoins(this.m_SecondPlayer.CoinType);

                if (firstUserCoins.Count == 0)
                {
                    m_WinnerIsFound = true;
                }
                else if (secondUserCoins.Count == 0)
                {
                    m_WinnerIsFound = true;
                }
            }

            if (m_WinnerIsFound)
            {
                SetWinner();
            }
        }
예제 #7
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);
        }