I'm implementing flood fill algorithm for minesweeper game on C++ and all is fine, but I'am not enjoyed the result of it's work. It's uncovers all area except bombs when in right version it needs to uncover some area with numbers, that represents amount of bombs nearby.
void floodfill(int x, int y)
{
if (x < 0 || y < 0 || x >= cellsX || y >= cellsY) // Check game field borders
return;
if (closedMap[x][y] == EMPTY && !visited[x][y]) // If this cell is empty and not visited...
{
openedMap[x][y] = closedMap[x][y];
visited[x][y] = true;
floodfill(x + 1, y);
floodfill(x, y + 1);
floodfill(x - 1, y);
floodfill(x, y - 1);
}
else if (closedMap[x][y] > EMPTY && closedMap[x][y] < CLOSED && !visited[x][y]) // If this cell is contains number and not visited...
{
openedMap[x][y] = closedMap[x][y];
visited[x][y] = true;
floodfill(x + 1, y);
floodfill(x, y + 1);
floodfill(x - 1, y);
floodfill(x, y - 1);
}
return;
}
Some matrixes for simple understanding:
How it is (* means covered):
[1][2][3][4][5]
[1] 1 * 2 *
[2] 1 2 4 2
[3] 2 * 3 *
[4] 2 * 4 2
[5] 1 1 2 *
How it must be:
[1][2][3][4][5]
[1] 1 * * *
[2] 1 * * *
[3] 2 * * *
[4] 2 * * *
[5] 1 * * *
I looks like you are treating a spaced filled with a number the same as a blank space. If you assign a number to a tile you wouldn't need to floodfill()
past it.