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


 
class Main
{
  public static Void main ()
  {
    Int lowerLimit := 1
    Int higherLimit := 100
 
    echo ("Think of a number between 1 and 100")
    echo ("Press 'enter' when ready")
    Env.cur.in.readLine
 
    while (true)
    {
      if (higherLimit < lowerLimit)
      { // check that player is not cheating!
        echo ("Something has gone wrong ... I give up")
        break
      }
      myGuess := (higherLimit + lowerLimit) / 2
      echo ("My guess is $myGuess")
      echo ("Enter 'H' if your number is higher, 'L' if lower, or 'E' if equal")
      switch (Env.cur.in.readLine.trim.upper)
      {
        case "E": 
          echo ("I got it correct - thankyou!")
          break // game over
        case "H": 
          lowerLimit = myGuess + 1
        case "L":
          higherLimit = myGuess - 1
        default:
          echo ("Pardon? Let's try that again")
      }
    }
  }
}
 