Search code examples
javaarraysmultidimensional-arrayminesweeper

Java- Returns incorrect values while working with multidimensional Arrays


I am working on Minesweeper game, and every thing looked fine, exactly till yesterday's morning: I got a problem which I can not solve by my own. The situation looks like this: I got 2 two-dimensional arrays, in one i have generated Minesweeper field with all values generated, where 9 means bomb, and 0 means free space. Lets call it field. It is an integer array. Basically it is 9x9 field. It has +1 row of zeros as a frame for a proper work of code. Otherwise it returns OutOfBoundsError when running code listed bellow. Example:

00000000000 0
00000019100 1
01110011100 2
02910000000 3
09321110000 4
02911910000 5
01111111110 6
01110112910 7
02920192110 8
02920111000 9
01110000000 10
00000000000 11

My second array is String array user_field and it is blank (all null field are replaced with .), here by entering commands user will open . fields.

........... 1
........... 2
........... 3
........... 4
........... 5
........... 6
........... 7
........... 8
........... 9
........... 10
........... 11
........... 12

It must output me on start all 0 from field, and all values around them. Example:

nnn 1
n0n 2
nnn 3

I use this code to call it:

       for (int i = 1; i < field_h-2; i++) {
            for (int j = 1; j < field_w-2; j++) {
                if (user_field[i][j]==null) {
                    user_field[i][j]=".";
                }
                if(field[i][j]==0) {
                    for(int k = 0; k< 3; k++) {
                        for (int n = 0; n< 3; n++) {
                            user_field[i-1+k][j-1+n] = String.valueOf(field[i-1+k][j-1+n]);
                            //System.out.print(String.valueOf(field[i-1+k][j-1+n]));
                        }
                    }
                }
                System.out.print(user_field[i][j]);
            }
            System.out.println(" "+ (i));
        }

But in reality, it outputs (n-means value, x-means nothing.):

xxx 1
x0n 2
nnn 3

I don't have any ideas why this happens. Can someone help me please? I am not so experienced in Java, maybe I don't understand something.


Solution

  • I will explain the problem only considering the second line (the one that is x0n), but my reasoning applies to the previous and next line naturally.

    Your program does this:

    1. Originally user_field is xxx, and field is n0n (let's say it's 102).

    2. You look at the first element of field. It is 1, and as such it is not 0, so you do not make any changes to user_field. Then you immediately print first element of user_field, which at that point is still x. So so far you printed x.

    3. You move to the second element of field. It is zero, so you update first and third elements of user_field. But at that point you already printed the first element of user_field. Now field is 102, user_field is 102, but you have already printed x. Now you print the second element of user_field, so you end up printing x0.

    4. You move to the third element of field. It is not 0, so you make no changes. You print the third element of user_field, which at this time is 2, so you end up printing x02, while the desired output is 102.

    To fix it, split your loop in two. In the first loop compute user_field, and in the second loop print it. Something along the lines of:

       for (int i = 1; i < field_h-2; i++) {
            for (int j = 1; j < field_w-2; j++) {
                if (user_field[i][j]==null) {
                    user_field[i][j]=".";
                }
                if(field[i][j]==0) {
                    for(int k = 0; k< 3; k++) {
                        for (int n = 0; n< 3; n++) {
                            user_field[i-1+k][j-1+n] = String.valueOf(field[i-1+k][j-1+n]);
                            //System.out.print(String.valueOf(field[i-1+k][j-1+n]));
                        }
                    }
                }
            }
        }
    
       for (int i = 1; i < field_h-2; i++) {
            for (int j = 1; j < field_w-2; j++) {
                System.out.print(user_field[i][j]);
            }
            System.out.println(" "+ (i));
        }