The Game of Life
Source: Structures and Abstractions: An Introduction to Computer Science with Pascal, William I. Salmon
Survivor: The First Game
The Game of Life-a survival simulation game-was invented by John H. Conway, a Cambridge mathematician, and first reported in print by Martin Gardner in the October 1970, issue of Scientific American magazine. (Look it up.) It has become one of the legendary pastimes among programmers. The game runs on a square, two-dimensional array, representing a flat surface that can contain II creatures." Initially, the surface is sprinkled randomly with creatures; that is, each cell in the two-dimensional grid is randomly either empty or occupied by a creature. Then the program goes into a loop, each trip though which represents a “tick" of the environmental clock. At each tick, creatures propagate or die according to two immutable laws:
- An empty cell contains a “birth" if exactly three neighboring cells contain creatures. (Note: This rule has been changed from the above source to match the rules used on the Internet sites below and the original article)
- In an occupied cell, the creature continues to live for another tick if and only if neighboring cells contain at least two but not more than three creatures.
Thus the game simulates in a crude way the effects of congregation, loneliness, and overcrowding.
Assignment:
Write a OOP program that plays the Game of Life, using the following
- size of the Universe is a 20 by 20 2-D array ( you may change this after you have it working)
- the universe will be a 2-d array of cells.
- a cell can be either Empty or Occupied.
- your program should be able to add life randomly to the world or read an initial state from a file
Fill your array by "flipping a coin” for each cell, using a random-number generator provided by your system. Notice that each cell has eight neighbors-up, down, right, left, and diagonally-except for the cells on the edge of the grid. Be careful that your algorithm doesn't walk off the edge of the grid.
Add
- Add the ability to read a starting grid from a file
- Allow the user to continue to play with the current universe until they would like to quit. You can even prompt the user for the number of generations to progress.
Modification
Write a version of the Game of Life in which the grid "wraps around" both horizontally and vertically. That is, the next cell to the “right” of the last cell on a row is the cell at the beginning of the row; the next cell "below” the cell at the bottom of a column is the cell at the top of the column. This is the effect you would get if you wrapped your grid horizontally and vertically to form a doughnut. (Hint: use MOD arithmetic.)
Game of Life Websites
http://www.bitstorm.org/gameoflife/
http://www.math.com/students/wonders/life/life.html