1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
headAt : xs i ->
with (
tmp : slice xs i (i + 1);
) ->
tmp.0;
get2 : grid r c ->
headAt (headAt grid r) c;
safeGet2 : grid r c ->
with (
rows : length grid;
cols : length (headAt grid 0);
rOk : r >= 0 and r < rows;
cOk : c >= 0 and c < cols;
) ->
when rOk and cOk is
true then get2 grid r c
_ then 0;
range : lo hi ->
when lo >= hi is
true then []
_ then prepend lo (range (lo + 1) hi);
sum : xs ->
reduce (acc x -> acc + x) 0 xs;
deltas : [-1, 0, 1];
neighborsValues : grid r c ->
reduce (acc dr -> reduce (acc2 dc -> when dr = 0 and dc = 0 is
true then acc2
_ then append acc2 (safeGet2 grid (r + dr) (c + dc))) acc deltas) [] deltas;
countNeighbors : grid r c ->
sum (neighborsValues grid r c);
nextCell : grid r c ->
with (
cell : get2 grid r c;
n : countNeighbors grid r c;
isAlive : cell = 1;
born : cell = 0 and n = 3;
survive : isAlive and n = 2 or n = 3;
) ->
when survive is
true then 1
_ then
when born is
true then 1
_ then 0;
rowNext : grid w r ->
map (c -> nextCell grid r c) (range 0 w);
step : grid ->
with (
h : length grid;
w : length (headAt grid 0);
) ->
map (r -> rowNext grid w r) (range 0 h);
g0 : [
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[1, 1, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
];
g1 : step g0;
g2 : step g1;
g3 : step g2;
g4 : step g3;
blinker0 : [[1, 1, 1], [0, 0, 0], [0, 0, 0]];
blinker1 : step blinker0;
blinker2 : step blinker1;
toad0 : [
[0, 1, 1, 1],
[1, 1, 1, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
toad1 : step toad0;
toad2 : step toad1;
beacon0 : [
[1, 1, 0, 0],
[1, 1, 0, 0],
[0, 0, 1, 1],
[0, 0, 1, 1]
];
beacon1 : step beacon0;
beacon2 : step beacon1;
block : [[1, 1], [1, 1]];
block1 : step block;
beehive : [[0, 1, 1, 0], [1, 0, 0, 1], [0, 1, 1, 0]];
beehive1 : step beehive;
pulsar0 : [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1],
[0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0]
];
pulsar1 : step pulsar0;
pulsar2 : step pulsar1;
pulsar3 : step pulsar2;
// Enhanced Conway's Game of Life display using io.print
io.print "";
io.print "Conway's Game of Life - Pattern Evolution";
io.print "==========================================";
io.print "";
io.print "Glider Pattern Evolution:";
io.print "Step 0:";
io.print g0;
io.print "Step 1:";
io.print g1;
io.print "Step 2:";
io.print g2;
io.print "Step 3:";
io.print g3;
io.print "Step 4:";
io.print g4;
io.print "";
io.print "Blinker Oscillation:";
io.print "Step 0:";
io.print blinker0;
io.print "Step 1:";
io.print blinker1;
io.print "Step 2:";
io.print blinker2;
io.print "";
io.print "Toad Breathing Pattern:";
io.print "Step 0:";
io.print toad0;
io.print "Step 1:";
io.print toad1;
io.print "Step 2:";
io.print toad2;
io.print "";
io.print "Beacon Blinking:";
io.print "Step 0:";
io.print beacon0;
io.print "Step 1:";
io.print beacon1;
io.print "Step 2:";
io.print beacon2;
io.print "";
io.print "Still Life Patterns:";
io.print "Block:";
io.print block;
io.print "Beehive:";
io.print beehive;
io.print "";
io.print "Pulsar Oscillation (Period 3):";
io.print "Step 0:";
io.print pulsar0;
io.print "Step 1:";
io.print pulsar1;
io.print "Step 2:";
io.print pulsar2;
io.print "Step 3:";
io.print pulsar3;
// End of program - visual patterns shown above
|