| 1 | package Acme::Dice; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.008008; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Data::Dumper; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use Exporter (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use vars qw(@ISA @EXPORT_OK); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | @ISA = qw(Exporter); | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | @EXPORT_OK = qw( roll_dice roll_craps ); | |||||||||||||||||||||||||||
| 16 | } | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | $Acme::Dice::VERSION = '1.01'; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | my $defaults = { | |||||||||||||||||||||||||||
| 21 | dice => 1, | ||||||||||||||||||||||||||||
| 22 | sides => 6, | ||||||||||||||||||||||||||||
| 23 | favor => 0, | ||||||||||||||||||||||||||||
| 24 | bias => 20, | ||||||||||||||||||||||||||||
| 25 | }; | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 4330 | ▶ | sub roll_dice { | ||||||||||||||||||||||||||
|
Subroutine
roll_dice: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | 4330 | ▶ | my $raw_args = @_ == 1 ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # no need to check params if coming from roll_craps | ||||||||||||||||||||||||||||
| 31 | my $args = | ||||||||||||||||||||||||||||
| 32 | delete( $raw_args->{skip_validate} ) | ||||||||||||||||||||||||||||
| 33 | 4330 | ▶ | ? $raw_args | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | : _validate_params($raw_args); | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4297 | my @rolls; | |||||||||||||||||||||||||||
| 37 | 4297 | my $roll_total = 0; | |||||||||||||||||||||||||||
| 38 | 4297 | for ( 1 .. $args->{dice} ) { | |||||||||||||||||||||||||||
| 39 | 9309 | my $roll = ( int( rand( $args->{sides} ) ) + 1 ); | |||||||||||||||||||||||||||
| 40 | 9309 | ▶ | _apply_bias( \$roll, $args ) if $args->{favor} && $args->{bias}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"favor"} and $args->{"bias"}
MC/DC0%0/2
$args->{"favor"} and $args->{"bias"}
$args->{"favor"}
$args->{"bias"}
Truth table66%2/3
A and B
A= $args->{"favor"}B= $args->{"bias"}
| |||||||||||||||||||||||||||||
| 41 | 9309 | push( @rolls, $roll ); | |||||||||||||||||||||||||||
| 42 | 9309 | $roll_total += $roll; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 4297 | ▶ | return wantarray ? @rolls : $roll_total; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 368 | ▶ | sub roll_craps { | ||||||||||||||||||||||||||
|
Subroutine
roll_craps: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 49 | 368 | ▶ | my $raw_args = @_ == 1 ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | croak "param present but undefined: bias" | ||||||||||||||||||||||||||||
| 52 | 368 | ▶ | if exists( $raw_args->{bias} ) && !defined( $raw_args->{bias} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
exists $raw_args->{"bias"} and not defined $raw_args->{"bias"}
MC/DC100%2/2
exists $raw_args->{"bias"} and not defined $raw_args->{"bias"}
exists $raw_args->{"bias"}
not defined $raw_args->{"bias"}
Truth table100%3/3
A and not(B)
A= exists $raw_args->{"bias"}B= not defined $raw_args->{"bias"}
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 367 | ▶ | my $bias = delete( $raw_args->{bias} ) || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
delete $raw_args->{'bias'} || 0
MC/DC100%1/1
delete $raw_args->{'bias'} || 0
delete $raw_args->{'bias'}
Truth table100%2/2
A
A= delete $raw_args->{'bias'}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 367 | ▶ | croak "Illegal value for 'bias': $bias" if $bias < 0 || $bias > 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$bias < 0 or $bias > 100
MC/DC100%2/2
$bias < 0 or $bias > 100
$bias < 0
$bias > 100
Truth table100%3/3
A or B
A= $bias < 0B= $bias > 100
| |||||||||||||||||||||||||||||
| 57 | 4 | croak 'RTFM! Unknown params: ' . join( ', ', keys( %{$raw_args} ) ) | |||||||||||||||||||||||||||
| 58 | 365 | ▶ | if keys( %{$raw_args} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # hey, this is Acme, remember? you were TOLD not to look inside! | ||||||||||||||||||||||||||||
| 61 | 361 | ▶ | return ( wantarray ? ( 3, 4 ) : 7 ) if rand(100) < 5; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 339 | my @rolls; | |||||||||||||||||||||||||||
| 64 | 339 | push( | |||||||||||||||||||||||||||
| 65 | @rolls, | ||||||||||||||||||||||||||||
| 66 | roll_dice( | ||||||||||||||||||||||||||||
| 67 | skip_validate => 1, | ||||||||||||||||||||||||||||
| 68 | dice => 1, | ||||||||||||||||||||||||||||
| 69 | sides => 6, | ||||||||||||||||||||||||||||
| 70 | favor => 3, | ||||||||||||||||||||||||||||
| 71 | bias => $bias | ||||||||||||||||||||||||||||
| 72 | ) | ||||||||||||||||||||||||||||
| 73 | ); | ||||||||||||||||||||||||||||
| 74 | 339 | push( | |||||||||||||||||||||||||||
| 75 | @rolls, | ||||||||||||||||||||||||||||
| 76 | roll_dice( | ||||||||||||||||||||||||||||
| 77 | skip_validate => 1, | ||||||||||||||||||||||||||||
| 78 | dice => 1, | ||||||||||||||||||||||||||||
| 79 | sides => 6, | ||||||||||||||||||||||||||||
| 80 | favor => 4, | ||||||||||||||||||||||||||||
| 81 | bias => $bias | ||||||||||||||||||||||||||||
| 82 | ) | ||||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 339 | ▶ | return wantarray ? @rolls : $rolls[0] + $rolls[1]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 3652 | ▶ | sub _validate_params { | ||||||||||||||||||||||||||
|
Subroutine
_validate_params: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||
| 89 | 3652 | ▶ | my $raw_args = @_ == 1 ? shift : {@_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 3652 | my $args = {}; | |||||||||||||||||||||||||||
| 92 | 3652 | my @errors; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # put put defaults in place for missing params | ||||||||||||||||||||||||||||
| 95 | # and detect incoming undefined params | ||||||||||||||||||||||||||||
| 96 | 3652 | for ( keys( %{$defaults} ) ) { | |||||||||||||||||||||||||||
| 97 | 14608 | ▶ | $raw_args->{$_} = $defaults->{$_} if !exists( $raw_args->{$_} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | push( @errors, "param present but undefined: $_" ) | ||||||||||||||||||||||||||||
| 99 | 14608 | ▶ | unless defined $raw_args->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 14608 | $args->{$_} = delete( $raw_args->{$_} ); | |||||||||||||||||||||||||||
| 101 | push( @errors, "$_ must be a non-negative integer: $args->{$_}" ) | ||||||||||||||||||||||||||||
| 102 | 14608 | ▶ | if defined( $args->{$_} ) && $args->{$_} !~ m/^\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $args->{$_} and not $args->{$_} =~ /^\d+$/
MC/DC100%2/2
defined $args->{$_} and not $args->{$_} =~ /^\d+$/
defined $args->{$_}
not $args->{$_} =~ /^\d+$/
Truth table100%3/3
A and not(B)
A= defined $args->{$_}B= not $args->{$_} =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | push( @errors, | ||||||||||||||||||||||||||||
| 105 | 1 | 'RTFM! Unknown params: ' . join( ', ', keys( %{$raw_args} ) ) ) | |||||||||||||||||||||||||||
| 106 | 3652 | ▶ | if keys( %{$raw_args} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 3652 | ▶ | croak join( "\n", @errors ) if @errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # validate individual params now | ||||||||||||||||||||||||||||
| 111 | push( @errors, "Illegal value for 'dice': $args->{dice}" ) | ||||||||||||||||||||||||||||
| 112 | 3635 | ▶ | if $args->{dice} < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | 3635 | ▶ | push( @errors, "Really? Roll $args->{dice} dice?" ) if $args->{dice} > 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | push( @errors, "Illegal value for 'sides': $args->{sides}" ) | ||||||||||||||||||||||||||||
| 116 | 3635 | ▶ | if $args->{sides} < 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | push( @errors, "Illegal value for 'favor': $args->{favor}" ) | ||||||||||||||||||||||||||||
| 119 | 3635 | ▶ | if $args->{favor} < 0 || $args->{favor} > $args->{sides}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"favor"} < 0 or $args->{"favor"} > $args->{"sides"}
MC/DC50%1/2
$args->{"favor"} < 0 or $args->{"favor"} > $args->{"sides"}
$args->{"favor"} < 0
$args->{"favor"} > $args->{"sides"}
Truth table66%2/3
A or B
A= $args->{"favor"} < 0B= $args->{"favor"} > $args->{"sides"}
| |||||||||||||||||||||||||||||
| 120 | push( @errors, "Illegal value for 'bias': $args->{bias}" ) | ||||||||||||||||||||||||||||
| 121 | 3635 | ▶ | if $args->{bias} < 0 || $args->{bias} > 100; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$args->{"bias"} < 0 or $args->{"bias"} > 100
MC/DC50%1/2
$args->{"bias"} < 0 or $args->{"bias"} > 100
$args->{"bias"} < 0
$args->{"bias"} > 100
Truth table66%2/3
A or B
A= $args->{"bias"} < 0B= $args->{"bias"} > 100
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3635 | ▶ | croak join( "\n", @errors ) if @errors; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 3619 | return $args; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub _apply_bias { | ||||||||||||||||||||||||||
|
Subroutine
_apply_bias: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 129 | 0 | my $roll_src = shift; | |||||||||||||||||||||||||||
| 130 | 0 | my $args = shift; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | ${$roll_src} = $args->{favor} | |||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ${$roll_src} != $args->{favor} && rand(100) < $args->{bias}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
${$roll_src;} != $args->{"favor"} and rand 100 < $args->{"bias"}
MC/DC0%0/2
${$roll_src;} != $args->{"favor"} and rand 100 < $args->{"bias"}
${$roll_src;} != $args->{"favor"}
rand 100 < $args->{"bias"}
Truth table0%0/3
A and B
A= ${$roll_src;} != $args->{"favor"}B= rand 100 < $args->{"bias"}
| |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 0 | return; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 3 | 1; | |||||||||||||||||||||||||||
| 139 | __END__ | ||||||||||||||||||||||||||||