From 7776b2011a2585723078b275c838fd7332488d76 Mon Sep 17 00:00:00 2001 From: dozens Date: Mon, 3 Jun 2024 14:41:37 -0600 Subject: feat: capturing cannot break a mill refactors the way mill? is written to make it a little more versatile --- lib/mill.fnl | 55 ++++++++++++++++++++++++++----------------------------- 1 file changed, 26 insertions(+), 29 deletions(-) (limited to 'lib/mill.fnl') diff --git a/lib/mill.fnl b/lib/mill.fnl index e3b3337..14df2e7 100644 --- a/lib/mill.fnl +++ b/lib/mill.fnl @@ -1,45 +1,42 @@ (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 + "take a list of booleans, returns true if any of them are true" + (accumulate [acc false i x (ipairs t)] (or acc x))) +(fn move-mills [moves-list] + (icollect [_ moves (ipairs moves-list)] + (let [player (. moves 1)] + (accumulate [acc true + _ m (ipairs moves)] + (and acc (not= m 0) (= player m)))))) -(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? +(fn candidate-moves [candidates moves] + "Just turning board spaces into player moves" + (icollect [_ spaces (ipairs candidates)] + (icollect [_ space (ipairs spaces)] + (. moves space)))) + +(fn mill-at? [all-mills current-moves move] + "Is there a mill at this move?" + (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)) + +{: mill-at? ;; not for consumption, ;; just for testing: : get-candidates - : candidates->moves - : moves->mills + : candidate-moves + : move-mills : any } -- cgit 1.4.1-2-gfad0