Appendix D

"Life"—a sample program

When we looked for an idea for a typical moderately large 3ample program, the "Lift?" game invented by Prof. John Morton Conway of the University or Cambridge seemed a natural choice. Life was first presented by Martin Gardner in the Mathematical Games section or "Scientiric American" in October 1970, then again in Pebuary 1971 along with an Introduction to cellular automata theory, from which the game was evolved. Since then the game hss achieved a continuing popularity, with numerous articles appearing in such magazines as "Byte" and "Kilobaud", rererences in serious mathematical texts and sporadic newsletters.

Lire consists of rules that govern the development or patterns or dots on a rectangular grid. Each dot la considered to be a "live cell"; a cell (or grid element) that is empty is considered to be "dead". Each cell has eight neighbors surrounding it. By counting the living neighbors or each cell (empty or not) and applying the rollowing rules to all the cells in a grid at once, a new "generation" is formed.

1. Any live cell with 2 or 3 live neighbors survives.

2. Any live cell with 0 or 1 living neighbors dies (or loneliness).

3. Any live cell with 'I or more neighbors dies (or overcrowding).

Any empty (or dead) cell with exactly 3 live neighbors i3 "born" (comes to lire).

Even patterns or only a few dots can evolve in surprising and interesting ways. Some patterns are atalic, 4 dots arranged adjacent to each other in a square is the simplest example. Some patterns oscillate, and return to their initial configuration after 2 to hundreds of generations in some case3. Three dots in a vertical line will alternate with 3 dots in a horizontal line, to make the simplest oscillator. A very simple pattern will often explode into chaotic activity berore eventually settling down to static or oscillating components. Try 3 vertical dots (like the simple oscillator), with 2 dots added. Add a dot in contact with the center cell on the right, and another in contact with the bottom cell on the left. Position this pattern about 1/3 of the way Trom the lert edge or the tv screen and below the center or the 3creen vertically; this will rorestall the disruption that occurs when the pattern hlt3 the edges of the (40

x U2 decimal) grid area.

Look especially for a 3mall pattern of dot3 that wiggles along diagonally and "flys" to the edge of the screen. Thi3 is called a "glider" and is the simplest example of a traveling pattern. It is often interesting to follow the progress of parts of the debris of a developing pattern. For example, look for a pattern like a small 4 that appears as part of the above 5 dot pattern, which in isolation has an interesting life of its own.

To run Life, enter it into the computer and begin execution at F15E. Life Is long, so disassemble it to check for key-in errors and save It on cassette if you have built the interface. While running, a slightly flickering "cursor" dot will appear in the center of the screen. It can be moved around by pressing keys on the left controller. L2 moves it up, L4 moves it left, L6 moves it right and L8 moves it down. L5 causes the dot at the cursor location to change from live to dead or vice versa. LO causes the current pattern to be updated to the next generation. This will continue as long a3 LO i3 held down. Notice that TV sync is lost during generation updating. If any left key (except LO) is held down, the cursor is suppressed and you can see what is "under" it. The cursor itselT is not a live cell and lt3 position has no affect on forming new generations. To clear the screen when It is cluttered, press "game reset" and run the program again.

We programmed Life Just the way any user might. First we decided how the game should look and what compromises we would accept to make it easy to program the first version. Then we identified the major components and flow of the program, including sections that should be subroutines. This Is often done with boxes and arrows, arranged In diagrams call flowcharts. Next we wrote out the program in text lines that would each be lroplemcntablo as a few 6502 instructions. Names were used for storage locations. We examined the text for logical flaws and disagreements with our overall plan. Then we assigned addresses for the named storage locations. Next we wrote the right side of the listing, still using names for many storage locations, and label names for locations that were the targets of long or frequent Jumps and branches. We then assigned F100 as the address of the fir3t location of the program and counted bytes carefully through the code, assigning addre33e3 to labeled Instructions. Next we looked up the opcodes and wrote the left side of the listing (the actual Hex), filling in addresses at the same time. Finally, the long relative branohes were calculated (using the Relc key) and written on the hex listing as it was being keyed in.

We disassembled (In3 Dump) to check for errors and saved the program on cassette. It ran the first time! Often even a 3hort program will not work at first, but care in planning, especially with the early text version, will yield working programs much more often than the "hacker" approach favored by some programmers. If the program had been much longer, we would havo tested it in parts. For example, the key input section can be run without the generation update code. In professional programming publications, techniques to produce correct programs quickly have received much attention recently under the name "structured programming".

Even if you enjoy this program, you will probably notice a number of things that you might want to improve, which we Ignored In the interest of a 3hort and simple program. Examples inolude: maintaining sync (if not the picture) during generation updating, handling of the edges of the grid to really use those cells (perhaps with wrap around), counting the number of generations, plotting and moving whole patterns instead of single dots, and better controller capability (diagonal motion, Joysticks). If you are really enthusiastic about Life, you may be interested in our "Lifecard" module which will soon be available. It features a 96 x 80 (decimal) game grid and display, all the Improvements mentioned above and more.

Memory Usage: (also F300 - F3D1 is used a3 a copy of the display)

name location use

Was this article helpful?

0 0

Post a comment