Solved

MineSweeper Recursive Question

Posted on 2004-04-02
16
455 Views
Last Modified: 2008-02-01
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[0].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?
0
Comment
Question by:mrfantastic
  • 6
  • 6
  • 4
16 Comments
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10746488
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[0].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();          
            }
        }

0
 

Author Comment

by:mrfantastic
ID: 10747970
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
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10748733
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.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10748859
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[0].length){

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

etc ...

0
 

Author Comment

by:mrfantastic
ID: 10750453
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[0].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[0].length){

                showNeighbor(row-1,col+1);

              }

              if(col-1>=0){

                showNeighbor(row,col-1);

              }

              if(col+1<=data[0].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[0].length){

                showNeighbor(row+1,col+1);

              }

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

          }

    }



ANY IDEAS - Points raised!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10750903
What runtime exception is thrown exactly? Difficult to solve a problem without all the facts.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 10751179
>> some sort of run time error when ...

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

Author Comment

by:mrfantastic
ID: 10751606
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.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 16

Expert Comment

by:krakatoa
ID: 10753365
You'd need to capture the stack trace and supply that, along with the surrounding source.
0
 

Author Comment

by:mrfantastic
ID: 10753680
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[0].length){

                showNeighbor(row-1,col+1);

              }

              if(col-1>=0){

                showNeighbor(row,col-1);

              }

              if(col+1<=data[0].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[0].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[0].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;

    }

}

0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10754363
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;
                              }
                        }
                  }
            }
      }
0
 
LVL 14

Accepted Solution

by:
Tommy Braas earned 250 total points
ID: 10754371
Simplify showNeighbor() method (if you wish):

      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) {
                        showNeighbor(row - 1, col);
                        if (col - 1 >= 0) {
                              showNeighbor(row - 1, col - 1);      
                        }
                        if (col + 1 <= data[0].length) {
                              showNeighbor(row - 1, col + 1);                              
                        }
                  }
                  if (col - 1 >= 0) {
                        showNeighbor(row, col - 1);
                  }
                  if (col + 1 <= data[0].length) {
                        showNeighbor(row, col + 1);
                  }
                  if (row + 1 <= data.length) {
                        showNeighbor(row + 1, col);
                        if (col - 1 >= 0) {
                              showNeighbor(row + 1, col - 1);
                        }
                        if (col + 1 <= data[0].length) {
                              showNeighbor(row + 1, col + 1);
                        }
                  }
                  data[row][col].setViewed();
            }
      }
0
 

Author Comment

by:mrfantastic
ID: 10754409
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[0].length - 1 ? data[0].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);

           }

         }

       }

     }

   }
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10754870
I'm glad that you found a solution. I was happy to help! :-)
0
 

Author Comment

by:mrfantastic
ID: 10756516
Because you were a great help and provided multiple examples 250 points to you!
0
 
LVL 14

Expert Comment

by:Tommy Braas
ID: 10757455
Thank you very much! :-D
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now