Login
7 branches 0 tags
Ben (X13/Arch) Simplified things a little 0643405 9 days ago 1260 Commits
nujel / stdlib_modules / games / guess.nuj
(require :ansi)
(import (rng) :random)

(defn win (guesses-left)
      (pfmtln "Fantastic, {} with {guesses-left} guesses left." (ansi/rainbow "you won"))
      (pfmtln "Hope you had fun and have a nice day!")
      (exit 0))

(defn loose ()
      (pfmtln "Too bad, you didn't guess the number in time, maybe next time.")
      (pfmtln "Hope you still had fun, have a nice day!")
      (exit 0))

(defn quit ()
      (pfmtln "Bye!")
      (exit 0))

(defn main (args)
      :export
      (println (ansi/rainbow "Guess the number!!!"))
      (println "")
      (println "You have 10 tries, with every guess I will tell you if it was too high or too low, the number is in the range of 0 to 100")
      (def rand (:new rng))
      (def guesses-left 10)
      (def number (:int rand 100))
      (while (> guesses-left 0)
        (def line (readline (fmt "Your {} guess? " (cond ((= guesses-left 1) (ansi/red "final"))
                                                         (#t (- 11 guesses-left))))))
        (when-not line (quit))
        (def cur-guess (read/int line))
        (when (= number cur-guess)
          (win guesses-left))
        (if (< cur-guess number)
            (println "Too low")
            (println "Too high"))
        (set! guesses-left (- guesses-left 1)))
      (loose))