Acme-Dice
view release on metacpan or search on metacpan
lib/Acme/Dice.pm view on Meta::CPAN
it will roll a single 6-sided die with no bias.
The parameters are as follows:
=over 4
=item dice
This is an integer specifying the number of dice to roll. Default: 1
An exception will be thrown if it is less than 1 or greater than 100.
=item sides
This is an integer specifying how many sides are on the dice to be rolled.
Default: 6
An exception will be thrown if it is less than 1. (Huh? A 1-sided die?
Nothing is impossible for Acme!)
=item favor
This integer specifies which number (if any) should be favored and must be
between 0 and the value specified for C<sides>. A value of 0 disables
any bias even if a value for C<bias> is given. Default: 0
=item bias
This is an integer between 0 and 100 that determines how much "weight" to
place on the favored side. A value of C<20> says to increase the chance of
rolling the favored number by 20%. A value of C<100> would mean to always
roll the favored number. A value of 0 will disable favoring completely,
even if a value for C<favor> is given. Default: 0
An exception will be thrown if the value is less than 0 or greater than 100.
=back
=head2 roll_craps
This function is sugar for C<roll_dice> that automatically rolls two 6-sided
dice. It will also automatically adjust the C<favor> parameter for "3" and "4"
as appropriate if a value for C<bias> is given, simulating "loaded" dice.
Like C<roll_dice>, the return value depends upon context.
my $total = roll_craps( bias => 30 );
my @dice = roll_craps( bias => 30 );
It will only accept a single, optional parameter: C<bias>
The C<bias> parameter behaves the same as described above for C<roll_dice>.
Any other parameters, including those that are otherwise legal for
C<roll_dice>, will cause an exception to be thrown.
The default is an un-biased roll of two 6-sided dice.
=head1 BUGS
Bugs? In an Acme module?!? Yeah, right.
=head1 SUPPORT
Support is provided by the author. Please report bugs or make feature requests
t/10_roll_dice.t view on Meta::CPAN
my @rolls = roll_dice();
ok( @rolls == 1, 'default roll has one die' );
ok( $rolls[0] >= 1 && $rolls[0] <= 6, 'default consistent with a d6' );
# check if undefined parameters are caught
for (qw(dice sides favor bias)) {
my $roll = eval { roll_dice( $_ => undef ); };
ok( !defined($roll), "dies with undefined param: $_" );
}
# and be sure unknown params throw an error
my $roll_u = eval { roll_dice( foo => 'bar' ); };
ok( !defined($roll_u), 'dies with unknown parameter' );
};
subtest 'dice parameter values' => sub {
plan tests => 15;
for ( 1, 2, 3, 5, 100 ) {
my $roll = roll_dice( dice => $_, sides => 1 );
ok( $roll == $_, "good dice param: $_" );
t/20_roll_craps.t view on Meta::CPAN
ok( @rolls == 2, 'default roll has two die' );
ok( $rolls[0] >= 1 && $rolls[0] <= 6, 'default consistent with a d6' );
ok( $rolls[1] >= 1 && $rolls[1] <= 6, 'default consistent with a d6' );
# check if unknown parameters are caught
for (qw(dice sides favor foo)) {
my $roll = eval { roll_craps( $_ => undef ); };
ok( !defined($roll), "dies with unknown param: $_" );
}
# and be sure an undefined param throws an error
my $roll_u = eval { roll_craps( bias => undef ); };
ok( !defined($roll_u), 'dies with undefined bias parameter' );
};
subtest 'bias parameter values' => sub {
plan tests => 2;
for ( -1, 101 ) {
my $roll = eval { roll_craps( bias => $_ ); };
ok( !defined($roll), "dies with bad bias param: $_" );
( run in 0.262 second using v1.01-cache-2.11-cpan-496ff517765 )