Introduction to Computer Science I

Project 3


The Game of Life

The Game of Life is an example of cellular automata---that is, it is the result of a collection of simple entities (cells) that behave automatically by following simple rules. It is, thus, not really a game in any usual sense. This particular cellular automata exhibits some surprisingly complex behavior, as groups of cells can move in definite directions and then interact with other groups of cells that they encounter. That is, higher-level, more complex behavior emerges from the simple, lower-level rules that control the cells.

Here is how the game works: Consider a rectangular grid of size n x m (rows by columns), known here as a universe. Each of the nm positions in the grid is occupied by a cell. Each cell is either in the state of being dead or of being alive at any given time. Time itself moves in discrete steps, which we will call generations. Thus, at generation 0, there are nm cells, some of which are alive, and the rest are dead. This state, at generation 0, is the initial state of the game.

When time advances from generation g to generation g+1, each cell must take the two following steps to evolve:

  1. Count live neighbors: The cell must examine each adjacent cell in the grid (there are 8 of them) and count how many of them are current (in generation g) alive.

  2. Apply rules: The cell must then apply the following rules to determine its state in generation g+1:

A game consists of advancing time through the generations until one of the following things occurs:

  1. A predetermined maximum generation number is reached.

  2. The universe becomes static---that is, in going from one generation to the next, none of the cells changes state.

Note that it is possible for a universe to enter a cycle of repeating states, where some sequence of states repeats indefinitely. Detecting such a case would be difficult (although not impossible!), and we will ignore that as a possible way of ending a game. [Question for the curious: Is it possible for a universe not to repeat?]


Your assignment

Write a program that carries out a Game of Life. Specifically, a run of your program will look like this:

(remus)[79]> java PlayLife tests/X-pattern.init 4
Generation = 0, Population = 21
+---------+
-+-------+-
--+-----+--
---+---+---
----+-+----
-----+-----
----+-+----
---+---+---
--+-----+--
-+-------+-
+---------+

Generation = 1, Population = 20
-----------
-+-------+-
--+-----+--
---+---+---
----+++----
----+-+----
----+++----
---+---+---
--+-----+--
-+-------+-
-----------

Generation = 2, Population = 24
-----------
-----------
--+-----+--
---+++++---
---++-++---
---+---+---
---++-++---
---+++++---
--+-----+--
-----------
-----------

Generation = 3, Population = 20
-----------
-----------
---+++++---
--+-----+--
--+-----+--
--+-----+--
--+-----+--
--+-----+--
---+++++---
-----------
-----------

Generation = 4, Population = 44
-----------
----+++----
---+++++---
--+-+++-+--
-+++---+++-
-+++---+++-
-+++---+++-
--+-+++-+--
---+++++---
----+++----
-----------

In order to get started with this code, you will need to copy a directory of pre-written, pre-compiled classes from my directory into your home directory, like so:

cp -r ~sfkaplan/public/cs11/project-3 .

There are two classes provided. We first describe what each class provides and how to use them. However, these classes also make some assumptions about your code and the methods that you will write, so we also describe below the Game class that you must write, specifying what methods it must contain.

PlayLife

This is the class that is responsible for kicking off the game. It contains the main() method, and it will handle the command-line arguments. It is from main() that a Game object is created and then called. More on that below, where the Game class is described.

Support

This class just provides some methods that should, ideally, make your life easier. To understand them, you must first observe that your code will be passed Scanner object. Until now, we've used Scanner objects to read things from the keyboard. This time, however, we'll be using them to read data from a file. For a variety of reasons that we won't go into, using a Scanner in this manner is a bit different, and so these Support methods make the task easier.

Here are the Support methods for your use:


What you need to do: The Game class

You must write the Game class (in a file named Game.java) that carries out a Game of Life. There is only one method that you game class must contain (although you should create others for your own internal use). This method is called from PlayLife in order to get the game started:


Testing your code

You copied, from my directory, useful files that will help in testing your code. In particular, within your project-3 subdirectory, there is another subdirectory named tests. Go look inside it. You will find two types of files:

There are three pairs of these files:

How to use the files for testing: It's too hard, beyond the simple case, to check the output of your program against the .results files with your eyes; doing so may cause blindness, seizures, or insanity. Instead, you can use some automation to do the comparing for you. To do so, follow these steps:

  1. Store the output in a file: When you run your program, don't let the output just dump onto a screen; instead, redirect the output into a file for later examination and comparison, like this:

    java PlayLife tests/X-pattern.init 9 > run.results

    The > symbol will redirect the output of your Java program into the file whose name follows that symbol---in this case, a file named run.results. You can open this file (e.g., in emacs) to see the output after the program is done.

  2. Compare: You can now compare the results of your program with the standard results that I provided, like so:

    cmp run.results tests/X-pattern.results

    If the two files are exactly the same, cmp prints nothing at all. Thus, no news is good news. If it finds a discrepency, it will tell you on what line(s) and at what position(s) on the line(s).


Submitting your work

From within your project-4 subdirectory, use the following command to submit you work (all of your Java code files) when you are done:

cs11-submit project-4 *.java

This assignment is due Thursday, 2008-May-09, at 5:00 pm

Scott F. H. Kaplan
Last modified: Fri Apr 25 11:28:40 EDT 2008