CS 11 -- Project 2


Note the updated due date!

The Game of Life

The Game of Life is not really a game (you don't play it), but is rather an instance of a cellular automaton. That is, there are a group of cells whose behavior is determined by a set of simple rules. While the behavior of a single cell isn't particularly interesting, the behavior of the group and how they interact can be.

Specifically, the Game of Life involves cells on a two-dimensional grid that are either alive or dead. There are rules for when cells are born, when they continue to live, and when they die. This section will describe the details of the Game. Below you will find your specific assignment for implementing this game.

Details of the game:


The LifeApplet class and grid configuration files

I am going to provide to you a class (that is, compiled Java code), named LifeApplet, that is responsible for two critical aspects of your program. First, it manages the graphical interface. LifeApplet will, from your perspective, automatically handle the graphical interface; your code does not need to invoke any LifeApplet method in order to keep the interface updated.

Second, LifeApplet simplifies reading of the grid configuration file. LifeApplet assumes that this file is named grid and that it will reside in your current directory. A grid configuration file looks like this:

    4 4
    0 0
    1 0
    0 1
    3 3
    3 2
    2 3

The first line provides the dimensions of the grid (here it is 4 cells x 4 cells). Subsequent lines indicate the coordinates of live cells, given as x,y coordinate pairs. That is, the last line in the file above indicates that the cell at column 2, row 3 should initially be alive.

To assist you in reading this information from the grid file, LifeApplet provides two methods that you should call to read the data from the file:


Your assignment

You must write methods that will play the Game of Life. More specifically, you will write the methods that create the grid and perform the evolution. In a class named Life (it must have that name), you must write the following methods:

IMPORTANT: Because your methods will be called by LifeApplet, and because your methods do not receive as parameters copies of the essential data that describes the state of the grid, you must create global variables to hold that information. For example, to keep track of the generation count, you should have a variable that is declared outside of any method, like so:

public static int generationCount;

Although it looks initially like a method declaration (thanks to the public static part), because there is no parameter list and no method body, Java will identify this as a variable declaration. This variable, generationCount will then be available for use inside any method within your Life class. You will need to declare all variables that hold the state of the game in this fashion.

Sample initial state files: I've provided initial files for you to use during the development of your program. Along with each initial state file is a results file, which contains the output that your program should generate on these samples. To obtain these files, you can copy them from my public CS 11 directory on romulus, like so:

cp ~sfkaplan/public/cs11/simple.init .
cp ~sfkaplan/public/cs11/simple.results .
cp ~sfkaplan/public/cs11/X-pattern.init .
cp ~sfkaplan/public/cs11/X-pattern.results .

The simple case is a small grid where the pattern will repeat every two generations. The X-pattern case is a medium-sized grid that resolves to a static state after a number of (somewhat interesting) steps. Feel free to create new grid initialization files of your own!


How to run your program

Because this program has a graphical interface, you must do a few things differently to get it to run:

  1. Put a grid file into place: You must use one of the grid initialization files (see above) to describe the size and initial state of the grid. This file must be named grid and it must reside in the directory from which you launch the program. I suggest simply making a copy of one of the grid initialization files, giving the copy the name grid, like so:

    cp simple.init grid

    If you want to use a different initialization file, just use the cp command, as above, to make a copy named grid that the program will read.

  2. Grab the LifeApplet.class file: This is the compiled code of the LifeApplet class. Note that you will not see the source code (i.e. the LifeApplet.java) file. You must work solely with the descriptions of the LifeApplet methods provided above.

    cp ~sfkaplan/public/cs11/LifeApplet.class .
  3. Grab the LifeApplet.html file: This program is an applet, which is an application with a graphical interface meant to be viewed through a web page. The LifeApplet.html file contains the minimal web page that invokes the applet. Copy this file into your directory:

    cp ~sfkaplan/public/cs11/LifeApplet.html .
  4. Run the applet: To run the applet, you will use the appletviewer program like so:

    appletviewer LifeApplet.html

    If all goes well, you should see a window with a grid and some live cells (blue dots), we well as a couple of buttons to click. When you click the Reset button, it will call your Life.reset method, and when you click the Evolve button, it will call your Life.evolve method.

Note that as your click the Evolve button and watch your program progress, you can compare the grid to the ones shown (in text form) in the simple.results and X-pattern.results files.


Submitting your work

Use the cs11-submit program to submit only your source code, which should have been written as a single file. Submit your work as project-2, like so:

cs11-submit project-2 Life.java

This assigment is due on Wednesday, November 30th at 11:59 pm!

Scott F. Kaplan
Last modified: Tue Nov 15 21:41:25 EST 2005