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

getIsOpenメソッドとsetIsOpenメソッドが意図したとおりに機能していることを確認してください。これらは再帰を止める鍵となるものなので、何か問題があると思います。

于 2012-09-05T18:09:14.980 に答える