Conway's Game of Life

Objective

To gain experience writing C programs, and working with binary data by writing a version of Conway's Game of Life.

Conway's Game of Life

This game is a type of cellular automata and simulates a simplified grid world.

For our purposes, the grid will consist of 40 rows and 80 columns. Each spot in the grid can either be empty, or have a live cell in it. The simulation consists of successive generations. At each generation, the following rules are applied:

1. Any live cell with fewer than two neighbors is dead in the next generation.
2. Any live cell with more than three neighbors is dead in the next generation.
3. Any live cell with two or three neighbors survives.
4. Any empty cell with exactly three neighbors becomes live in the next generation.
5. Any empty cell with a number of neighbors not equal to three remains empty.

Here "neighbors" refers to live cells that are to the left, right, top, bottom or diagonal of the cell. A cell can have from zero to eight neighbors.

Note that the neighbors count cells that are live in this generation, regardless of whether or not they will be live the next.

Program Details

Your program should take two parameters. First is the name of the input file. This file contains a bit for each cell in the grid. There are $40 \times 80 = 3200$ bits which is equal to $3200 \div 8 = 400$ bytes.

If a bit is 1, that means the corresponding cell is alive. If it is 0, it means that cell is dead.

The first byte stores the first eight cells from the first row of the grid world. The most significant bit in the byte comes first, so location (0, 0) is given by the most significant bit of the first byte in the file.

To see if a given cell is alive, you need to first find which of the 400 bytes it is in, and then test the individual bit in that byte.

The second parameter is a positive integer, $N$, which indicates the number of generations to simulate.

Your program should simulate $N$ generations, according to the rules above, and output the final state of the grid to the screen. Any dead cells should be output as spaces, and live cells should be output with the 'O' character.

Test Files

You can test your program with the following input files.

 Test Files Output After 100 Generations still still-output.txt blinkers blinkers-output.txt glider glider-output.txt random random-output.txt

Tips

• Start by reading the 400 bytes into an array of unsigned chars.
• Next, write a function which takes a row and column number and returns whether that cell is alive or not.
• Use this function to write another function which prints the current grid to the screen. At this point, you should be able to run your program on the "still" test file and get the right output (because the grid never changes for that test file).
• Next, write functions to set and clear any cell based off of its row and column number.
• Use these functions to apply the simulation logic as described above.
• Start early since working with binary data can be hard at first!

Submitting

When your program works, email the source code to ifinlay@umw.edu.