A Simple Game of Life in Java

The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in
1970. It is the best-known example of a cellular automaton.

The “game” is a zero-player game, meaning that its evolution is determined by its initial state, needing no
input from human players. One interacts with the Game of Life by creating an initial configuration and
observing how it evolves.

Rules in the game of life

For a space that is ‘populated’:

  • Each cell with one or no neighbors dies, as if by loneliness.
  • Each cell with four or more neighbors dies, as if by overpopulation.
  • Each cell with two or three neighbors survives.

For a space that is ’empty’ or ‘unpopulated’

  • Each cell with three neighbors becomes populated.

You can download my simple implementation of Game of Life in Java. You can open the project in Netbeans or you can just copy the src directory and edit them in your own if you don’t have Netbeans or you don’t want to use Netbeans.

The source code consists of the following classes:

  • Cell.java -This class is responsible for representing a single cell in the game of life. The following are the states that it has to keep track of and its responsibilities: (1) Keeps track of its state (dead or alive), (2) Keeps track of its neighbors, and (3) Identifies if it will live or die in the next iteration given knowledge of its neighbors
  • CellWorld.java – This class is responsible for keeping track of the world where all cells reside. The following are the state that it has to keep track of and its responsibilities: (1) Keeps track of all cells existing in the world, (2) When the CellWorld is created, the number of rows and columns in the world are provided and a Cell is placed in each location in the world, (3) The state of a specific Cell in the world can be set. (4)  The CellWorld can be asked to take a single step of iteration which will inform all cells of their neighbors and update their states causing them to either live or die.
  • CellWorldCanvas.java – This class is a CellWorld representation in a Java Canvas.  The CellWorld knows to draw itself.
  • GameOfLife.java – This is the runner class. The main method resides here.

Here is an example screenshot of the program with 9 generations (read pictures from left to right):

Cyrus Game of Life in Java
Cyrus Game of Life in Java

Enjoy playing the Game of Life.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s