This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn transitions | |
"Return the transitions available for the next player" | |
[turn] | |
(:transitions turn)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(s/def ::game (s/coll-of ::turn/turn)) | |
(s/fdef current-turn | |
:args (s/cat :game ::game) | |
:ret ::turn/turn) | |
(s/fdef play-move | |
:args (s/cat :game ::game | |
:coord ::board/coord) | |
:ret ::game) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn new-game [] | |
(list (turn/new-init-turn))) | |
(defn current-turn | |
"Return the current state of the game: | |
* Board (cells and owners) | |
* Player score | |
* Next player to play" | |
[game] | |
(first game)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Partitioning by owner of the board gives contiguous sequences | |
(let [first-line (map vector (repeat 0) (range 0 16))] | |
(partition-by #(board/get-owner-at board %) first-line)) | |
;; Contiguous sequences of coordinates | |
=> | |
(([0 0]) ([0 1] [0 2] [0 3] [0 4]) ([0 5]) | |
([0 6]) ([0 7]) ([0 8] [0 9]) ([0 10]) | |
([0 11] [0 12] [0 13] [0 14] [0 15])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- ^boolean empty-cell? | |
[board [x y]] | |
(= :none (aget board x y))) | |
(defn- available-jumps-at | |
"Provides the list of jumps that can be done at a given `jump-destination`" | |
[board jump-destination] | |
(if (empty-cell? board jump-destination) | |
(eduction | |
(keep #(seek-jump-source-toward board jump-destination %)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- ^boolean block-jump? [owner] | |
(or (= owner :none) (= owner :wall))) | |
(defn- ^boolean jump-start? [looser owner] | |
(and looser (not= looser owner))) | |
(defn- ^boolean in-board? [x y] | |
(and (< -1 x board/width) (< -1 y board/height))) | |
(defn- seek-jump-source-toward |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn all-transitions | |
[board] | |
(let [aboard (board/board->array board)] ;; Convert to JS array | |
(transduce | |
(mapcat #(available-jumps-at aboard %)) ;; Compute jumps at a given position | |
(group-by-reducer :winner :destination) ;; Group by jump winner and destination | |
board/coordinates))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn group-by-reducer | |
"Create a reducer that groups by the provided key" | |
[& key-fns] | |
(fn | |
([] {}) | |
([result] result) | |
([result val] | |
(let [keys ((apply juxt key-fns) val)] | |
(update-in result keys conj val))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new-board :: IO Board | |
convert-cell :: Board -> Coord -> Player -> Board | |
get-owner-at :: Board -> Coord -> Player | |
to-seq :: Board -> [(Coord, Player)] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
board :: Turn -> Board | |
player :: Turn -> Player | |
scores :: Turn -> Scores |