2004-08-28 16:43:43 -04:00
|
|
|
! Numbers game example
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
: read-number ( -- n ) read parse-number ;
|
|
|
|
|
|
|
|
: 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 -- ? )
|
|
|
|
2dup = [
|
2004-08-31 23:42:30 -04:00
|
|
|
2drop correct f
|
2004-08-28 16:43:43 -04:00
|
|
|
] [
|
|
|
|
inexact-guess t
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: number-to-guess ( -- n ) 0 100 random-int ;
|
|
|
|
|
|
|
|
: numbers-game-loop ( actual -- )
|
|
|
|
dup guess-prompt read-number judge-guess [
|
|
|
|
numbers-game-loop
|
|
|
|
] [
|
|
|
|
drop
|
|
|
|
] ifte ;
|
|
|
|
|
|
|
|
: numbers-game number-to-guess numbers-game-loop ;
|