Paste: first attempt

Author: sykopomp
Mode: factor
Date: Thu, 14 Jan 2010 03:27:20
Plain Text |
USING: kernel random io math.parser math combinators ;
IN: high-low

: guess ( -- guess ) "Your guess? " write flush readln string>number ;

: check-guess ( answer guess -- succeeded? )
    {
        { [ 2dup > ] [ 2drop "Too low" print f ] }
        { [ 2dup < ] [ 2drop "Too high" print f ] }
        [ 2drop "You win!" print t ]
    } cond ;

: game-loop ( answer -- ) dup guess check-guess [ drop ] [ game-loop ] if ;

: start ( max -- ) random game-loop ;

Annotation: final version, i think

Author: sykopomp
Mode: factor
Date: Thu, 14 Jan 2010 03:37:00
Plain Text |
USING: kernel random io math.parser math combinators ;
IN: high-low

: guess ( -- guess ) "Your guess? " write flush readln string>number ;

: check-guess ( answer guess -- succeeded? )
    {
        { [ 2dup > ] [ 2drop "Too low" print f ] }
        { [ 2dup < ] [ 2drop "Too high" print f ] }
        [ 2drop "You win!" print t ]
    } cond ;

: (game-loop) ( answer -- ) [ guess check-guess not ] curry loop ;

: game-loop ( max -- ) random game-loop ;

: start ( -- ) 30 game-loop ;

MAIN: start

Annotation: one more time

Author: sykopomp
Mode: factor
Date: Thu, 14 Jan 2010 04:42:21
Plain Text |
USING: kernel random io math.parser math combinators ;
IN: high-low

: guess ( -- guess ) "Your guess? " write flush readln string>number ;

: check-guess ( answer guess -- succeeded? )
    {
        { [ 2dup > ] [ 2drop "Too low" print f ] }
        { [ 2dup < ] [ 2drop "Too high" print f ] }
        [ 2drop "You win!" print t ]
    } cond ;

: (game-loop) ( answer -- ) [ guess check-guess not ] curry loop ;

: game-loop ( max -- ) random (game-loop) ;

: start-game ( -- ) 30 game-loop ;

MAIN: start-game

Annotation: improved version.

Author: sykopomp
Mode: factor
Date: Thu, 14 Jan 2010 16:47:34
Plain Text |
USING: kernel random io math.parser math combinators ;
IN: high-low

: (guess) ( -- guess ) "Your guess? " write flush readln string>number ;

: guess ( -- guess ) (guess) dup [ ] [ drop "Not a number. Try again.\n" write flush guess ] if ;

: check-guess ( answer guess -- succeeded? )
    {
        { [ 2dup > ] [ 2drop "Too low" print f ] }
        { [ 2dup < ] [ 2drop "Too high" print f ] }
        [ 2drop "You win!" print t ]
    } cond ;

: (game-loop) ( answer -- ) [ guess check-guess not ] curry loop ;

: game-loop ( max -- ) random (game-loop) ;

: start-game ( -- ) 30 game-loop ;

MAIN: start-game

New Annotation

Summary:
Author:
Mode:
Body: