Acme-Dice

 view release on metacpan or  search on metacpan

lib/Acme/Dice.pm  view on Meta::CPAN

BEGIN {
    use Exporter ();
    use vars qw(@ISA @EXPORT_OK);
    @ISA = qw(Exporter);

    @EXPORT_OK = qw( roll_dice roll_craps );
}

$Acme::Dice::VERSION = '1.01';

my $defaults = {
    dice  => 1,
    sides => 6,
    favor => 0,
    bias  => 20,
};

sub roll_dice {
    my $raw_args = @_ == 1 ? shift : {@_};

    # no need to check params if coming from roll_craps

lib/Acme/Dice.pm  view on Meta::CPAN


    return wantarray ? @rolls : $rolls[0] + $rolls[1];
}

sub _validate_params {
    my $raw_args = @_ == 1 ? shift : {@_};

    my $args = {};
    my @errors;

    # put put defaults in place for missing params
    # and detect incoming undefined params
    for ( keys( %{$defaults} ) ) {
        $raw_args->{$_} = $defaults->{$_} if !exists( $raw_args->{$_} );
        push( @errors, "param present but undefined: $_" )
          unless defined $raw_args->{$_};
        $args->{$_} = delete( $raw_args->{$_} );
        push( @errors, "$_ must be a non-negative integer: $args->{$_}" )
          if defined( $args->{$_} ) && $args->{$_} !~ m/^\d+$/;
    }
    push( @errors,
        'RTFM! Unknown params: ' . join( ', ', keys( %{$raw_args} ) ) )
      if keys( %{$raw_args} );

lib/Acme/Dice.pm  view on Meta::CPAN

using normal dice normally allows. Here at last is a package that gives one
exactly the flexibility that has been lacking.

With Acme::Dice, not only can one specify the number and type of dice to be
rolled, not only can one choose to have just the total number or the
individual die results returned, but one can exert some amount of influence
over the outcome as well!

=head1 FUNCTIONS

Nothing is C<EXPORT>ed by default, However, the following functions are
available as imports.

=head2 roll_dice

This is the primary function. It accepts the parameters listed below to
control behavior and will return either the sum of the rolls or an array
containing the results of individual dice rolls depending upon context.

 my $total = roll_dice( dice => 3, sides => 6, favor => 6, bias => 30 );
 my @dice = roll_dice( dice => 3, sides => 6, favor => 6, bias => 30 );

lib/Acme/Dice.pm  view on Meta::CPAN


  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
to the author or use the GitHub repository:

t/01_load.t  view on Meta::CPAN

# -*- perl -*-

# t/01_load.t - check module loading and create testing directory

use Test::More tests => 3;

BEGIN { use_ok('Acme::Dice'); }

my $dice_roll = eval { roll_dice(); };
ok( !defined($dice_roll), 'roll_dice not imported by default' );

my $craps_roll = eval { roll_craps(); };
ok( !defined($craps_roll), 'roll_craps not imported by default' );

done_testing();

exit;

t/10_roll_dice.t  view on Meta::CPAN


# t/10_roll_dice.t - check operation of the roll_dice function

use Test::More tests => 7;

BEGIN { use_ok( 'Acme::Dice', qw(roll_dice) ); }

subtest 'basic parameter tests' => sub {
    plan tests => 7;

    # default roll should return a single d6
    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' );

t/20_roll_craps.t  view on Meta::CPAN

# t/20_roll_craps.t - check operation of the roll_craps function

use Test::More tests => 4;

BEGIN { use_ok( 'Acme::Dice', qw(roll_craps) ); }

subtest 'basic parameter tests' => sub {
    plan tests => 8;

    my @rolls = roll_craps();
    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' );



( run in 0.465 second using v1.01-cache-2.11-cpan-0a6323c29d9 )