Repository navigation
Expand file tree
/
Copy pathlife.lisp
More file actions
249 lines (207 loc) · 4.63 KB
/
Copy pathlife.lisp
File metadata and controls
249 lines (207 loc) · 4.63 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
;; life.lisp - Game of life.
;;
;; Board size: 80 x 25
;;
(defconst WIDTH 80)
(defconst HEIGHT 25)
;;
;; The board we're working on and displaying
;;
(defvar board nil)
;;
;; Generation count
;;
(defvar generation 0)
;;
;; The next board
;;
(defvar next-board nil)
;;
;; Stop the processing after the given number of generations.
;;
;; If the arguments are "nil" then we set this to ten, and that
;; covers the case where we're launched by inception.
;;
(defvar limit 100)
;; Build a row containing WIDTH zeroes.
(defun make-row ()
(repeated WIDTH 0))
;; Build a board containing HEIGHT rows.
(defun make-board ()
(map
(lambda (n)
(make-row))
(seq (- HEIGHT 1))))
;; Clear each cell of the board
(defun clear-board (b)
(let ((y 0))
(while (< y HEIGHT)
(let ((row (nth b y))
(x 0))
(while (< x WIDTH)
(nth! row x 0)
(set! x (+ x 1))))
(set! y (+ y 1))))
b)
;; Draw the state of the board.
(defun draw-board (b)
(print "\e[?25l") ; hide cursor
(print "\e[H") ; move home & clear
(print "\e[2J") ; clear screen
(let ((y 0))
(while (< y HEIGHT)
(let ((row (nth b y))
(x 0))
(while (< x WIDTH)
(if (= (nth row x) 1)
(print "#")
(print " "))
(set! x (+ x 1)))
(newline)
(set! y (+ y 1)))))
(println "generation " generation " heap size:" (sys-heap-bytes))
(print "\e[?25h") ; restore cursor
;; allow execution to terminate when testing
(if (> generation limit)
(exit 0))
)
;;
;; Standard glider:
;;
;; . O .
;; . . O
;; O O O
;;
(defun place-glider (b x y)
(set-cell b (+ x 1) (+ y 0) 1)
(set-cell b (+ x 2) (+ y 1) 1)
(set-cell b (+ x 0) (+ y 2) 1)
(set-cell b (+ x 1) (+ y 2) 1)
(set-cell b (+ x 2) (+ y 2) 1))
;;
;; Random entries for the board
;;
(defun randomize (b)
(let ((y 0))
(while (< y HEIGHT)
(let ((row (nth b y))
(x 0))
(while (< x WIDTH)
(if (= (random 5) 0)
(nth! row x 1)
(nth! row x 0))
(set! x (+ x 1))))
(set! y (+ y 1)))))
;;
;; Count the eight neighbours around a cell.
;;
(defun count-neighbours (b x y)
(let ((n 0))
(set! n (+ n (get-cell b (- x 1) (- y 1))))
(set! n (+ n (get-cell b (+ x 0) (- y 1))))
(set! n (+ n (get-cell b (+ x 1) (- y 1))))
(set! n (+ n (get-cell b (- x 1) (+ y 0))))
(set! n (+ n (get-cell b (+ x 1) (+ y 0))))
(set! n (+ n (get-cell b (- x 1) (+ y 1))))
(set! n (+ n (get-cell b (+ x 0) (+ y 1))))
(set! n (+ n (get-cell b (+ x 1) (+ y 1))))
n))
;;
;; Evolve.
;;
(defun next-state (alive neighbours)
(if (= alive 1)
(if (< neighbours 2)
0
(if (> neighbours 3)
0
1))
(if (= neighbours 3)
1
0)))
;;
;; Get the given row
;;
(defun row (b y)
(nth b y))
;;
;; Get a cell, by coordinates.
;;
(defun get-cell (b x y)
(if (< x 0)
0
(if (< y 0)
0
(if (>= x WIDTH)
0
(if (>= y HEIGHT)
0
(nth (row b y) x))))))
;;
;; Set the contents of the given cell
;;
(defun set-cell (b x y value)
(nth! (row b y) x value))
;;
;; Step forwards, evolving each cell.
;;
(defun step-board (src dst)
(let ((y 0))
(while (< y HEIGHT)
(let ((src-row (row src y))
(dst-row (row dst y))
(x 0))
(while (< x WIDTH)
(nth! dst-row
x
(next-state
(nth src-row x)
(count-neighbours src x y)))
(set! x (+ x 1)))
)
(set! y (+ y 1))))
dst)
;;
;; Swap the global boards.
;;
(defun swap-boards ()
(let ((tmp board))
(set! board next-board)
(set! next-board tmp)))
;;
;; Seed withe either a bunch of gliders, or
;; random cells.
;;
(defun seed (glide)
(clear-board board)
(if glide
(do
(place-glider board 4 2)
(place-glider board 25 2)
(place-glider board 20 10)
(place-glider board 20 20)
(place-glider board 50 14)
(place-glider board 48 15))
(randomize board)))
;;
;; Entry point.
;;
(defun main (args)
(set! board (make-board))
(set! next-board (make-board))
;; no arguments? then random display
;; otherwise gliders
(if (nil? args)
(do
(set! limit 10)
(seed 1) ; glider
)
(if (> (length args) 1)
(seed 1) ; glider
(seed nil))) ; random
(while 1
(set! generation (+ generation 1))
(draw-board board)
(step-board board next-board)
(swap-boards)
(system "sleep 0.1")))