A Simple Game of Life in Java

December 26, 2008

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.

8 Responses to “A Simple Game of Life in Java”

  1. Jernej Sila Says:

    Hi, when i click to download your source of Game of Life, the browser can’t find it. it says:

    “The requested URL /~cyrus/repos/GameOfLife.zip was not found on this server.”

    This is my fifth year being in java, but first in graphic, so i really want to test program on my own

  2. Cederash Says:

    Сорри за оффтоп, кто-нить смотрел ролики на ютьюбе про конец света? Ну, про андронный колайдер Ваще страшно!

  3. Ferinannnd Says:

    Довольно интересно конечно. Я немогу подписаться под каждым вашим словом, но в общем соглашусь.

  4. Oscar Says:

    Hi there,

    I was trying to get your code but I get page cannot be displayed I tried both links but same result.
    Can you please send me the right address or the code if is possible?

    Thanks!!!

  5. Chris Says:

    Could you send me your code too?


Leave a Reply