I'm creating a java implementation of Java for fun and I'm trying to fill in all zeros when you click a pool of them. (Play Minesweeper to see what I'm talking about)
Here is my recursive call:
private void revealZeros(int x, int y) {
if (board[y][x].revealed)
return;
board[y][x].revealed = true;
if (y > 0) {
if (x > 0)
if (!board[y - 1][x - 1].revealed && board[y - 1][x - 1].b == 0)
revealZeros(y - 1, x - 1);
if (x < 15) {
if (!board[y - 1][x + 1].revealed && board[y - 1][x + 1].b == 0)
revealZeros(y - 1, x + 1);
}
if (!board[y - 1][x].revealed && board[y - 1][x].b == 0)
revealZeros(y - 1, x);
}
if (x > 0)
if (!board[y][x - 1].revealed && board[y][x - 1].b == 0)
revealZeros(y, x - 1);
if (x < 15)
if (!board[y][x + 1].revealed && board[y][x + 1].b == 0)
revealZeros(y, x + 1);
if (y < 15) {
if (x > 0)
if (!board[y + 1][x - 1].revealed && board[y + 1][x - 1].b == 0)
revealZeros(y + 1, x - 1);
if (x < 15)
if (!board[y + 1][x + 1].revealed && board[y + 1][x + 1].b == 0)
revealZeros(y + 1, x + 1);
if (!board[y + 1][x].revealed && board[y + 1][x].b == 0)
revealZeros(y + 1, x);
}
}
The call is not working properly. It reveals blocks other than 0 and does not reveal all 0 blocks.
Space.b = the number of bombs around it
Space.revealed = is the space revealed?