Question

This code will check each cell in a 12x12 grid to see if their value can spread or move. x is a standard floor tile, P is the player and I is the infection. The infection can duplicate itself to any adjacent tile whilst the player obviously moves from tile to tile. For some reason I keep getting an java.lang.ArrayIndexOutOfBoundsException: -1 error whenever I get to a certain number of turns.

Any ideas? (please save tweaking code til after the problem is fixed guys, thanks. :)

public static void tileMovement(char t, int i, int j) {
        Random rand = new Random();
        int dir = rand.nextInt(3);

        //System.out.println(t);
        if(t == 'x') {
        }
        else if(t == 'I') {
            double chance = Math.random();
            //System.out.println(chance);
            if(chance < 0.35) {
                myGrid[i][j] = t;
                switch(dir) {
                    case 0:
                        if(myGrid[i-1][j] == 'x'||i != 1){
                            myGrid[i-1][j] = t;
                            break;
                        }
                        else {
                            break;
                        }
                    case 1:
                        if(myGrid[i+1][j] == 'x'||i != 11){
                            myGrid[i+1][j] = t;
                            break;
                        }
                        else {
                            break;
                        }
                    case 2:
                        if(myGrid[i][j-1] == 'x'||j != 1){
                            myGrid[i][j-1] = t;
                            break;
                        }
                        else {
                            break;
                        }
                    case 3:
                        if(myGrid[i][j+1] == 'x'||j != 11){
                            myGrid[i][j+1] = t;
                            break;
                        }
                        else {
                            break;
                        }
                }
            }
        }
        else if(t == 'P'){
            myGrid[i][j] = 'x';                         // j = sideways, i = vertical
            switch(dir) {
                    case 0:
                            if(myGrid[i-1][j] == 'x'||myGrid[i-1][j] == 'I'||i != 1){
                                myGrid[i-1][j] = t;
                                break;
                            }
                            else {
                                break;
                            }
                    case 1:
                            if(myGrid[i+1][j] == 'x'||myGrid[i+1][j] == 'I'||i != 11){
                                myGrid[i+1][j] = t;
                                break;
                            }
                            else {
                                break;
                            }
                    case 2:
                            if(myGrid[i][j-1] == 'x'||myGrid[i][j-1] == 'I'||j != 1){
                                myGrid[i][j-1] = t;
                                break;
                            }
                            else {
                                break;
                            }
                    case 3:
                            if(myGrid[i][j+1] == 'x'||myGrid[i][j+1] == 'I'||j != 11){
                                myGrid[i][j+1] = t;
                                break;
                            }
                            else {
                                break;
                            }
            }
        }
    }
Was it helpful?

Solution

You are incorrectly determining the edge of the board. Here's one of your lines:

if(myGrid[i-1][j] == 'x'||i != 1){

First, indices start at 0. Second, check if you are on the left edge first before attempting to access the array. Do this by taking advantage of "short circuit" evaluation:

if (i != 0 && myGrid[i - 1][j] == 'x') {

Here, if i is 0, then the condition is false and myGrid[i - 1][j] == 'x' is never evaluated, and it doesn't have a chance to throw an ArrayIndexOutOfBoundsException.

You can change your other conditions similarly. You were correct about 11 being the max here.

Incidentally, in each case, you generally break in both cases of your if condition, and the else does nothing but break. It looks more readable and more concise to just break outside the if:

if (condition) {
    // perform operation
}
break;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top