2

I am trying to use flood fill to clear an open area in a minesweeper game. I made a simple flood fill function but I keep getting a stack overflow error code

 public void revealEmpty(int givenIndex){
        if(buttons.get(givenIndex).isEnabled())
            open(givenIndex);

            if(gm.surroundingbombs(givenIndex)==0){

                if(gridsize>givenIndex+gridwidth)
                    revealEmpty(givenIndex+gridwidth);

                if(gridsize>givenIndex+gridwidth+1)
                    revealEmpty(givenIndex+gridwidth+1);

                if(gridsize>givenIndex+gridwidth-1)
                    revealEmpty(givenIndex+gridwidth-1);

                if(gridsize<givenIndex-gridwidth)
                    revealEmpty(givenIndex-gridwidth);

                if(gridsize<givenIndex-gridwidth+1)
                    revealEmpty(givenIndex-gridwidth+1);
                if(gridsize<givenIndex-gridwidth-1)

                    revealEmpty(givenIndex-gridwidth-1);

                 if(gm.rightEdge(givenIndex,gridwidth)){//checks if the button pressed is on the right edge

                        revealEmpty(givenIndex+1);
                }

                 if(gm.leftEdge(givenIndex,gridwidth)){//checks if the button pressed ison the left edge

                    revealEmpty(givenIndex-1);
                }



            }   
            else{
                return;
            }



    }

this is the code used top "open" a cell in the grid

public void open(int Bindex){
        Font f = new Font("Arial", Font.BOLD, 26);//font for the buttons
        Font f2 = new Font("Arial", Font.BOLD, 15);//font for the move tracker
        if(gm.surroundingbombs(Bindex)!=0){
            buttons.get(Bindex).setBorder(BorderFactory.createBevelBorder(1, Color.LIGHT_GRAY, Color.DARK_GRAY));
            buttons.get(Bindex).setIcon(null);
            if(gm.surroundingbombs(Bindex)!=0)
            buttons.get(Bindex).setText(Integer.toString(gm.surroundingbombs(Bindex)));
            if(small)
            buttons.get(Bindex).setFont(f2);
            else
            buttons.get(Bindex).setFont(f);
            buttons.get(Bindex).setBorderPainted(true);
            buttons.get(Bindex).setEnabled(false);
            buttons.get(Bindex).setContentAreaFilled(true);
            buttons.get(Bindex).setBackground(Color.LIGHT_GRAY);
        }
        else
        buttons.get(Bindex).setBorder(BorderFactory.createBevelBorder(1, Color.LIGHT_GRAY, Color.DARK_GRAY));
        buttons.get(Bindex).setIcon(null);
        buttons.get(Bindex).setBorderPainted(true);
        buttons.get(Bindex).setEnabled(false);
        buttons.get(Bindex).setContentAreaFilled(true);
        buttons.get(Bindex).setBackground(Color.LIGHT_GRAY);


    }

I have some inkling as to why it is working, specifically that the way I am tracking my visited cells is not working but I am totaly cluless above that.

w_o_w
  • 21
  • 1

1 Answers1

1

The problem here is that you check all surrounding JButtons no matter if they are already revealed. To fix the recursive calls try changing the if-statement to surround the entire code. Like this:

    public void revealEmpty(int givenIndex){
    if(buttons.get(givenIndex).isEnabled()) { //If statement opens here
        open(givenIndex);

        if (gm.surroundingbombs(givenIndex) == 0) {

            if (gridsize > givenIndex + gridwidth)
                revealEmpty(givenIndex + gridwidth);

            if (gridsize > givenIndex + gridwidth + 1)
                revealEmpty(givenIndex + gridwidth + 1);

            if (gridsize > givenIndex + gridwidth - 1)
                revealEmpty(givenIndex + gridwidth - 1);

            if (gridsize < givenIndex - gridwidth)
                revealEmpty(givenIndex - gridwidth);

            if (gridsize < givenIndex - gridwidth + 1)
                revealEmpty(givenIndex - gridwidth + 1);
            if (gridsize < givenIndex - gridwidth - 1)

                revealEmpty(givenIndex - gridwidth - 1);

            if (gm.rightEdge(givenIndex, gridwidth)) {//checks if the button pressed is on the right edge

                revealEmpty(givenIndex + 1);
            }

            if (gm.leftEdge(givenIndex, gridwidth)) {//checks if the button pressed ison the left edge

                revealEmpty(givenIndex - 1);
            }


        } else {
            return;
        }
    } else { //And ends here
        return;
    }


}

Based on the Information you provided im not 100% sure if this will fix your problem so please check it and tell me if it worked.

DerBobby
  • 356
  • 3
  • 9
  • this stops the function from working, but I am pretty sure that the reason that it's not working is that the way I'm checking if the button has already been opened is not working. – w_o_w Jan 26 '18 at 16:27