private void updateWinStreak(Gameboard.PlayerPiece winningPlayer)
        {
            switch (winningPlayer)
            {
            case Gameboard.PlayerPiece.X:
                if (_winningPlayer == Gameboard.PlayerPiece.X)
                {
                    _winStreak++;
                }
                else
                {
                    _winStreak = 1;
                }
                break;

            case Gameboard.PlayerPiece.O:
                if (_winningPlayer == Gameboard.PlayerPiece.O)
                {
                    _winStreak++;
                }
                else
                {
                    _winStreak = 1;
                }
                break;

            case Gameboard.PlayerPiece.None:
                _winStreak = 0;
                break;

            default:
                throw new InvalidOperationException("Specified incorrect player piece.");
            }
        }
예제 #2
0
        /// <summary>
        /// Attempt to get a valid player move.
        /// If the player chooses a location that is taken, the CurrentRoundState remains unchanged,
        /// the player is given a message indicating so, and the game loop is cycled to allow the player
        /// to make a new choice.
        /// </summary>
        /// <param name="currentPlayerPiece">identify as either the X or O player</param>
        private void ManagePlayerTurn(Gameboard.PlayerPiece currentPlayerPiece)
        {
            int column = _gameView.PlayerCoordinateChoice();

            while (column == _gameboard.HELP_CODE)
            {
                _gameView.DisplayGameRules();
                _gameView.DisplayGameArea();
                column = _gameView.PlayerCoordinateChoice();
            }

            if (column == _gameboard.EXIT_ROUND_CODE)
            {
                _numberOfCatsGames++;
                _playingRound = false;
                _gameView.DisplayCurrentGameStatus(_roundNumber, _playerXNumberOfWins, _playerONumberOfWins, _numberOfCatsGames);
                return;
            }

            //
            // player chose an open position on the game board, add it to the game board
            //
            if (_gameboard.GameboardColumnAvailable(column - 1))
            {
                _gameboard.SetPlayerPiece(column, currentPlayerPiece);

                //
                // Evaluate and update the current game board state
                //
                _gameboard.UpdateGameboardState(column - 1, applause);
            }
        }
        /// <summary>
        /// Attempt to get a valid player move.
        /// If the player chooses a location that is taken, the CurrentRoundState remains unchanged,
        /// the player is given a message indicating so, and the game loop is cycled to allow the player
        /// to make a new choice.
        /// </summary>
        /// <param name="currentPlayerPiece">identify as either the X or O player</param>
        private void ManagePlayerTurn(Gameboard.PlayerPiece currentPlayerPiece)
        {
            GameboardPosition gameboardPosition = _gameView.GetPlayerPositionChoice();

            if (_gameView.CurrentViewState != ConsoleView.ViewState.PlayerUsedMaxAttempts)
            {
                try
                {
                    //
                    // player chose an open position on the game board, add it to the game board
                    //
                    if (_gameboard.GameboardPositionAvailable(gameboardPosition))
                    {
                        _gameboard.SetPlayerPiece(gameboardPosition, currentPlayerPiece);
                    }
                    //
                    // player chose a taken position on the game board
                    //
                    else
                    {
                        _gameView.DisplayGamePositionChoiceNotAvailableScreen();
                    }
                }
                catch (Gameboard.GamePositionException pe)
                {
                    _gameView.DisplayGamePositionChoiceNotAvailableScreen();
                    Console.WriteLine(pe.Message);
                }
            }
        }
        /// <summary>
        /// Attempt to get a valid player move.
        /// If the player chooses a location that is taken, the CurrentRoundState remains unchanged,
        /// the player is given a message indicating so, and the game loop is cycled to allow the player
        /// to make a new choice.
        /// </summary>
        /// <param name="currentPlayerPiece">identify as either the X or O player</param>
        private void ManagePlayerTurn(Gameboard.PlayerPiece currentPlayerPiece)
        {
            if (_gameView.CurrentViewState != ConsoleView.ViewState.PlayerQuit)
            {
                GameboardPosition gameboardPosition = _gameView.GetPlayerPositionChoice();

                if (_gameView.CurrentViewState == ConsoleView.ViewState.Active)
                {
                    //
                    // player chose an open position on the game board, add it to the game board
                    //
                    if (_gameboard.GameboardPositionAvailable(gameboardPosition))
                    {
                        _gameboard.SetPlayerPiece(gameboardPosition, currentPlayerPiece);
                    }
                    //
                    // player chose a taken position on the game board
                    //
                    else
                    {
                        _gameView.DisplayGamePositionChoiceNotAvailableScreen();
                    }
                }
            }
        }
        /// <summary>
        /// Initialize the multi-round game.
        /// </summary>
        public void InitializeGame()
        {
            //
            // Initialize game variables
            //
            _playingGame         = true;
            _playingRound        = true;
            _roundNumber         = 0;
            _playerONumberOfWins = 0;
            _playerXNumberOfWins = 0;
            _numberOfCatsGames   = 0;
            _winningPlayer       = Gameboard.PlayerPiece.None;

            //
            // Initialize game board status
            //
            _gameboard.InitializeGameboard();
        }
        /// <summary>
        /// Attempt to get a valid player move.
        /// If the player chooses a location that is taken, the CurrentRoundState remains unchanged,
        /// the player is given a message indicating so, and the game loop is cycled to allow the player
        /// to make a new choice.
        /// </summary>
        /// <param name="currentPlayerPiece">identify as either the X or O player</param>
        private void ManagePlayerTurn(Gameboard.PlayerPiece currentPlayerPiece)
        {
            GameboardPosition gameboardPosition = _gameView.GetPlayerPositionChoice();

            if (_gameView.CurrentViewState == ConsoleView.ViewState.ViewCurrentStats)
            {
                _gameView.DisplayCurrentGameStatus(_roundNumber, _playerXNumberOfWins, _playerONumberOfWins, _numberOfCatsGames);
                _gameView.CurrentViewState = ConsoleView.ViewState.Active;
                _gameView.DisplayGameArea();
                gameboardPosition = _gameView.GetPlayerPositionChoice();
            }

            if (_gameView.CurrentViewState != ConsoleView.ViewState.ResetCurrentRound)
            {
                //
                //Proceed with turn as normal.
                //
                if (_gameView.CurrentViewState != ConsoleView.ViewState.PlayerUsedMaxAttempts)
                {
                    //
                    // player chose an open position on the game board, add it to the game board
                    //
                    if (_gameboard.GameboardPositionAvailable(gameboardPosition))
                    {
                        _gameboard.SetPlayerPiece(gameboardPosition, currentPlayerPiece);
                    }
                    //
                    // player chose a taken position on the game board
                    //
                    else
                    {
                        _gameView.DisplayGamePositionChoiceNotAvailableScreen();
                    }
                }
            }
        }
        /// <summary>
        /// Game Loop
        /// </summary>
        public void PlayGame()
        {
            _gameView.DisplayWelcomeScreen();

            while (_playingGame)
            {
                //
                // Round loop
                //
                Gameboard.PlayerPiece winner = Gameboard.PlayerPiece.None;
                while (_playingRound)
                {
                    //
                    // Perform the task associated with the current game and round state
                    //
                    ManageGameStateTasks();

                    //
                    // Evaluate and update the current game board state
                    //

                    _gameboard.UpdateGameboardState(out winner);
                }

                //
                // Round Complete: Display the results
                //

                _winningPlayer = winner;


                _gameView.DisplayCurrentGameStatus(_roundNumber, _playerXNumberOfWins, _playerONumberOfWins, _numberOfCatsGames);

                //
                // Confirm no major user errors
                //
                if (_gameView.CurrentViewState != ConsoleView.ViewState.PlayerUsedMaxAttempts ||
                    _gameView.CurrentViewState != ConsoleView.ViewState.PlayerTimedOut)
                {
                    //
                    // Prompt user to play another round
                    //
                    if (_gameView.DisplayNewRoundPrompt())
                    {
                        _gameView.DisplayMenuUserAction();
                        _gameboard.InitializeGameboard();
                        _gameView.InitializeView();
                        _playingRound = true;
                    }
                    else
                    {
                        // end previous round stats
                        _playingRound        = false;
                        _playerONumberOfWins = 0;
                        _playerXNumberOfWins = 0;
                        _numberOfCatsGames   = 0;
                        _roundNumber         = 0;
                        _winningPlayer       = Gameboard.PlayerPiece.None;

                        if (_gameView.DisplayExitGame())
                        {
                            _playingGame = false;
                        }
                        else
                        {
                            _gameView.DisplayMenuUserAction();
                            _gameboard.InitializeGameboard();
                            _gameView.InitializeView();
                            _playingRound = true;
                        }
                    }
                }
                //
                // Major user error recorded, end game
                //
                else
                {
                    _playingGame = false;
                }
            }

            _gameView.DisplayClosingScreen();
        }