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


MaxGuesses = 50
 
GetParams(LowerBound,UpperBound)
If Not GuessNum(LowerBound,UpperBound,MaxGuesses)
 MsgBox, 16, Error, Could not guess number within %MaxGuesses% guesses.
 
GetParams(ByRef LowerBound,ByRef UpperBound)
{
 WinWait, Number Guessing ahk_class #32770
 Sleep, 100
 WinGet, InputID, ID
 ControlGetText, Temp1, Static1, ahk_id %InputID%
 Temp2 := InStr(Temp1,A_Space,False,32)
 LowerBound := SubStr(Temp1,31,Temp2 - 31)
 UpperBound := SubStr(Temp1,Temp2 + 5,-1)
}
 
GuessNum(LowerBound,UpperBound,MaxGuesses)
{
 Loop, %MaxGuesses%
 {
  Guess := LowerBound + ((UpperBound - LowerBound) // 2)
  Temp1 := SendGuess(Guess)
  ToolTip % Temp1
  If Temp1 = Too Low
   LowerBound = %Guess%
  Else If Temp1 = Too High
   UpperBound = %Guess%
  Else
   Return, 1
 }
}
 
SendGuess(Guess)
{
 WinGet, InputID, ID, Number Guessing ahk_class #32770
 ControlSetText, Edit1, %Guess%, ahk_id %InputID%
 ControlSend, Button1, {Enter}, ahk_id %InputID%
 Loop
 {
  Sleep, 50
  IfWinExist, Correct ahk_class #32770
   Return
  Else IfWinExist, Incorrect ahk_class #32770
   Break
 }
 ControlGetText, Temp1, Static2
 WinClose
 WinWaitClose
 IfInString, Temp1, low
  Return, "Too Low"
 Else
  Return, "Too High"
}