Skip to content

Instantly share code, notes, and snippets.

@mistercam
Created April 29, 2012 16:29
Show Gist options
  • Save mistercam/2551707 to your computer and use it in GitHub Desktop.
Save mistercam/2551707 to your computer and use it in GitHub Desktop.
Function that asks for and accepts a Reversi move from the user.
(defn prompt-for-move [piece board]
"Prompts the user for a move. Returns the move, if-valid. Returns nil
if user has quit."
(loop [input (atom "")]
(if (= :w piece) ; Prints an input prompt
(print "White's Turn. Enter move: ")
(print "Black's Turn. Enter move: "))
(flush) ; We printed the prompt with a 'print' instead of 'println'. Flushing forces the prompt to appear.
(reset! input (str (read-line))) ; Read input and store back into 'input'
(cond
(= "quit" (string/lower-case @input)) nil
(malformed-input? @input) (do
(println "Malformed input! Try again.")
(recur input))
(not (move-valid? board piece (parse-input @input))) (do
(println "Invalid move! Try again.")
(recur input))
:else (parse-input @input)))) ; Input is OK, so return a move
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment