Carp/examples/guessing_game.carp
Erik Svedäng a873099640
chore: Move some examples to test/produces-output (#989)
* chore: Moved examples that work more as tests to folder 'test/produces-output'

* fix: Corrections to the release script

* fix: Correct filename on Windows

* fix: Move more files around

* fix: Remove check-malloc example

* fix: Apparently unicode example does not work

* chore: Move nested_lambdas.carp back to examples

* chore: Remove .DS_Store files

* fix: Bring back unicode test

* test: Make sure benchmark compile (had to remove mandelbrot and n-bodies)

* fix: Replacement implementation of clock_gettime on Windows

* chore: Trigger CI

* fix: Define CLOCK_REALTIME

Co-authored-by: Erik Svedang <erik@Eriks-iMac.local>
2020-11-23 06:30:43 +01:00

55 lines
1.4 KiB
Plaintext

;; The number guessing game
(use IO)
(use Int)
(use String)
(Project.config "title" "Guessing")
(def guessing true)
(def answer 0)
(defn start-new-game! []
(do (println "~ The Number Guessing Game ~")
(println "(Enter q to quit.)\n")
(set! guessing true)
(set! answer (random-between 1 100))
(print "Please enter a number between 1 - 99: ")))
(defn exit! []
(do (println "Good bye...")
(set! guessing false)))
(defn play-again? [user-input]
(if (= user-input "y\n") true false))
(defn correct! []
(do (println "Correct!\n")
(print "Play again? (y/n): ")
(let [user-input (get-line)]
(if (play-again? &user-input)
(start-new-game!)
(exit!)))))
(defn guess-again [low-or-high]
(do (println &(str* @"Too " @low-or-high @"."))
(print "\nPlease guess again: ")))
(defn main []
(do (println "Seeding random number generator...\n")
(Random.seed-from (Double.from-int (System.time)))
(start-new-game!)
(while guessing
(let [user-input (get-line)
guessed-num (from-string &user-input)]
(if (= &user-input "q\n")
(exit!)
(match guessed-num
(Maybe.Nothing) (print "Invalid input.\nPlease guess again: ")
(Maybe.Just n)
(cond (< n answer) (guess-again "low")
(> n answer) (guess-again "high")
(correct!))))))))