UMBC CMSC201, Computer Science I, Fall '97

Project 4: The Game of Life

Originally Due: Wednesday, November 26, 1997

Bonus Date: Wednesday, November 26, 1997 (10 point bonus)

New Due Date: Friday, November 28, 1997, by midnight

The Game of Life, invented by John H. Conway, is supposed to model 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. For extra credit, you can add code that lets the user play it as a game against the computer, but more about that later.

This project will give you practice using two-dimensional arrays, passing an array to a function, pointers, getting input from the user using scanf, and reading from a file using ansi C file-handling functions.

You are not allowed to use any of the Roberts' libraries for this project


Description of the Game of Life

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 and also NorthEast, SouthEast, SouthWest, 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 "population.dat". Basically, 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 the file will be anything other than a 0 or a 1. I guarantee that there are 225 integers in the file, so checking for EOF is not necessary. Here is the contents of the file:

   
   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 display the array, labelling it Time: 0. Next you will need to calculate the next generation and store it in a new array, copy the new array into the original array and display it, labelling it Time: 1. This process could go on indefinitely and so "the game of life" is commonly used as a screen saver. For this project, you will ask the user to say how many time intervals should pass between displaying the array, and also the total amount of time for the entire simulation.

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


The data file

The data file for this project is called population.dat and it is found in my 201 directory. You should copy this file into your own directory. The executable and this data file 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 ~sbogar1/201/population.dat population.dat

  

What to Turn In

You must use separate compilation for this project and should have a file, called proj4.c, that contains only the function main(). You should also have an 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. Possibly util.c and util.h if you need them. Submit as follows:

submit cs201 proj4 proj4.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.



Extra Credit :

There will be extra credit available on this project. More about the extra credit portion will follow in a separate document.