March 2014 update
This commit is contained in:
parent
09687c4926
commit
a25938f123
1846 changed files with 21876 additions and 5203 deletions
|
|
@ -1,45 +1,27 @@
|
|||
(ns rosettacode.24game)
|
||||
|
||||
(defn gen-new-game-nums [amount] (repeatedly amount #(inc ( rand-int 9))))
|
||||
(def ^:dynamic *luser*
|
||||
"You guessed wrong, or your input was not in prefix notation.")
|
||||
|
||||
(defn orderless-seq-eq? [seq1 seq2] (apply = (map frequencies (list seq1 seq2))))
|
||||
(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 valid-input?
|
||||
"checks whether the expression is somewhat valid prefix notation
|
||||
(+ 1 2 3 4) (+ 3 (+ 4 5) 6)
|
||||
this is done by making sure the only contents of the list are numbers operators and brackets
|
||||
flatten gets rid of the brackets, so we just need to test for operators and integers after that"
|
||||
[user-input]
|
||||
(if (re-find #"^\(([\d-+/*] )+\d?\)$" (pr-str (flatten user-input)))
|
||||
true
|
||||
false))
|
||||
(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)))))
|
||||
|
||||
(defn game-numbers-and-user-input-same?
|
||||
"input form: (+ 1 2 (+ 3 4))
|
||||
tests to see if the numbers the user entered are the same as the ones given to them by the game"
|
||||
[game-nums user-input]
|
||||
(orderless-seq-eq? game-nums (filter integer? (flatten user-input))))
|
||||
|
||||
(defn win [] (println "you won the game!\n"))
|
||||
(defn lose [] (println "you guessed wrong, or your input was not in prefix notation. eg: '(+ 1 2 3 4)'\n"))
|
||||
(defn game-start [goal game-numbers] (do
|
||||
(println "Your numbers are " game-numbers)
|
||||
(println "Your goal is " goal)
|
||||
(println "Use the numbers and +*-/ to reach your goal\n")
|
||||
(println "'q' to Quit\n")))
|
||||
|
||||
(defn play-game
|
||||
"typing in 'q' quits.
|
||||
to play use (play-game) (play-game 24) or (play-game 24 '(1 2 3 4)"
|
||||
([] (play-game 24))
|
||||
([goal] (play-game goal (gen-new-game-nums 4)))
|
||||
([goal game-numbers]
|
||||
(game-start goal game-numbers)
|
||||
(let [input (read-line)
|
||||
input-as-code (read-string input)]
|
||||
(if (and (valid-input? input-as-code)
|
||||
(game-numbers-and-user-input-same? game-numbers input-as-code)
|
||||
(try (= goal (eval input-as-code)) (catch Exception e (do (lose) (play-game goal game-numbers)))))
|
||||
(win)
|
||||
(when (not (= input "q"))
|
||||
(do (lose) (recur goal game-numbers)))))))
|
||||
; * 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