我正在使用Java,而且我正在制作扫雷游戏。
单击空单元格时,我尝试打开所有相邻的空单元格。 我在这个网站上看了类似的问题,看不出我哪里出错了。 我正在获得stackOverflow。
非常感谢任何帮助。
下面,'buttons'数组是按钮的2D数组,'cells'数组是单元格对象的2D数组(用于确定该单元格的状态)。显然,每个单元格对应一个按钮。
public void findEmptyCells(int i, int j) // this method is called when a cell is clicked, therefore all adjacent empty cells will be opened
{
if (i >= 0 && j >= 0 && i < 9 && j < 9) //ie the block actually exists on the grid
{
if (cells[i][j].getAdjMines() == 0 && cells[i][j].getIsMine() == false && cells[i][j].getIsFlagged() == false && cells[i][j].getIsOpen() == false) //if cell is empty & not a mine & not flagged
{
buttons[i][j].setIcon(new ImageIcon("buttonImages/but" + cells[i][j].getAdjMines() + ".png")); //here the getAdjMines value will be 0, so the empty cell icon will be placed
cells[i][j].setIsOpen(true); //for later, if we need to identify which cells are still unclicked
//now to check all adjacent cells
findEmptyCells(i - 1, j); //left
findEmptyCells(i + 1, j); //right
findEmptyCells(i, j + 1); //up
findEmptyCells(i, j - 1); //down
findEmptyCells(i - 1, j + 1); //up-left
findEmptyCells(i + 1, j + 1); //up-right
findEmptyCells(i - 1, j - 1); //down-left
findEmptyCells(i + 1, j - 1); //down-right
}
else if (cells[i][j].getAdjMines() > 0)
{
buttons[i][j].setIcon(new ImageIcon("buttonImages/but" + cells[i][j].getAdjMines() + ".png"));
cells[i][j].setIsOpen(true); //for later, if we need to identify which cells are still unclicked
return;
}
}
else
{
return;
}
}
答案 0 :(得分:2)
确保您的getIsOpen
和setIsOpen
方法正常运行。那些是阻止递归的关键,所以我的猜测是那里有一些错误。