Java 扫雷游戏中寻找空单元格时的递归堆栈溢出错误

Java 扫雷游戏中寻找空单元格时的递归堆栈溢出错误,java,recursion,stack-overflow,minesweeper,Java,Recursion,Stack Overflow,Minesweeper,编辑:解决了! 我只是忘了在“else-if”下包含一个“else”语句,该语句返回空白 我在用Java,我在做一个扫雷游戏 我试图在单击空单元格时打开所有相邻的空单元格。 我在这个网站上看过类似的问题,看不出哪里出了问题。 我有一个堆垛机 任何帮助都将不胜感激 下面,“按钮”数组是按钮的二维数组,“单元格”数组是单元格对象的二维数组(用于确定该单元格的状态)。显然,每个单元格对应一个按钮 public void findEmptyCells(int i, int j) // this meth

编辑:解决了! 我只是忘了在“else-if”下包含一个“else”语句,该语句返回空白

我在用Java,我在做一个扫雷游戏

我试图在单击空单元格时打开所有相邻的空单元格。 我在这个网站上看过类似的问题,看不出哪里出了问题。 我有一个堆垛机

任何帮助都将不胜感激

下面,“按钮”数组是按钮的二维数组,“单元格”数组是单元格对象的二维数组(用于确定该单元格的状态)。显然,每个单元格对应一个按钮

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;  
    }
}
public void findEmptyCells(int i,int j)//单击单元格时调用此方法,因此将打开所有相邻的空单元格
{
如果(i>=0&&j>=0&&i<9&&j<9)//ie块实际存在于网格上
{
如果(单元格[i][j].getAdjMines()==0&&cells[i][j].getIsMine()==false&&cells[i][j].getIsFlagged()==false&&cells[i][j].getIsOpen()==false)//如果单元格为空且不是我的且未标记
{
按钮[i][j].setIcon(新的图像图标(“buttonImages/but”+单元格[i][j].getAdjMines()+“.png”);//此处getAdjMines值将为0,因此将放置空单元格图标
cells[i][j].setIsOpen(true);//稍后,如果我们需要确定哪些单元格仍然未被选中
//现在检查所有相邻单元
findEmptyCells(i-1,j);//左
findEmptyCells(i+1,j);//对
findEmptyCells(i,j+1);//向上
findEmptyCells(i,j-1);//向下
findEmptyCells(i-1,j+1);//左上角
findEmptyCells(i+1,j+1);//右上角
findEmptyCells(i-1,j-1);//左下
findEmptyCells(i+1,j-1);//右下
}
else if(单元格[i][j].getAdjMines()>0)
{
按钮[i][j].setIcon(新的图像图标(“按钮图像/但“+单元格[i][j].getAdjMines()+”.png”);
cells[i][j].setIsOpen(true);//稍后,如果我们需要确定哪些单元格仍然未被选中
返回;
}
}
其他的
{
返回;
}
}

确保
getIsOpen
setIsOpen
方法按预期工作。这些都是停止递归的关键,所以我猜这里出了问题