Here are two versions of the "game" of Life, an
example of cell automata; things (cells) that can reproduce and die
based on their environment.
Background & Techniques
Every puzzle and game programmer's portfolio should include two
standards, John Conway's Game of Life and Fractals, neither one of which
currently appears here on DFF. Today's posting will fill one of those
gaps. I decided to do it while searching for a beginner's level
program since I have been neglecting them recently.
The problem is, I now serve two masters - programmers interested in the code
and real people who are interested in running the code for some
entertainment, educational, or practical purpose. And most
beginner level programs are not likely to be very interesting to users. So
here are two versions; one as simple as possible for the beginning Delphian
and one that that builds on the simple version.
Mathematician John Conway designed the game in 1970 based on two simple
rules. The board is a rectangular grid of cells each of which is
either occupied or empty. After setting an initial starting set
of occupied cells, we move to the next generation according to rules about the
the state of the neighbors, the 8 cells which surround it.
Rule 1: If an occupied cell has two or three neighbors, it is happy
and remains unchanged. Otherwise it is too lonely or too crowded and
dies (cell becomes unoccupied).
Rule 2: If an empty cell has exactly 3 neighbors, a birth occurs
and the cell becomes occupied. Don't ask me what kind of life form
requires three parents for a birth - I didn't make the rules!
That's it. Search the web for Conway's Life and you'll find many,
many
pages of sample patterns, an entire lexicography of named terms for patterns
and behaviors, and a number of other programs including one written in
Delphi which handles board sizes up to 1 million x 1 million cells!
And lots of discussions of gliders, sliders, boats, guns, spaceships and
oscillators just to name a few.
The simple version, V1, changes the state of each cell that
gets a mouse click for setting up a pattern. The Step button creates the next generation.
The board is 25x25 cells. That's about it.
Version 2 adds the ability to load and save pattern files,
automatically moves from generation to generation at a user specified rate,
and allows board to be set to 25x25, 50x50, or 100x100 cells.
I have included a few sample pattern files as a starter set.
You can find hundreds more at
http://www.argentum.freeserve.co.uk/lex.htm. Simply copy and
paste a pattern into a text file to load and run it. The files
are the simplest possible format, a text file array of '.' and 'O' characters define
the pattern ('O' = 'Occupied'). The array is centered on am empty grid
when loaded into the program.
For either version, patterns that expand beyond the limits of the
grid "wrap around" as if the first column was to the right of the last
column and the top row was below the bottom row. Topologically its as
if the board was printed on a cylinder whose ends were then joined together
to make doughnut (Ok, a torus, if you want to be technical)..
Non-programmers are welcome to read on, but may
want to skip to the bottom of this page to download
executable version of Version 2 of the program.
Notes for Programmers
Version 1 uses two 2-dimensional arrays of Boolean (true/false)
variables, CurrentGrid
and NextGrid, to hold the
current and the next generation configurations, (True for occupied, False
for empty). The new generation depends on
the old generation neighbor counts before any updating occurs, thus the need
for two grids. A TStringrid control, Stringgrid1, provides the visual
picture of the current configuration. The
MakeStep procedure generates each new generation by counting the
neighbors for each cell and applying the rules described above to set the
cell value in NextGrid and StringGrid1. After all cells
have been checked, NextGrid is copied to CurrentGrid.
MakeStep contains about half of the 100 user
written instructions in the program. The other routines are event
exits which are called automatically when certain events occur: