1

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

我正在使用 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;  
    }
}
4

1 回答 1

2

确保您的getIsOpensetIsOpen方法按预期工作。这些是停止递归的关键,所以我的猜测是那里有问题。

于 2012-09-05T18:09:14.980 回答