Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created March 9, 2013 21:15
Show Gist options
  • Select an option

  • Save practicingruby/5125783 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/5125783 to your computer and use it in GitHub Desktop.
(ns weiqi.core
(:require [seesaw.mouse :as mouse]
[clojure.math.numeric-tower :as math])
(:use seesaw.core
seesaw.graphics
seesaw.color))
(def scale-factor 30)
(def board-size 19)
(def border-width 1)
(def stone-styles
{ :black (style :background (color :black))
:white (style :background (color :white)
:foreground (color :black)
:stroke 2) })
(def players (agent '(:black :white)))
(defn stone [[x y]]
(circle (* scale-factor x) (* scale-factor y) (/ scale-factor 2)))
(def board-state (agent #{}))
(defn paint [c g]
(doseq [x (range 1 board-size) y (range 1 board-size)]
(draw g
(rect (* x scale-factor) (* y scale-factor) scale-factor)
(style :background ( color :white )
:foreground ( color :black )
:stroke 2 )))
(doseq [stone-data @board-state]
(draw g (stone (stone-data :pos)) (stone-styles (stone-data :color)))))
(def board
(canvas :id :canvas :background "#BBBBDD" :paint paint))
(defn clicked-intersection [e]
(let [[x y] (mouse/location e)]
(send board-state conj
{ :pos [(math/round (/ x scale-factor)) (math/round (/ y scale-factor))]
:color (first @players)})
(repaint! board)
(send players reverse)))
(defn -main [& args]
(invoke-later
(-> (frame :title "Weiqi"
:width (+ (* scale-factor (+ board-size 1)) (* 2 border-width))
:height (+ (* scale-factor (+ board-size 2)) (* 2 border-width))
:content board
:on-close :exit)
show!)
(listen board :mouse-pressed clicked-intersection)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment