Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
41
Task/Pig-the-dice-game/Clojure/pig-the-dice-game.clj
Normal file
41
Task/Pig-the-dice-game/Clojure/pig-the-dice-game.clj
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
(def max 100)
|
||||
|
||||
(defn roll-dice []
|
||||
(let [roll (inc (rand-int 6))]
|
||||
(println "Rolled:" roll) roll))
|
||||
|
||||
(defn switch [player]
|
||||
(if (= player :player1) :player2 :player1))
|
||||
|
||||
(defn find-winner [game]
|
||||
(cond
|
||||
(>= (:player1 game) max) :player1
|
||||
(>= (:player2 game) max) :player2
|
||||
:else nil))
|
||||
|
||||
(defn bust []
|
||||
(println "Busted!") 0)
|
||||
|
||||
(defn hold [points]
|
||||
(println "Sticking with" points) points)
|
||||
|
||||
(defn play-round [game player temp-points]
|
||||
(println (format "%s: (%s, %s). Want to Roll? (y/n) " (name player) (player game) temp-points))
|
||||
(let [input (clojure.string/upper-case (read-line))]
|
||||
(if (.equals input "Y")
|
||||
(let [roll (roll-dice)]
|
||||
(if (= 1 roll)
|
||||
(bust)
|
||||
(play-round game player (+ roll temp-points))))
|
||||
(hold temp-points))))
|
||||
|
||||
(defn play-game [game player]
|
||||
(let [winner (find-winner game)]
|
||||
(if (nil? winner)
|
||||
(let [points (play-round game player 0)]
|
||||
(recur (assoc game player (+ points (player game))) (switch player)))
|
||||
(println (name winner) "wins!"))))
|
||||
|
||||
(defn -main [& args]
|
||||
(println "Pig the Dice Game.")
|
||||
(play-game {:player1 0, :player2 0} :player1))
|
||||
Loading…
Add table
Add a link
Reference in a new issue