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


 
  [ [ $ "lower higher equal" nest$ ]
    constant ]                       is responses (   --> $ )
 
  [  trim reverse 
     trim reverse
     $ "" swap witheach
       [ lower join ] ]              is cleanup   ( $ --> $ )
 
  [ $ "Think of a number from 1 to"
    $ " 100 and press enter." join
    input drop 
    0 temp put
    [] 100 times [ i 1+ join ]
    [ dup size 0 = iff
        [ say "Impossible!" cr 0 ] 
        done
      dup size 2 / split behead
      dup temp replace
      say "I guess " echo say "." cr
      $ "lower, higher or equal? "
      input cleanup responses find
      [ table
        [ nip false ] 
        [ drop false ] 
        [ say "I guessed it!" 
          cr true ] 
        [ say "I do not understand."
          temp share swap join join
          cr false ] ] 
      do until ] 
  2drop temp release ]               is play      (   -->   )