CPSC 225, Spring 2002
Programming Assignment 3
Conway's "Game of Life"


CONWAY'S "GAME OF LIFE" is not really a game, and it doesn't have anything obvious to do with life. It is, however, a traditional programming assignment. For your third programming assignment in this course, you will write a version of the "Game of Life" that uses the ncurses library to display the board and to allow the user to control the game.

This is a two-week assignment. It is due in class on Wednesday, February 13. Please turn in a printout (made using a2ps), and put copies of the source code and compiled program in your homework directory.

Work with a friend? You can do this assignment with a partner, if you want. (No more than two people in a group.) In that case, you should at least implement "torus mode" as described under the "Enhancements" section below. Don't forget to tell me whose homework directory to look in.



The "Game of Life" is an example of a cellular automaton. You have to imagine an infinite, two-dimensional grid of squares or "cells". Each square is occupied by a not-very-intelligent little automaton (a fancy word for a computing device). Each automaton can be in one of two states, which are called "alive" and "dead". There is a universal clock that goes "tick tock tick tock...". On each "tick", each automaton counts the number of "alive" automata among its eight neighbors (horizontal, vertical, and diagonal). On each "tock", each automaton can change its state according to the following rules:

To play the Game of Life, you bring some of the cells to life, start the clock, and see what happens. Even though the behavior of each individual automaton is very simple, the pattern of living and dead cells can have interesting behavior over time.


For the assignment, you will simulate the Game of Life. Of course, you won't be able to use an infinite board. Instead, you will use a finite, two-dimensional array as the playing board. The state of the game will be displayed as a two-dimensional grid of characters arranged in rows and columns. A blank character will represent a "dead" cell, and an asterisk (*) will represent an "alive" cell. The board will be displayed using the ncurses library, along with a menu that will allow the user to run the game and to set up the initial state. In fact, of course, you will use the menu from Programming Assignment 2, and you will show the board in the large blank area that occupied most of the screen in that assignment. For this assignment, you will implement all the menu commands except for the "Load from File" and "Save to File" commands, which will be part of a future assignment. Here is what the commands should do:


You will find my compiled version of the game of life in the directory /home/cs225/life. The name of the program is also life. This version does everything that you need to do for this assignment. It also handles files, and you will find a few sample files that you can load into the program if you are curious. Remember that you do not have to handle the Save and Load commands for this assignment.

Here are a few points of advice for your program:


Enhancements

There are several ways that the Life program can be enhanced. You are not required to do any of these, unless you are working with a partner. (If you are working with a partner, you should at least implement Torus Mode.)

Torus Mode: Cells along an edge of the board don't have as many neighbors as cells in the center, and this tends to distort what happens along an edge. A solution to this is to imagine that the cells along the left edge of the board are actually next to the cells along the right edge of the board, and the cells along the bottom are next to the cells along the top. This only affects the way that you count neighbors, making it substantially more complicated. If you implement torus mode, it's nice to have a menu item that can be used to turn it on and off.

Variable run speed: As the Run command is executed, you might use the number keys 1, 2, 3, 4 and 5 to control the speed at which the Game is run. Just set the value of timeout() when one of these keys is pressed.

Variable-sized board: Although variable-sized C-style two-dimensional arrays are impossible, something similar is possible in C++. It uses the vector template from the Standard Template Library. (This is discussed in Section 7.3 of the text.) You don't have to understand vectors in order to use them. Here is how you can make the size of your board depend on the size of the window in which your program is running. I assume that you want to use a two-dimensional array of bools :

          Include the vector header file:
          
                 #include <vector>

          Outside of any function, define the type BoolArray2D as follows:

                 class BoolArray2D : public vector< vector<bool> > {
                   public:
                     void setSize(int rows, int cols) {
                       resize(rows);
                       for (int r = 0; r < rows; r++)
                          (*this)[r].resize(cols); 
                     }
                 };

          (There must be a space between the two >'s).

          Declare a variable of type BoolArray2D.  For example

                 BoolArray2D alive;

          This gives an array that has size zero.  When you know the number
          of rows and columns that you want, use the setSize() method to
          set the size of the array.  For example:

                 alive.setSize(rows+2,columns+2);

          Now, you can use alive in exactly the same way that you would use
          a normal 2-dimensional array of bools.

A version of the program that implements all these enhancements is (or will soon be) in the /home/cs225/life directory. You can probably come up with other enhancements. For example, you might try to enhance manual input mode with some hot keys.


David Eck, January 2002