Assignment 3: The Game of Life

1
Assignment 3: The Game of Life
IMPORTANT: Please read the assignment rules on the web page at
http://www.ee.surrey.ac.uk/Teaching/Courses/C/rules.html
and the submission procedure at
http://www.ee.surrey.ac.uk/Teaching/Courses/C/submission.html
BEFORE starting the assignments.
The submission deadline is:
• 4pm, Tuesday 26th May 2015 (altered deadline!) for all lab classes.
NOTE 1: When submitting the files, you MUST follow the name specification as described in the end of
each part of the assignment. Failing to follow the name specification will result in a penalty of 10 percent
reduction from your final mark.
NOTE 2: For each part of the assignment, submit two versions of your program to SurreyLearn: one
with extension .c file and one with extension .txt (the latter is used for originality check).
NOTE 3:
Compile the program with the command
gcc -ansi -Wall employee1.c -o employee1
and make sure to remove all warnings before submission.
NOTE 4: Make sure that the output requested by the program specifications goes to stdout and all other
output, debugging and user prompts, goes to stderr.
Description: Your task in this assignment will be to implement the game of Life, invented by Professor
John Conway. More information about the game may be obtained from the WWW page
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
which you should try out for lots of interesting information on the game of Life.
The game consists of a board of varying size, where each cell can be either alive or dead at any time step.
Then the next timestep is calculated according to the three following rules:
1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overcrowding.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
where a neighbour is any cell that is horizontally, vertically or diagonally adjacent.
Note that in each part below, any output from your program should go to stdout (using printf statements)
and NOT stderr.
2
When submitting the files, you MUST follow the name specification as described in the end of each part of
the assignment. Failing to follow the name specification will result in a penalty of 10 percent reduction
from your final mark.
3
Part 1: Simple Life game (40 marks)
The first part of the assignment is a basic implementation of Life. The initial configuration of the game will
be provided in a file, and you should print the configuration of the game at each time step.
Download the following input files for the first part from SurreyLearn (http://surreylearn.surrey.ac.uk)
or from http://info.ee.surrey.ac.uk/Teaching/Courses/C/:
block, beehive, traffic, glider and methusalah.
The initial configuration specifies the number and position of cells that are initially live. The number of
initially live cells is provided in the first line of the file. Then the row/column position of each live cell is
provided on a separate line. So for instance the “traffic light” life form is defined by the file traffic, which
contains:
3
2 1
2 2
2 3
In this case three cells are initially alive, with row/column positions as indicated.
To display the life game “board” graphically you should use the primitive but effective function printf().
At each time step, including the first, print the complete board, the width and height being specified on
the command line as described below. Cells outside the game board are to be considered as always “dead”.
Print an ‘X’ for each live cell and a space ‘ ’ for each dead cell. Row 0 is the first row you should print, i.e.
the top row. Column 0 is the left column. You should also print a border for the board, with asterisks ‘*’
at the four corners, hyphens ‘-’ along the top and bottom, and vertical lines ‘|’ (pipe symbols) along the
left and right vertical borders. Note that the board width and height are excluding the border, so that the
total printed width and height including the border will be width+2 and height+2. Two blank lines should
be printed between the outputs for each time step. A fixed number of time steps is provided (see below),
after which the program should exit, with a blank line followed by the word “Finished” by itself on a single
line. So for instance, given the traffic as initial configuration, width 7 cells and height 5 cells, and three
time steps, your program should output as in Fig. 1. The first board printed is the initial configuration, so
four configurations are printed in total. It so happens that the “traffic” configuration is a “blinker” with a
period of two, so the configuration repeats.
Write your program in the file life1.c, and compile it with the command
gcc -ansi -Wall life1.c -o life1
The program should take exactly four arguments on the command line, in the order file-name, width,
height, generations. The maximum size for the game board will be width 78 cells and height 50 cells. So
to run the program on the traffic file as above, the command line would be
./life1 traffic 7 5 3
You may find it convenient to print messages to standard error. It is also useful to be able to control
the progress of the game by waiting for keyboard input at each generation. You are recommended to use
fgets() for this purpose. However if you do this you should REMOVE the keyboard input source code
before submitting your program.
Test your program with the other initial configurations, the block, beehive, glider and methusalah life
forms are provided, as described in the game description attached at the back. The block and beehive are
“still-lifes”, life forms that do not change. As described above, traffic is a “blinker”, repeating itself every
second generation.
4
*-------*
|
|
|
|
| XXX
|
|
|
|
|
*-------*
*-------*
|
|
| X
|
| X
|
| X
|
|
|
*-------*
*-------*
|
|
|
|
| XXX
|
|
|
|
|
*-------*
*-------*
|
|
| X
|
| X
|
| X
|
|
|
*-------*
Finished
Figure 1: Program output given the traffic as initial configuration, width 7 cells and height 5 cells, and
three time steps.
For the glider and methusalah life-forms you will need to use a larger board, say width 70 and height 40.
The glider should repeat every four generations, but also shift diagonally down and to the right at every
such repeat. methusalah is a simple initial pattern that gives rise to increasingly complex patterns, before
eventually stabilising. In Fig. 2 is the configuration of methusalah after 100 generations, with width/height
set to 70/40 as suggested. You will find that the methusalah eventually becomes too large for the board.
You are encouraged to create your own life-forms for testing.
When you have thoroughly debugged and tested your program, and have written the comment blocks into
the source code as described in the rules for assignments on the Web page, re-name your file according to
the following specification and then submit the program to SurreyLearn.
NOTE 1: The name of the submitted files MUST be proceeded with your surname and initial followed
by the name of the program. For example, if your surname is “Brown”, and your first name is “Peter”, you
need to name the files as follows:
5
*----------------------------------------------------------------------*
|
|
|
XXX
|
|
X X
|
|
X
X
|
|
X
|
|
XX
X
XX
|
|
XX
X
XX
|
|
X
|
|
XX XX
|
|
|
|
XXX
X
XX
|
|
X
X X XX
X
X X
|
|
X X X
X X X
|
|
XX X X X
X
XX
X X
|
|
XXXXX
XX X X
XX
X XX
|
|
XX XX
XXX
XXX
|
|
XX XXX
XX XX
|
|
XX X
|
|
XX XX
XX
X
XXX
|
|
X XX
XX
XX
X
|
|
XX
|
|
XX
XX
|
|
X X
XX
|
|
X X
|
|
XX
|
|
|
*----------------------------------------------------------------------*
Figure 2: Configuration of methusalah after 100 generations, with width/height set to 70/40 as suggested.
BROWNP-life1.c
BROWNP-life1.txt
The first part is your surname and initial followed by a hyphen, and then followed by the original filename
(life1.c). If you also have middle names, ignore them in the file name. If your files do not follow
this name specification, as a penalty, 10 percent will be deducted from your final mark.
NOTE 2: Follow the argument list format, input format and output format accurately. Your program
will not be deemed to be working unless it follows the specification given above.
NOTE 3: You can assume that all arguments and input files contain valid and compatible values, so
no error checking is required in this assignment. However you are recommended to test that the initial
configuration does not involve setting live cells outside the specified width/height of the game board, to
avoid one possible source of debugging problems.
6
Part 2: Show cell age (30 marks)
Copy your program life1.c into a new file life2.c, which is to be the program to work on in the second
part. You are to modify the program to show the “age” of live cells. This is done by replacing the ‘X’ symbol
for live cells by a digit from ‘0’ to ‘9’ indicating the number of generations that the cell has survived since
it was “born”. An age greater than 9 is indicated as before by an ‘X’. Cells are assigned an age of zero upon
creation. To repeat the traffic test from part 1, run the command
./life2 traffic 7 5 3
This should produce the output in Fig. 3
*-------*
|
|
|
|
| 000
|
|
|
|
|
*-------*
*-------*
|
|
| 0
|
| 1
|
| 0
|
|
|
*-------*
*-------*
|
|
|
|
| 020
|
|
|
|
|
*-------*
*-------*
|
|
| 0
|
| 3
|
| 0
|
|
|
*-------*
Finished
Figure 3: Output with cell age
When you have thoroughly debugged and tested your program, and have written the comment blocks into
the source code as described in the rules for assignments on the Web page, re-name your file according to
the following specification and then submit the program to SurreyLearn.
7
NOTE 1: The name of the submitted files MUST be proceeded with your surname and initial followed
by the name of the program. For example, if your surname is “Brown”, and your first name is “Peter”, you
need to name the files as follows:
BROWNP-life2.c
BROWNP-life2.txt
The first part is your surname and initial followed by a hyphen, and then followed by the original filename
(life2.c). If you also have middle names, ignore them in the file name. If your files do not follow
this name specification, as a penalty, 10 percent will be deducted from your final mark.
8
*-------*
|
|
|
|
| 000
|
|
|
|
|
*-------*
*-------*
|
|
| 0
|
| 1
|
| 0
|
|
|
*-------*
*-------*
|
|
|
|
| 020
|
|
|
|
|
*-------*
Period detected (2): exiting
Figure 4: Period detection
Part 3: Detect repetitions (30 marks)
Copy your program life2.c into a new file life3.c, which is to be the program to work on in the third part.
You are to modify the program to detect a repetition of a previous pattern. Your program should be able
to detect repeating patterns with periods of up to four generations. When such a repetition is discovered,
the program should exit with a different message:
Period detected (4): exiting
replacing the “Finished” message, with the length of the period indicated by the number in brackets. The
repeated pattern should be printed before exiting. So running the program with the traffic data file for
three or more generations should produce the output in Fig. 4
When you have thoroughly debugged and tested your program, and have written the comment blocks into
the source code as described in the rules for assignments on the Web page, re-name your file according to
the following specification and then submit the program to both SurreyLearn.
NOTE 1: The name of the submitted files MUST be proceeded with your surname and initial followed
by the name of the program. For example, if your surname is “Brown”, and your first name is “Peter”, you
need to name the files as follows:
9
BROWNP-life3.c
BROWNP-life3.txt
The first part is your surname and initial followed by a hyphen, and then followed by the original filename
(life3.c). If you also have middle names, ignore them in the file name. If your files do not follow
this name specification, as a penalty, 10 percent will be deducted from your final mark.