Results 1 to 8 of 8
  1. #1
    jedi92 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default MineSweeper java, index out of bounds exception. Please help

    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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: MineSweeper java, index out of bounds exception. Please help

    You need to post a specific question, not just requirements and a code dump.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default Re: MineSweeper java, index out of bounds exception. Please help

    Quote Originally Posted by jedi92 View Post
    Java 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
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    jedi92 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: MineSweeper java, index out of bounds exception. Please help

    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.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: MineSweeper java, index out of bounds exception. Please help

    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.

  6. #6
    jedi92 is offline Member
    Join Date
    Dec 2011
    Posts
    3
    Rep Power
    0

    Default Re: MineSweeper java, index out of bounds exception. Please help

    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

  7. #7
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default Re: MineSweeper java, index out of bounds exception. Please help

    That sounds like a good approach. Making if tests too complicated can make for problems.

  8. #8
    dheaven is offline Member
    Join Date
    Oct 2011
    Posts
    15
    Rep Power
    0

    Default Re: MineSweeper java, index out of bounds exception. Please help

    Quote Originally Posted by jedi92 View Post
    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

Similar Threads

  1. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 11:27 AM
  2. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 10:24 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-18-2010, 11:25 PM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 04:02 PM
  5. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 07:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •