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
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.
Generation 1
X | X | |
X | ||
X | ||
X | X |
The next generation of organisms is determined according to the following criteria:
Generation 2
X | ||
X | X | X |
X | X | X |
X |
Generation 3
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
You are REQUIRED to have a function called GetInput which gets both of these items from the user. Since this function has to "return" two pieces of data, the function must have pointers as arguments. Here is the function prototype you are to use for this function :
void GetInput (int *intervalPtr, int *timePtr);
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
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.
There will be extra credit available on this project. More about the
extra credit portion will follow in a separate document.