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


/*REXX program plays  guess─the─number  (with itself)  with  positive integers.         */
parse arg low high seed .                        /*obtain optional arguments from the CL*/
if  low=='' |  low=="," then  low=    1          /*Not specified?  Then use the default.*/
if high=='' | high=="," then high= 1000          /* "      "         "   "   "     "    */
if datatype(seed, 'W')  then call random ,,seed  /*Useful seed?  Then use a random seed.*/
?= random(low, high)                             /*generate random number from low->high*/
$= "──────── Try to guess my number  (it's between  "        /*part of a prompt message.*/
g=                                                           /*nullify the first guess. */
    do #=1;                        oldg= g       /*save the guess for later comparison. */
    if pos('high', info)\==0  then high= g       /*test if the guess is too  high.      */
    if pos('low' , info)\==0  then low = g       /*  "   "  "    "    "  "   low.       */
    say                                          /*display a blank line before prompt.  */
    say $ low  '  and  '   high  "  inclusive):" /*issue the prompt message to terminal.*/
    say                                          /*display a blank line  after prompt.  */
    g= (low +  (high - low) / 2)   / 1           /*calculate the next guess & normalize.*/
    if g=oldg   then g= g + 1                    /*bump guess by one 'cause we're close.*/
    say 'My guess is'       g                    /*display computer's guess to the term.*/
    if g=?  then leave                           /*this guess is correct; leave & inform*/
    if g>?  then info= right(' Your guess is too high.', 60, "─")
            else info= right(' Your guess is too low.' , 60, "─")
    say info
    end   /*try*/
say                                              /*stick a fork in it,  we're all done. */
say 'Congratulations!   You guessed the secret number in'    #    "tries."