-1

I'm making Java Minesweeper for as a simple project, but the program can never count the correct number of bombs. Here is the function, I've written, numFunction() that finds all possible combinations of touching x and y cords and iterates over all of them together. Note that I've used try/catch here to eliminate any index errors:

public static int numTouching(int x, int y){
    int n=0;
    //define every possible coordinate touching the bomb 
    //iterate through the lists of coordinates to count the number of touching bombs
    int[]xCords = {x-1, x, x+1};
    int[]yCords = {y-1, y, y+1};        
    if(board[x][y] != "B"){
        try{
            for(int i=0; i<xCords.length; i++){
                for(int j=0; j<yCords.length; j++){
                int xPos = xCords[i];
                int yPos= yCords[j];
                if(board[xPos][yPos]=="B" && board[xPos][yPos]!=board[x][y]){
                    n++;
                    board[x][y]=n;
                    }
                }
            }
            
        }
        catch(Exception e){}
    }
    return n;
        }

Here is the sample output for this program (I want to make it so that all of the elements touching B are set to 1):

Column count = 3
0   0   0   
0   B   0   
0   1   1   
Num touching (0,1): 0
Num touching (1,0): 0
0   0   0   
0   B   0   
0   1   1

The weird thing is that it changes depending on the order of the elements in xCords and yCords (it seems that only the first two values of each array matters). For instance if xCords = {x, x-1, x+1}, and yCords = {y, y-1, y+1}, this is the sample output:

Column count = 3
0   0   0   
0   B   1   
0   1   0   
Num touching (0,1): 0
Num touching (1,0): 0
0   0   0   
0   B   1   
0   1   0   

Thanks in advance.

Wisely
  • 51
  • 2
  • 5
  • 2
    "Note that I've used try/catch here to eliminate any index errors:" ...but when you start close to the left or top edge and get an exception, then the loops will STOP and you won't check the rest of the grid! – Idle_Mind Jan 19 '23 at 13:24
  • 1
    These `xCords` and `yCords` arrays are entirely unnecessary. Just loop like `for(int xPos = x-1; xPos <= x+1; xPos++) { for(int yPos = y-1; yPos <= y+1; yPos++) { … } }` Besides that, as Idle_Mind said, don’t “handle” exceptions this way. And don’t compare strings using `==`. In your case, it seems the strings in `board` are just tokens, so it’s cleaner to define a dedicated `enum` type for this use case. – Holger Jan 19 '23 at 13:34
  • Also, 2D arrays are typically accessed ROW `y` first, then COLUMN `x` second, like: `board[row][col]` – Idle_Mind Jan 19 '23 at 13:37
  • `if(board[x][y] != "B")` *will* fail eventually. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – VGR Jan 19 '23 at 16:36

1 Answers1

0

Your problem is due to java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds, which you cannot see because you are not printing your exceptions in your catch statement. Please add a e.printStackTrace(); into your catch statement to see this in action.

All you need to do is check if your indices are valid, the following code placed right above your =="B" check works for me (also as Holger stated, you should use .equals("B") instead of =="B")

if (xPos < 0 || yPos < 0 || xPos >= board.length || yPos >= board[0].length) {
    continue;
}
ellis
  • 126
  • 3