Solved

MineSweeper Recursive Question

Posted on 2004-04-02
16
468 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
javap not working 8 58
hibernate jars 4 45
pagenation logic how it is working in my code 1 52
how to debug htl and js pages 8 38
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

789 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