Home > Back-end >  java.lang.StackOverflowError: stack size 8MB Problem, in recursion android studio
java.lang.StackOverflowError: stack size 8MB Problem, in recursion android studio

Time:11-02

I am currently building a minesweeper game in android studio. I am now building a function that basically makes the numbers show ("bomb") when you "mine"(click on one of the squares) basically, the algorithm is: the function is getting the index of the square as parameters ,when pressing that square(imageView) if you press on a square- it will be revealed. if it is a square with 0 mines nearby it will also be revealed and will also do the same function on all of the squars nearby. I did this function with recursion but im getting an error "java.lang.StackOverflowError: stack size 8MB". I might be because the recursion is calling itself too much or that it is an endless loop. anyways, I dont know how to solve that. please help (code down here:)

public void bomb(int i,int j)
{
   if(i>-1&&i<8&&j>-1&&j<10)
    {
        board[i][j].setRevealed(true);
        if(board[i][j].getNumOfMinesNearby()==1)
        {
            board[i][j].setPicture("@drawable/one");
            board_xml[i][j].setImageResource(R.drawable.one);
        }

        if(board[i][j].getNumOfMinesNearby()==2)
        {
            board[i][j].setPicture("@drawable/two");
            board_xml[i][j].setImageResource(R.drawable.two);
        }
        if(board[i][j].getNumOfMinesNearby()==3)
        {
            board[i][j].setPicture("@drawable/three");
            board_xml[i][j].setImageResource(R.drawable.three);
        }
        if(board[i][j].getNumOfMinesNearby()==4)
        {
            board[i][j].setPicture("@drawable/four");
            board_xml[i][j].setImageResource(R.drawable.four);
        }
        if(board[i][j].getNumOfMinesNearby()==0)
        {
            board[i][j].setPicture("@drawable/zero");
            board_xml[i][j].setImageResource(R.drawable.zero);
            bomb(i-1,j 1);
            bomb(i-1,j);
            bomb(i-1,j-1);
            bomb(i,j 1);
            bomb(i,j-1);
            bomb(i 1,j 1);
            bomb(i 1,j);
            bomb(i 1,j-1);
        }

    }

}

CodePudding user response:

You have an infinite recursion problem. Let's say you have a 0 at board[0][0] and board[0][1]. When the user presses at [0,0] it will go into the recursive case. That will call it on [0,1]. Which will call it on [0,0]. Etc. You need to alter the algorithm so it doesn't call bomb if you have already called it on that index.

CodePudding user response:

The problem with your code is that you are creating an infinite loop. When you call your function for i,j it will trigger invocation for i 1,j-1, which in turn invoke the function for i-1,j 1, which is i,j (where you've started from).

You need to modify your code, so that each cell is only visited once. Recursion should also be avoided. Better to do it in a for loop:

for(int i=0;i<ni;i  ) {
  for(int j=0;j<nj;j  ) {
     // process cell here
  }
}
  • Related