Solved

MineSweeper Recursive Question

Posted on 2004-04-02
16
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

623 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