/// <summary> /// Processing a right click on a button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void ProcessFlagging(object sender, EventArgs e) { // Set the current button MinesweeperButton currentButton = (MinesweeperButton)sender; // Check for lost game and right click if (!hasLostGame && ((MouseEventArgs)e).Button == MouseButtons.Right) { if (currentButton.IsFlagged) { currentButton.IsFlagged = false; currentButton.Image = null; } else { currentButton.IsFlagged = true; currentButton.Image = flagImage; } } }
/// <summary> /// Preocess a left click on a button. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void ProcessClick(object sender, EventArgs e) { restartGameButton.Select(); MinesweeperButton currentButton = ((MinesweeperButton)sender); if (!currentButton.IsFlagged && !hasLostGame && !hasWonGame) { clickedCellCount++; currentButton.Cell.HasBeenClicked = true; currentButton.Visible = false; switch (currentButton.Cell.Image) { case GameImage.Non: int count = currentButton.Cell.Neighbors.Count; for (int i = 0; i < count; i++) { GridCell cell = currentButton.Cell.Neighbors[i]; if (!cell.HasBeenClicked) { gameButtons[cell.I, cell.J].PerformClick(); } } break; case GameImage.Mine: gameTimer.Enabled = false; foreach (MinesweeperButton button in gameButtons) { if (button.Cell.HasMine && !button.Cell.HasBeenClicked) { button.Visible = false; clickedMineCount++; } } if (clickedMineCount == mineCount - 1) { // Inform player hasLostGame = true; MessageBox.Show("You Lose!", "Game Over"); // Store the loss lifetimeStats.LifetimeLosses++; Util.SerializeOut(lifetimeStats, statsFilePath); } break; } // Check for win if (!hasLostGame && clickedCellCount == gameButtons.Length - mineCount) { hasWonGame = true; // Store the game gameTimer.Enabled = false; StoreGameWinForm sg = new StoreGameWinForm(this, lifetimeStats, gameTime, msGame.Diffaculty, statsFilePath); sg.ShowDialog(); } } }
/// <summary> /// Rebuilds the form and all of its buttons and things. /// </summary> /// <param name="mg">The MinesweeperGame object.</param> void SetUpTheForm(MinesweeperGame mg) { currentBoardWidth = mg.CurrentGridSize; hasLostGame = false; hasWonGame = false; // Reset clicked bombs mineCount = 0; clickedMineCount = 0; foreach (GridCell cell in mg.Cells) { if (cell.HasMine) { mineCount++; } } // Flush pictures if (pictureBoxs != null) { foreach (PictureBox pictureBox in pictureBoxs) { Controls.Remove(pictureBox); } } pictureBoxs = new List <PictureBox>(); // Flush the buttons and set the window size if (gameButtons != null) { int tmp = gameButtons.GetLength(0); for (int i = 0; i < tmp; i++) { for (int j = 0; j < tmp; j++) { Controls.Remove(gameButtons[i, j]); } } } gameButtons = new MinesweeperButton[mg.CurrentGridSize, mg.CurrentGridSize]; // Define the form size and locations Width = (currentBoardWidth * (ButtonSize + BufferSize)) + Shim; Height = (TopOffset + (currentBoardWidth * (ButtonSize + BufferSize)) + (3 * Shim)) - 10; timerTextBox.Location = new Point(((Width / 2) - timerTextBox.Width) + Shim, 13); aboutButton.Location = new Point(Width - aboutButton.Width - 13 - Shim, 13); // Create the buttons for (int i = 0; i < currentBoardWidth; i++) { for (int j = 0; j < currentBoardWidth; j++) { gameButtons[i, j] = new MinesweeperButton(msGame.Cells[i, j]) { Size = new Size(ButtonSize, ButtonSize), Left = i * (ButtonSize + BufferSize), Top = (j * (ButtonSize + BufferSize)) + TopOffset, Height = ButtonSize, Width = ButtonSize, BackColor = Color.AntiqueWhite }; gameButtons[i, j].Click += ProcessClick; gameButtons[i, j].MouseDown += ProcessFlagging; gameButtons[i, j].Show(); Controls.Add(gameButtons[i, j]); PutImageUnderButton(gameButtons[i, j], gameButtons[i, j].Cell.Image); } } // Start the timer gameTimer.Interval = 1000; gameTimer.Enabled = true; gameTime = 0; }