Debian Codegraph @

ipl/packs/loadfunc/cspace.icn

raw
1 ############################################################################
2 #
3 # File: cspace.icn
4 #
5 # Subject: Program to demonstrate a cellular automata
6 #
7 # Author: Gregg M. Townsend
8 #
9 # Date: August 4, 2000
10 #
11 ############################################################################
12 #
13 # Usage: cspace [-W width] [-H height]
14 #
15 # This program demonstrates a two-dimensional cellular automata designed
16 # by David Griffeath of the University of Wisconsin. A. K. Dewdney
17 # calls this "Cyclic Space".
18 #
19 # The window is seeded randomly and successive generations are displayed.
20 # Press the space bar to single step, G to run free, R to reseed, or
21 # Q to quit.
22 #
23 # See A.K.Dewdney, Computer Recreations, Scientific American, Aug. 1989.
24 # (Reprinted in Dewdney, The Magic Machine, W.H.Freeman, 1990.)
25 #
26 ############################################################################
27
28
29 $include "libnames.icn"
30
31 $define SIZE "size=600,401" # default window size
32
33 $define PALETTE "c1" # color palette to use
34 $define CYCLE "MAOBPCQDSFUHIVYL" # colors (and cycle length)
35
36 #some other possibilities:
37 #light $define CYCLE "aBPCcdefgh<ijklm"
38 #dark $define CYCLE "aBPCQRSTUuVJWXYZ"
39 #bright $define CYCLE "NAOBPCQDSFUHVIYL"
40
41 link graphics, random
42
43
44 procedure main(args)
45 local w, h, u, g
46 local stopped, cspgen
47
48 # Load the C code that ages the automata.
49 cspgen := loadfunc(FUNCLIB, "cspgen")
50
51 # Open the window. Don't use the last row: If the entire window
52 # is redrawn, the color map is cleared and reloaded, causing delays.
53 Window(SIZE, args)
54 w := WAttrib("width")
55 h := WAttrib("height") - 1
56
57 # Initialize the first generation randomly.
58 randomize()
59 u := seed(w, h)
60
61 # Process events and display generations.
62 g := 0
63 repeat {
64 while (*Pending() > 0) | \stopped do case Event() of {
65 " ": { stopped := 1; break }
66 !"\n\rgG": { stopped := &null; break }
67 !"rR": { u := seed(w, h); break }
68 QuitEvents(): { log(w, h, g); exit() }
69 &resize: { w:=WAttrib("width"); h:=WAttrib("height")-1; break }
70 }
71 DrawImage(,,u) # display current generation
72 u := cspgen(u, CYCLE) # create next generation
73 g +:= 1
74 if g % 100 = 0 then log(w, h, g) # log statistics every 100th gen
75 }
76 end
77
78 procedure log(w, h, g)
79 write(w, " x ", h, ":", right(g, 6), " generations in ",
80 &time / 1000.0, " seconds")
81 return
82 end
83
84 procedure seed(w, h)
85 local u, n
86
87 u := w || "," || PALETTE || ","
88 n := w * h
89 every 1 to n do
90 u ||:= ?CYCLE
91 return u
92 end