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


function wait(waittime)--wait function is used so that app will not quit immediately
  local timer = os.time()
  repeat until os.time() == timer + waittime
end
 
 
upperBound = 100
lowerBound = 0
print("Think of an integer between 1 to 100.")
print("I will try to guess it.")
while true do
	upper1 = upperBound+1
	upper2 = upperBound-1
	if upperBound == lowerBound or upper1 == lowerBound or upper2 == lowerBound or lowerBound > upperBound then--make sure player is not cheating
		io.write("You're cheating! I'm not playing anymore. Goodbye.")
		wait(3)
		break
	else
		Guess = math.floor((upperBound + lowerBound)/2)--guessing mechanism
		print("My guess is: "..Guess..". Is it too high, too low, or correct? (h/l/c)")
		input = io.read()
 
	if input == "h" then --higher
		upperBound = Guess
	elseif input == "l" then --lower
		lowerBound = Guess
	elseif input == "c" then --correct
		io.write("So I win? Thanks for playing with me.")
		wait(3)
		break
	else
		print("Invalid input. Please try again. ")
		end
	end
end
 