Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
27
Task/24-game/Clojure/24-game.clj
Normal file
27
Task/24-game/Clojure/24-game.clj
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
(ns rosettacode.24game)
|
||||
|
||||
(def ^:dynamic *luser*
|
||||
"You guessed wrong, or your input was not in prefix notation.")
|
||||
|
||||
(def ^:private start #(println
|
||||
"Your numbers are: " %1 ". Your goal is " %2 ".\n"
|
||||
"Use the ops [+ - * /] in prefix notation to reach" %2 ".\n"
|
||||
"q[enter] to quit."))
|
||||
|
||||
(defn play
|
||||
([] (play 24))
|
||||
([goal] (play goal (repeatedly 4 #(inc (rand-int 9)))))
|
||||
([goal gns]
|
||||
(start gns goal)
|
||||
(let [input (read-string (read-line))
|
||||
flat (flatten input)]
|
||||
(println
|
||||
(if (and (re-find #"^\([\d\s+*/-]+\d?\)$" (pr-str flat))
|
||||
(= (set gns) (set (filter integer? flat)))
|
||||
(= goal (eval input)))
|
||||
"You won the game!"
|
||||
*luser*))
|
||||
(when (not= input 'q) (recur goal gns)))))
|
||||
|
||||
; * checks prefix form, then checks to see that the numbers used
|
||||
; and the numbers generated by the game are the same.
|
||||
Loading…
Add table
Add a link
Reference in a new issue