Created
April 29, 2012 16:43
-
-
Save mistercam/2551799 to your computer and use it in GitHub Desktop.
Function to start a text-based Reversi game
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 print-winner [board] | |
"Prints game over to standard out and declares the winner" | |
(let [p (winner board)] ; 'p' is the piece with the most coverage | |
(cond | |
(= p :b ) (println "Game over. Black Wins!") | |
(= p :w ) (println "Game over. White Wins!") | |
:else (println "Game over. Tie!")))) | |
(defn reversi-text [] | |
"Starts a text-based Reversi game." | |
(let [board (atom (initialize-board)) ; the active board (mutable) | |
piece (atom :b ) ; the active player (mutable) | |
game-over (atom false) ; game state (mutable) | |
execute-move (fn [move] (if move | |
(do | |
(swap! board (fn [b] (place-piece b @piece move))) ; make the move and update board | |
(cond | |
(game-over? @board) (do | |
(print-board @board) | |
(print-winner @board) | |
(reset! game-over true)) | |
(empty? (take 1 (valid-moves @board (opposite-piece @piece)))) (println "Still your turn!") | |
:else (swap! piece opposite-piece))) | |
(reset! game-over true)))] | |
(while (not @game-over) | |
(do | |
(print-board @board) | |
(execute-move (prompt-for-move @piece @board)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment