CS 420 - Assignment 1

Due: Tuesday, Sept 11th by 11:59 PM

This assignment is a C programming warm-up exercise.

Getting Started

Download the file CS420_Assign1.zip and extract its contents.

Your Task

Your task is to write C code to implement Conway's Game of Life, which is a simulation of one-celled organisms.  The Game of Life is played on a two-dimensional grid.  The Rules of Life determine, based on the cells which are alive or dead in the current generation of the simulation, which cells will be alive and dead in the next generation of the simulation.  The Rules of Life are:

If a cell which is alive has 2 or 3 living neighbors, it survives in the next generation, otherwise it dies.

If a cell which is dead has exactly 3 living neighbors, it is born in the next generation.

Although these rules are very simple, the Game of Life can exhibit some surprisingly complex behavior.

You will start from a skeletal program that defines a set of functions you must implement:

The functions that you must implement are in the file LifeLib/life.c

Each function is documented in the header file LifeLib/life.h

In the implementation of the game_board_compute_next_gen function, cells that are alive should have the value 1 and cells that are dead should have the value 0.

Compiling and Testing

In Visual Studio, open the file CS420_Assign1.sln and build the solution.  The resulting executable files will be in a folder called debug.

In Linux, Mac OS, or other Unix-based system, open a shell window run the command make from the top-level directory.

The executable called LifeTests.exe (or just lifetests on non-Windows systems) runs several tests of the functions you have implemented - if all of the tests pass, there is a high probability that your code is correct.  The program takes a single command line argument indicating which of the tests 1 through 4 to run.  For example, if you run the command

lifetests 4

in a command interpreter, you should see the output

Test 4 (test_blinker)...passed

if the test executed successfully.

The executable called LifeProg.exe (or lifeprog) loads a text file containing a game board, computes a specified number of generations of the simulation, and then outputs the updated game board.  The format of the text file is two integer values representing the number of rows and columns of the game board, followed by a sequence of cell values (row 0, then row 1, etc.)

For example, the following game board file

blinker.txt

defines a Life pattern called a "blinker".  If you run the following command

lifeprog blinker.txt 1

you should see the following output

5 5
0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0

(If you look at the contents of the original file, you'll notice that the row of alive cells runs horizontally rather than vertically.)

If you run the command

lifeprog blinker.txt 2

the output should be identical to the original file.

You should try running some of your own game board files and verifying that the output is correct.

Hints

To dynamically allocate memory in C, use the malloc function.  For example, to create a dynamically allocated game_board object:

struct game_board *gb;
// ...
gb = malloc(sizeof(struct game_board));

To dynamically allocate an array of int values:

int *arr;
int how_many = ...
// ...
arr = malloc(sizeof(int) * how_many);

To de-allocate a dynamically allocated chunk of memory, use the free function, passing a pointer to the memory you want to de-allocate.

Submitting

Instructions for submitting the completed assignment will be posted soon.