summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/index.fnl20
-rw-r--r--lib/no-moves.fnl26
-rw-r--r--lib/no-moves.test.fnl51
-rw-r--r--lib/string.fnl4
-rw-r--r--lib/table.fnl17
5 files changed, 104 insertions, 14 deletions
diff --git a/lib/index.fnl b/lib/index.fnl
index 601ccb0..2eff31e 100644
--- a/lib/index.fnl
+++ b/lib/index.fnl
@@ -1,23 +1,15 @@
+(local str (require :lib.string))
+(local tbl (require :lib.table))
 (local {: all-mills?} (require :lib.all-mills))
-(local {: contains} (require :lib.contains))
-(local {: head} (require :lib.head))
-(local {: keys} (require :lib.keys))
-(local {: kvflip} (require :lib.kvflip))
 (local {: mill-at?} (require :lib.mill))
-(local {: pprint} (require :lib.tableprint))
-(local {: slice} (require :lib.slice))
 (local {: space-is-neighbor?} (require :lib.space-is-neighbor))
-(local {: tail} (require :lib.tail))
+(local {: no-moves?} (require :lib.no-moves))
 
 {
+ : str
+ : tbl
  : all-mills?
- : contains
- : head
- : keys
- : kvflip
  : mill-at?
- : pprint
- : slice
+ : no-moves?
  : space-is-neighbor?
- : tail
  }
diff --git a/lib/no-moves.fnl b/lib/no-moves.fnl
new file mode 100644
index 0000000..591cb7c
--- /dev/null
+++ b/lib/no-moves.fnl
@@ -0,0 +1,26 @@
+(local {: tail} (require :lib.tail))
+
+(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? }
diff --git a/lib/no-moves.test.fnl b/lib/no-moves.test.fnl
new file mode 100644
index 0000000..db0613c
--- /dev/null
+++ b/lib/no-moves.test.fnl
@@ -0,0 +1,51 @@
+(let [{: no-moves?} (require :lib.no-moves)
+      {: neighbors} (require :lib.constants)
+      {: describe :end test-end} (require :lib.test)
+      with-neighbors (partial no-moves? neighbors)
+      ]
+
+  (describe "no-moves()" (fn [t]
+    (let [moves [ 1 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 ]
+          player 1
+          ]
+        (t {:given "one move with no moves"
+            :should "return true"
+            :expected true
+            :actual (with-neighbors moves player)
+            }))
+    (let [moves [ 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
+          player 1
+          ]
+        (t {:given "one move with one move"
+            :should "return false"
+            :expected false
+            :actual (with-neighbors moves player)
+            }))
+    (let [moves [ 1 1 1 0 2 0 0 0 0 2 0 0 0 0 2 0 0 0 0 0 0 0 ]
+          player 1
+          ]
+        (t {:given "several moves with no moves"
+            :should "return true"
+            :expected true
+            :actual (with-neighbors moves player)
+            }))
+    (let [moves [ 0 2 0 2 1 2 0 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ]
+          player 1
+          ]
+        (t {:given "four occupied neighbors"
+            :should "return true"
+            :expected true
+            :actual (with-neighbors moves player)
+            }))
+    (let [moves [ 1 2 1 2 0 2 1 2 1 2 1 0 1 2 1 2 2 2 0 1 0 0 0 0 0 ]
+          player 2
+          ]
+        (t {:given "this turn that is giving me trouble"
+            :should "return true"
+            :expected true
+            :actual (with-neighbors moves player)
+            }))
+
+    (test-end))))
+
+
diff --git a/lib/string.fnl b/lib/string.fnl
new file mode 100644
index 0000000..510b0ed
--- /dev/null
+++ b/lib/string.fnl
@@ -0,0 +1,4 @@
+(fn capitalize [s]
+  (.. (string.upper (string.sub s 1 1)) (string.sub s 2)))
+
+{: capitalize}
diff --git a/lib/table.fnl b/lib/table.fnl
new file mode 100644
index 0000000..f40c299
--- /dev/null
+++ b/lib/table.fnl
@@ -0,0 +1,17 @@
+(local {: contains} (require :lib.contains))
+(local {: head} (require :lib.head))
+(local {: keys} (require :lib.keys))
+(local {:kvflip invert} (require :lib.kvflip))
+(local {:pprint print} (require :lib.tableprint))
+(local {: slice} (require :lib.slice))
+(local {: tail} (require :lib.tail))
+
+{
+ : contains
+ : head
+ : keys
+ : invert
+ : print
+ : slice
+ : tail
+ }