Created
February 7, 2015 16:57
-
-
Save taylorlapeyre/4663672782884a1c8fa5 to your computer and use it in GitHub Desktop.
Rock Paper Sissors
This file contains hidden or 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? | |
[machine-choice player-choice] | |
(= machine-choice player-choice)) | |
(defn player-win? | |
[machine-choice player-choice] | |
(case machine-choice | |
"rock" (= player-choice "paper") | |
"paper" (= player-choice "sissors") | |
"sissors" (= player-choice "rock"))) | |
(defn play-game | |
[rounds] | |
(loop [round 1 | |
machine-points 0 | |
player-points 0] | |
(if (> round rounds) ; Game Over | |
(do (println "Machine score:" machine-points) | |
(println "Player score:" player-points)) | |
(do (println "Round" round ": choose either rock, paper, or sissors.") | |
(let [player-choice (read-line) | |
machine-choice (rand-nth ["rock" "paper" "sissors"])] | |
(cond | |
(draw? machine-choice player-choice) | |
(do (println "It was a draw! Try again.") | |
(recur round machine-points player-points)) | |
(player-win? machine-choice player-choice) | |
(do (println "Machine chose" machine-choice ", you won!") | |
(recur (inc round) machine-points (inc player-points))) | |
:else | |
(do (println "Machine chose" machine-choice ", machine won!") | |
(recur (inc round) (inc machine-points) player-points)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment