/// <summary> /// Repaint all tiles and check if game over /// </summary> void RefreshBoard() { for (int row = 0; row < rows; row++) { for (int col = 0; col < columns; col++) { Tile tile = tiles[row, col]; TileState tileState = tileStates[row, col]; if (tileState.Flag) // Flagging tiles { tile.SetStatus(TileStatus.Flagged); } else if (tileState.Flip) // Flipping tiles { if (tileState.Mine && mineHit) { tile.SetStatus(TileStatus.Mine); } else { if (tileState.Warning != 0) { tile.SetStatus(TileStatus.Warning, tileState.Warning); } else { tile.SetStatus(TileStatus.Flipped); } } } else // Unflagging tiles and revealing mines { if (tileState.Mine && mineHit) { // Reveal all mines if a mine was hit tile.SetStatus(TileStatus.Mine); } else { // Unflagging tiles tile.SetStatus(TileStatus.Unflippped); } } } } // Check if we are done // Alternate ending: stop when all unmined tiles are flipped, i.e. don't require complete flagging //(mineHit || tilesUntouched <= totalMines) if (mineHit || tilesUntouched == 0) { if (mineHit) { MessageBox.Show("Oops! You stepped on a mine.", "Game Over", MessageBoxButtons.OK, MessageBoxIcon.Error); } else { MessageBox.Show("You won! The minefield is clear.", "Success"); } LayMines(); } }