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


MODULE raden;
 
IMPORT  InOut;
 
VAR     done, ok                : BOOLEAN;
        guess, upp, low         : CARDINAL;
        res                     : CHAR;
 
BEGIN
  InOut.WriteString ("Choose a number between 0 and 1000.");
  InOut.WriteLn;
  InOut.WriteLn;
  upp := 1000;
  low := 0;
  REPEAT
    ok := FALSE;
    guess := ( ( upp - low ) DIV 2 ) + low;
    InOut.WriteString ("My guess is");  InOut.WriteCard (guess, 4);     InOut.Write (11C);
    InOut.WriteString ("How did I score? 'L' = too low, 'H' = too high, 'Q' = OK : ");
    InOut.WriteBf;
    REPEAT
      InOut.Read (res);
      res := CAP (res)
    UNTIL  (res = 'Q') OR (res = 'L') OR (res = 'H');
    CASE  res  OF
      'L'       : low := guess  |
      'H'       : upp := guess
    ELSE
      ok := TRUE
    END;
  UNTIL ok;
  InOut.WriteString ("So the number is");       InOut.WriteCard (guess, 4);
  InOut.WriteLn;
  InOut.WriteString ("Thanks for letting me play with you.");
  InOut.WriteLn
END raden.