1. Member Join Date
Nov 2014
Posts
1
Rep Power
0

## Java Recursion Minesweeper

Hey guys. Need some major help with recursion in a MineSweeper game.

So far, when a user clicks on a square and it has a bomb on it, I reveal the bomb to the user - see clicked(). I also have the ability to show the user if the squares surrounding them have any adjacent bombs - see countAdjacentSquares().

However, I am having trouble with my fillZeros() recursive method. When a user clicks on a square that has zero bombs in its surrounding squares, it not only reveals that square, but also any adjacent squares (horizontally, vertically or diagonally adjacent) that also have zero bombs in its surrounding squares.

I don't have any recursion, if I click on a square with no bombs all it does it set the square to 0, but doesn't continue to check for other empty squares.

Java Code:
```import javax.swing.*;

import java.awt.*;
import java.util.*;

public class Square extends GameSquare
{
private boolean revealed = false;
private boolean squareHasBomb = false;
public static final int MINE_PROBABILITY = 10;
public int mineCount = 0;

public Square(int x, int y, GameBoard board)
{
super(x, y, "images/blank.png", board);

Random r = new Random();
squareHasBomb = (r.nextInt(MINE_PROBABILITY) == 0);
}

for(int xOffset = -1; xOffset <= 1; xOffset++)
for (int yOffset = -1; yOffset <= 1; yOffset++){
Square adjacentSquare = (Square)board.getSquareAt(xLocation +xOffset, yLocation +yOffset);
mineCount++;
revealed = true;
}
}
setImage("images/" + mineCount +".png");
return mineCount;
}

// Recursive method that will 0-fill all surround empty squares
private void fillZeros(){
for(int xOffset = -1; xOffset <= 1; xOffset++)
for (int yOffset = -1; yOffset <= 1; yOffset++){
Square zeroSquare = (Square)board.getSquareAt(xLocation +xOffset, yLocation +yOffset);

// Base Case. Square must have bomb or a neighbouring bomb
if(zeroSquare.squareHasBomb == true && revealed == true){
return;
}
// The square doenst have a bomb and an adjacent bomb and can be set to empty
else if(zeroSquare.squareHasBomb == false && revealed == false){
setImage("images/0.png");
revealed = true;
}

}fillZeros();
}

public void clicked(){
if (squareHasBomb == true)
setImage("images/bomb.png");

else if(squareHasBomb == false && revealed == false)

else if(squareHasBomb == false)
fillZeros();

}
}```
I cannot see where I am going wrong here. Any help would be greatly appreciated

Thanks  Reply With Quote

2. ## Re: Java Recursion Minesweeper

but doesn't continue to check for other empty squares.
How are you debugging the code?
Add some println statements that print out messages as the loops execute. Include the x and y values and the has bomb values in the printed messages.

The print out will help you see what the computer sees when it executes the code.
Last edited by Norm; 11-16-2014 at 09:08 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•