I am creating a minesweeper game and I have a flood fill algorithm. If an empty button is pressed the algorithm checks the surrounding buttons and if they are not bombs and have not yet revealed themselves they should do so and display their bomb count as the button's text.
But when I click on an empty button the adjacent ones text are not being shown as it should be. My flood fill method runs ok. I tested the code by having it print out the names of the buttons and their bomb count in my button_click event handler method and it prints out the button's names and their bomb count. so I'm not what's causing the issue.
here's my flood fill algorithm method
public void FloodFill(int x, int y)
{
Cell neighbour = new Cell();
Button btn = new Button();
for (int i = -1; i < 2; i )
{
for (int j = -1; j < 2; j )
{
int row = i x;
int column = j y;
if (row > -1 && row < rows && column > -1 && column < col)
{
neighbour = grid[row, column];
if (!neighbour.GetIsBomb() && !neighbour.isRevealed)
{
btn = neighbour.individualButton(row, column);
btn.Click = Button_Click;
// show(row, column, btn);
btn.PerformClick();
btn.Text = neighbour.GetBombCount().ToString();
}
}
}
}
}
And here is my button click event
void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
int x = Convert.ToInt32(button.Name.Substring(0, 1));
int y = Convert.ToInt32(button.Name.Substring(1, 1));
Console.WriteLine(x.ToString() " " y.ToString());
Console.WriteLine("Button " row.ToString() " " column.ToString() " Text is " btn.Text);
grid[x, y].isRevealed = true;
show(x, y,button);
button.Text = grid[x, y].GetBombCount().ToString();
label2.Text = button.Text ", ";
}
The show method
public void show(int x,int y,Button button)
{
if (grid[x, y].GetIsBomb())
{
button.BackgroundImage = MineSweepers.Properties.Resources.bomb2;
System.Media.SoundPlayer player = new System.Media.SoundPlayer(Properties.Resources.Bomb_1);
player.Play();
}
else if(!grid[x, y].GetIsBomb()&& grid[x, y].GetBombCount()!=0)
{
button.Text = grid[x, y].GetBombCount().ToString();
}
else if (!grid[x, y].GetIsBomb() && grid[x, y].GetBombCount() == 0)
{
FloodFill(x, y);
}
}
CodePudding user response:
