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


BEGIN
    INT    lower    :=    1;
    INT    upper    :=  100;
    print( ( "Think of a number between ", whole( lower, 0 ), " and ", whole( upper, 0 ), newline ) );
    print( ( "Please enter Y if I guess correctly, L is it is lower, G if it is greater or Q if you've had enough", newline ) );
    WHILE
        INT mid = lower + ( ( upper - lower ) OVER 2 );
        CHAR reply;
        WHILE
            print( ( "Is it ", whole( mid, 0 ), "?    Y/L/G/Q: " ) );
            read( ( reply, newline ) );
            NOT char in string( reply, NIL, "YLGQylgq" )
        DO SKIP OD;
        IF   reply = "Q" OR reply = "q" OR reply = "Y" OR reply = "y"
        THEN FALSE
        ELIF lower >= upper THEN
            print( ( "Based on your answers so far, it must be ", whole( lower, 0 ), newline ) );
            FALSE
        ELSE
            IF   reply = "L" OR reply = "l" THEN upper := mid - 1
            ELIF reply = "G" OR reply = "g" THEN lower := mid + 1
            FI;
            TRUE
        FI
    DO SKIP OD
END