1

EDIT: SOLVED! I simply forgot to include an 'else' statement under the 'else if' which does a blank return

I'm using Java, and I'm making a Minesweeper game.

I am trying to open all adjacent empty cells when an empty cell is clicked. I have taken a look at similar question on this site and can't see where I'm going wrong. I am getting a stackOverflow.

Any help will be greatly appreciated.

Below, the 'buttons' array is a 2D array of buttons, and the 'cells' array is a 2D array of cell objects (used to determine the state of that cell). Obviously each cell corresponds to a button.

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;  
    }
}

1 Answer 1

2

Make sure your getIsOpen and setIsOpen methods are working as intended. Those are the ones that are key to stopping recursion, so my guess is that there's something wrong there.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.