Task

Write a game (computer program) that follows the following rules:

 The computer chooses a number between given set limits.
 The player is asked for repeated guesses until the the target number is guessed correctly
 At each guess, the computer responds with whether the guess is:
 higher than the target,
 equal to the target, 
 less than the target,   or
 the input was inappropriate. 


Related task

   Guess the number/With Feedback (Player)


open string datetime random core monad io
 
guess () = do
  putStrLn "What's the upper bound?"
  ub <- readAny
  main ub
  where main ub
          | ub < 0 = "Bound should be greater than 0."
          | else = do 
              putStrLn $ format "Guess a number from 1 to {0}" ub
              dt <- datetime.now
              guesser (rnd (milliseconds $ dt) 1 ub)
        guesser v = do
          x <- readAny
          if x == v then
              cont ()
            else if x < v then
              do putStrLn "Too small!"
                 guesser v
            else 
              do putStrLn "Too big!"
                 guesser v
        cont () = do
          putStrLn "Correct! Do you wish to continue (Y/N)?"
          ask ()
        ask () = do
          a <- readStr
          if a == "y" || a == "Y" then
              guess ()
            else if a == "n" || a == "N" then
              do putStrLn "Bye!"
            else 
              do putStrLn "Say what?"
                 ask ()
 
guess () ::: IO