I am working on a minesweeper project and everything is working fine EXCEPT for when you click on a cell and it contains a 0 (no mines around it) and it is supposed to open all the other "0" cells around it using recursion. Right now, I keep on getting stack overflow errors when I try to find all of the "0" cells in the cluster around the clicked cell. I have 2 2D arrays keeping track of 1. the tile status (OPEN, CLOSED, FLAG, QUESTION) and 2. the number of mines adjacent to the cell (0-9 with 9 being a bomb). Here is what I currently have for the recursion:

``````// r = row, c = column, t= what to mark the tile (ex. OPEN, FLAG...)
// All input (r and c) is already checked to be in bounds
public void markTiles(int r, int c, int t) {

// If the spot has no neighboring mines, search all around for other neighbors without any
// mines and open them all up

// base case ... WORKS
//      if the click is on a cell that is anything but an empty (0 neighboring) cell, just open it
if(mines[r][c] != 0)
{
tiles[r][c] = t;
return;
}

// recursive case
if(tiles[r][c] != OPEN)
{
markTiles(r, c, 0, 0);
}
}

// r = row, c = column, rInc = row increment/decrement, cInc = column increment/decrement
private void markTiles(int r, int c, int rInc, int cInc)
{
int nextRow = r + rInc;
int nextCol = c + cInc;

// Not a cell containing a zero
if (mines[r][c] != 0)
return;

if (0 <= nextRow && nextRow < rows && 0 <= nextCol && nextCol < cols && tiles[r][c] == OPEN)
{
markTiles(nextRow, nextCol, -1, -1);
markTiles(nextRow, nextCol, -1, 0);
markTiles(nextRow, nextCol, -1, 1);
markTiles(nextRow, nextCol, 0, 1);  // Stack overflow here..
markTiles(nextRow, nextCol, 0, -1);  // ...and here
markTiles(nextRow, nextCol, 1, -1);
markTiles(nextRow, nextCol, 1, 0);
markTiles(nextRow, nextCol, 1, 1);
}
}
``````

I am not exactly sure where I am going wrong in this, any advice would be greatly appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.