• 12-10-2011, 01:45 PM
jedi92
Hello, I've been trying to code a very simple minesweeper game, but i find myself stuck when i try to update my non-mine cells in a 2d array to clues (number of mines surrounding that particular cell). If someone can please help me, i would really appreciate it. Thank you.

Code:
public static void InitMap () { // randomize map
int r, c, a_num;
a_num=0;
Random my_rand_obj = new Random();// use Random class to create a rand_obj

for (r = 0; r <= map.length-1; r++)
{
for (c = 0; c <= map[r].length-1; c++){
a_num = my_rand_obj.nextInt(10);
map[r][c] = a_num;
if (a_num > THRESHOLD){
map[r][c] = 9;
}
else {
map[r][c] = 0;
}
}
System.out.println(); // continue in next line
}
for (r = 0; r <= map.length-1; r++)
{
for (c = 0; c <= map[r].length-1; c++){
if (map[r][c] == 0){
map[r][c]=CountMines(r,c);

}

}
}

} // end InitMap()

public static int CountMines( int r, int c ) {
int count = 0; // count # of mines in neighbors

if ( (r-1) >= 0 && (c-1) >= 0 && map[r-1][c-1] == 9 )
{
count++;
}
if ( (r-1) >= 0 && (c) >= 0 && map[r-1][c] == 9 )
{
count++;
}
if ( (r-1) >= 0 && (c+1) >= 0 && map[r-1][c+1] == 9 ) // error out of bounds
{
count++;
}
if ( (r) >= 0 && (c-1) >= 0 && map[r][c-1] == 9 )
{
count++;
}
if ( (r) >= 0 && (c+1) >= 0 && map[r][c+1] == 9 ) // error out of bounds
{
count++;
}
if ( (r+1) >= 0 && (c-1) >= 0 && map[r+1][c-1] == 9 )
{
count++;
}
if ( (r+1) >= 0 && (c) >= 0 && map[r+1][c] == 9 )
{
count++;
}
if ( (r+1) >= 0 && (c+1) >= 0 && map[r+1][c+1] == 9 )
{
count++;

}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at MS2D.CountMines(MS2D.java:106)
at MS2D.InitMap(MS2D.java:170)
at MS2D.main(MS2D.java:20)
is the error i get. Is there any way to fix this?
• 12-10-2011, 02:30 PM
Fubarable
You need to post a specific question, not just requirements and a code dump.
• 12-10-2011, 04:07 PM
JosAH
Quote:

Originally Posted by jedi92
Code:

```                 if (  (r+1) >= 0 && (c+1) >= 0 &&  map[r+1][c+1] == 9 )```

The values r and/or c might be in range but the values r+1 and/or c+1 might be out of range (i.e. too large) ...

kind regards,

Jos
• 12-10-2011, 09:24 PM
jedi92
sorry about that. The method countmines should be couting the 8 neighboring cells around the center cell. For some reason it works when i'm at row= any number, column= 0 which is the first column and thus only have 3 neighboring cells. I would think i would get an index out of bounds since c-1 = -1 but i dont, however, when i pick a cell in row= last row, column= (any column) it goes out of bound and throws the error outofbounds=10. I suppose it was because of adding r+1 in this line if ( (r+1) >= 0 && (c+1) >= 0 && map[r+1][c+1] == 9 ), what i would like know now is, is there any way so fix this to avoid an error and have the program ignore the out of bounds and just count the neighboring cells that exist around this cell? I was thinking either writing an excpetion argument, or just simply write a bunch more code for every specific case.
the grid i have, and i should probably had included this in my first post is : map[10][9].
thank you for taking your time.
• 12-10-2011, 10:14 PM
Norm
Quote:

have the program ignore the out of bounds and just count the neighboring cells that exist around this cell?
That's sort of sloppy coding. Most programs can test if the index will be out of bounds and not use the index if it will be.
• 12-10-2011, 11:43 PM
jedi92
yeah, im new to java, but i solved the problem. I just deleted everything and started from scratch again. This time instead of putting so many && i split the statements in two so the program wont assume certain cell exists and wont throw me the outofbounds exception. It worked! I finally get to get some sleep. Thanks anyways
• 12-10-2011, 11:46 PM
Norm
That sounds like a good approach. Making if tests too complicated can make for problems.
• 12-11-2011, 10:06 AM
dheaven
Quote:

Originally Posted by jedi92
Hello, I've been trying to code a very simple minesweeper game, but i find myself stuck when i try to update my non-mine cells in a 2d array to clues (number of mines surrounding that particular cell). If someone can please help me, i would really appreciate it. Thank you.

Code:
public static void InitMap () { // randomize map
int r, c, a_num;
a_num=0;
Random my_rand_obj = new Random();// use Random class to create a rand_obj

for (r = 0; r <= map.length-1; r++)
{
for (c = 0; c <= map[r].length-1; c++){
a_num = my_rand_obj.nextInt(10);
map[r][c] = a_num;
if (a_num > THRESHOLD){
map[r][c] = 9;
}
else {
map[r][c] = 0;
}
}
System.out.println(); // continue in next line
}
for (r = 0; r <= map.length-1; r++)
{
for (c = 0; c <= map[r].length-1; c++){
if (map[r][c] == 0){
map[r][c]=CountMines(r,c);

}

}
}

} // end InitMap()

public static int CountMines( int r, int c ) {
int count = 0; // count # of mines in neighbors

if ( (r-1) >= 0 && (c-1) >= 0 && map[r-1][c-1] == 9 )
{
count++;
}
if ( (r-1) >= 0 && (c) >= 0 && map[r-1][c] == 9 )
{
count++;
}
if ( (r-1) >= 0 && (c+1) >= 0 && map[r-1][c+1] == 9 ) // error out of bounds
{
count++;
}
if ( (r) >= 0 && (c-1) >= 0 && map[r][c-1] == 9 )
{
count++;
}
if ( (r) >= 0 && (c+1) >= 0 && map[r][c+1] == 9 ) // error out of bounds
{
count++;
}
if ( (r+1) >= 0 && (c-1) >= 0 && map[r+1][c-1] == 9 )
{
count++;
}
if ( (r+1) >= 0 && (c) >= 0 && map[r+1][c] == 9 )
{
count++;
}
if ( (r+1) >= 0 && (c+1) >= 0 && map[r+1][c+1] == 9 )
{
count++;

}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at MS2D.CountMines(MS2D.java:106)
at MS2D.InitMap(MS2D.java:170)
at MS2D.main(MS2D.java:20)
is the error i get. Is there any way to fix this?

If you need some inspiration, you may want to check out an already finished Minesweeper game:
Dystopian Code: Minesweeper Clone in Java AWT