Assignment 7: Game of Life
Due by: Friday August 11 2006 @ 9 PM
If you haven't already done so, you should check out the Wikipedia article to obtain background about the game.
For this programming assignment, you will implement the computer simulation of the Game of Life. Your simulation will be carried out in a two-dimensional grid that consist of 25x30 cells. The game is entirely played by the computer with no user input.
The game begins with an initial pattern or configuration of the grid where certain cells are alive (occupied) and remaining cells are dead (empty). Given this initial configuration, your program then simulates the evolution of the pattern over time. At each time tick, the following two steps are carried out repeatedly:
- The grid is displayed to the user
- Next, the cells in the grid are updated according to the rules of the game:
- A cell that is alive stays alive if it has 2 or 3 neighbours otherwise it dies
- A dead cell becomes alive if it has exactly 3 neighbours
If there are no living cells in the grid then the game terminates. Otherwise the sequence of displaying the grid and updating it continues forever.
Specification
Your implementation should follow this specification exactly. Save your program in a file called life.cc and when you are finished email it to Subi.
Your program should make use of the header file life.h which contain functions that can be used to generate initial patterns in the grid. The header file also defines symbol values for ROWS (set to 25), COLS (set to 30), TRUE (set to 1) and FALSE (set to 0).
The main datastructure that we will use for this program is a two-dimensional character array called cell of dimensions 25x30 (25 rows, 30 columns). The array is used to represent the grid. A particular cell in the grid can be accessed by specifying both the row number and column number. Your program should implement the following required list of functions:
- void info ( )
This function prints the following message to the output display. Substitute your name instead of John Doe.
Game of Life
Written by John Doe
- void init (char cell[][COLS])
This function initializes the two-dimensional grid. There are 25x30 cells starting from cell[0][0] through cell[24][29]. The function initializes all these cells to an empty character. Like:
cell[0][0] = ' ';
cell[0][1] = ' ';
You can (and should) actually use a nested-loop for initialization. The function doesnot return anything.
- void display (char cell[][COLS])
This function prints the grid using the information in the cell array.
Before displaying any output the function should clear the terminal screen of any previous output.
The function prints 25 lines to the display terminal using printf statements. Each line is used to display one row of information.
- void update (char cell[][COLS])
This function is called to update the cells in the grid according to the rules of the Game of Life. Be careful not to modify the cell contents until you have processed the entire grid.
- int terminate (char cell[][COLS])
This function is called to check if the game should be terminated. If all the cells are empty the function return TRUE. Otherwise the function returns FALSE.
- int main ()
Our traditional main () function that acts as the game controller. The function should return the value 0. Within this function you will declare a two-dimensional character array called cell with dimensions ROWSxCOLS. After initializing the grid you should call the function configure () located in the header file life.h and pass the cell array to it. This function will fill the grid with an initial configuration (pattern). Next, you will make use of the functions you have written above (1-5) to produce the behavior of the Game of Life.
As given, the function configure () always fills the grid with the tencell configuration. The header file also has other patterns like spaceship, glider, and exploder. You should test your program against these patterns too.
Header File: life.h
Output Examples: Example 1, Example 2, Example 3