CPSC 124, Spring 2006
Lab 14: Life

For the last lab of the term, you will be working on John H. Conway's "Game of Life," which was discussed in class on Friday. You will be working on only a small part of the program that deals with two-dimensional arrays, and you should be able to finish before the end of the lab period. You should turn in a printout of your work either before you leave or else in class on Wednesday.

You can use any remaining time that you have in lab to work on your final project. Remember that the final project is due next Tuesday, May 2, the last class day of the term, during our regular lab period. At that time you will present your work to the class. Hopefully, you will have a working program to show. In any case, you should be able to discuss the design of the program and show some of the code of which you are most proud. If your program is not quite finished in time for next Tuesday's lab, I will give you an extension of a few days. However, no extension will be given unless you have a substantial amount of work done and present it in lab on May 2.

Here is an applet version of the Game of Life program. Click-and-drag on the board to make white "living" cells, or click "Random Fill" to fill the board randomly. Right-click a "living" cell to reset it to "dead." Click "One Frame" to compute the next generation, or click "Start" to let the game run from generation to generation automatically.

You can also run the program using the executable jar file, Life.jar, which you can find in the directory /classes/s06/cs124.

Many Life patterns that display interesting behavior are known. You can find an applet that can show many of these patterns at http://www.ibiblio.org/lifepatterns/. That page also contains links to other information about the Game of Life, including Conway's original article from Scientific American magazine in 1970.

To begin the lab, you should start a new Eclipse project and import the following two files from /classes/s06/cs124/lab14: Life.java and MosaicPanel.java. (This is a slightly modified version of the MosaicPanel class that you used in previous labs.) You can run Life.java as an application. The program is complete except that the "Clear," "One Frame," and "Start" buttons do not function.

For the lab, you will be writing one simple method and one that is somewhat more complicated. First, you should fill in the body of the doClear() method. This method simply stores "false" into every element of the two-dimensional array named alive. This is a very easy first exercise on two-dimensional arrays.

Next, you should fill in the body of the doFrame() method. This is the key method in the implementation of the game. Its job is to change the contents of the alive array from one generation of the game to the next. You must consider each cell and determine whether it will be alive or dead in the next generation by considering the state of the cell and the states of its eight neighbors. The exact rules for the game are given in the comment at the top of the file Life.java. Note that for cells along an edge of the board, some "neighbors" of the board are actually on the opposite edge of the board. That is, the left edge of the board is considered to be next to the right edge and the top edge is considered to be next to the bottom edge.

Remember that to compute the next generation correctly, you need to use a supplementary two-dimensional array. This is so that you can avoid changing the original array at the same time that you are trying to count neighbors. There are two approaches to this: (1) Use a two-dimensional array of integers in which you store the neighbor count for each cell; after doing all the counting, go back and make changes to the alive array. Or, (2) Use another two-dimensional array of boolean in which you create the new generation; after computing the entire new generation, replace the old generation with the new generation.

You do not need to make any changes to the program outside the doClear() and doFrame() methods.

David J. Eck, April 2006