I'm wanting to use Floodfill to uncover neighbouring cells in a Minesweeper game. I'm new to Floodfill and perhaps I'm misunderstanding it. It never stops when it reaches a cell that isn't surrounded by a mine.
Here is the code for the uncovering method:
public static void uncoverSurroundings(int x, int y, JButton[][] buttons)
{
queue.add(new Point(x,y));
int currentPnt = queue.size() - 1;
Point p = queue.get(currentPnt);
try
{
if (mineLayout[x][y+1].equals("Mine"))
queue.remove(p);
else if (mineLayout[x][y-1].equals("Mine"))
queue.remove(p);
else if (mineLayout[x+1][y+1].equals("Mine"))
queue.remove(p);
else if (mineLayout[x-1][y-1].equals("Mine"))
queue.remove(p);
else if (mineLayout[x-1][y].equals("Mine"))
queue.remove(p);
else if (mineLayout[x+1][y].equals("Mine"))
queue.remove(p);
else if (mineLayout[x-1][y+1].equals("Mine"))
queue.remove(p);
else if (mineLayout[x+1][y-1].equals("Mine"))
queue.remove(p);
}
catch (NullPointerException|ArrayIndexOutOfBoundsException e)
{
}
try
{
if (currentPnt + 1 == queue.size())
{
Point r = queue.get(currentPnt);
queue.remove(currentPnt);
buttons[r.x][r.y].setEnabled(false);
queue.add(new Point (x, y+1));
queue.add(new Point (x, y-1));
queue.add(new Point (x+1, y+1));
queue.add(new Point (x-1, y-1));
queue.add(new Point (x-1, y));
queue.add(new Point (x+1, y));
queue.add(new Point (x-1, y+1));
queue.add(new Point (x+1, y-1));
}
}
catch (NullPointerException|ArrayIndexOutOfBoundsException e)
{
}
if (!queue.isEmpty())
index = queue.size() - 1;
Point nextPnt = queue.get(index);
uncoverSurroundings(nextPnt.x, nextPnt.y, buttons);
}
}