The Game of Life

In this assignment you will implement Conway's Game of Life. You are not expected to write a user interface, this is provided for you in the lab tarball. Your task is to write the function to calculate whether each cell lives or dies.

Here are the rules of the game:

The universe of the Game of Life is a two-dimensional grid of cells, each of which is either alive or dead. Cells interact with their eight neighbours, which are the cells that are directly horizontally, vertically, or diagonally adjacent. At each step in time, the following effects occur:

  1. Any live cell with fewer than two neighbours dies, as if by loneliness.
  2. Any live cell with more than three neighbours dies, as if by overcrowding.
  3. Any live cell with two or three neighbours lives, unchanged, to the next generation.
  4. Any dead cell with exactly three neighbours comes to life.

The initial pattern constitutes the first generation of the system. The second generation is created by applying the above rules simultaneously to every cell in the first generation -- births and deaths happen simultaneously, and the discrete moment at which this happens is called a tick. The rules continue to be applied repeatedly to create further generations.

Download the tarball and extract it with the following commands:

cd ~
tar xvzf life.tar.gz
cd life

The prototype of your functions is given in life.c, and looks like this:

/* calculate the next generation of cells from the current one */
void iteration(int current[LINES][COLS], int next[LINES][COLS]);

As you can see, your function is passed two 2-dimensional arrays, current and next, which are both LINES by COLS in size and represent the current and next generations. current[y][x] (for any 0≤y<LINES and 0≤x<COLS) is either 1, if the cell is currently alive, or 0 if the cell is currently dead. Likewise, when your function returns next[y][x] should be set to 1 if that cell is alive in the next generation, or 0 if the cell is dead in the next generation.

Here's some psuedocode that might help you:

for each coordinate (y,x)
    count the number of neighbors in the current generation
    decide whether the cell (y,x) will live in the next generation

For information on running the game, read the README file in the tarball.