-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame-of-life.clj
78 lines (61 loc) · 1.42 KB
/
game-of-life.clj
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
; left as of at the end of first "Geneva clojurians meeting"
(def test-grid
[
[0 0 0 0 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 0 1 0 0]
[0 0 0 0 0] ])
(defn print-grid [grid]
(doseq [line grid]
(println line))
)
(print-grid test-grid)
(defn cell-at [grid x y]
((grid y) x)
)
(cell-at test-grid 2 1)
(cell-at test-grid 3 1)
(defn around-and-self [grid x y]
(let [
begin-x (max (- x 1) 0)
end-x (min (+ x 1) (- (count (grid y)) 1))
begin-y (max (- y 1) 0)
end-y (min (+ y 1) (- (count grid) 1))]
(for
[i (range begin-x (+ 1 end-x))
j (range begin-y (+ 1 end-y))
]
[ i j ] )))
(defn around [grid x y]
(filter #(not (= [x y] %)) (around-and-self grid x y)))
(around test-grid 2 1)
;(around test-grid 0 0)
;(around test-grid 4 4)
(defn next [current neighbours]
(cond
(< neighbours 2) 0
(= neighbours 2) current
(= neighbours 3) 1
(> neighbours 3) 0
))
(defn count-alive [grid x y]
(reduce + 0 (map (fn [[x y]] (cell-at grid x y)) (around grid x y))))
(count-alive test-grid 0 0)
(count-alive test-grid 4 4)
(count-alive test-grid 2 2)
(defn vectorize [flat n]
)
(defn next-gen [grid]
(partition (count (grid 0))
(map (fn [[x y]]
(next ((grid y) x) (count-alive grid x y)))
(for [j (range (count grid))
i (range (count (grid j)))]
[i j] ))))
(print-grid test-grid)
(print-grid (next-gen test-grid))
;
;(defn count-alive-neighbours [grid x y]
; (reduce + 0 (grid x y))
; )