0

I want to check the 8th positions adjacent to each of the positions of my two-dimensional array but the firsts ones give me error because there aren't positions there.

How can I avoid that error? The same happened to me in the past and I couldn't solve it neither.

Example image of what I want to do:

enter image description here

Code of my method:

 private static void evoluciona(char[][] tauler2, char[][] tauler22) {

        for(int i = 0; i < files; i++) {
            for(int j = 0; j < columnes; j++) {
                tauler2[i][j] = tauler[i][j];
            }
        }

        for(int i = 0; i < files; i++) {
            for(int j = 0; j < columnes; j++) {
                if(tauler2[i][j] == 'x') {

                    int cont = 0;

                    if(tauler2[i-1][j] == '*') {
                        cont++;
                    }

                    if(tauler2[i-1][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i-1][j+1] == '*') {
                        cont++;
                    }

                    if(tauler2[i][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j+1] == '*') {
                        cont++;
                    }

                    if(tauler2[i][j+1] == '*') {
                        cont++;
                    }

                    if(cont == 2 || cont == 3) {
                        tauler2[i][j] = '*';
                    }
                }

                else if(tauler2[i][j] == '*') {

                    int cont = 0;

                    if(tauler2[i-1][j] == '*') {
                        cont++;
                    }

                    if(tauler2[i-1][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i-1][j+1] == '*') {
                        cont++;
                    }

                    if(tauler2[i][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j-1] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j] == '*') {
                        cont++;
                    }

                    if(tauler2[i+1][j+1] == '*') {
                        cont++;
                    }

                    if(tauler2[i][j+1] == '*') {
                        cont++;
                    }

                    if(cont == 2 || cont == 3) {
                        tauler2[i][j] = '*';
                    }

                    else {
                        tauler2[i][j] = 'x';
                    }
                }
            }
        }   
    }
Óscar73
  • 37
  • 6
  • 1
    Ignoring the exception doesn't solve the problem. – MarsAtomic Feb 12 '18 at 19:13
  • It sure does... – Idos Feb 12 '18 at 19:14
  • @Idos You mistake a matter of education for a matter of code. Hiding the exception will allow the code to run for in-bounds cases, but it doesn't help OP understand what his code is doing. As he explicitly mentioned, he's run into this type of problem before without understanding how to resolve it. Stack Overflow is about learning, not "fix my code for me." – MarsAtomic Feb 12 '18 at 19:26

4 Answers4

2

I had a similar problem when programming my own little Minesweeper clone around christmas.

I ended up with a field class that knows its neighbors.

class Field {
    char value;
    Field[] neighbors;
}

Whenever initializing a new board those 'connections' were sort of precompiled.

class FieldManager {

    void initializeFields(int firstDimension, int secondDimension) {
        fields = new Field[firstDimension][secondDimension];
        for(int i = 0; i < fields.length; i++) {
            for(int j = 0; j < fields[i].length; j++) {
                fields[i][j] = new Field();
                fields[i][j].neighbors = neighborsFor(i, j);
            }
        }
    }

    private Field[] neighborsFor(int iIn, int jIn) {
        List<Field> result = new ArrayList<>();
        for(int i = iIn - 1; i <= iIn + 1; i++) {
            for(int j = jIn - 1; j < jIn + 1; j++)
                // do some range checks and fill result
            }
        }
        return result.toArray(new Field[result.size()]);
    }

    Field[][] fields;
}

Instead of array you can as well use Collections, such as ArrayList. Especially the newer java versions have a lot of useful utility built around those interfaces.

Izruo
  • 2,246
  • 1
  • 11
  • 23
1

I'd recommend explicitly checking against boundaries! Modify your if statements, so that whenever you have i+1, j+1 you check it's not too large ( i < files and j < columnes), and i-1, j-1 that they are not 0 ( i > 0 and j > 0).

As you are counting neighboring stars, this method will not add extra stars if you are on the border.

Alternatively, the "lazy way", you can try{} catch(ArrayOutOfBoundsException e) { continue; } inside the inner loops!

Balint Pato
  • 1,497
  • 1
  • 13
  • 28
  • That "try{} catch(ArrayOutOfBoundsException e) { continue; }" made the trick! Many thanks! – Óscar73 Feb 12 '18 at 19:28
  • 1
    There are situations where ignoring exceptions is an appropriate thing to do, but I'd say that's rare (or possibly non-existent for out-of-bounds exceptions). Ignoring exceptions is generally a bad practice which new developers should avoid in favour of focusing on understanding the reason for the exception and learning how to write code to *properly* avoid it. @Óscar73 – Bernhard Barker Feb 12 '18 at 19:42
1

In all the places that you have an index of i-1 or j-1, on the first iteration of either cycle (i or j) you can have an index of -1 as per your exception in the stack trace, which causes the ArrayIndexOutOfBoundsException.

Instead of catching it and ignoring it, place an if around the areas that can cause this exception before doing anything.

// your code
...

if (i - 1 >= 0) {
    if(tauler2[i-1][j] == '*') {
        cont++;
    }
}

...

and likewise in the case of tauler2[i][j-1], a condition check on j - 1 >= 0.

geco17
  • 5,152
  • 3
  • 21
  • 38
1

you are accessing such an index that is not in array. As you are starting forloops i-e i=0 and j=0, and in if conditions u r using indexes that are [i-1] and [j-1]. here u r getting exception of index out of bounds. eliminate or change these conditions.

hassan mirza
  • 139
  • 2
  • 8