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


-- defining the range of the number to be guessed
property minLimit : 1
property maxLimit : 100
 
on run
	-- ask the user to think of  a number in the given range
	display dialog "Please think of a number between " & minLimit & " and " & maxLimit
 
	-- prepare a variable for the lowest guessed value	
	set lowGuess to minLimit
	-- prepare a variable for the highest guessed value	
	set highGuess to maxLimit
 
	repeat
		-- guess a number inside the logical range
		set computersGuess to (random number from lowGuess to highGuess)
		-- ask the user to check my guess
		set guessResult to button returned of (display dialog "I guess " & computersGuess & "!" & return & "What do you think?" buttons {"Lower", "Correct", "Higher"})
		if guessResult = "Lower" then
			-- the number is less than the guess, switch the upper limit to the guess
			set highGuess to computersGuess
		else if guessResult = "Higher" then
			-- the number is greater than the guess, switch the lower limit to the guess
			set lowGuess to computersGuess
		else if guessResult = "Correct" then
			-- the computer guessed the number, beep and exit
			beep
			exit repeat
		end if
	end repeat
end run