Skip to content

Instantly share code, notes, and snippets.

(defn transitions
"Return the transitions available for the next player"
[turn]
(:transitions turn))
(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)
(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))
;; 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]))
(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 %))
(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
(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)))
(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)))))
new-board :: IO Board
convert-cell :: Board -> Coord -> Player -> Board
get-owner-at :: Board -> Coord -> Player
to-seq :: Board -> [(Coord, Player)]
board :: Turn -> Board
player :: Turn -> Player
scores :: Turn -> Scores