public void checkWin() { // Check if game has been won int cellsVisited = 0; int visitedcounter = size * size; for (int i = 0; i < size; i++) { // Create counter for number of Cells in row for (int j = 0; j < size; j++) { // Check all cells is they have been visited if (square[i, j].getVisited()) { cellsVisited++; } // Check all cells if they are a mine if (square[i, j].getLive()) { cellsVisited++; } } } // If you have won the game if (cellsVisited == visitedcounter) { // stop timer stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); string text = "Congratulations, your have Won!" + Environment.NewLine + "Time elapsed: " + elapsedTime + "seconds."; // show dialog box and capture return DialogResult result = MessageBox.Show(text, "", MessageBoxButtons.OK); // if return is ok if (result == DialogResult.OK) { // bool for win or lose bool win = true; highScore_Form highScore = new highScore_Form(difficulty, ts, win); highScore.Show(); } // if return is cancel or close else if (result == DialogResult.Cancel) { // bool for win or lose bool win = true; highScore_Form highScore = new highScore_Form(difficulty, ts, win);; highScore.Show(); } } }
// Mouse controls protected override void OnMouseDown(MouseEventArgs e) { switch (MouseButtons) { // On Left Mouse Click case MouseButtons.Left: // if you hit a bomb if (this.getNeighbors() == 9) { ControlGrid(Grid); string text = "Game Over"; DialogResult result = MessageBox.Show(text, "", MessageBoxButtons.OK); if (result == DialogResult.OK) { // stop timer Grid.stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = Grid.stopWatch.Elapsed; // bool for win or lose bool win = false; highScore_Form highScore = new highScore_Form(Grid.difficulty, ts, win); highScore.Show(); } else if (result == DialogResult.Cancel) { // stop timer Grid.stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = Grid.stopWatch.Elapsed; // bool for win or lose bool win = false; highScore_Form highScore = new highScore_Form(Grid.difficulty, ts, win);; highScore.Show(); } } else if (this.getNeighbors() == 0) { // if you hit an 0 this.BackColor = Color.LightGray; this.Image = null; revealZerosGrid(Grid); } // if you hit anything else else if (this.getNeighbors() > 0) { this.BackColor = Color.LightGray; this.Image = null; this.Text = Convert.ToString(this.getNeighbors()); } this.setVisited(true); winGrid(Grid); break; // On Right Mouse Click case MouseButtons.Right: // change text this.Text = ""; // Assign an image to the button. this.BackColor = Color.LightGray; this.Image = Properties.Resources.flag; this.BackgroundImageLayout = ImageLayout.Stretch; break; } }