Skip to main content
1 of 5

I would strongly suggest to change how your grid is represented in memory. Imagine you have a 10000x10000 grid and just 1 cell at coordinates (5,5) that's alive.

If you use an array to store that information, you have to create thousands upon thousands of data points pretty much all of which basically say nothing but “nothing to see here”. Dead cells are the default and therefore there's not really much we need to hold in memory about them.

So let's use a HashSet that only stores the alive cells. If the HashSet contains a given coordinate, the corresponding cell is alive. Otherwise the HashSet returns false, meaning it is dead. Now you can have a virtually infinite grid which, especially if mostly empty (as a Game of Life usually is), hardly needs any memory.

My Java is super rusty so consider the following something like pseudo code, but you get the idea:

public class Coordinate {
    public int x, y;
}

public class Grid {
    private HashSet grid;
   
    public void setAlive(Coordinate c) {
        grid.add(c);
    }

    public void setDead(Coordinate c) {
        grid.remove(c);
    }

    public boolean isAlive(Coordinate c){
        return grid.contains(c);
    }

}
```