A number guessing game

"Paul Dufresne" <[email protected]> Sun, 16 Oct 2005 21:48:22 -0400
Newsgroups gmane.comp.lang.goo.general
Message-ID <[email protected]>
; Copyright Paul Dufresne released under LGPL
; Comments are welcome at [email protected]

; There seems to be a small problem when you want to quit the game
; Maybe I don't use the exception correctly?

(use goo/random)

(dv $min-number 1)
(dv $max-number 20)

(dm empty-input () ; necessary because of the "\n", that stay there
  (if (ready? in) (gets in)))

(dm intro ( min|<int> max|<int> )
  (msg out "Hi! Do you possess the ability to read minds?\n")
  (msg out "I am thinking of a number between %d and %d\n" min max)
  (msg out "Guess which one it is.\n")
  (msg out "Dont worry, I'll give you some clues.\n"))

(dc <guess-my-number> (<any>))
(dp secret (<guess-my-number> => <int>) (random-number))
(dp! number-trys (<guess-my-number> => <int>) 0)

(df random-number ( => <int> )
  (+ (random(- $max-number $min-number)) $min-number))

(dm try-num (game|<guess-my-number> some-num|<int> => <log>)
  (incf (number-trys game))
  (cond
    ((> some-num (secret game)) (seq (too-high game some-num) #t))
    ((< some-num (secret game)) (seq (too-low game some-num) #t))
    (#t (got-it game))))

(dm too-high (game|<guess-my-number> guess|<int>)
  (msg out "I was thinking to a number lower than %d!\n" guess))

(dm too-low (game|<guess-my-number> guess|<int>)
  (msg out "I was thinking to a number higher than %d!\n" guess))

(dc <end-of-game> (<simple-error>))

(dm got-it (game|<guess-my-number> => <log>)
  (msg out "Yes! You got it in %d trys!\n" (number-trys game))
  (sig (new <end-of-game>)))

(df want-to-replay? ( => <log> )
  (msg out "Do you want to replay? (yes/no) (default no)")
  (set again (case-by (gets in) =
    ( ("yes") #t)
    ( ("y")   #t)
    ( ("oui") #t)))
  (msg out "\n")
  again)

(df end-of-game (<end-of-game> resume-funct)
  (if (want-to-replay?) 
    (play-game (new <guess-my-number>))
    (msg out "it has been my pleasure to play with you.\n")))

(df ask-number ( => <int>)
  (rep again ()
  (msg out "Quel nombre: ")
  (if (= (peek in) #\newline) (get in))
  (set number (str-to-num (gets in)))
  (msg out "\n" number)
  (if (= #f number) (again) number)))

(dm play-game (game|<guess-my-number>)
  (intro $min-number $max-number)
  (try <end-of-game> end-of-game 
    (rep again ()
      (set a-guess (ask-number))
      (try-num game a-guess)
      (again))))

(dm play ()
  (empty-input)
  (play-game (new <guess-my-number>)))

(export play) ; added just before posting, it there is a bug, this is
here :-)

(play)

; It was quite harder to do than what I was expecting, learning when and
where to use
; parenthesis is not so easy!

-- 
http://www.fastmail.fm - Faster than the air-speed velocity of an
                          unladen european swallow


_______________________________________________
Googoogaga mailing list
[email protected]
https://lists.csail.mit.edu/mailman/listinfo/googoogaga