We help IT Professionals succeed at work.

# MineSweeper Recursive Question

on
610 Views
Hi,

I am trying to create a minesweeper game, what I have so far is a board that appears and when the user clicks it reveals what is below the square (ex. a bomb).  I am trying to do the code when a user clicks on a square that is not near a bomb or a bomb itself it shows the squares of the same type in the same area.

In my method called "select" the board passes a row and a column and then it is cross-referenced with a 2d Array.  If the 2d array detects it is not a bomb, it is supposed to recursively show each square that is connected (as stated above).  The problem is that I receive an ArrayIndexOutOfBoundsException when clicking on a square that is not a bomb.  So, I am assuming there is a problem with the recursive method.  Also, I need to have it when a user clicks a location near a bomb it displays the number of bombs in the area.  Below is the method that "should" work.

if (data[row][col].getViewed() == false){

if (row > data.length || row < 0 || col > data.length || col < 0){

return;

}

data[row][col].setViewed();

if (data[row][col].isBomb() == false && data[row][col].getNearbyBombs() == 0){

data[row][col].setValue(data[row][col].getValue());

select(row-1,col-1);

select(row-1,col);

select(row-1,col+1);

select(row,col-1);

select(row,col+1);

select(row+1,col-1);

select(row+1,col);

select(row+1,col+1);

}

if (data[row][col].isBomb() == true){

data[row][col].setValue(-1);

hasLost();

}

}

ANY IDEAS?
Comment
Watch Question

## View Solution Only

Commented:
The problem is that you're checking data[row][col].getViewed() BEFORE you check the bounds checking. Try this;

if (row > data.length || row < 0 || col > data.length || col < 0){
return;
}

if (data[row][col].getViewed() == false) {

data[row][col].setViewed();

if (!data[row][col].isBomb() && data[row][col].getNearbyBombs() == 0) {
data[row][col].setValue(data[row][col].getValue());
select(row-1,col-1);
select(row-1,col);
select(row-1,col+1);
select(row,col-1);
select(row,col+1);
select(row+1,col-1);
select(row+1,col);
select(row+1,col+1);
}
else if (data[row][col].isBomb()) {
data[row][col].setValue(-1);
hasLost();
}
}

Commented:
No that didn't work.  Still giving off the ArrayIndexOutOfBoundsException, when trying to click an area without a bomb or numbers underneath.

Any other ideas? - points raised
CERTIFIED EXPERT

