factor/extra/dice/dice.factor

30 lines
861 B
Factor
Raw Normal View History

! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
2015-07-14 23:35:10 -04:00
USING: fry kernel lexer macros math math.parser namespaces
random random.private sequences splitting ;
IN: dice
: (random-roll) ( n-dice n-sides obj -- n )
2015-07-14 23:35:10 -04:00
[ 0 ] 3dip '[ _ _ (random-integer) + 1 + ] times ;
: random-roll ( n-dice n-sides -- n )
2015-07-14 23:35:10 -04:00
random-generator get (random-roll) ;
: random-rolls ( length n-dice n-sides -- seq )
2015-07-14 23:35:10 -04:00
random-generator get '[ _ _ _ (random-roll) ] replicate ;
: parse-roll ( string -- n-dice n-sides n-added )
2015-07-14 23:35:10 -04:00
"d" split1 "+" split1 [ string>number ] tri@ ;
2015-07-14 23:35:10 -04:00
: roll ( string -- n )
parse-roll [ random-roll ] dip [ + ] when* ;
: roll-quot ( string -- quot: ( -- n ) )
parse-roll [
'[ _ _ random-roll _ + ]
] [
'[ _ _ random-roll ]
] if* ;
SYNTAX: \roll: scan-token roll-quot append! ;