Skip to content

Instantly share code, notes, and snippets.

@mistercam
Created April 29, 2012 17:07
Show Gist options
  • Save mistercam/2551972 to your computer and use it in GitHub Desktop.
Save mistercam/2551972 to your computer and use it in GitHub Desktop.
Function to draw Reversi components in Swing
(defn draw-piece [g 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)]
(.setColor g color)
(.fillOval g
(+ (* 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 [g board r c]
"Draws the square for the specified row and column."
(.setColor g color-green)
(.fillRect g
(+ (* c piece-size) (* c gap-size)) ; x
(+ (* r piece-size) (* r gap-size)) ; y
piece-size ; width
piece-size) ; height
(draw-piece g r c ((board r) c)))
(defn draw-player [g piece]
"Draws a message below the board indicating the current player."
(let [p (if (= :w piece) "WHITE" "BLACK")]
(.setColor g color-white)
(.drawString g (str "CURRENT PLAYER: " p) 10 (+ board-size (quot footer-height 2)))))
(defn draw-board [g board piece]
"Draws the Reversi game board."
(.setColor g color-black)
(.fillRect g
0 ; x
0 ; y
board-size ; width
(+ board-size footer-height)) ; height
(doseq [r (range 8) c (range 8)]
(draw-square g board r c))
(draw-player g piece))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment