Conway Game of Life in Java
Articles —> Conway Game of Life in Java
Conway's Game of Life is a simple cellular automata often represented visually in 2D space by a 2-dimensional grid. Within this grid are cells, each of which can have two states - alive or dead. A random or predetermined state - also known as a seed - initializes the Game of Life, after which the grid is ran through several iterations, within each iteration the rules determine the state of each cell. Although simple rules and even random seeds determine the future states of the Game of Life, unique shapes and behaviors emerge within the grid, for instance shapes which alternate states, cells (or groups of cells) which remain constant, and even groups of cells which move across the grid.
The rules of the Game of Life are quite simple:
- Any live cell remains alive if it has 2 or 3 neighbors, otherwise the cell dies (as if from overcrowding or undercrouding).
- Any dead cell with exactly 3 neighbors comes to life.
Below is a java class which implements the Game of Life - including the grid, a random seed, and the rules:
/** * Java class for simulation of Conway's Game of Life. * @author G. Cope * */ public class Conway2D { private final int width; private final int height; private final int size; private int seedCount = 9500; /* * Data representing the grid in 1d format. Specified as byte to allow for multiple * phases. */ byte[] data; public Conway2D(){ this(100, 100); } /** * Constructs a new Game of Life with the specified dimensions. * @param width * @param height */ public Conway2D(int width, int height){ this.width = width; this.height = height; this.size = width * height; data = new byte[size]; } /** * Iterates the game one step forward */ public void iterate(){ byte[] prev = new byte[size]; System.arraycopy(data, 0, prev, 0, size); byte[] next = new byte[size]; for ( int i = 0; i < width; i++ ){ for ( int j = 0; j < height; j++ ){ int type = isAlive(i, j, prev); if ( type > 0 ){ next[j * width + i] = 1; }else{ next[j * width + i] = 0; } } } System.arraycopy(next, 0, data, 0, size); } /** * Checks if the cell is alive * @param x The x position * @param y The y position * @param d The grid data. * @return */ protected int isAlive(int x, int y, byte[] d){ int count = 0; int pos1 = y * width + x; for ( int i = x-1; i <= x + 1; i++ ){ for ( int j = y - 1; j <= y + 1; j++ ){ int pos = j * width + i; if ( pos >= 0 && pos < size - 1 && pos != pos1){ if ( d[pos] == 1 ){ count++; } } } } //dead if ( d[pos1] == 0 ){ if ( count == 3 ){//becomes alive. return 1; } return 0;//still dead }else{//live if ( count < 2 || count > 3 ){//Dies return 0; } return 1;//lives } } /** * Randomly seeds the grid. */ public void randomSeed(){ for ( int i = 0; i < seedCount; i++ ){ int x = (int)(Math.random() * width); int y = (int)(Math.random() * height); data[y * width + x] = 1; } } /** * Retrieves the grid data. * @return */ public byte[] getData(){ return data; } }
The above java class is quite simplistic, and can be utilized by first initializing the seed followed by subsequent calls to the iterate method. Visualizing the grid is easy within java Swing, where one can watch the randomness come to life in the form of 'blinkers', 'beacons', 'glider's, and 'pulsars'.
There are ample examples available depicting the shapes and animations that the Game of Life creates (see the Game of Life on wikipedia). To create a somewhat different perspective, I implemented a 2D version of the Game of Life using the java class above, but rendered the game in 3D using OpenGL (java and JOGL). The rendering provides different colors for current versus historic states, in which the current stats is shown in red and previous states of the game faded green. A slight rotation gives a nice representation of the transitions of shapes within the Game of Life, where one can see a variety of common forms emerge from the random seed:
Next up: the Game of Life in 3-dimensions.
There are no comments on this article.