This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)) |
NewerOlder