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


 
main :: IO ()
main = do
    putStrLn "Please enter the range:"
    putStr   "From: "
    from <- getLine
    putStr   "To: "
    to   <- getLine
    case (from, to) of
         (_) | [(from', "")] <- reads from
             , [(to'  , "")] <- reads to
             , from'         < to' -> loop from' to' 
         (_)  -> putStrLn "Invalid input." >> main
 
loop :: Integer -> Integer -> IO ()
loop from to = do
    let guess = (to + from) `div` 2
    putStrLn $ "Is it " ++ show guess ++ "? ((l)ower, (c)orrect, (h)igher)"
    answer <- getLine
    case answer of
        "c" -> putStrLn "Awesome!"
        "l" -> loop from  guess
        "h" -> loop guess to
        (_) -> putStrLn "Invalid answer." >> loop from to
 