996
|
1
|
|
2 life2.i is a revised version of my Life program, differing in two
|
|
3 ways from the original. First, throughout the program I have
|
|
4 streamlined the loops by using COME FROM statements and combining
|
|
5 initialization and re-initialization phases where practical. Second,
|
|
6 instead of just printing the coordinates of live cells, I use the new
|
|
7 binary output feature to print out the board as an array of .'s and
|
|
8 O's. This new output section is made into a subroutine---entry point
|
|
9 (300)---which I call from within the main loop as well as at the end of
|
|
10 the program. If you don't want to see the position at every time step,
|
|
11 just remove the call to (300) from within the main loop, and only the
|
|
12 final position will be printed.
|
|
13
|
|
14 Notes for programmers: The output routine prints the state matrix a
|
|
15 full line at a time, using a 1-D array one cell longer than a row of
|
|
16 the matrix. This final cell is used to print a line feed (ASCII 012)
|
|
17 at the end of each line. Live cells are represented as O's (0117),
|
|
18 while dead cells are shown as .'s (056). If your machine does not
|
|
19 use ASCII or does not use LF to terminate lines this may not work.
|
|
20 The INTERCAL output routine reverses the order of bits in each of
|
|
21 these characters, so the constants hard-coded into the program are
|
|
22 #242 for 'O', #116 for '.', and #80 for '\n'. The value of the last
|
|
23 character output, which is needed by the Turing Text output model,
|
|
24 is stored in .2. This variable is initialized to #0 and then STASHed
|
|
25 at the beginning of the program, then RETRIEVEd by the output routine
|
|
26 each time it is called. This is opposite from the usual way STASH is
|
|
27 used, but I was feeling a little twisted the day I wrote all this.
|
|
28
|
|
29 There is still considerable room for improvement in the (300) output
|
|
30 algorithm. With printing at each time step the program spends
|
|
31 roughly two-thirds of its time doing output, and I suspect that
|
|
32 most of that is squandered in the (1010) subtraction routine. It
|
|
33 should be possible to greatly speed up the process by precomputing
|
|
34 and storing all the possible differences. Except for initialization
|
|
35 there are only six to consider: "LF-.", ".-LF", "O-.", ".-O", ".-.",
|
|
36 and "O-O". Of these the first two occur predictably at the beginning
|
|
37 and end of each line, and the last two are trivially zero. The
|
|
38 necessary program logic would be a little more complex; perhaps it
|
|
39 could be done with array indexing? Consider it a challenge!
|
|
40
|
|
41 Despite all its inefficiencies, the INTERCAL version on a Sparc 1 is
|
|
42 still faster than the first Life program I ever wrote, in BASIC on
|
|
43 a TRS-80, by a factor of about 50. Sobering thought.
|
|
44
|
|
45 Louis Howell
|
|
46 December 15, 1991
|