Assigned: Monday November 28.
Program Due: Friday December 9, 11:59 PM.
In this project, you'll write a program in C to play the Game of Life.
The Game of Life is an example of what's called a "cellular automata."
It's not a game in the traditional sense: there aren't winners and
losers. Instead, it's more of a simulation of life and evolution on a
The rules are simple: We start out with a grid of squares, some of
which are 'alive' and some of which are 'dead'. Each iteration, some
live squares die, and some dead squares are reborn, according to the
- If you are alive and have 0 or 1 live neighbor, you die of
- If you are alive and have more than three live neighbors, you
die from overcrowding.
- If you are alive and have 2 or 3 live neighbors, you stay alive.
- If you are dead and have exactly 3 live neighbors, you come
What's interesting about the Game of Life is that these four simple
rules produce some amazing patterns. For some examples, check out the
We'll be building the Game of Life in C. Lectures in class over the
next week will cover the different components you'll need to build the
game, including multidimensional arrays, working with files, and drawing
to the screen with curses.
Your program must have the following features:
A source file called gameOfLife.c - this should contain your
'main' method that sets up the board and plays the game.
- A header file called Board.h. Board.h should contain:
- a definition for the Board struct. The Board struct should
contain ints representing the numbers of rows and columns, and a
pointer to a two-dimensional array of ints.
- The following function prototypes:
- void createRandomBoard(board *b, int rows, int cols);
- void updateBoard(board *b);
- int getAliveNeighbors(board *b, int row, int col);
- void readBoardFromFile(board *b, FILE *f, int rows, int cols);
- int isValidCell(board *b, int i,int j);
- void drawBoard(board *b);
- printBoard(board *b);
- A source file called Board.c - Board.c should implement each of
the functions defined in Board.h. Here's some more detail on what they
- createRandomBoard - the user should be able to pass in a
reference to a Board struct. This function should allocate a 2D array
of integers ifor the board and fill each cell in with a random value
(either 0 or 1).
- void updateBoard(board *b) - this should apply the Game of Life
rules to the current board.
- int getAliveNeighbors(board *b, int row, int col) - this is a
helper function that, for a given x,y location, tells how many
neighbors are alive.
- void readBoardFromFile(board *b, FILE *f, int rows, int
cols). This is a function that, given a board reference and a file
pointer, allocates a 2D array for the board and fills in the contents
from a file. (this is nice for loading in specific starting patterns).
- int isValidCell(board *b, int i,int j). This should return 1 if x
and y are both between 0 and nrows (ncols), or 0 otherwise.
- void drawBoard(board *b) - this should use curses to draw the
board to the screen.
- printBoard(board *b) - this should use printf() to print the board
to the screen.
Reading from the command line: Users should optionally be
able to specify how long the game should run, a file with a starting
configuration, and the number of rows and colums. So, any of these
should be legal ways of starting your program:
- ./gameOfLife (random start, default iterations, board size)
- ./gameOfLife 100 (random start, 100 iterations, default board
- ./gameOfLife 100 lifeFile (start with the contents of lifeFile,
100 iterations, default board size)
- ./gameOfLife 100 lifeFile 50 50 (start with the contents of lifeFile,
100 iterations, 50x50 board)
- Board struct: 10 points
- createRandomBoard: 10 points
- void updateBoard: 10 points
- int getAliveNeighbors: 5 points
- void readBoardFromFile: 15 points
- int isValidCell: 5 points
- void drawBoard: 20 points
- printBoard: 5 points
- Fully working program: 20 points