Skip to content

Instantly share code, notes, and snippets.

View mistercam's full-sized avatar

Cameron Donaldson mistercam

  • Toronto, ON, Canada
View GitHub Profile
@mistercam
mistercam / reversi-quil-message.clj
Created April 29, 2012 21:40
Function to set a message that should be drawn
(defn set-message [m message]
"Specifies a new message to be drawn to the screen, and the length of time it
remain."
(let [seconds (* 3 fps)]
(reset! m [seconds message])))
@mistercam
mistercam / reversi-quil-mouse.clj
Created April 29, 2012 20:25
Mouse handler functions for the Quil Reversi game
(defn announce-winner [msg board]
"Sets the winner of the Reversi game"
(let [p (winner board)]
(cond
(= :w p) (set-message msg (str "White Wins!"))
(= :b p) (set-message msg (str "Black Wins!"))
:else
(set-message msg "It's a Tie!"))))
(defn parse-mouse-pos [x y]
@mistercam
mistercam / reversi-quil-draw.clj
Created April 29, 2012 20:18
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)
@mistercam
mistercam / reversi-quil.clj
Created April 29, 2012 18:53
A Reversi sketch
(defn setup []
"Configures the properties of our sketch."
(smooth) ; turns on anti-aliasing
(frame-rate fps) ; the number of times per second we'll redraw the board
(apply background color-black)) ; sets the background
(defn reversi-quil []
"Starts a quil-based Reversi game."
(let [board (atom (initialize-board))
piece (atom :b )
@mistercam
mistercam / reversi-quil-defs.clj
Created April 29, 2012 18:50
Some required vars for our Quil Reversi sketch
(def footer-height 30) ; space below the board to show the current player
(def piece-size 70) ; the width and height of a board square
(def gap-size 5) ; the vertical and horizontal spacing between squares
(def board-size (+ (* 8 piece-size) (* 7 gap-size))) ; the board size
(def padding 5) ; the spacing within a square that surrounds a piece
(def message-width (- board-size 80)) ; the width of the message box
(def message-height 80) ; the height of the message box
(def fps 10) ; the number of times per second are sketch will be redrawn
(def color-black [0 0 0])
(def color-grey [200 200 200])
@mistercam
mistercam / reversi-swing.clj
Created April 29, 2012 18:07
Function that starts a Swing-based Reversi game
(defn reversi-swing []
"Starts a Swing-based Reversi game."
(let [board (atom (initialize-board)) ; the active board (mutable)
piece (atom :b) ; the active piece (mutable)
frame (JFrame. "Reversi") ; the outer Swing container. "Reversi" appears in the title bar.
panel (reversi-panel frame board piece)] ; the Reversi board
(doto panel
(.setFocusable true) ; focus can be given to this panel
(.addMouseListener panel)) ; have the panel listen for mouse events
(doto frame
@mistercam
mistercam / reversi-swing-panel.clj
Created April 29, 2012 17:57
Swing JPanel for our Reversi Game
(defn popup [frame msg]
"Displays a popup containing the specified message."
(JOptionPane/showMessageDialog frame msg))
(defn announce-winner [frame board]
"Displays a popup containing the winner of the game."
(let [p (winner board)]
(cond
(= p :w) (popup frame "White Wins!")
(= p :b) (popup frame "Black Wins!")
@mistercam
mistercam / reversi-swing-mouse.clj
Created April 29, 2012 17:18
Function maps a mouse position to a row and coordinate on the Reversi board
(defn parse-mouse-pos [x y]
"Returns the row and column corresponding to the mouse position."
[(quot y (+ piece-size gap-size))
(quot x (+ piece-size gap-size))])
@mistercam
mistercam / reversi-swing-draw.clj
Created April 29, 2012 17:07
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
@mistercam
mistercam / reversi-swing-defs.clj
Created April 29, 2012 16:52
Defs required to customize the look of a Swing-based Reversi game
(def footer-height 30) ; space below the board to show the current player
(def piece-size 70) ; the width and height of an individual board square
(def gap-size 5) ; the vertical and horizontal spacing between squares
(def padding 5) ; the spacing within a square that surrounds a piece
(def board-size (+ (* 8 piece-size) (* 7 gap-size))) ; the board size
(def color-white (Color. 255 255 255))
(def color-black (Color. 0 0 0))
(def color-green (Color. 0 136 0))