/// <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> /// Add player's move to the game board. /// </summary> /// <param name="gameboardPosition"></param> /// <param name="PlayerPiece"></param> public void SetPlayerPiece(GameboardPosition gameboardPosition, PlayerPiece PlayerPiece) { // // Row and column value adjusted to match array structure // Note: gameboardPosition converted to array index by subtracting 1 // _positionState[gameboardPosition.Row - 1, gameboardPosition.Column - 1] = PlayerPiece; // // Change game board state to next player // SetNextPlayer(); }
/// <summary> /// Determine if the game board position is taken /// </summary> /// <param name="gameboardPosition"></param> /// <returns>true if position is open</returns> public bool GameboardPositionAvailable(GameboardPosition gameboardPosition) { // // Confirm that the board position is empty // Note: gameboardPosition converted to array index by subtracting 1 // if (_positionState[gameboardPosition.Row - 1, gameboardPosition.Column - 1] == PlayerPiece.None) { return(true); } else { return(false); } }
/// <summary> /// Get a player's position choice within the correct range of the array /// Note: The ConsoleView is allowed access to the GameboardPosition struct. /// </summary> /// <returns>GameboardPosition</returns> public GameboardPosition GetPlayerPositionChoice() { // // Initialize gameboardPosition with -1 values // GameboardPosition gameboardPosition = new GameboardPosition(-1, -1); // // Get row number from player. // gameboardPosition.Row = PlayerCoordinateChoice("Row"); // // Get column number. // if (CurrentViewState != ViewState.PlayerUsedMaxAttempts) { gameboardPosition.Column = PlayerCoordinateChoice("Column"); } return(gameboardPosition); }