summary refs log tree commit diff
path: root/lib/mill.fnl
blob: e3b33372aa2517bf356f48620fa1d7e47026f714 (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
43
44
45
(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 candidates->moves [candidates current-moves move player]
  "a list of the candidate mills expressed as current moves"
  (icollect [_ spaces (ipairs candidates)]
    (icollect [_ space (ipairs spaces)] 
              (if (= space move) :x (. current-moves space)))))

(fn moves->mills [spaces player]
  "a list of bools if the candidate moves + player are all the same"
  (let [next-move (icollect [_ y (ipairs spaces)]
                            (icollect [_ x (ipairs y)]
                                      (if (= x :x) player x))) ]
    (icollect [_ move (ipairs next-move)]
      (accumulate [acc true
                   idx m (ipairs move)]
        (and acc (= player m))))))

(fn any [t]
	(accumulate [acc false
               i x (ipairs t)]
               (or acc x)))


(fn mill? [all-mills current-moves next-move player]
  "Does the current move for the current player create a mill?"
  (let [candidates (get-candidates all-mills next-move)
        moves (candidates->moves candidates current-moves next-move player)
        mills (moves->mills moves player)
        result (any mills)]
		result))

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