Game Of Life game: Check 2D array 8 closest surroundings; ArrayIndexOutOfBoundsException issue

Tags: , , , ,



I have encountered this problem and i couldn’t find the solution. There is a 2D array with only zeros and ones in it (the size of the array doesnt really matter, i worked with 10×10). Zero means dead and 1 is alive. I double for looped this to check the elements sorroundings and when it sorrounded with other “cells” the code would work fine. But when its in the corner or one of the other edge of the array then it throws an ArrayIndexOutOfBoundsException. My question is how to write a code for this without handle all the possible situation?

public class Main {

public static void main(String[] args) {
    int[][] grid = {
            {1,1,1,0,1,0,1,0,0,0},
            {1,1,1,0,1,1,0,0,1,0},
            {1,1,0,0,1,0,0,1,0,0},
            {0,1,0,1,0,0,1,0,0,0},
            {0,0,1,0,0,1,0,0,1,0},
            {0,0,0,0,0,0,0,0,1,0},
            {0,0,0,1,0,0,0,1,0,0},
            {0,1,0,0,0,0,0,0,0,0},
            {0,0,0,1,0,0,1,0,0,0},
            {0,1,0,0,0,0,0,0,1,0},
    };

    Simulation sm = new Simulation(grid);
    sm.printOutOriginalGrid();
    sm.gameOfLife();
}

}

public class Simulation {

private int[][] grid;

public Simulation(int[][] grid){
    this.grid = grid;
}
public  void printOutOriginalGrid() {
    System.out.println("Original grid");
    for (int i = 0; i < this.grid.length; i++) {
        for (int j = 0; j < this.grid[i].length; j++) {
            System.out.print(this.grid[i][j] + "  ");
        }
        System.out.println(" ");
    }
}

public int[][] gameOfLife() {
    for (int i = 0; i < this.grid.length; i++) {
        for (int j = 0; j < this.grid.length; j++) {
            int currentCell = this.grid[i][j];
            if(currentCell == 1){
                int currentCellNeighbours = numberOfAliveNeighbours(i,j); 
            }
        }
    }

    return new int[12][12];
}

private int numberOfAliveNeighbours(int i, int j){
    int numberOfAliveNeighbours = 0;

    numberOfAliveNeighbours += this.grid[i-1][j-1];
    numberOfAliveNeighbours += this.grid[i][j-1];
    numberOfAliveNeighbours += this.grid[i+1][j-1];

    numberOfAliveNeighbours += this.grid[i-1][j];
    numberOfAliveNeighbours += this.grid[i+1][j];

    numberOfAliveNeighbours += this.grid[i-1][j+1];
    numberOfAliveNeighbours += this.grid[i][j+1];
    numberOfAliveNeighbours += this.grid[i+1][j+1];

    return numberOfAliveNeighbours;
}

}

Answer

In the numberOfAliveNeighbours method, you have to test for an array index less than zero or greater than the size of the array – 1.

In other words, an int array 8 values long has index values from 0 – 7.

Here’s your code fixed up.

public class GameOfLife {

    public static void main(String[] args) {
        int[][] grid = { 
                { 1, 1, 1, 0, 1, 0, 1, 0, 0, 0 }, 
                { 1, 1, 1, 0, 1, 1, 0, 0, 1, 0 },
                { 1, 1, 0, 0, 1, 0, 0, 1, 0, 0 }, 
                { 0, 1, 0, 1, 0, 0, 1, 0, 0, 0 }, 
                { 0, 0, 1, 0, 0, 1, 0, 0, 1, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 1, 0 }, 
                { 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, 
                { 0, 1, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0 }, 
                { 0, 1, 0, 0, 0, 0, 0, 0, 1, 0 }, };

        Simulation sm = new GameOfLife().new Simulation(grid);
        sm.printOutOriginalGrid();
        sm.gameOfLife();
    }

    public class Simulation {

        private int[][] grid;

        public Simulation(int[][] grid) {
            this.grid = grid;
        }

        public void printOutOriginalGrid() {
            System.out.println("Original grid");
            for (int i = 0; i < this.grid.length; i++) {
                for (int j = 0; j < this.grid[i].length; j++) {
                    System.out.print(this.grid[i][j] + "  ");
                }
                System.out.println(" ");
            }
        }

        public int[][] gameOfLife() {
            for (int i = 0; i < this.grid.length; i++) {
                for (int j = 0; j < this.grid[i].length; j++) {
                    int currentCell = this.grid[i][j];
                    if (currentCell == 1) {
                        int currentCellNeighbours = 
                                numberOfAliveNeighbours(i, j, grid.length);
                    }
                }
            }

            return new int[12][12];
        }

        private int numberOfAliveNeighbours(int i, int j, int limit) {
            int numberOfAliveNeighbours = 0;
            int a = i - 1;
            int b = i + 1;
            int c = j - 1;
            int d = j + 1;

            if (c >= 0) {
                if (a >= 0) {
                    numberOfAliveNeighbours += this.grid[a][c];
                }
                numberOfAliveNeighbours += this.grid[i][c];
                if (b < limit) {                    
                    numberOfAliveNeighbours += this.grid[b][c];
                }
            }

            if (a >= 0) {
                numberOfAliveNeighbours += this.grid[a][j];
            }
            if (b < limit) {
                numberOfAliveNeighbours += this.grid[b][j];
            }

            if (d < limit) {
                if (a >= 0) {
                    numberOfAliveNeighbours += this.grid[a][d];
                }
                numberOfAliveNeighbours += this.grid[i][d];
                if (b < limit) {
                    numberOfAliveNeighbours += this.grid[b][d];
                }
            }

            return numberOfAliveNeighbours;
        }

    }

}


Source: stackoverflow