1 USING: kernel math math.parser random io ;
4 : read-number ( -- n ) readln string>number ;
7 "I'm thinking of a number between 0 and 100." print ;
8 : guess-prompt ( -- ) "Enter your guess: " write ;
9 : too-high ( -- ) "Too high" print ;
10 : too-low ( -- ) "Too low" print ;
11 : correct ( -- ) "Correct - you win!" print ;
13 : inexact-guess ( actual guess -- )
14 < [ too-high ] [ too-low ] if ;
16 : judge-guess ( actual guess -- ? )
17 2dup = [ 2drop correct f ] [ inexact-guess t ] if ;
19 : number-to-guess ( -- n ) 100 random ;
21 : numbers-game-loop ( actual -- )
22 dup guess-prompt read-number judge-guess
23 [ numbers-game-loop ] [ drop ] if ;
25 : numbers-game ( -- ) number-to-guess numbers-game-loop ;