Minesweeper game problem

Hello all.I am trying to write a console Minesweeper game.But my program crashes from one function that if none of its neighbors contains a bomb, then all the adjacent neighbors are also revealed.If any of the neighbors have no adjacent bombs, they too are revealed.So I have done that recursively.I have three classes Tile,Board and the Game.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  public void Reveal_Tile(int i,int j) // i and j are coordinates of the tile 
{
//board is a matrix of tiles n*n
board[i][j].revealed=true;
//I it's better to have getters and setters but for the simplicity let's make it public
if (board[i][j].number_of_neighbours==0)
//we are iterating to all of the neighbours of the given tile in 3*3 square
for (int g=0; g<3; g++)
for (int h=0;h<3;h++)
if (is_on_the_board(i-1+g,j-1+h)) //check if the tile it's o the board 
{
 board[i-1+g][j-1+h].revealed=true;
if (board[i-1+g][j-1+h].number_of_neighbours==0  && !(g==1 && h==1) )
this.Reveal_Tile(i-1+g,j-1+h);
//Problem here stackoverflow
}


}

reveal_tile is public function in board because Game has an instance of the board and from there I first call this function
Last edited on
stack overflow usually means your boards are too large or something: you have used up all your stack's memory. you will have to make some of your memory dynamic, by using a vector or a pointer or the like.

how big is board?!
you can also increase stack size on many compilers, up to some fairly large amount. This can solve the issue if you are only a little over.

it could also mean your recursion is wrong. I missed that when I wrote the above: your logic may be wrong so it may be doing recursion forever until it blows out the stack.
Put a print statement in this function and see how many times it is called :)

It can also be BOTH problems. If the board is huge and you recursively call it for each tile, that may be too many calls even if correct.

A professor of mine said that looping with recursion is a recipe for disaster.

games like this, the programmer often adds a hidden ring around the board with values that fit the needs (eg all zero/empty tiles) so that going out of bounds is harmless when you check a 3x3 or whatever and it gives the right answer even if some of the 3x3 are in the off-the-edge area.
Last edited on
Like 9*9
For easy level in minesweeper.
0. 1. 2. 3. 4. 5. 6. 7. 8.
0.F F F F F F F F F
1.F T F F T F F F F
2.T F F F F F F T F
3.T F F F F F F F F
4.F F F F F F F F F
5.F F F T F F F F F
6.F F F T F F F T F
7.F F F F F F F F F
8.F F T F F F F F T
Last edited on
One, please intent your code. It makes it easier to read quickly. http://mrbool.com/importance-of-code-indentation/29079

Two, are you doing bounds checks to make sure i and j are >= 0 and <= (size of dimension)

If you're getting a stack overflow, it means you have no mechanism to stop infinitely recursiving, meaning you're probably checking the same tiles over and over again.
Perhaps you need to mark a title and "visited" and if so, don't recursive. (You might be able to re-use your "revealed" bool for this)

Minesweeper basically does the equivalent of a "Flood Fill" in computer graphics, so I would look that up.
Last edited on
T and F are if there is a bomb or not to help me visualize the game
@Genado
Yes.
bool is_on_the_board(i,j)
{
if (i<0 || j<0 || i>n-1 || j>n-1)
return false;
else
return true;

}
Ah, yep I missed that. So it's probably the latter, that you're checking the same title over and over again because there's no logic preventing that.
Yes thank you very much.I get it now.Because I do not check if that tile was already visited the recursion will never finis.
The recursion is an elegant way but sometimes can blow up your mind trying to find out what is going on under the hood.Thanks once again @jonnin,@Ganado
Topic archived. No new replies allowed.