-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathgame-of-life.crumb
70 lines (58 loc) · 1.5 KB
/
game-of-life.crumb
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
w = (integer (divide (columns) 2))
h = (subtract (rows) 1)
world = (map (range h) {_ y ->
<- (map (range w) {item x ->
<- (integer (add (random) 0.5))
})
})
get_block = {n ->
<- (if (is n 1) {<- "██"} {<- " "})
}
render = {world n ->
<- (join "\e[H" (reduce world {acc row y ->
<- (join acc
(reduce row {acc item x -> <- (join acc (get_block item))} "")
"\n")
} "") "Frame: " (string n))
}
get_cell = {world x y ->
out_of_bounds = (or
(greater_than x (subtract w 1))
(less_than x 0)
(greater_than y (subtract h 1))
(less_than y 0)
)
<- (if out_of_bounds {<- 0} {<- (get (get world y) x)})
}
count_neighbors = {world x y ->
<- (add
(get_cell world (add x 1) (add y 1))
(get_cell world (add x 1) y)
(get_cell world (add x 1) (subtract y 1))
(get_cell world x (add y 1))
(get_cell world x (subtract y 1))
(get_cell world (subtract x 1) (add y 1))
(get_cell world (subtract x 1) y)
(get_cell world (subtract x 1) (subtract y 1))
)
}
update_cell = {world x y ->
neighbors = (count_neighbors world x y)
state = (get_cell world x y)
<- (if (is neighbors 3) {<- 1}
(is neighbors 2) {<- state}
{<- 0}
)
}
update_world = {world ->
<- (map world {row y ->
<- (map row {item x ->
<- (update_cell world x y)
})
})
}
(until "stop" {curr_world n ->
(print (render curr_world n))
res = (update_world curr_world)
<- res
} world)