private void DoneButtonClick(object sender, EventArgs e) { if (Game.gameMode) { Game.AIDeployShips(); MainGameForm mainGame = new MainGameForm(); mainGame.Location = Location; mainGame.Show(); // Dispose does not trigger FormClosing event. Dispose(); } else { if (Game.playerSwitch) { Game.playerSwitch = !Game.playerSwitch; MultiplayerSettingsForm multiplayerSettingsForm = new MultiplayerSettingsForm(); multiplayerSettingsForm.Location = Location; multiplayerSettingsForm.Show(); // Dispose does not trigger FormClosing event. Dispose(); } else { Game.playerSwitch = !Game.playerSwitch; PasswordCheckForm passwordCheckForm = new PasswordCheckForm(); passwordCheckForm.Location = Location; passwordCheckForm.Show(); // Dispose does not trigger FormClosing event. Dispose(); } } }
private void deckPictureBoxClick(object sender, EventArgs e) { if (mouseCellX != -1 && mouseCellY != -1 && !opponentPlayer.RevealedCells[mouseCellX, mouseCellY]) { // Is the game over? if (Game.PerformAttack(mouseCellX, mouseCellY, yourPlayer, opponentPlayer)) { // End of game, actual player wins redraw final state. deck1PictureBox.Refresh(); // Reset side statistics. RedrawStatistics(); // Wait few seconds. Thread.Sleep(4000); // Play the winning sound. AudioContext.vicotrySoundPlayer.Play(); // Show the informational message box. MessageBox.Show("Congratulations " + yourPlayer.Name + "! You have won against " + opponentPlayer.Name + " in " + Game.roundCount + " rounds!", "Battleships: Game Over!"); // Dispose the form and return to the main menu. Dispose(); // Play the menu music in loop. AudioContext.menuSoundPlayer.PlayLooping(); GlobalContext.MainMenuForm.Location = Location; GlobalContext.MainMenuForm.Show(); } else { // The game is not over yet. // Redraw the deck. deck1PictureBox.Refresh(); // Scroll the battle log to the end. battleLogRichTextBox.ScrollToCaret(); // Reset the side statistics. RedrawStatistics(); // Wait few seconds. Thread.Sleep(4000); // Is the game a singleplayer? if (Game.gameMode) { int[] aiMove = Game.AIChooseCellToHit(yourPlayer); if (Game.PerformAttack(aiMove[0], aiMove[1], opponentPlayer, yourPlayer)) { // End of game, the computer has won. // Scroll the battle log to the end. battleLogRichTextBox.ScrollToCaret(); // Reset the side statistics. RedrawStatistics(); // Reveal all the other ships. for (int currentShip = 0; currentShip < 5; currentShip++) { opponentPlayer.ShipLeftCells[currentShip] = 0; } // Computer has won. deck2PictureBox.Refresh(); // Wait few seconds. Thread.Sleep(4000); // Play the menu music in loop. AudioContext.menuSoundPlayer.PlayLooping(); // Show the informational message box. MessageBox.Show("You were beaten, " + yourPlayer.Name + "! You have lost against " + opponentPlayer.Name + " in " + Game.roundCount + " rounds!", "Battleships: Game Over!"); // Dispose the form and return to the main menu. Dispose(); GlobalContext.MainMenuForm.Location = Location; GlobalContext.MainMenuForm.Show(); } else { // Computer has not won yet. // Increase the round count. Game.roundCount++; // Scroll the battle log to the end. battleLogRichTextBox.ScrollToCaret(); // Computer has not won yet. deck2PictureBox.Refresh(); // Reset the side statistics. RedrawStatistics(); // Wait few seconds. Thread.Sleep(4000); } } else { // The game is a multiplayer. if (!Game.playerSwitch) { // It has been the second player's turn. // Increment the round count. Game.roundCount++; } // Switch the player, show the lock form. Dispose(); Game.playerSwitch = !Game.playerSwitch; PasswordCheckForm passwordCheckForm = new PasswordCheckForm(); passwordCheckForm.Location = Location; passwordCheckForm.Show(); } } } }