0

I am new to Java programming and would like to seek your help. I'm trying to develop a simple minesweeper game using Java. However, I keep getting the error "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at practice.week.pkg4.PracticeWeek4.main(PracticeWeek4.java:55)"

This occurs when I'm trying to place digits around the square which has a bomb. I understand that perhaps the 1 has went out of the array, causing the exception to occur. However, I'm not sure how to go about catching the error. Would appreciate any kind help.

Eg: Sample Output

1 1 1

1 B 1

1 1 1

Here is my code snippet:

public static void main(String[] args) {
        // TODO code application logic here
        int rows = 9;
        int cols = 9;
        char[][] map = new char[rows][cols];
        int count = 0;


        for(int i = 0; i<map.length; i++)
        {
            for(int j = 0; j<map[i].length; j++)
            {
                map[i][j] = '.';
            }
        }

        Random rnd = new Random();
        do
        {
            int x = rnd.nextInt(rows);
            int y = rnd.nextInt(cols);

            for(int i = 0; i<map.length; i++)
            {
                for(int j = 0; j<map[i].length; j++)
                {


if(map[x][y] != 'B' && x > 0 & y > 0)
                { 
                    map[x][y] = 'B';
                    map[x-1][y-1] = '1'; 
                    map[x-1][y] = '1';
                    map[x-1][y+1] = '1';
                    map[x][y-1] = '1';
                    map[x][y+1] = '1';
                    map[x+1][y-1] = '1';
                    map[x+1][y] = '1';
                    map[x+1][y+1] = '1';
                    count++; 
                }

                }   

            }
        }
        while(count < 10);


        for(int x = 0; x<map.length; x++)
        {
            for(int y = 0; y <map[x].length; y++)
            {

            }
        }

        for(int x = 0; x<map.length; x++)
        {
            for(int y = 0; y<map[x].length; y++)
            {
                System.out.print(map[x][y] + " ");
            }
            System.out.println("");
        }



    }
12
  • 1
    x and y might be zero, thus, when subtracting 1 it could be -1 which is not a valid index causing the error Commented May 23, 2016 at 3:33
  • There is no negative index -1 : map[0-1][0-1] = '1' ==> map[-1][-1] = '1' Commented May 23, 2016 at 3:35
  • How do i go about rectifying it? @AndrewL Commented May 23, 2016 at 3:35
  • 2
    Add an if statement that checks if x or y are 0. For example: if(x > 0 && y > 0){ map[x-1][y-1]='1'; } Commented May 23, 2016 at 3:36
  • Thank u all! it works fine now. I just have another qn. What if i want the 1 to become a 2 or 3? how can i go about doing it then? This is based on the number of bombs around the square similarly. Commented May 23, 2016 at 3:38

1 Answer 1

1

The do-while loop for setting the mines is on the right track, but the way you are updating the counts for surrounding blocks is causing the IndexOutOfBoundsException. And these two loops

for(int i = 0; i < map.length; i++)
{
    for(int j = 0; j < map[i].length; j++)

serve no purpose. You need to rearrange it to handle multiple mines, etc, so why not set all the mines first:

do
{
    int x = rnd.nextInt(rows);
    int y = rnd.nextInt(cols);
    if (map[x][y] != 'B')
    {
        map[x][y] = 'B';
        count++;
    }
} while(count < 10);

Then go through the map, and count the number of mines surrounding each block:

for (int x = 0; x < map.length; x++)
{
    for (int y = 0; y < map[x].length; y++)
    {
        if (map[x][y] == 'B')
            continue;

        // Count the number of mines around map[x][y]
        int mines = 0;
        for (int xOffset = -1; xOffset <= 1; xOffset++)
        {
            // This is an important step - without it, we will access elements off the edge of the map
            if (x + xOffset < 0 || x + xOffset >= map.length)
                continue;

            for (int yOffset = -1; yOffset <= 1; yOffset++)
            {
                // Another check for the edge of the map
                if (y + yOffset < 0 || y + yOffset >= map[x].length)
                    continue;

                if (map[x + xOffset][y + yOffset] == 'B')
                    mines++;
            }
        }

        map[x][y] = "012345678".charAt(mines); // Get the number as a character
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi Andrew, i don't get what the last line does! can you explain to me?
It converts a number into its' character equivalent. So if you have the number 6, and you get the character at that index, it will be '6'. And it puts that number into the map. You may want to use a space instead of '0' though...
I changed it to a ',' instead. Can you also explain about the offset for loops and if? the code works but i don't really understand it.
The offsets go between -1 and +1. So when you add that range to [x, y], you get a 3X3 grid of all the pixels around [x,y]. If the X offset means that we go off the edge of the map, then we skip that offset. Same goes for the Y offset.
So its like a subarray of our main array?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.