Guess The Number

January 17, 2012

This is mostly an exercise in getting the prompts and responses in the right order. The only interesting bit is the binary search when the computer tries to guess the child’s number. We’ll start with that:

(define (computer-plays limit)
  (show-line "Think of a number from 1 to " limit ".")
  (let loop ((lo 1) (hi limit) (k 0))
    (let ((mid (quotient (+ lo hi) 2)))
      (cond ((= lo mid)
              (show-line "I guessed your number "
                         lo " in " k " tries.")
              k)
      (else (show "Is your number less than " mid "? ")
            (case (get-yes-no)
            ((#\y) (loop lo mid (+ k 1)))
            ((#\n) (loop mid hi (+ k 1)))))))))

When the child is guessing, the computer needs to prompt for a guess and then reply:

(define (human-plays limit)
  (let ((n (randint 1 limit)))
    (show-line "I am thinking of a number from 1 to " limit)
    (display "What is your guess? ")
    (let loop ((g (read)) (k 0))
      (cond ((< g n)
              (show-line "Your guess is less than my number.")
              (show "What is your next guess? ")
              (loop (read) (+ k 1)))
            ((< n g)
              (show-line "My number is less than your guess.")
              (show "What is your next guess? ")
              (loop (read) (+ k 1)))
            (else
              (show-line "You guessed my number "
                         n " in " k " tries!")
              k)))))

The play function scores the game; to start a game, say something like (play 100):

(define (play limit)
  (show-line "Let's play Guess-The-Number!")
  (show-line "You go first.")
  (let ((k-human (human-plays limit)))
    (show-line "Now it's my turn.")
    (let ((k-computer (computer-plays limit)))
      (cond ((< k-human k-computer)
              (show-line "You made less guesses than me.")
              (show-line "You win! Congratulations!"))
            ((< k-computer k-human)
              (show-line "I made less guesses than you.")
              (show-line "I win. Better luck next time!"))
            (else
              (show-line "We both made the same number of guesses.")
              (show-line "The game is a tie. Good game!")))
      (show "Would you like to play again? ")
      (if (char=? (get-yes-no) #\y)
          (play limit)
          (show-line "Thanks for playing! Good-bye!")))))

We used randint from the Standard Prelude, as well as the three utility functions shown below:

(define (show . msg) (for-each display msg))
(define (show-line . msg) (apply show msg) (newline))

(define (get-yes-no)
  (let ((answer (char-downcase (string-ref
                  (symbol-&gt;string (read)) 0))))
    (if (member answer '(#\y #\n)) answer
      (begin (show-line "Please answer yes or no.")
             (get-yes-no)))))

You can run the program at http://programmingpraxis.codepad.org/joeEXDmD.

About these ads

Pages: 1 2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 621 other followers

%d bloggers like this: