home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
mbug
/
mbug134.arc
/
LIFEDEMO.DOC
< prev
next >
Wrap
Text File
|
1979-12-31
|
6KB
|
167 lines
Conway's LIFE revisited
by Frank Connell
What computer owner hasn't heard of John Conway's famous game of
"LIFE"?
And what computer programmer, after typing in or downloading simple
programs, has not been disappointed with the display and lack of
speed? Let's see if we can't hurry things up a bit!
This is the programming scheme most are familiar with:
make two arrays of x columns and y rows
place live cells in first array
for each generation:
for column = 1 to x
for row = 1 to y
count neighbours
decide if alive or dead in next generation
put live cells into second array
next row
next column
copy second array to first
next
The algorithm has the advantages of simplicity and an easy-to-grasp
logic, but inspection reveals some reasons for its poor performance.
1) The time required to scan the arrays increases geometrically with
size.; a 10x10 array has 100 positions, a 20x20 array has 400. As you
make the array bigger, performance rapidly suffers.
2) The algorithm spends much time in pointless calculations. A 20x20
array might have only 20 of its 400 cells occupied, but the algorithm
does calculations for all 400 cells, ignoring the obvious fact that
cells isolated from living cells have no neighbours and will not
become occupied.
A much faster algorithm, described below, uses lists of only those
cells which might possibly change in the next generation.
The scheme uses three lists:
live_list cells alive this generation
die_list living cells which will die next generation
check_list dead cells which may become alive next generation
and two arrays:
nbr_array the number of neighbours for each cell
flag_array records if cell alive or dead, and if its
neighbours have been added to check_list
(In the assembly language version, I have utilised the unused bits of
nbr_array as my flag_array, saving on space).
.pa
initialise:
set up PCG characters for display
initialise arrays to zero
read live_list from disk, data or screen
null check_list, die_list
mainloop:
for each cell in die_list
flag_array = 0
erase cell
next
for each cell in live_list
flag_array = 1
display cell
next
for each cell in live_list
for each of its 8 neighbours
increment nbr_array
if high bit flag_array not set
add neighbour to check_list
set high bit flag_array
endif
next
next
for each cell in die_list
for each of its 8 neighbours
decrement nbr_array
if high bit flag_array not set
add neighbour to check_list
set high bit flag_array
endif
next
next
null live_list, die_list
for each cell in check_list
reset high bit flag_array
if flag_array = 0 AND nbr_array = 3
add cell to live_list
else if flag_array = 1 AND (nbr_array<2 OR nbr_array>3)
add cell to die_list
endif
next
repeat mainloop
.pa
Check your bulletin board for the file LIFEDEMO.COM, a demonstration
of the technique using an array of 80x48 cells.
As you read this, I am working on a "deluxe version" which will have
many added features: a large library of starting patterns, control
over display speed (sometimes things change too fast to follow!), the
ability to design or modify your own patterns with a mouse (256TC) or
cursor keys, and the option of saving interesting discoveries to your
own disk library. This program, NEWLIFE.COM, will be available soon
for the paltry sum of $20. Keep watching the BBS!
In the meantime, you may enjoy exploring the fast LIFE algorithm in
your own programming hours.
Reference: Data Structures and Program Design
Robert L. Kruse
Prentice-Hall International (Sydney) 1984
This is an excellent book for programmers, using Pascal to illustrate
many valuable techniques concerning linked lists, binary trees,
sorting methods and information retrieval.
The Game of Life
The game (better described as a simulation) of LIFE was introduced to
the computer hackers of the world in 1970 by British mathematician
J.H. Conway, and helped on its way to being a computer classic by
Martin Gardner in the pages of Scientific American magazine. It owes
its popularity to the fact that it is an ideal display for home
computers, and an ideal vehicle for home programmers.
LIFE takes place on a rectangular grid of cells which can either be
occupied by an organism or not. Which cells live and which die from
generation to generation are found from the following rules:
1) The neighbours of a cell are those which touch it
vertically, horizontally and diagonally.
2) A living cell with either two or three neighbours remains
alive in the next generation, otherwise it dies.
3) If a cell is dead, it will become alive in the next
generation only if it has exactly three live neighbours.
4) All births and deaths occur at the same time.
It is a fact of life that from very simple starting configurations,
complicated, beautiful and often fascinating patterns of cell colonies
can grow over many generations.
The grid in my versions of LIFE "wraps around" so that cells moving
off the left and bottom of the screen reappear at the right and top.
This is not quite what Mr Conway described, but an infinite unbounded
grid is a little beyond this programmer's capabilities!
Frank Connell 5/10/89