2012-09-05 48 views
1

编辑:已解决! 我只是忘了在'else if'下包含一个'else'语句,它会返回空白当在扫雷游戏中发现空单元格时发生递归stackOverflowError

我正在使用Java,并且正在制作扫雷游戏。

我试图打开所有相邻的空单元格时单击一个空单元格。 我在这个网站上看过类似的问题,看不到我要去哪里错了。 我得到一个stackOverflow。

任何帮助将不胜感激。

在下面,“按钮”阵列是按钮的2D阵列,和“细胞”阵列小区对象的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; 
    } 
} 

回答

2

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