summary refs log tree commit diff
path: root/lib/game/no-moves.fnl
blob: 02482ccfb982e51668c821827c9557dbea3ea5c6 (plain)
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
(local {: tail} (require :lib.table))

(fn get-player-idxs [player moves]
  (icollect [i p (ipairs moves)] (when (= p player) i)))

(fn idx-to-neighbors [idxs all-neighbors]
  (icollect [_ i (ipairs idxs)] (tail (. all-neighbors i))))

(fn neighbor-is-occupied? [neighbors moves]
  (icollect [_ move (ipairs neighbors)]
    (icollect [_ neighbor (ipairs move)]
      (not= (. moves neighbor) 0))))

(fn reduce-to-bool [xs]
  (accumulate [acc true
               _ x (ipairs xs)]
               (and x)))

(fn no-moves? [neighbors all-moves player]
  (-> (get-player-idxs player all-moves)
    (idx-to-neighbors neighbors)
    (neighbor-is-occupied? all-moves)
    (reduce-to-bool)
    (reduce-to-bool)))

{: no-moves? }