CMSC 201
Programming Project Two

The Game of Life

Out: Tuesday 10/1/02
Due: before Midnight, Sunday 10/13/02

Sun Oct 13 23:59:59 EDT 2002 is on time, but one second later the date command will
show Mon Oct 14 00:00:00 EDT 2002, which is midnight and will count as being 1 day late.

The design document for this project, design2.txt ,
is due: Before Midnight, Sunday 10/06/02

The Objective

This project will give you practice using good design techniques, UNIX redirection, separate compilation, two-dimensional arrays, and passing arrays to functions.

The Background

The Game of Life, invented by John H. Conway, models the population laws for birth, survival, and death (see Scientific American, October 1970, p. 120). It really isn't a "game", but a simulation of population growth that is displayed over time. The basic assignment is to write a program that simulates population growth over time governed by specific rules.

The Task

You will be working with a population that is contained within a 15 X 15 square-unit area (225 squares), known as the board. Each square can be empty or it can contain an X indicating the presence of an organism. Each square, except for the border squares, has eight neighbors; North, South, East, West, NorthEast, SouthEast, SouthWest, and NorthWest.

. . .
X . X
. X .
. X .
X . X

The next generation of organisms is determined according to the following criteria:

  1. Birth - an organism will be born at each empty location that has exactly 3 neighbors
  2. Death - an organism with 4 or more organisms as neighbors will die from overcrowding. An organism with fewer than 2 neighbors will die from lonliness.
  3. Survival - an organism with 2 or 3 neighbors will survive to the next generation. Generations 2 and 3 for this sample segment of the board are shown below:
            1. Generation 2

. . .
. X .
X X X
X X X
. X .
. . .
X X X
. . .
. . .
X X X

The initial population, consisting of the presence or absence of an individual for each square-unit location are found in the file pop1.dat The first line of the file will contain 2 integers, the first of which is the number of time units in one time interval. The second integer is total number of time units to be used for the entire simulation. The remainder of the file contains 225 integer values separated by whitespace, where a 1 indicates the existance of an organism at that location and a 0 means there is no life there. You may assume that none of the integers found in this part of the file will be anything other than a 0 or a 1. I guarantee that there are 225 integers in this part of the file, so checking for EOF is not necessary.

Here is the contents of the file, pop1.dat:

5 20 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 1 0 0 0 1 1 0 1 1 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1

You will be reading the values found in the file into a two-dimensional array and then displaying the array, labelling it Time: 0. Then, you will need to calculate the next generation, store it in a new array and copy the new array into the original array. This process could go on indefinitely and so "the game of life" is commonly used as a screen saver. For this project, you should only display the array at the appropriate time interval and quit when you reach the last interval. So, for a simulation with a time interval of 3 and a total time of 10, you would display the array at Time: 0, 3, 6, 9 and 10. Note that you always display Time: 0, as well as Time: X, where X is the total amount of time for the entire simulation, which you read in from the data file.

For the input file shown above, the original array should be displayed like this:

Time: 0 XXX XX XXX X XX X XX X X X XXX X XX XX X X X XX X X X X X X

More details

Hints

The data files

There are several data files available for this project. The one shown is pop1.dat. There is also a pop2.dat, pop3.dat and a pop4.dat, all of which may be found in my pub directory, /afs/umbc.edu/users/s/b/sbogar1/pub You should copy these files into your own directory. The executable and these data files need to be in the same directory. Here's how:

Change directory until you are in the directory where you will write your code and have the executable, then type the following command at the unix prompt.

cp /afs/umbc.edu/users/s/b/sbogar1/pub/pop?.dat .

Sample Output for pop2.dat

linux3[72] % a.out < pop2.dat Game of Life... blah blah blah Time: 0 X X XX XXX XX XX X XX X X X XX X XX X XXX X X X X X X XX X X X XX X X X X X X XX X X X Time: 1 X XXX X XX XX X X X XX XX XX XX X XX XX X X XX X X XX X XXX XX X X XX X XXX X XXX X Time: 2 XX X XX X XX XX XXX XX XX X XX XXX X X XX XX X X XXX X X XX X X XX X XX X X X X Time: 3 X XX X XXX XX X X X X XXX X X XX XX X X XX X X X X X X XX XX X X X X X X X XXX X X Time: 4 X XX XX XX XX X X X X X XX X X XX XX XX X X XX X X X X X XX XX XX XXX X XX XX XX X X Time: 5 XX XX XXX X XX XXXX XX X XX XX XX XX X XX X XX X X XX X X XX X X X X XX XX linux3[73] %

What to Turn In

You must use separate compilation for this project and should have a file, called proj2.c, that contains only the function main(). You should also have life.c and life.h, that contain functions related to the game of life and the prototypes for those functions, respectively. You may, of course, have other .c and .h files, as you see fit.

Submit as follows:

submit cs201 Proj2 proj2.c life.c life.h

The order in which the files are listed doesn't matter. However, you must make sure that all files necessary to compile your project are listed.