summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/all-mills.fnl25
-rw-r--r--lib/all-mills.test.fnl41
-rw-r--r--lib/index.fnl2
-rw-r--r--lib/mill.fnl3
4 files changed, 69 insertions, 2 deletions
diff --git a/lib/all-mills.fnl b/lib/all-mills.fnl
new file mode 100644
index 0000000..562bb97
--- /dev/null
+++ b/lib/all-mills.fnl
@@ -0,0 +1,25 @@
+(local {: mill-at? } (require :lib.mill))
+(local {: mills } (require :lib.constants))
+
+(fn toggle-player [p] (if (= p 1) 2 1))
+
+(fn only-player-moves [moves player]
+  (icollect [_ move (ipairs moves)] (if (= move player) player 0)))
+
+(fn all-moves-are-mills? [moves player]
+  (accumulate [result true
+               i m (ipairs moves) ]
+              (and result (if (= m 0) true (mill-at? mills moves i)))))
+
+(fn all-mills? [all-moves current-player]
+  (let [next-player (toggle-player current-player)
+        player-moves (only-player-moves all-moves next-player)
+        all-mills (all-moves-are-mills? player-moves current-player)]
+    all-mills))
+
+{: all-mills?
+ ;; do not use; just for testing:
+ : toggle-player
+ : only-player-moves
+ : all-moves-are-mills?
+ }
diff --git a/lib/all-mills.test.fnl b/lib/all-mills.test.fnl
new file mode 100644
index 0000000..7f33ab1
--- /dev/null
+++ b/lib/all-mills.test.fnl
@@ -0,0 +1,41 @@
+(let [{: describe
+       :end test-end} (require :lib.test)
+      {: all-mills?
+       : toggle-player
+       : only-player-moves
+       : all-moves-are-mills?
+       } (require :lib.all-mills)]
+
+  (describe "all-mills" (fn []
+    (describe "#toggle-player()" (fn [t]
+      (t {:given "a player"
+          :should "return the next"
+          :expected 2
+          :actual (toggle-player 1)
+          })))
+    (describe "#only-player-moves()" (fn [t]
+      (let [moves    [ 0 2 0 2 2 2 0 0 0 0 0 0 0 2 0 0 0 2 0 2 0 1 1 1 ]
+            expected [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 ]
+            ]
+        (t {:given "a bunch of moves and a player"
+            :should "filter out all the moves not belonging to the player"
+            : expected
+            :actual (only-player-moves moves 1)
+            }))))
+    (describe "#all-moves-are-mills?()" (fn [t]
+      (let [moves [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 ]
+            ]
+        (t {:given "a bunch of moves and a player"
+            :should "return true if all the player moves are mills"
+            :expected true
+            :actual (all-moves-are-mills? moves 1)
+            }))
+      (let [moves [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 ]
+            ]
+        (t {:given "a bunch of moves and no mill and a player"
+            :should "return false"
+            :expected false
+            :actual (all-moves-are-mills? moves 1)
+            }))))
+    (test-end))))
+
diff --git a/lib/index.fnl b/lib/index.fnl
index 1fb686c..601ccb0 100644
--- a/lib/index.fnl
+++ b/lib/index.fnl
@@ -1,3 +1,4 @@
+(local {: all-mills?} (require :lib.all-mills))
 (local {: contains} (require :lib.contains))
 (local {: head} (require :lib.head))
 (local {: keys} (require :lib.keys))
@@ -9,6 +10,7 @@
 (local {: tail} (require :lib.tail))
 
 {
+ : all-mills?
  : contains
  : head
  : keys
diff --git a/lib/mill.fnl b/lib/mill.fnl
index 14df2e7..f9c8673 100644
--- a/lib/mill.fnl
+++ b/lib/mill.fnl
@@ -28,8 +28,7 @@
   (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 (any my-mills)]
     result))
 
 {: mill-at?