Skip to content

Instantly share code, notes, and snippets.

(s/def ::blue int?)
(s/def ::red int?)
(s/def ::green int?)
(s/def ::scores
(s/keys :req-un [::blue ::red ::green]))
(s/def ::good-binary-tree
(s/cat
:value int?
:children (s/map-of #{:left :right} ::good-binary-tree)))
[1 {:left [2 {}]
:right [3 {:left [4 {}]}]
}]
(defprotocol IUserInteractions
"A protocol for the interactions that can be triggered from the GUI"
(on-new-game [this] "Send a new game command")
(on-toogle-help [this] "Send a toggle help command")
(on-restart [this] "Send a restart command")
(on-undo [this] "Send an undo command")
(on-player-move [this x y] "Send a play command at [x y]"))
(defn main-frame
[turn suggestions interactions]
[:div.game-panel
[menu/show-top-menu (:scores turn) (:player turn) interactions]
[board/render-board (:board turn) suggestions interactions]])
(defn show-top-menu
"Show the top menu of the game that contains
* The player scores
* The main commands"
[scores current-player interactions]
[:div.scores
[make-button #(i/on-new-game interactions) vutils/star]
[make-button #(i/on-toogle-help interactions) "?"]
(show-scores scores #(= % current-player))
[make-button #(i/on-restart interactions) vutils/circle-arrow]
(defn- make-button
[on-click txt]
[:button.help-button {:on-click on-click} txt])
(defn- player->css-style
[player highlight?]
(let [player-class (str "score--" (name player))
score-class (if (highlight? player) "score--is-current" "score")]
(str player-class " " score-class)))
(defn- player->score-text
[player score]
(str (str/capitalize (name player)) " - " score))
(defn render-board
[board suggestions interactions]
(into
(empty-svg-board)
(for [[position cell] (board/to-seq board)]
^{:key position}
(if (= :none cell)
[empty-cell position interactions (if (suggestions position) :help :none)]
[rect-cell position cell]))))
(def relative-size 0.9)
(def border-size (/ (- 1 relative-size) 2))
(defn- rect-cell
[[x y] player options]
[:rect.cell
(merge
{:class (str "cell--" (name player))
:x (+ border-size x) :width relative-size
:y (+ border-size y) :height relative-size}