I am writing a simple minesweeper, but I can't find a way to reveal the adjacent tiles properly. If a tile is blank, it is revealed and then the algorithm reveals all adjacent blank tiles. However I would like to reveal a layer of non blank tiles as well, just like the real minesweeper.
Here is my code:
void revealAdjCells(Tile [][] t,int x, int y) {
if (!checkBounds(x,y)) {
return; // check for bounds
}
if ((t[x][y].getNeighbours() == 0) && (!t[x][y].getVisibleState())) { // NO neighbours and not revealed
t[x][y].setVisibleState(true); // reveal tile
revealAdjCells(t,x+1,y); // recursion, reveal adjacent tiles
revealAdjCells(t,x-1,y);
revealAdjCells(t,x,y-1);
revealAdjCells(t,x,y+1);
}
else
{
return;
}
}
getNeighbours()
returns the amount of bombs that surround a nearby tile (horizontal,vertical,diagonal) and getVisibleState()
returns a boolean that indicates whether a tile has been revealed or not.
Things that I have tried:
1) Removing getVisibleState()
from if condition (terrible idea, leads obviously to stack overflow).
2) Checking bounds (x-1,x+1,y+1,y-1) and then revealing the tiles accordingly (doesn't work, getVisibleState()
won't let the statement execute, because the tile that is examined by the recursion is already revealed).
So... yeah... I am stuck and I can't find a solution. Any algorithmic help is appreciated.