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


$ include "seed7_05.s7i";
$ include "keybd.s7i";
 
const proc: main is func
  local
    var boolean: okay is FALSE;
    var boolean: quit is FALSE;
    var integer: low is 1;
    var integer: high is 1000;
    var integer: guess is 0;
    var char: command is ' ';
  begin
    writeln("Choose a number between 1 and 1000.");
    write("Press Enter and I will start to guess the number.");
    readln;
    repeat
      guess := low + (high - low) div 2;
      write("My guess is: " <& guess);
      write(". How did I score (l=too low, h=too high, c=correct, q=quit)? ");
      flush(OUT);
      repeat
        command := lower(getc(KEYBOARD));
      until command in {'l', 'h', 'c', 'q'};
      writeln(command);
      case command of
        when {'l'}: low := succ(guess);
        when {'h'}: high := pred(guess);
        when {'c'}: okay := TRUE;
        when {'q'}: quit := TRUE;
      end case;
    until quit or okay or low > high;
    if not quit then
      writeln("So the number is: " <& guess);
      if low > high then
        writeln("Why did you cheat?");
      end if;
    end if;
    writeln("It was nice to play with you.");
  end func;