我想使用 Floodfill 来发现扫雷游戏中的相邻单元格。我是 Floodfill 的新手,也许我误解了它。当它到达一个没有被地雷包围的牢房时,它永远不会停止。
这是揭开方法的代码:
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);
}
}