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.

About these ads

43 responses »

  1. 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. Сорри за оффтоп, кто-нить смотрел ролики на ютьюбе про конец света? Ну, про андронный колайдер Ваще страшно!

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

  4. 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?


  5. can you send me the code to my email? I can’t download it with the link that you provided. I really want to test the program…thanks :)

  6. Hey :) Blink.. can I have a look at your gameOfLife code.. please send it to my email address, would be great :)

    Thank You so much!

  7. good sir. I cannot download the codes from your given download links. can you send me the code sir. thank you.

  8. Yo your links are broken (as I am sure you know) and I am trying to do some independent programming studies. Please send the code to my email… there are many other codes for this game, but I would like to read yours, as it seems far simpler (in a good way) than the others.

  9. Heya all! THIS IS FOR ANYBODY. NOT JUST THE CREATOR OF THIS PROGRAM. PLEASE HELP! I already posted asking the original poster for the code but this is for anybody who may have it. My email address is nexro00@gmail.com… please and thanks :)

  10. im new to programming, and would really like to see how something like this is coded. if you still check up on this i would really like to see your code

    thanks :)

    (ps. if you need my email comment me back)

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s