-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgol.goof
More file actions
186 lines (168 loc) · 3.44 KB
/
gol.goof
File metadata and controls
186 lines (168 loc) · 3.44 KB
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
182
183
184
185
186
include "std.goof"
const ROWS 8 end
const COLS 16 end
const PUTD_BUFFER_CAP 32 end
memory nbors 8 end
memory time 8 end
memory putd-buffer PUTD_BUFFER_CAP end
memory matrix 0 end
// 10 -> "10"
proc putd int -- --
if dup 0 = do
"0" puts
else
putd-buffer PUTD_BUFFER_CAP +
// n ptr
while over 0 > do
1 - dup rot rot
// ptr ptr n
10 divmod
// ptr ptr n%10 n/10
rot rot swap
// ptr n/10 ptr n%10
48 + . swap
end
dup // n ptr len
putd-buffer PUTD_BUFFER_CAP + swap - swap puts
end
drop
end
proc set_value int int int -- --
// value j i
COLS * + 8 * matrix + swap .64
end
proc get_value int int -- --
// j i
COLS * + 8 * matrix + ,64
end
proc mod int int -- int --
// a b
dup // a b b
rot // b a b
+ // b (a+b)
swap
%
end
proc norm int int -- int int --
// j i
ROWS mod // j i%ROWS
swap COLS mod // i%ROWS j%COLS
swap
end
proc get_nbor int int int int -- int --
// j i dj di
rot rot // j dj di i
+ rot + // j dj i+di --> i+di j+dj
swap
norm
get_value
if dup 1 = swap 3 = | do
nbors ,64 1 + nbors swap .64
end
end
proc count_nbors int int -- int --
nbors 0 .64 // nbors
// j i
2dup -1 -1 get_nbor
2dup 0 -1 get_nbor
2dup 1 -1 get_nbor
2dup -1 0 get_nbor
2dup 1 0 get_nbor
2dup -1 1 get_nbor
2dup 0 1 get_nbor
2dup 1 1 get_nbor
drop drop
end
proc display -- --
ROWS 0
// ROWS i
while 2dup > do
COLS 0
// ROWS i COLS j
while 2dup > do
rot rot // ROWS COLS j i
2dup // ROWS COLS j i j i
get_value
if dup 1 = do
"#" puts
else
"." puts
end
drop
rot // ROWS i COLS j
1 +
end
drop drop
"\n" puts
1 +
end
drop drop
end
proc next -- --
ROWS 0
// ROWS i
while 2dup > do
COLS 0
// ROWS i COLS j
while 2dup > do
rot rot // ROWS COLS j i
2dup // ROWS COLS j i j i
count_nbors // ROWS COLS j i
if 2dup get_value 1 = do
// ROWS COLS j i
if nbors ,64 dup 2 < swap 3 > | do
2dup 3 rot set_value
end
elif 2dup get_value 0 = do
if nbors ,64 3 = do
2dup 2 rot set_value
end
end
rot // ROWS i COLS j
1 +
end
drop drop
1 +
end
drop drop
ROWS 0
// ROWS i
while 2dup > do
COLS 0
// ROWS i COLS j
while 2dup > do
rot rot // ROWS COLS j i
2dup // ROWS COLS j i j i
get_value
if dup 3 = do
drop
2dup 0 rot set_value
elif dup 2 = do
drop
2dup 1 rot set_value
else
drop
end
rot // ROWS i COLS j
1 +
end
drop drop
1 +
end
drop drop
end
// 010
// 001
// 111
1 1 0 set_value
1 2 1 set_value
1 0 2 set_value
1 1 2 set_value
1 2 2 set_value
time 1 .
while 1 (bool) do
next
display
"\033[" puts ROWS putd "A\033[" puts COLS putd "D" puts
0 time sleep drop
end