Skip to content

Instantly share code, notes, and snippets.

(s/fdef new-board
:ret ::board)
(s/fdef convert-cell
:args (s/cat :board ::board
:coord ::coord
:owner ::player/player)
:ret ::board)
(s/fdef get-owner-at
(def coordinates
(vec (for [x (range width)
y (range height)]
[x y])))
(s/def ::coord (set coordinates))
(defn zip
"Create a sequence of tuples, each element being drawn from one collection"
[& colls]
(apply map vector colls))
(defn pick-n-of-each
"Pick n `elements` for each of the `groups` starting from the beginning"
[n elements groups]
(zip elements (mapcat #(repeat n %) groups)))
(deftest test-pick-n-of-each
(let [pick-n-of-each (comp vec algo/pick-n-of-each)]
(testing "Varying number of each group"
(are
[expected n] (= expected (pick-n-of-each n (range) [:a :b]))
[] 0
[[0 :a] [1 :b]] 1
[[0 :a] [1 :a] [2 :b] [3 :b]] 2
))
(testing "Varying number of groups"
(defn new-board
"Creates a new board with initial positions of each players"
[]
(->>
(conj player/all :wall)
(algo/randomly-pick-n-of-each cst/init-block-count coordinates)
(reduce
(fn [board [coord owner]] (convert-cell board coord owner))
empty-board)))
;; Imported from "triboard.logic.scores.cljs"
(s/def ::scores
(s/map-of ::player/player int?))
(s/def ::turn
(s/keys :req-un
[::board/board
::player/player
::scores/scores]))
(s/fdef next-turn
:args (s/cat :turn ::turn
:transition ::transition/transition)
:ret ::turn)
(s/fdef transitions
:args (s/cat :turn ::turn)
:ret (s/map-of ::transition/destination ::transition/transition))
(s/def ::destination ::board/coord)
(s/def ::taken (s/coll-of ::board/coord))
(s/def ::winner ::player/player)
(s/def ::looser ::player/player)
(s/def ::jump
(s/keys :req-un [::destination ::taken ::winner ::looser]))
(s/def ::transition (s/every ::jump))
(defn next-turn
"Apply the transtion to the current turn, yielding a new turn"
[turn transition]
(-> turn
(update :board transition/apply-transition transition)
(update :scores scores/update-scores transition)
(with-next-player)))
(defn- who-can-play
"Given the current player and the transitions for the next turn,
returns the next player that has at least one transition available"
[player transitions]
(filter ;; Keep only the players which
#(contains? transitions %) ;; have at least a valid move to play
(player/next-three player))) ;; from the next three players
(defn- with-next-player
"Complete the turn to compute to find the next player than can act