--- Games/Dice.pm.old 2004-12-17 14:17:54.172707400 -0500 +++ Games/Dice.pm.new 2004-12-17 15:56:22.599186000 -0500 @@ -19,7 +19,7 @@ sub roll ($) { - my($line, $dice_string, $sign, $offset, $sum, @throws, @result); + my($line, $dice_string, @throws); $line = shift; @@ -34,38 +34,29 @@ % # a percent sign for d% = d100 ) ) - (?: # grouping-only parens - ([-+xX*/bB]) # a + - * / b(est) in $2 - (\d+) # an offset in $3 - )? # both of those last are optional + ((?: # capture + [-+xX*/bB] # a modifier + \d+ # a, number + )*) # capture }x; # whitespace allowed - $dice_string = $1; - $sign = $2 || ''; - $offset = $3 || 0; + @throws = roll_array( $1 ); + return unless @throws; - $sign = lc $sign; - - @throws = roll_array( $dice_string ); - return undef unless @throws; - - if( $sign eq 'b' ) { + my $mod = $2; + if( my($offset) = ($mod =~ /[bB](\d+)/) ) { $offset = 0 if $offset < 0; $offset = @throws if $offset > @throws; @throws = sort { $b <=> $a } @throws; # sort numerically, descending - @result = @throws[ 0 .. $offset-1 ]; # pick off the $offset first ones - } else { - @result = @throws; + @throws = @throws[ 0 .. $offset-1 ]; # pick off the $offset first ones + $mod = ''; } - $sum = 0; - $sum += $_ foreach @result; - $sum += $offset if $sign eq '+'; - $sum -= $offset if $sign eq '-'; - $sum *= $offset if ($sign eq '*' || $sign eq 'x'); - do { $sum /= $offset; $sum = int $sum; } if $sign eq '/'; - + my $sum = 0; + $sum += $_ foreach @throws; + (my $expr = $mod) =~ tr/Xx/**/; + $sum = int eval "$sum $expr" if $expr; return $sum; }