2007-09-20 18:09:08 -04:00
|
|
|
USING: kernel math math.parser random io ;
|
|
|
|
IN: numbers-game
|
|
|
|
|
2017-07-02 18:13:06 -04:00
|
|
|
: printfl ( s -- )
|
|
|
|
print flush ;
|
|
|
|
|
|
|
|
: writefl ( s -- )
|
|
|
|
write flush ;
|
|
|
|
|
|
|
|
: read-number ( -- n )
|
|
|
|
readln string>number ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
2008-06-09 03:14:14 -04:00
|
|
|
: guess-banner ( -- )
|
2017-07-02 18:13:06 -04:00
|
|
|
"I'm thinking of a number between 0 and 100." printfl ;
|
|
|
|
|
|
|
|
: guess-prompt ( -- ) "Enter your guess: " writefl ;
|
|
|
|
|
|
|
|
: too-high ( -- ) "Too high" printfl ;
|
|
|
|
|
|
|
|
: too-low ( -- ) "Too low" printfl ;
|
|
|
|
|
|
|
|
: correct ( -- ) "Correct - you win!" printfl ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
: inexact-guess ( actual guess -- )
|
|
|
|
< [ too-high ] [ too-low ] if ;
|
|
|
|
|
|
|
|
: judge-guess ( actual guess -- ? )
|
|
|
|
2dup = [ 2drop correct f ] [ inexact-guess t ] if ;
|
|
|
|
|
|
|
|
: number-to-guess ( -- n ) 100 random ;
|
|
|
|
|
|
|
|
: numbers-game-loop ( actual -- )
|
|
|
|
dup guess-prompt read-number judge-guess
|
|
|
|
[ numbers-game-loop ] [ drop ] if ;
|
|
|
|
|
2012-02-09 00:31:31 -05:00
|
|
|
: numbers-game ( -- )
|
|
|
|
guess-banner number-to-guess numbers-game-loop ;
|
2007-09-20 18:09:08 -04:00
|
|
|
|
|
|
|
MAIN: numbers-game
|