Task

Write a player for the game that follows the following rules:

The scorer will choose a number between set limits. The computer player will print a guess of the target number. The computer asks for a score of whether its guess is higher than, lower than, or equal to the target. The computer guesses, and the scorer scores, in turn, until the computer correctly guesses the target number.
The computer should guess intelligently based on the accumulated scores given. One way is to use a Binary search based algorithm.



Related tasks

   Guess the number/With Feedback
   Bulls and cows/Player


 
(defun guess-the-number (&optional (max 1000) (min 0))
  (flet ((get-feedback (guess)
           (loop
              initially (format t "I choose ~a.~%" guess)
              for answer = (read)
              if (member answer '(greater lower correct))
              return answer
              else do (write-line "Answer greater, lower, or correct."))))
    (loop
       initially (format t "Think of a number between ~a and ~a.~%" min max)
       for guess  = (floor (+ min max) 2)
       for answer = (get-feedback guess)
       until (eq answer 'correct)
       if (eq answer 'greater) do (setf min guess)
       else do (setf max guess)
       finally (write-line "I got it!"))))
 