示例#1
0
        /// <summary>
        /// displays who goes first and returns a 0 or 1 for X and O
        /// </summary>
        public Gameboard.GameboardState  DisplayWhosOnFirst()
        {
            bool   validChoice = false;
            string userResponse;

            Gameboard.GameboardState readyPlayerOne = 0;

            Random random = new Random();

            StringBuilder sb = new StringBuilder();

            ConsoleUtil.HeaderText = GAME_NAME + " | Who Goes First";
            ConsoleUtil.DisplayReset();

            ConsoleUtil.DisplayMessage("Choose who will go first, X or O");
            Console.WriteLine();

            ConsoleUtil.DisplayMessage("Please enter either an X or an O for first player.");
            ConsoleUtil.DisplayMessage("If you want the game to decide press any other key!");
            userResponse = Console.ReadLine();

            while (!validChoice)
            {
                if (userResponse == "x" | userResponse == "X")
                {
                    readyPlayerOne = Gameboard.GameboardState.PlayerXTurn;
                    validChoice    = true;
                }
                else if (userResponse == "o" | userResponse == "O")
                {
                    readyPlayerOne = Gameboard.GameboardState.PlayerOTurn;
                    validChoice    = true;
                }
                else
                {
                    if (random.Next(0, 2) == 0)
                    {
                        readyPlayerOne = Gameboard.GameboardState.PlayerXTurn;
                        validChoice    = true;
                    }
                    else
                    {
                        readyPlayerOne = Gameboard.GameboardState.PlayerOTurn;
                        validChoice    = true;
                    }
                }
            }

            if (readyPlayerOne == Gameboard.GameboardState.PlayerXTurn)
            {
                Console.WriteLine("First player is X");
            }
            else
            {
                Console.WriteLine("First player is O");
            }

            DisplayContinuePrompt();
            return(readyPlayerOne);
        }
        /// <summary>
        /// manage each new task based on the current game state
        /// </summary>
        private void ManageGameStateTasks()
        {
            switch (_gameView.CurrentViewState)
            {
            case ConsoleView.ViewState.Active:
                _gameView.DisplayGameArea();

                switch (_gameboard.CurrentRoundState)
                {
                case Gameboard.GameboardState.NewRound:

                    _roundNumber++;

                    //Generate a random number to decide what player goes first.
                    Random coinFlip = new Random();
                    if (coinFlip.Next(0, 2) == 0)
                    {
                        //If the random number is 0...

                        //Set Player O as the first player to take a turn.
                        _gameboard.CurrentRoundState = Gameboard.GameboardState.PlayerOTurn;

                        _gameView.DisplayMessageBox("Player O will start first this round.  Press any key to start playing.");
                        Console.ReadKey();
                    }
                    else
                    {
                        //If the random number is 1...

                        //Set Player X as the first player to take a turn.
                        _gameboard.CurrentRoundState = Gameboard.GameboardState.PlayerXTurn;

                        _gameView.DisplayMessageBox("Player X will start first this round.  Press any key to start playing.");
                        Console.ReadKey();
                    }

                    break;

                case Gameboard.GameboardState.PlayerXTurn:
                    _currentPlayerTurn = Gameboard.GameboardState.PlayerXTurn;
                    ManagePlayerTurn(Gameboard.PlayerPiece.X);
                    break;

                case Gameboard.GameboardState.PlayerOTurn:
                    _currentPlayerTurn = Gameboard.GameboardState.PlayerOTurn;
                    ManagePlayerTurn(Gameboard.PlayerPiece.O);
                    break;

                case Gameboard.GameboardState.PlayerXWin:
                    _playerXNumberOfWins++;
                    _playingRound = false;
                    break;

                case Gameboard.GameboardState.PlayerOWin:
                    _playerONumberOfWins++;
                    _playingRound = false;
                    break;

                case Gameboard.GameboardState.CatsGame:
                    _numberOfCatsGames++;
                    _playingRound = false;
                    break;

                default:
                    break;
                }

                break;

            case ConsoleView.ViewState.PlayerTimedOut:
                _gameView.DisplayTimedOutScreen();
                _playingRound = false;
                break;

            case ConsoleView.ViewState.PlayerUsedMaxAttempts:
                _gameView.DisplayMaxAttemptsReachedScreen();
                _playingRound = false;
                _playingGame  = false;
                break;

            default:
                break;
            }
        }