public void RevealAllBlanks(MinesweeperButton toggledButton) { for (int xoff = -1; xoff <= 1; xoff++) { int i = toggledButton.ColumnValue + xoff; if (i < 0 || i >= this.ColumnCount) { continue; } for (int yoff = -1; yoff <= 1; yoff++) { int j = toggledButton.RowValue + yoff; if (j < 0 || j >= this.RowCount) { continue; } MinesweeperButton neighbor = this.ButtonArray[i, j]; if (!neighbor.IsRevealed) { neighbor.ToggleRevealed(); if (neighbor.SurroundingMineCount == 0 && !neighbor.IsMine) { RevealAllBlanks(neighbor); } } } } }
public void CountMines(int ColumnCount, int RowCount, MinesweeperButton[,] ButtonArray) { if (this.IsMine) { this.SurroundingMineCount = -1; return; } int totalMineCount = 0; for (int xoff = -1; xoff <= 1; xoff++) { int i = this.ColumnValue + xoff; if (i < 0 || i >= ColumnCount) { continue; } for (int yoff = -1; yoff <= 1; yoff++) { int j = this.RowValue + yoff; if (j < 0 || j >= RowCount) { continue; } MinesweeperButton neighbor = ButtonArray[i, j]; if (neighbor.IsMine) { totalMineCount++; } } } this.SurroundingMineCount = totalMineCount; }
private void BoardButton_Click(object sender, RoutedEventArgs e) { MinesweeperButton toggledButton = sender as MinesweeperButton; StackPanel stackPanel = new StackPanel(); stackPanel.Orientation = Orientation.Horizontal; Image flagImage = new Image(); flagImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\flag.gif"); stackPanel.Children.Add(flagImage); if (!toggledButton.IsFlagged && (enableFlagButton.IsChecked ?? false)) { // If the target button is not flagged and the EnableFlagButton is turned ON if (game.FlagCount > 0) { remainingMinesTextBlock.Text = game.DecrementFlagCounter().ToString(); toggledButton.ToggleFlagOnButton(); toggledButton.Content = stackPanel; buttonReset.Content = FindResource("neutral_emoji"); if (game.FlagCount == 0) { remainingMinesTextBlock.Foreground = Brushes.IndianRed; remainingMinesImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\badflag.png"); } } else { MessageBox.Show("You have no more flags left."); buttonReset.Content = FindResource("neutral_emoji"); } } else if (!toggledButton.IsFlagged) { // If the target button is not flagged and the EnableFlagButton is turned OFF toggledButton.ToggleRevealed(); if (toggledButton.SurroundingMineCount == 0) { game.RevealAllBlanks(toggledButton); } if (toggledButton.IsMine) { buttonReset.Content = FindResource("mineclicked_emoji"); gameTimer.Stop(); game.GameOver(); StartAnimation(); } else { buttonReset.Content = FindResource("neutral_emoji"); if (GameWon()) { buttonReset.Content = FindResource("gameover_emoji"); gameTimer.Stop(); game.GameOver(); leaderboardObject = new Leaderboard(playerName, mineCount.ToString(), timerTextBlock.Text); leaderboardObject.WriteDataToFile(); StartAnimation(); } } } else if (toggledButton.IsFlagged && (enableFlagButton.IsChecked ?? false)) { // If the target button IS FLAGGED and the EnableFlagButton is turned ON toggledButton.ToggleFlagOnButton(); remainingMinesTextBlock.Text = game.IncrementFlagCounter().ToString(); remainingMinesTextBlock.Foreground = Brushes.DarkGreen; toggledButton.Content = ""; buttonReset.Content = FindResource("neutral_emoji"); remainingMinesImage.Source = ImageWorker.GenerateImage(@"..\..\Assets\goodflag.png"); } }