Project 3 -- The Game of Life

For project 3, you should implement Conway's Game of Life. The purpose of this project is to give you a bit of experience with simulations of the biological and physical world, practice in using arrays (including 2 dimensional arrays), and experience in modifying an existing program that you get from elsewhere.

There is only one part to this project, due 11pm November 17. You should review the project soon. However, we'll be discussing it in lecture Nov 13, so you may want to wait until after that lecture to begin working on it seriously. The project actually only involves writing around 30 lines of code, but you need to understand an existing larger program. (We're giving you a starting version that you can modify.) There is also an extra credit option, in response to some requests for this.

Turn in the entire program using turnin as usual (including all the code, both the part that you use without modification and the part that you modify).

The Game of Life is a simple example of simulation using cellular automata. The mathematician John Conway proposed it in 1970, and it has attracted interest ever since. It's not a game in the sense of two players competing, but more in the sense of a simple set of rules that can give rise to complex behavior. The game is played on a 2 dimensional array of cells. Each cell is either alive or dead. We are given some starting configuration of live and dead cells. Then, at each step (in other words, at each tick of the simulation clock), we compute the next state of the array. This is done as follows. For each cell, calculate how many live neighbors it has. Each cell has 8 neighbors: up, down, left, right, upper-left, upper-right, lower-left, and lower-right. If the cell is dead and if it has 3 live neighbors, it comes alive. If the cell is alive and has 2 or 3 live neighbors, it stays alive. Otherwise it dies (or stays dead), either of loneliness or overcrowding. Important: the next states for all the cells are computed using the current states of all the cells -- you'll get wrong results if you update a cell and then use the updated state of that cell when computing the state of its neighbors.

The game continues indefinitely, although obviously you'll want to stop it at some point. Also, conceptually the 2-dimensional array of cells is infinite in all directions. In practice, of course, we have to make it finite; in the starting code it is 30 by 30 cells.

Here is an executable file that shows what the solution should look like:

To begin, download the following code as a starting point. To download the code, click on each link and save the file under the suggested name. The version above uses a simplified update rule that makes the cells just move 1 to the right each step. To do the assignment, you have to replace the simplifed update rule with the correct one. The procedure that you need to modify is called "oneStep". There are other procedures that draw the image, respond to commands, and so forth -- these are all OK as they are, and you don't need to change them. So concentrate on the "oneStep" procedure!

The code uses a global variable "cells", a 2-dimensional array, that holds the current state of each cell. There is a second array "newCells" that we use to hold the next states of all the cells. After computing all these new states, we copy "newCells" into "cells". (Remember that the next states for all the cells are computed using the current states of all the cells.) Use these variables in your final version as well.

What about cells at the edge of the grid? Do we need to write special cases to avoid looking at neighbors that are off the grid? No -- here we use a standard trick. We make the array cells 1 bigger in each direction than it needs to be, so that it goes from 0 to nRows+1 and from 0 to nCols+1. However, we only update the cells from 1 to nRows and from 1 to nCols. The cells just outside this region always stay dead. We can check their state when doing updates, however; thus avoiding a lot of special cases. The simplified update rule uses this trick already, so you can look at the sample code for more details.

You only need to turn in the final working program. However, we suggest that you do a few simpler experiments to get started.

  1. Download the files, and run the program as is to make sure that it's working. It will use the simplifed update rule.
  2. Change the update rule so that the cells move down and to the left each step instead. Then throw this version away. (This is just to make sure you're looking at the right place in the code to modify, and figuring out how the cells are stored.)
Finally, do the assignment.

Extra Credit

There have been several requests for an extra credit opportunity, so here it is. There are 3 parts to the extra credit assignment, in increasing order of difficulty. You can do just Part 1, or Parts 1 and 2, or Parts 1, 2, and 3. If you do an extra credit version, turn in just one file, with a comment at the top of your code sayin which option you chose. When computing grades, extra credit will be handled as follows. We'll compute the grades, ignoring the extra credit. (See the Syllabus for more information on how grades are computed.) Then we'll increase the grades appropriately for people who did extra credit work.
  1. Add two additional command buttons to make the simulation go faster or slower while it's running. This should be easy, and we've covered all the concepts in class already to do this. Here is an executable version of this part of the extra credit version: LifeWithControls.exe.

  2. Add a facility to save and load files containing simulation states. There are many fascinating Game of Life patterns that have been devised, for example, gliders, puffer trains, and many others. Having a load/save facility lets you save them and reload them without re-setting each cell for the pattern. To do this, use a sequential file that holds 0's and 1's corresponding to each cell contents. We won't be talking about files in class, so to do this extra credit option you'll need to read about files in the Visual Basic reference book. You'll also need to figure out a particular order to write the 1's and 0's out in. (Be sure and read them back in the same order.) For this version of the extra credit problem, you can use a text box that lets the user type in the file name.

  3. Rather than using a text box for the file name for load/save, provide a graphical interface for picking an existing file name, or typing in a new one. You can either use the DirListBox and FileListBox controls, or the "Common Dialog Control" (see page 255 in the VB book). Again, these are topics we won't cover in class so you'll need to figure out this part on your own.

Further Information

The above information should be all you need to do the assignment. If you're interested, however, here are some links to further information on the Game of Life: Or type "Conway game life" into your favorite web search tool.