CMSC 201
Programming Project Two
The Game of Life
Out: Monday 3/2/09
Due: before 11:59 PM, Sunday 3/15/09
The design document for this project, design2.txt ,
is due: Before 11:59 PM, Sunday 3/8/09
|
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,
NorthEast, East, SouthEast, South, SouthWest, West, and NorthWest.
The next generation of organisms is determined according to the following
criteria:
- Birth - an organism will be born at each empty location that
has exactly 3 neighbors
- 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.
- 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:
Generation 2
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.
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 then 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
- If you view the results of the simulation after each unit of time,
the change in the population is less noticeable than if you wait for several
time units before viewing, therefore the file contains the length of time of
one time interval as well as the total time for the simulation.
- To begin, you will need to read in the data from one of the available data
files, i.e. pop1.dat You are to use UNIX redirection for this program. You
will need to read in the interval and total time and then read in the 225
integers into a two-dimensional array of integers.
- For each unit of time, the presence or absence of an organism in each
square-unit area must be calculated, using the rules for birth, death and
survival shown above.
Hints
- Since every square will be altered by its neighbors, and you must use
the original values of each square for the calculations, you cannot just
move through the array making changes as you go. This would give erroneous
results. A second two-dimensional array needs to be made that will hold
either the presence or absence of an organism that is being calculated
for each square, while the original array is still intact to provide the
values needed for the calculations.
The data files
There are several data files available for this project. The one shown
above 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/b/o/bogar/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/b/o/bogar/pub/pop?.dat .
Sample Output for pop2.dat
(which has an interval of 1 and a total time of 5)
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 the function main() and any functions that
are specific to this project. You will be writing some functions for handling
two-dimensional arrays, that can be used as modules, so you should be adding
those to your util.c file. You may, of course, have other .c and .h files, as
you see fit.
Submit as follows:
submit cs201 Proj2 proj2.c util.c util.h
and any others needed separated by spaces
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.