Beispiel #1
0
        /// <summary>
        /// Actual running of the (Game Mode)!
        /// </summary>
        /// <param name="player1">First player</param>
        /// <param name="player2">Second player</param>
        private void RunGameMode(Player player1, Player player2)
        {
            int   key;                        // Holds the option the user pressed after game is completed
            Shape winnerPlayer = Shape.Empty; // Holds the winner shape of the game
            bool  isFullClear  = true;        // Indicator if the user wants a full clear

            #region Music (PlayDesk)
            System.Media.SoundPlayer playDeskMusic = new System.Media.SoundPlayer(); // for the music!
            playDeskMusic.SoundLocation = "3.wav";
            playDeskMusic.PlayLooping();
            #endregion

            do
            {
                Displayer.DisplayGameDesk(player1, player2, this);

                if (PlayerTurn == Shape.X)
                {
                    player1.Play(this);
                }
                else if (PlayerTurn == Shape.O)
                {
                    player2.Play(this);
                }

                playsCounter++;     // Every play it counts it
                ChangePlayerTurn(); // Change the turn after a play

                #region Related to winning

                // NOTE: ~~~ Performance improvement ~~~
                // If the game has no 5 play moves, it will never find a win! till we pass 5 plays!
                // Only after 5 play moves, we can check for a win!
                if (playsCounter >= 5)
                {
                    winnerPlayer = CheckForWin();
                    if (winnerPlayer == Shape.X) // Found X win?
                    {
                        gameWins[0]++;           // Count this win!
                        Displayer.DisplayWinner(player1.Name, winnerPlayer, this);
                        break;
                    }
                    else if (winnerPlayer == Shape.O) // Found O win?
                    {
                        gameWins[1]++;                // Count this win!
                        Displayer.DisplayWinner(player2.Name, winnerPlayer, this);
                        break;
                    }
                }

                if (playsCounter == 9) // If Game Table is full, then no one wins :(
                {
                    playsCounter = 0;  // Reset counter
                    Displayer.DisplayNoWinner();
                    break;
                }

                #endregion
            } while (GameMode != GameMode.ExitMode); // If pressed Esc

            // If pressed Esc --> while playing the game, below won't take action!
            // It takes action only after a game is completed! (after displaying result of game)
            #region Options after game is completed:

            if (GameMode != GameMode.ExitMode)
            {
                key = UserInput.GameCompletedOptions();

                if (key == 1) // If pressed Enter
                {
                    Displayer.ClearWindow();
                    isFullClear = false; // Not a full clear, because user want to keep winning history!
                    ClearGame(isFullClear);

                    // Return to Game Desk
                    this.RunGameMode(player1, player2);
                }
                else if (key == 0) // If pressed Esc
                {
                    Displayer.ClearWindow();

                    // Return to Main Menu (and clears the game with the code below the loop)
                }
            }

            #endregion

            // Below takes action when Esc pressed - while playing the game, or after game is completed and user pressed Esc!
            isFullClear = true; // Full clear! (clears winning history)
            ClearGame(isFullClear);
        }