Skip to content

Instantly share code, notes, and snippets.

@mistercam
Created April 29, 2012 20:25
Show Gist options
  • Save mistercam/2553075 to your computer and use it in GitHub Desktop.
Save mistercam/2553075 to your computer and use it in GitHub Desktop.
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]
"Returns the row and column corresponding to the mouse position."
[(quot y (+ piece-size gap-size))
(quot x (+ piece-size gap-size))])
(defn mouse-clicked [board piece game-over msg]
"Attempts to play the specified piece in the square the user clicked."
(if (= 0 (@msg 0)) ; handle mouse click only if we're not showing a message
(let [move (parse-mouse-pos (mouse-x) (mouse-y))]
(if (move-valid? @board @piece move)
(do
(swap! board #(place-piece % @piece move))
(cond
(game-over? @board) (do
(announce-winner msg @board)
(reset! game-over true))
(empty? (take 1 (valid-moves @board (opposite-piece @piece)))) (set-message msg "Still your turn!")
:else (swap! piece opposite-piece)))
(set-message msg "Invalid move!")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment