summary refs log tree commit diff
path: root/lib/mill.fnl
blob: 14df2e7af06ec71fa9595c97fc517e2cfc1a0463 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(local {: contains} (require :lib.contains))

(fn get-candidates [all-mills next-move]
  "a list of mills that contain next-move"
  (icollect [_ mill (ipairs all-mills)] (if (contains mill next-move) mill)))

(fn any [t]
  "take a list of booleans, returns true if any of them are true"
  (accumulate [acc false
               i x (ipairs t)]
               (or acc x)))

(fn move-mills [moves-list]
  (icollect [_ moves (ipairs moves-list)]
    (let [player (. moves 1)]
      (accumulate [acc true
                   _ m (ipairs moves)]
                  (and acc (not= m 0) (= player m))))))

(fn candidate-moves [candidates moves]
  "Just turning board spaces into player moves"
  (icollect [_ spaces (ipairs candidates)]
            (icollect [_ space (ipairs spaces)]
                      (. moves space))))

(fn mill-at? [all-mills current-moves move]
  "Is there a mill at this move?"
  (let [candidates (get-candidates all-mills move)
        my-moves (candidate-moves candidates current-moves)
        my-mills (move-mills my-moves)
        result (any my-mills)
        ]
    result))

{: mill-at?
 ;; not for consumption,
 ;; just for testing:
 : get-candidates
 : candidate-moves
 : move-mills
 : any
 }