/// <summary> /// Preparations before running the game mode: "Player Vs. Player" /// </summary> private void PlayerVsPlayer() { Displayer.ClearWindow(); Human human1 = new Human(string.Empty, Shape.X, UserInput); Human human2 = new Human(string.Empty, Shape.O, UserInput); RunGameMode(human1, human2); // Run the mode }
/// <summary> /// Run the game, Main Menu window shows in the start, or after running a game! /// </summary> /// <param name="isFirstRun">Indicator if the game is running for the first time</param> public void Run() { System.Media.SoundPlayer music = new System.Media.SoundPlayer(); // Creating an instance for the music! Displayer.WindowStyle(); do { // While "Esc" is not pressed, the main menu will be showen after every game! Displayer.ClearWindow(); #region Related to first run options if (!isFirstRun) { Displayer.Logo(); // Tic Tac Toe Logo } else { Displayer.LogoAnimation(); // Logo with Animation only at first run! } #endregion #region Menu Music music.SoundLocation = "1.wav"; music.PlayLooping(); #endregion Displayer.DisplayGameOptions(true); // Sends true for not showing error msg at the beginning UserInput.SetGameMode(this); // Sends the game to set it's mode! if (GameMode == GameMode.PlayerVsPlayer) { #region Music (Typing Players) music.SoundLocation = "2.wav"; music.PlayLooping(); #endregion PlayerVsPlayer(); // Go to the chosen mode! } else if (GameMode == GameMode.PlayerVsComputer) { #region Music (Typing Players) music.SoundLocation = "2.wav"; music.PlayLooping(); #endregion PlayerVsComputer(); // Go to the chosen mode! } isFirstRun = false; // If it's first run: now we completed the first run of the game! } while (GameMode != GameMode.ExitMode); // If pressed Esc Displayer.CloseGame(); }
/// <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); }