0

I just picked up Java a few days ago, and started coding a Minesweeper game. The problem is, when I try to cascade the surrounding blocks and reveal blocks, either blank or with numbers depending on the amount of adjacent bombs, my recursion fails and gives me a stack over flow error. My code is as follows:

public void revealCell(int row, int col) 
{
  if(mAnswerBoard.get (row, col).equals ("1")||mAnswerBoard.get (row, col).equals("2")||
  mAnswerBoard.get (row, col).equals("3")||mAnswerBoard.get (row, col).equals("4")||
  mAnswerBoard.get (row, col).equals("5"))
  {
      mMinesweeperBoard.set (row, col, mAnswerBoard.get (row, col));
      return;
  }

  else if(mAnswerBoard.get (row, col).equals("0"))
  {
      mMinesweeperBoard.set (row, col, " ");
      for(int i = row - 1; i <= row + 1; i++)
      {
        if (i == -1)
        {
          i = 0;
       }
      for(int j = col - 1; j <= col + 1; j++)
      {
        if (j == -1)
        {
          j = 0;
        }
        if (mAnswerBoard.get (i, j).equals ("0") == false)
            {
              mMinesweeperBoard.set (i, j, mAnswerBoard.get (i, j));
            }
            else if (mAnswerBoard.get (i, j).equals (("0")))
            {
             if (mMinesweeperBoard.get (j, i).equals ("."))
             {
                mMinesweeperBoard.set(i, j, " "); 
               revealCell(i, j);
             }
           }
          }
      }
  }
}

I have been staring at this for hours, and I really can't grasp my head around why it doesn't stop looping. My initial gameboard holds "." in not revealed spots, and if 0, the board will hold a whitespace. I try to check if the answer key holds a 0 in the spot, and if it does and if the actual board still holds a"." in the spot, I can set a whitespace. For some reason, revealCell gets called over and over again, and I really can't figure it out. Am I doing something completely wrong?

Side note: I have to set i and j to 0 if they are -1 because my code allows the user to enter in 0,0 as a valid coordinate.

Thank you for the help, and I am sorry if the answer is right in front of my face...

6
  • Do you need a recursive solution? I think your iterative approach is probably easier here. Commented Jan 10, 2014 at 5:51
  • I do not necessarily need a recursive solution, but I guess I thought a recursive solution would be more appealing to the eye. In my case though, the code already has lost the appeal. Would an iterative approach be easier? Commented Jan 10, 2014 at 5:53
  • Are you using a debugger? Recursion problems are often easily troubleshot with some strategic breakpoints. Commented Jan 10, 2014 at 5:54
  • You're checking if the recursion goes off the left and top of the board, but not the bottom or right sides. That may be an issue later. Commented Jan 10, 2014 at 5:57
  • @DrewBuckley I have been trying to debug for a while. I'll keep going and see if I can pinpoint the issue. A break may be needed...maybe I've been looking past it all along. Commented Jan 10, 2014 at 6:09

1 Answer 1

1
             if (mMinesweeperBoard.get (j, i).equals ("."))

You switched i and j here, so you're testing the wrong cell. Switch them back and see if that fixes it.

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

4 Comments

That gave me an array index out of bounds exception of 9? Not sure how that happened exactly...
That's probably the other issue I mentioned in a comment. You're running off the sides of the array, since you only have half the necessary bounds-checking.
So now i'll have to add checks alongside the checks for the -1?
Yup. And because that's too short to be a comment, double yup.

Your Answer

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