Skip to content

Instantly share code, notes, and snippets.

@johnwalker
Last active January 1, 2016 15:28
Show Gist options
  • Save johnwalker/8163939 to your computer and use it in GitHub Desktop.
Save johnwalker/8163939 to your computer and use it in GitHub Desktop.
(ns peg-game.core
(:require
[cljs.core.async :as async
:refer [<! >! chan close! sliding-buffer put! alts! timeout]]
[jayq.core :refer [$ append ajax inner css $deferred
when done resolve pipe on bind attr
offset] :as jq]
[goog.dom :as dom]
[goog.events :as events]
[monet.canvas :as monet])
(:require-macros [cljs.core.async.macros :as m :refer [go]]))
(defn log [& items]
(.log js/console (apply pr-str items)))
(defn generate-triangular-slots [length]
(into #{}
(for [y (range length)
x (range (- length y)) ]
[x y])))
(defn mouse-chan []
(let [c (chan)]
(events/listen js/window "click" #(put! c %))
c))
(defn draw! [ctx slots color radius lt1 lt2]
(doseq [[x y id] slots]
(monet/fill-style ctx color)
(monet/circle ctx {:r radius
:x (lt1 x)
:y (lt2 y)})))
(defn listen-chan [el type]
(let [out (chan)]
(events/listen el type (fn [e] (log e) (put! out e)))
out))
(defn ^:export init []
(let [canvas (.getElementById js/document "peg")
ctx (.getContext canvas "2d")
width (.-width canvas)
height (.-height canvas)]
(.scale ctx 1 -1)
(.translate ctx 0 (- height))
(log "width" width "height" height)
(let [mouse (mouse-chan)]
(go
(loop [pegs (generate-triangular-slots 5)
holes #{}]
(draw! ctx pegs "orange" 9
#(+ (* 20 %) 20)
#(+ (* 20 %) 20))
(draw! ctx holes "black" 9
#(+ (* 20 %) 20)
#(+ (* 20 %) 20))
(<! mouse)
(let [choice (first pegs)]
(recur (disj pegs choice)
(conj holes choice))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment