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


inclusiveRange = [1,100]
lowRange = inclusiveRange.x
maxRange = inclusiveRange.y
guesses = 1
inf =  "Think of a number between % and % and I will try to guess it.\n" +\
		"Type -1 if the guess is less than your number,\n"+\
		"0 if the guess is correct, " +\
		"or 1 if it's too high.\nPress esc to exit.\n"
clearListener()
format inf (int lowRange) (int maxRange)
while not keyboard.escpressed do
(
	local chosen = ((lowRange + maxRange) / 2) as integer
	if lowRange == maxRange do format "\nHaving fun?"
	format "\nI choose %.\n" chosen
	local theAnswer = getKBValue prompt:"Answer? "
	case theAnswer of
	(
		(-1): (lowRange = chosen; guesses += 1)
		(0): (format "\nYay. I guessed your number after % %.\n" \
				guesses (if guesses == 1 then "try" else "tries")
				exit with OK)
		(1): (maxRange = chosen; guesses += 1)
		default: (format "\nI don't understand your input.")
	)
)