Search code examples
javaminesweeper

minesweeper surrounding mines works half the time


I'm writing minesweeper clone in Java, and I've been having some trouble with the part where the surrounding number of bombs are calculated. For some reason, either some cells detect additional bombs, or no bombs at all (even weirdly, some cells work fine). Could someone help? Thanks!

note: key[][] is an int[][] array that records the locations of the bombs, designated as 9.

0 represents an empty space

int count represents the amount of bombs that are in the 8 cells surrounding each cell (that's why I also have 8 try-catch loops to account for the border cells)

ps: sorry for the bad formatting

EDIT: I figured out where my problem was (i called i twice). Any suggestions as to how I could make this code simpler and more efficient?

private void numberSet() {

    int count = 0;
    for (int i = 0; i < key.length; i++) {
        for (int a = 0; a < key[0].length; a++) {

            if (key[i][a] == 0) {

                try {
                    if (key[i + 1][a] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][a] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i][a - 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i + 1][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][a + 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i + 1][a - 1] == 9) {
                        count++;
                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                try {
                    if (key[i - 1][i - 1] == 9) {
                        count++;

                    }

                } catch (java.lang.ArrayIndexOutOfBoundsException e) {
                    continue;
                }
                key[i][a] = count;
            }
            count = 0;
        }
    }

}

Solution

  • I would seriously consider completely abandoning the way you're doing this. When I first started to code, I too made a minesweeper game, and I did what you were doing. After a few revisions, I was able to put all of it into a few loops. I tried to comment enough in my code to tell you what every line does. This code is not tested, so it probably won't work right away to be completely honest. However, I looked through it a couple of times and it seems like it will work to me. I suggest you take this code and try to make it work. Give it a try.

    //For every Cell on the board
    for (int i = 0; i < key.length; i++){
        for (int a = 0; a < key[0].length; a++){
    
            //If it Is not a mine
            if (key[i][a] == 0){ 
                int count = 0;
    
                //Position of cells around key[i][a] relative to key[i][a]
                for (int x = -1; x < 2; x++){  
                    for (int y = -1; y < 2; y++){ 
    
                        //Storing x and y test points
                        int testX = i + x;//testX = i-1, i, and i+1
                        int testY = i + y;//testY = i-1, y, and y+1
    
                        //If the testX and testY values are within the range of the array
                        if ((testX >= 0 && testX < key[0].length) && testY >= 0 && testY < key.length){
    
                            //If there is a mine
                            if (key[testX][testY] == 9){
                                count++;
                            }
                        }
                    }
                }
    
                key[i][a] = count;
    
                //count = 0; This is redundant. Line 7 count gets set to 0.
            }
        }
    }