2004-08-28 16:43:43 -04:00
|
|
|
IN: numbers-game
|
2005-06-19 17:50:35 -04:00
|
|
|
USING: kernel math parser random io ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
2005-09-14 00:37:50 -04:00
|
|
|
: read-number ( -- n ) readln string>number ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
|
|
|
: guess-banner
|
|
|
|
"I'm thinking of a number between 0 and 100." print ;
|
|
|
|
: guess-prompt "Enter your guess: " write ;
|
|
|
|
: too-high "Too high" print ;
|
|
|
|
: too-low "Too low" print ;
|
|
|
|
: correct "Correct - you win!" print ;
|
|
|
|
|
|
|
|
: inexact-guess ( actual guess -- )
|
|
|
|
< [ too-high ] [ too-low ] ifte ;
|
|
|
|
|
|
|
|
: judge-guess ( actual guess -- ? )
|
2005-07-22 23:39:28 -04:00
|
|
|
2dup = [ 2drop correct f ] [ inexact-guess t ] ifte ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
|
|
|
: number-to-guess ( -- n ) 0 100 random-int ;
|
|
|
|
|
|
|
|
: numbers-game-loop ( actual -- )
|
2005-07-22 23:39:28 -04:00
|
|
|
dup guess-prompt read-number judge-guess
|
|
|
|
[ numbers-game-loop ] [ drop ] ifte ;
|
2004-08-28 16:43:43 -04:00
|
|
|
|
|
|
|
: numbers-game number-to-guess numbers-game-loop ;
|