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


100 PROGRAM "GuessIt.bas"
110 LET N=100
120 TEXT 80
130 PRINT "Choose a number between 1 and;" N:PRINT "I will start guess the number."
140 LET BL=1:LET UL=100:LET NR=0
150 DO
160   LET GUESS=INT((BL+UL)/2):LET NR=NR+1
170   SET #102:INK 3:PRINT :PRINT "My";NR;". guess: ";GUESS:SET #102:INK 1
180   LET ANSWER=QUESTION
190   SELECT CASE ANSWER
200   CASE 1
210     LET UL=GUESS-1
220   CASE 2
230     LET BL=GUESS+1
240   CASE ELSE
250   END SELECT
260   IF BL>UL THEN PRINT "You are cheating!":LET ANSWER=9
270 LOOP UNTIL ANSWER=0 OR ANSWER=9
280 PRINT "So the number is:" GUESS
290 DEF QUESTION
300   PRINT "Your number:  1 - Is lower?;  2 - Is higher?;  0 - Is equal?"
310   DO
320     LET K$=INKEY$
330   LOOP UNTIL K$>="0" AND K$<="3"
340   LET QUESTION=VAL(K$)
350 END DEF