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)


get "libhdr"
static $( randstate = ? $)
 
let rand(min,max) = valof
$(  let x = ?
    let range = max-min
    let mask = 1
    while mask < range do mask := (mask << 1) | 1
    $(  randstate := random(randstate)
        x := (randstate >> 6) & mask
    $) repeatuntil 0 <= x < range
    resultis x + min
$)
 
let readguess(min,max) = valof
$(  let x = ?
    writes("Guess? ")
    x := readn()
    if min <= x < max then resultis x
    writes("Invalid input.*N")
$) repeat
 
let play(min,max,secret) be
$(  let tries, guess = 0, ?
    $(  guess := readguess(min,max)
        if guess < secret then writes("Too low!*N")
        if guess > secret then writes("Too high!*N")
        tries := tries + 1
    $) repeatuntil guess = secret
    writef("Correct! You guessed it in %N tries.*N", tries)
$)
 
let start() be
$(  let min, max = ?, ?
    writes("Guess the number*N*N")
    writes("Random seed? ") ; randstate := readn()
    $(  writes("Lower bound? ") ; min := readn()
        writes("Upper bound? ") ; max := readn() + 1
        test max-1 > min break or writes("Invalid bounds.*N")
    $) repeat
    wrch('*N')
    play(min, max, rand(min, max))
$)