I am creating a sweeping program in java for the school, and I am having problems clearing areas that have no mines closest to them, the square must be turned off, and all the surrounding squares showed if there is another square that touches without bombs, it will execute the same operation. I get an error, I know this is due to an infinite loop, but I cannot find where it gets stuck in my code.
I do not know what will be wrong, so any suggestions will be useful.
Here is the relative part of the code (if instructions for special cases, if you clicked a square on the edge of the board)
private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx)
{
clicked--;
bArray2[y][x].setBackground(lighterGray);
bArray2[y][x].setEnabled(false);
if (x > 0 && x<gridx-1 && y> 0 && y<gridy-1)
{
clearSquare(y-1, x-1,gridy, gridx, bArray2);
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y-1, x+1,gridy, gridx, bArray2);
clearSquare(y, x-1,gridy, gridx, bArray2);
clearSquare(y, x+1,gridy, gridx, bArray2);
clearSquare(y+1, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
clearSquare(y+1, x+1,gridy, gridx, bArray2);
}
if(y == 0 && x != 0 && x != gridx-1)
{
clearSquare(y, x-1,gridy, gridx, bArray2);
clearSquare(y, x+1,gridy, gridx, bArray2);
clearSquare(y+1, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
clearSquare(y+1, x+1,gridy, gridx, bArray2);
}
if (y == 0 && x == 0)
{
clearSquare(y, x+1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
clearSquare(y+1, x+1,gridy, gridx, bArray2);
}
if (y == 0 && x == gridx-1)
{
clearSquare(y, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
}
if (x == 0 && y != 0 && y != gridy-1)
{
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y-1, x+1,gridy, gridx, bArray2);
clearSquare(y, x+1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
clearSquare(y+1, x+1,gridy, gridx, bArray2);
}
if (x == gridx-1 && y != 0 && y != gridy-1)
{
clearSquare(y-1, x-1,gridy, gridx, bArray2);
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x-1,gridy, gridx, bArray2);
clearSquare(y+1, x,gridy, gridx, bArray2);
}
if(y == gridy-1 && x != 0 && x != gridx-1)
{
clearSquare(y-1, x-1,gridy, gridx, bArray2);
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y-1, x+1,gridy, gridx, bArray2);
clearSquare(y, x-1,gridy, gridx, bArray2);
clearSquare(y, x+1,gridy, gridx, bArray2);
}
if (y == gridy-1 && x == 0)
{
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y-1, x+1,gridy, gridx, bArray2);
clearSquare(y, x+1,gridy, gridx, bArray2);
}
if (y == gridy-1 && x == gridx-1)
{
clearSquare(y-1, x-1,gridy, gridx, bArray2);
clearSquare(y-1, x,gridy, gridx, bArray2);
clearSquare(y, x-1,gridy, gridx, bArray2);
}
}
private void clearSquare(int y,int x, int gridy, int gridx, JButton[][] bArray2)
{
int value = array[y][x];
System.out.println(value);
String text = bArray2[y][x].getText();
if (text == "")
{
if (value == 0)
{
doClear(y, x, bArray2, gridy, gridx);
}
else{
clicked--;
bArray2[y][x].setText(""+value);
}
}
}