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)


import "io" for Stdin, Stdout
import "random" for Random
 
var rand = Random.new()
var n = rand.int(1, 21) // computer number from 1..20 inclusive, say
System.print("The computer has chosen a number between 1 and 20 inclusive.")
while (true) {
    System.write("  Your guess 1-20 : ")
    Stdout.flush()
    var g = Num.fromString(Stdin.readLine())
    if (!g || g.type != Num || !g.isInteger || g < 1 || g > 20) {
        System.print("  Inappropriate")
    } else if (g > n) {
        System.print("  Too high")
    } else if (g < n) {
        System.print("  Too low")
    } else {
        System.print("  Spot on!")
        break
    }
}