Sunday, January 12, 2014

Conway's Game of Life in Java

Conway's Game of Life was devised by the mathematician John Horton Conway in 1970 as a cellular automaton. Conway created the rules of the game to offer a simplified answer to John von Neumann's question of a self replicating machine.

The rules of the game of life are simple. An infinite plane is populated with cells. These cells have only two possible states, alive or dead. Cells surrounded with 0-2 neighbors will die of loneliness.

Likewise, alive cells surrounded with 4-8 neighbors will die of overcrowding.

However, a dead cell surrounded with exactly 3 neighboring alive cells will be born.

Through these simple rules, we can create complex and interesting structures and "lifeforms".

I developed a Java, Swing based implementation of Conway's Game of Life that is available on GitHub.

Jar download:

The one issue with this implementation is that it is not an infinite plane; the array is defined based on the size of the window. The edges of the window are considered to be permanently dead cells, but it still shows some of the interesting patterns and is a good starting point for anyone looking for an example.

Below is the executable .jar available in the /dist/ folder in the GitHub link above. First we need to fill in some spaces. This can be done by clicking and/or dragging the mouse in the game board or by navigating to Game > Autofill in the menu.

User defined points using the mouse

Randomly defined points via Game > Autofill
Then, observe the results by navigating in the menu to Game > Play.

The results can be interesting to watch! You can also interact with the board while the game is running, by drawing new points or lines.

No comments:

Post a Comment