summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authordozens2024-06-06 22:49:52 -0600
committerdozens2024-06-06 22:49:52 -0600
commitf985dc4e5c9fdec06436c21440c3dc7245369847 (patch)
tree336d7287757c54a66824ed03c7531f23122a33f2 /lib
parent7776b2011a2585723078b275c838fd7332488d76 (diff)
feat: add moving phase
also moves `moves` into the game object and moves updating `moves` to
game:update
Diffstat (limited to 'lib')
-rw-r--r--lib/index.fnl6
-rw-r--r--lib/kvflip.fnl (renamed from lib/flip.fnl)4
-rw-r--r--lib/kvflip.test.fnl (renamed from lib/flip.test.fnl)8
-rw-r--r--lib/space-is-neighbor.fnl18
-rw-r--r--lib/space-is-neighbor.test.fnl22
5 files changed, 50 insertions, 8 deletions
diff --git a/lib/index.fnl b/lib/index.fnl
index 6323160..1fb686c 100644
--- a/lib/index.fnl
+++ b/lib/index.fnl
@@ -1,19 +1,21 @@
 (local {: contains} (require :lib.contains))
-(local {: flip} (require :lib.flip))
 (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))
 
 {
  : contains
- : flip
  : head
  : keys
+ : kvflip
  : mill-at?
  : pprint
  : slice
+ : space-is-neighbor?
  : tail
  }
diff --git a/lib/flip.fnl b/lib/kvflip.fnl
index 9d21b63..25fc222 100644
--- a/lib/flip.fnl
+++ b/lib/kvflip.fnl
@@ -1,6 +1,6 @@
-(fn flip [t]
+(fn kvflip [t]
   "takes a table of {key value} and returns a table of {value key}"
   (collect [k v (pairs t)] (values v k)))
 
-{: flip}
+{: kvflip}
 
diff --git a/lib/flip.test.fnl b/lib/kvflip.test.fnl
index 32fa005..162650d 100644
--- a/lib/flip.test.fnl
+++ b/lib/kvflip.test.fnl
@@ -1,13 +1,13 @@
-(let [{: flip} (require :lib.flip)
+(let [{: kvflip} (require :lib.kvflip)
       {: describe :end test-end} (require :lib.test)]
-  (describe "flip()" (fn [t]
+  (describe "kvflip()" (fn [t]
                        (let [input {:apple "red" :banana "yellow"}
                              expected {:red "apple" :yellow "banana"}
                              ]
                          (t {:given "a table"
-                             :should "flip that table!"
+                             :should "kvflip that table!"
                              : expected
-                             :actual (flip input)})
+                             :actual (kvflip input)})
                          (test-end)))))
 
 
diff --git a/lib/space-is-neighbor.fnl b/lib/space-is-neighbor.fnl
new file mode 100644
index 0000000..380607c
--- /dev/null
+++ b/lib/space-is-neighbor.fnl
@@ -0,0 +1,18 @@
+(local {: contains} (require :lib.contains))
+(local {: head} (require :lib.head))
+(local {: tail} (require :lib.tail))
+
+(lambda space-is-neighbor? [all-neighbors from to]
+  ;; i have learned to check that i'm passing the correct type of move
+  ;; i.e. a number and not a string
+  (assert (= "number" (type from)) "from must be a number")
+  (assert (= "number" (type to)) "to must be a number")
+  (assert (= "table" (type all-neighbors)) "all-neighbors must be a table")
+
+  (let [neighborhood-list (icollect [_ n (ipairs all-neighbors)] (if (= from (head n)) n))
+        neighborhood (head neighborhood-list)
+        neighbors (tail neighborhood)
+        is-neighbor (contains neighbors to)]
+    is-neighbor))
+
+{: space-is-neighbor?}
diff --git a/lib/space-is-neighbor.test.fnl b/lib/space-is-neighbor.test.fnl
new file mode 100644
index 0000000..7b0c0af
--- /dev/null
+++ b/lib/space-is-neighbor.test.fnl
@@ -0,0 +1,22 @@
+(let [{: space-is-neighbor?} (require :lib.space-is-neighbor)
+      {: neighbors} (require :lib.constants)
+      {: describe :end test-end} (require :lib.test)
+      with-neighbors (partial space-is-neighbor? neighbors)
+      ]
+
+  (describe "space-is-neighbor()" (fn [t]
+    (t {:given "space of 3"
+        :should "know 2 is a neighbor"
+        :expected true
+        :actual  (with-neighbors 3 2)})
+    (t {:given "space of 3"
+        :should "know 15 is a neighbor"
+        :expected true
+        :actual  (with-neighbors 3 15)})
+    (t {:given "space of 3"
+        :should "know 1 is not a neighbor"
+        :expected false
+        :actual  (with-neighbors 3 1)})
+
+    (test-end))))
+