summary refs log tree commit diff
path: root/main.fnl
diff options
context:
space:
mode:
authordozens2024-06-03 14:41:37 -0600
committerdozens2024-06-05 21:53:45 -0600
commit7776b2011a2585723078b275c838fd7332488d76 (patch)
treeddf78da1eb7c4a34e4771d6a2e5a74043597bfb5 /main.fnl
parent7c07d6e6ececbf73e18a639e00b3690d4827e12a (diff)
feat: capturing cannot break a mill
refactors the way mill? is written to make it a little more versatile
Diffstat (limited to 'main.fnl')
-rw-r--r--main.fnl47
1 files changed, 28 insertions, 19 deletions
diff --git a/main.fnl b/main.fnl
index d9ec3b4..9954984 100644
--- a/main.fnl
+++ b/main.fnl
@@ -5,12 +5,12 @@
   : flip
   : pprint
   : slice
-  :mill? mill-maker
+  :mill-at? mill-at-maker
   } (require :lib.index))
 ;; constants...more like just strings
 (local const (require :lib.constants))
 ;; front-loading mill with a partial
-(local mill? (partial mill-maker const.mills))
+(local mill-at? (partial mill-at-maker const.mills))
 
 
 ; there are three phases of play:
@@ -40,6 +40,7 @@
 ; 0 = unoccupied
 ; 1 = Player 1
 ; 2 = Player 2
+;; TODO: move this to game.moves?
 (local moves (fcollect [i 1 24] 0))
 
 
@@ -48,14 +49,21 @@
   :player player.one
   :stage stages.placing
   :update (fn [self move]
-             (if (mill? moves move self.player)
-               (do
-                 (print "Mooooooo")
-                 (tset self :stage stages.capture)
-                 )
-               (tset self :player (if (= player.one self.player) player.two player.one))
-               )
-             )
+    (case self.stage
+      4 ;; capture
+        (do
+          (tset moves move 0)
+          (tset self :player (self:next-player))
+          (tset self :stage stages.placing)
+          )
+      1 ;; placing
+        (if (mill-at? moves move)
+          (tset self :stage stages.capture)
+          (tset self :player (self:next-player))
+          )
+    )
+  )
+  :next-player (fn [self] (if (= player.one self.player) player.two player.one))
 })
 
 
@@ -72,8 +80,8 @@
         (do
           (let [offset (+ index slots)
                 myslice (slice moves index offset)]
-						(print (string.format row-template (table.unpack myslice)))
-						(set index offset)))
+            (print (string.format row-template (table.unpack myslice)))
+            (set index offset)))
         (print row))))
   (print (.. "Stage: " (string-upper (. (flip stages) game.stage))))
   (print (.. "Player " game.player "'s turn:")))
@@ -107,8 +115,9 @@
 
 
 (fn space-is-occupied-by-opponent? [m]
-	(let [opponent (if (= game.player 1) 2 1)]
-    (= opponent (. moves (index-of-move m)))))
+  (let [opponent (if (= game.player 1) 2 1)
+        result (= opponent (. moves (index-of-move m))) ]
+    result))
 
 
 
@@ -126,14 +135,15 @@
     (and
       (= stages.placing game.stage)
       (or (space-exists? move)
-          (print "That space does not exist!\nHint: 1a 1A A1 a1 are all equal moves."))
+          (print "That space does not exist!\nHint: 1a 1A A1 a1 are all the same move."))
       (or (space-is-unoccupied? move)
           (print "That space is occupied!")))
     (and
-      ;; TODO: add capturing phase
-      (= stages.capturing game.stage)
-			(or (space-is-occupied-by-opponent? move)
+      (= stages.capture game.stage)
+      (or (space-is-occupied-by-opponent? move)
           (print "Choose an opponent's piece to remove."))
+      (or (not (mill-at? moves (index-of-move move)))
+          (print "Ma'am, it is ILLEGAL to break up a mill."))
       )
     (and
       ;; TODO: add flying phase
@@ -143,7 +153,6 @@
   )
 
 
-
 ; get player input
 (fn get-move []
   (io.read))