Skip to content

Commit c6b8b55

Browse files
committed
refactor: rewrote examples/games/game_of_life.ark to use more stdlib constructs
1 parent 962de17 commit c6b8b55

File tree

2 files changed

+58
-65
lines changed

2 files changed

+58
-65
lines changed

examples/games/game_of_life.ark

Lines changed: 57 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,68 @@
1-
(let board [0 0 0 0 0 1 1 1 1 1 1 0 0 0 0 0])
2-
(let width 4)
3-
(let height 4)
1+
(import std.List)
2+
(import std.String)
3+
(import std.Switch)
4+
5+
(let board [0 0 0 1 1 1 0 0 0])
6+
(let width 3)
7+
(let height 3)
48
(let dead 0)
59
(let alive 1)
610

7-
(let get (fun (board_ i width height)
8-
(if (and (>= i 0) (< i (* width height)))
9-
(@ board_ i)
11+
(let get (fun (board_ x y)
12+
(if (and (>= x 0) (>= y 0) (< x width) (< y height))
13+
(@ board_ (+ x (* y width)))
1014
dead)))
1115

12-
(let neigh (fun (board_ index width height) {
16+
(let neigh (fun (board_ index) {
1317
(let x (math:floor (mod index width)))
1418
(let y (math:floor (/ index width)))
15-
(mut count 0)
16-
17-
(if (>= (- y 1) 0)
18-
(set count (+ count (get board_ (- index width) width height))))
19-
20-
(if (< (+ y 1) height)
21-
(set count (+ count (get board_ (+ index width) width height))))
22-
23-
(if (>= (- x 1) 0)
24-
(set count (+ count (get board_ (- index 1) width height))))
25-
26-
(if (< (+ x 1) width)
27-
(set count (+ count (get board_ (+ index 1) width height))))
28-
29-
(if (and (>= (- x 1) 0) (>= (- y 1) 0))
30-
(set count (+ count (get board_ (- index 1 width) width height))))
31-
32-
(if (and (< (+ x 1) width) (< (+ y 1) height))
33-
(set count (+ count (get board_ (+ index width 1) width height))))
34-
35-
(if (and (>= (- x 1) 0) (< (+ y 1) height))
36-
(set count (+ count (get board_ (+ index width -1) width height))))
37-
38-
(if (and (< (+ x 1) width) (>= (- y 1) 0))
39-
(set count (+ count (get board_ (- index width -1) width height))))
40-
count }))
19+
(+
20+
(get board_ (- x 1) y)
21+
(get board_ (+ x 1) y)
22+
(get board_ x (- y 1))
23+
(get board_ x (+ y 1))
24+
(get board_ (- x 1) (- y 1))
25+
(get board_ (+ x 1) (- y 1))
26+
(get board_ (- x 1) (+ y 1))
27+
(get board_ (+ x 1) (+ y 1))) }))
28+
29+
(let indices (list:iota 0 (* height width)))
30+
(let update (fun (board) {
31+
(mut copy (list:fill (* height width) dead))
32+
33+
(list:forEach indices
34+
(fun (i) {
35+
(let neighs (neigh board i))
36+
(switch neighs
37+
2 (@= copy i (@ board i))
38+
3 (@= copy i alive)
39+
_ (@= copy i dead)) }))
40+
41+
copy }))
42+
43+
(let display (fun (board width height)
44+
(print (string:join
45+
(list:map
46+
(list:chunkBy
47+
(list:map board
48+
(fun (cell)
49+
(if (= alive cell) "x" "•")))
50+
3)
51+
(fun (sublist)
52+
(string:join sublist "")))
53+
"\n"))))
4154

42-
(mut copy (list:fill (* height width) dead))
43-
(mut i 0)
44-
(while (< i (* width height)) {
45-
(mut neighs (neigh board i width height))
46-
47-
(if (= 3 neighs)
48-
(set copy (list:setAt copy i alive)))
49-
50-
(if (= 2 neighs)
51-
(set copy (list:setAt copy i (@ board i))))
52-
53-
(if (or (< neighs 2) (> neighs 3))
54-
(set copy (list:setAt copy i dead)))
55-
(set i (+ 1 i)) })
56-
57-
(let display (fun (board width height) {
58-
(mut i 0)
59-
60-
(while (< i (* width height)) {
61-
(mut y (math:floor (/ i width)))
62-
(mut x (math:floor (mod i width)))
55+
(print "initial board:")
56+
(display board width height)
6357

64-
(if (= 0 x) (puts "\n"))
58+
(print "\ngen 2:")
59+
(mut new (update board))
60+
(display new width height)
6561

66-
(if (= alive (@ board i))
67-
(puts "x")
68-
(puts " "))
69-
(set i (+ 1 i)) })
70-
(puts "\n") }))
62+
(print "\ngen 3:")
63+
(set new (update new))
64+
(display new width height)
7165

72-
(print "initial board:")
73-
(display board width height)
74-
(print "new board:")
75-
(display copy width height)
66+
(print "\ngen 4:")
67+
(set new (update new))
68+
(display new width height)

0 commit comments

Comments
 (0)