Commented:
Well orangehead911 is onto it, (haven't looked at *his* code though as to why it still fails) but in principle I think he's right.

Say you clicked on row 0 col 0, and the other tests were passed; then it calls recursion as in

select(row-1,col-1);

which is likely to cause an AIOOBEx I would think, but if not, then the metod itself surely will, because

if (data[row][col].getViewed() == false){ ....

will be calling a method on negative - non-existant- indices.
CERTIFIED EXPERT

Commented:
You'd be better of grouping these:

select(row-1,col-1);

select(row-1,col);

select(row-1,col+1);

select(row,col-1);

select(row,col+1);

select(row+1,col-1);

select(row+1,col);

select(row+1,col+1);

Like this :

if(row-1>=0&&col-1>=0&&col+1<=data.length){

select(row-1,col-1);}
. . .

etc ...

Commented:
Well, here is what I tried, it does not display the AIOOB error anymore, but it displays some sort of run time error when executing.  Here is the source of each method.

public void select(int row, int col) {

if (row > data.length || row < 0 || col > data.length || col < 0){

return;

}

if (data[row][col].getViewed() == false) {

data[row][col].setViewed();

if (!data[row][col].isBomb() && data[row][col].getNearbyBombs() == 0) {

data[row][col].setValue(data[row][col].getValue());

showNeighbor(row-1,col-1);

showNeighbor(row-1,col);

showNeighbor(row-1,col+1);

showNeighbor(row,col-1);

showNeighbor(row,col+1);

showNeighbor(row+1,col-1);

showNeighbor(row+1,col);

showNeighbor(row+1,col+1);

}

else if (data[row][col].isBomb()) {

data[row][col].setValue(-1);

hasLost();

}

}

}

public void showNeighbor(int row, int col){

if (!data[row][col].isBomb() && data[row][col].getNearbyBombs() == 0){

if(row-1>=0&&col-1>=0){

showNeighbor(row-1,col-1);

}

if(row-1>=0){

showNeighbor(row-1,col);

}

if(row-1>=0&&col+1<=data.length){

showNeighbor(row-1,col+1);

}

if(col-1>=0){

showNeighbor(row,col-1);

}

if(col+1<=data.length){

showNeighbor(row,col+1);

}

if(row+1<=data.length&&col-1>=0){

showNeighbor(row+1,col-1);

}

if(row+1<=data.length){

showNeighbor(row+1,col);

}

if(row+1<=data.length&&col+1<=data.length){

showNeighbor(row+1,col+1);

}

data[row][col].setValue(0);

}

}

ANY IDEAS - Points raised!

Commented:
What runtime exception is thrown exactly? Difficult to solve a problem without all the facts.
CERTIFIED EXPERT

Commented:
>> some sort of run time error when ...

It would help speed things up if you could tell us what sort of error this is.

Commented:
all it says is at MineSweeperDatabase.showNeighbor(MineSweeperDatabase.java:82) and then on the next line it reads MineSweeperDatabase.showNeighbor(MineSweeperDatabase.java:79), every other line is like this.  And when scrolling to the very top there is no description as to what the error was - just those two lines over and over again.
CERTIFIED EXPERT

Commented:
You'd need to capture the stack trace and supply that, along with the surrounding source.

Commented:
I'm not sure how to 'capture the stack trace' but here is the entire class file (some methods are not completed yet - obviously):

public class MineSweeperDatabase {

public Cell[][] data;

MineSweeperDatabase(int row, int col, int numBombs) {

data = new Cell[row][];

for (int i = 0; i < col; i++){

data[i] = new Cell[col];

for (int j = 0; j < row; j++){

Cell s = new Cell(i,j);

data[i][j] = s;

}

}

boolean mine = false;

int count = 0;

while (!mine){

for (int x = 0; x < col; x++){

for (int j = 0; j < row; j++){

if (Math.random() > .8 && mine != true){

data[x][j].setBomb();

try{

data[x-1][j-1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x-1][j].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x-1][j+1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x][j-1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x][j+1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x+1][j-1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x+1][j].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

try{

data[x+1][j+1].setNearbyBombs();

} catch (ArrayIndexOutOfBoundsException e){

}

count++;

}

if (count >= numBombs){

mine = true;

}

}

}

}

}

public void showNeighbor(int row, int col){

if (!data[row][col].getViewed()){

data[row][col].setValue(0);

}

if (!data[row][col].isBomb()){

if(row-1>=0&&col-1>=0){

showNeighbor(row-1,col-1);

}

if(row-1>=0){

showNeighbor(row-1,col);

}

if(row-1>=0&&col+1<=data.length){

showNeighbor(row-1,col+1);

}

if(col-1>=0){

showNeighbor(row,col-1);

}

if(col+1<=data.length){

showNeighbor(row,col+1);

}

if(row+1<=data.length&&col-1>=0){

showNeighbor(row+1,col-1);

}

if(row+1<=data.length){

showNeighbor(row+1,col);

}

if(row+1<=data.length&&col+1<=data.length){

showNeighbor(row+1,col+1);

}

data[row][col].setViewed();

}

}

public int getValue(int row, int col){

return data[row][col].getValue();

}

public void select(int row, int col) {

if (row > data.length || row < 0 || col > data.length || col < 0){

return;

}

if (data[row][col].getViewed() == false) {

if (!data[row][col].isBomb() && data[row][col].getNearbyBombs() == 0) {

data[row][col].setValue(data[row][col].getValue());

showNeighbor(row-1,col-1);

showNeighbor(row-1,col);

showNeighbor(row-1,col+1);

showNeighbor(row,col-1);

showNeighbor(row,col+1);

showNeighbor(row+1,col-1);

showNeighbor(row+1,col);

showNeighbor(row+1,col+1);

data[row][col].setViewed();

} else if (data[row][col].isBomb()) {

data[row][col].setViewed();

data[row][col].setValue(-1);

hasLost();

} else if (!data[row][col].isBomb() && data[row][col].getNearbyBombs() != 0){

data[row][col].setViewed();

data[row][col].setValue(data[row][col].getNearbyBombs());

}

}

}

public void markAsBomb(int row, int col){

}

public boolean hasWon(){

return false;

}

public boolean hasLost(){

return false;

}

}

Commented:
Change the constructorr to:
public MineSweeperDatabase(int row, int col, int numBombs) {
data = new Cell[row][col];

for (int i = 0; i < col; i++) {
for (int j = 0; j < row; j++) {
data[i][j] = new Cell(i, j);
}
}

boolean mine = false;
int count = 0;
while (!mine) {
for (int x = 1; x < col-1; x++) {
for (int j = 1; j < row-1; j++) {
System.out.println("x: " + x + ", j: " + j);
if (Math.random() > .8 && mine != true) {
data[x][j].setBomb();
data[x - 1][j - 1].setNearbyBombs();
data[x - 1][j].setNearbyBombs();
data[x - 1][j + 1].setNearbyBombs();
data[x][j - 1].setNearbyBombs();
data[x][j + 1].setNearbyBombs();
data[x + 1][j - 1].setNearbyBombs();
data[x + 1][j].setNearbyBombs();
data[x + 1][j + 1].setNearbyBombs();
count++;
}
if (count >= numBombs) {
mine = true;
}
}
}
}
}
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)

Commented:
I tried your example - but it still didn't work.  However, I made something that does work.  If your curious here is what does work.

public void showNeighbor(int row, int col) {

int result = 0;

int minx = (row <= 0 ? 0 : row - 1);

int miny = (col <= 0 ? 0 : col - 1);

int maxx = (row >= data.length - 1 ? data.length : row + 2);

int maxy = (col >= data.length - 1 ? data.length : col + 2);

for (int i = minx; i < maxx ; i++) {

for (int j = miny; j < maxy; j++) {

if (!data[i][j].isBomb()) {

if (!data[i][j].getViewed()) {

data[i][j].setValue(data[row][col].getNearbyBombs());

data[i][j].setViewed();

showNeighbor(i, j);

}

}

}

}

}

Commented:
I'm glad that you found a solution. I was happy to help! :-)

Commented:
Because you were a great help and provided multiple examples 250 points to you!

Commented:
Thank you very much! :-D
Unlock the solution to this question.