Skip to content

Instantly share code, notes, and snippets.

@mistercam
Created April 29, 2012 20:18
Show Gist options
  • Save mistercam/2553041 to your computer and use it in GitHub Desktop.
Save mistercam/2553041 to your computer and use it in GitHub Desktop.
Quil functions to draw the Reversi board
(defn draw-message [msg]
"Displays the specified message in the middle of the sketch."
(if (not= 0 (@msg 0)) ; only draw the message if the count != 0
(let [mx (quot (- board-size message-width) 2)
my (quot (- board-size message-height) 2)]
(do
(apply fill color-grey)
(rect mx my message-width message-height) ; make a black box centered on the board
(apply fill color-black) ; make the text yellow
(text-size 60)
(text-align :center :center ) ; center text horizontally and vertically within box
(text (@msg 1) mx my message-width message-height) ; write text in box
(swap! msg #(assoc % 0 (dec (% 0))))))))
(defn draw-player [piece]
"Draws a message below the board indicating the current player."
(let [p (if (= :w piece) "WHITE" "BLACK")]
(apply fill color-white) ; make the text white
(text-size 14)
(text-align :left :baseline)
(text (str "CURRENT PLAYER: " p) 10 (+ board-size (quot footer-height 2)))))
(defn draw-piece [r c piece]
"Draws a white or black piece in the square of the specified row
and column."
(if (not= nil piece)
(let [color (if (= :w piece) color-white color-black)]
(apply fill color) ; sets the current fill-color to white or black
(ellipse-mode :corner ) ; the (x,y)-coord in our call to ellipse below specifies the top left corner of bounding box
(ellipse
(+ (* c piece-size) (* c gap-size) padding) ; x
(+ (* r piece-size) (* r gap-size) padding) ; y
(- piece-size (* 2 padding)) ; width
(- piece-size (* 2 padding)))))) ; height
(defn draw-square [board r c]
"Draws the square for the specified row and column."
(apply fill color-green) ; sets the current fill-color to green
(rect
(+ (* c piece-size) (* c gap-size)) ; x
(+ (* r piece-size) (* r gap-size)) ; y
piece-size ; width
piece-size) ; height
(draw-piece r c ((board r) c)))
(defn draw-board [board piece msg]
"Draws the Reversi game board and pieces, or a warning message if one exists."
(apply background color-black)
(doseq [r (range 8) c (range 8)]
(draw-square @board r c))
(draw-player @piece)
(draw-message msg))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment