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:
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:
Jump to the beginning of the grid configuration file. Use this method just before using readValue (described below) to ensure that you will read all the values of the grid configuration file from the beginning. Should you need to re-read the configuration file and re-initialize the grid, then you should use this method to allow your code to read the file a second time.
Read the next value from the grid configuration file. This method remembers where it is within the file and always returns the next value. When the end of the file has been reached, this method returns -1 as an indication that there are no more values to read from the file.
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:
Read the grid initialization file using the LifeApplet methods described above. Create a two-dimensional array in which to store the grid. This array must keep track of the state of every cell (dead or alive).
Evolve the grid to the next generation.
Return whether the cell at column x and row y is alive.
Return the number of columns in the grid.
Return the number of rows in the grid.
Return the total number of live cells in the current generation.
Return the current generation number. When the initial grid is loaded, this counter should be set to 0. With each evolution, it should be incremented.
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!
Because this program has a graphical interface, you must do a few things differently to get it to run:
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.
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 .
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 .
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.
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