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.