blib/lib/Games/Dice/Advanced.pm

filter: Statementstmt 94.3% 50 / 53 Branchbran 71.4% 20 / 28 Conditioncond 50.0% 6 / 12 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 12 / 12 Podpod 50.0% 2 / 4 totaltotal 78.6% 92 / 117 SCAR 31.1
CC
20
Cov
82%
new
13.4
roll
8.9
foldl
2.0
CRAP 22.4
↑ Summary
1 package Games::Dice::Advanced;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '1.1';
8  
9 =head1 NAME
10  
11 Games::Dice::Advanced - simulate dice rolls, including weird and
12 loaded dice
13  
14 =head1 SYNOPSIS
15  
16 print Games::Dice::Advanced->roll(); # roll a six-sided die
17 print Games::Dice::Advanced->roll('d4'); # roll a four-sided die
18  
19 # roll a four-sided die and a 6-sided die and return the total
20 $die1 = Games::Dice::Advanced->new('d4');
21 $die2 = Games::Dice::Advanced->new('d6');
22 print Games::Dice::Advanced->roll($die1, $die2);
23  
24 print $die1->roll(); # roll the d4 we created above
25  
26 # roll 2 four-sided dice and a 6-sided die and return the total
27 print Games::Dice::Advanced->roll('2d4', 'd6');
28  
29 # create a four-sided die with the squares of 1, 2, 3 and 4
30 Games::Dice::Advanced->new(sub { int(1+rand(4)) ** 2 });
31  
32 =head1 METHODS
33  
34 =over 4
35  
36 =item roll
37  
38 Roll one or more dice. If called as a class method, it first calls
39 appropriate constructors and creates objects before calling those objects'
40 roll() methods. When called on an object it simply rolls the die.
41  
42 When called as a class method, it takes a list of arguments defining a
43 'recipe' to roll. These are added
44 together to produce a result. Each item in the recipe must be a valid
45 argument to the constructor (see the description of the 'new' method below).
46 If no argument is given to a class method, we assume a six-sided die - 'd6'.
47 You will note that the multiplier constructor is not available when roll
48 is called in this way.
49  
50 When called as an object method, no arguments are permitted.
51  
52 =cut
53  
54 sub roll {
55 130045 ▶ my($self, @args) = @_;
Subroutine roll: called CC=8 SCAR=21.8
Pod documented
56 130045 ▶ if(ref($self) && $self->isa('Games::Dice::Advanced')) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self and $self->isa('Games::Dice::Advanced')) { } 130030 15 130045
elsif ($self eq 'Games::Dice::Advanced') { } 15 0 15
Condition66%2/3
ref $self and $self->isa("Games::Dice::Advanced")
!ll&&!rl&&r
150130030
MC/DC50%1/2
ref $self and $self->isa("Games::Dice::Advanced")
ref $self $self->isa("Games::Dice::Advanced")
Truth table66%2/3
A and B
A= ref $selfB= $self->isa("Games::Dice::Advanced")
ABresult
0X0
100
111
57 # called as object method
58 130030 ▶ die("roll() called incorrectly") if(@args);
Branch: 1 branch
BranchTrueFalseTotal
if @args 0 130030 130030
59 130030 return &{$self};
60 } elsif($self eq 'Games::Dice::Advanced') {
61 # called as class method
62 15 ▶ @args = ('d6') unless(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 15 15
63 return sum(map {
64 15 ▶ (ref($_) && $_->isa('Games::Dice::Advanced')) ?
Branch: 1 branch
BranchTrueFalseTotal
ref $_ && $_->isa('Games::Dice::Advanced') ? : 10 15 25
Condition66%2/3
ref $_ and $_->isa("Games::Dice::Advanced")
!ll&&!rl&&r
15010
MC/DC50%1/2
ref $_ and $_->isa("Games::Dice::Advanced")
ref $_ $_->isa("Games::Dice::Advanced")
Truth table66%2/3
A and B
A= ref $_B= $_->isa("Games::Dice::Advanced")
ABresult
0X0
100
111
65 $_->roll() :
66 Games::Dice::Advanced->new($_)->roll()
67 } @args);
68 } else {
69 0 die("Out of cucumber error\n");
70 }
71 }
72  
73 =item new
74  
75 This method defines a die. You may call it yourself to create a die for
76 later rolling, or it may be called by the roll() method. It takes zero,
77 one or two
78 arguments. If no argument is given, we silently assume that the user
79 wants to create a six-sided die, a 'd6'. Valid arguments are:
80  
81 =over 4
82  
83 =item integer constant, eg '5'
84  
85 Creates a die that always returns that constant
86  
87 =item dN, where N is integer, eg 'd10'
88  
89 Creates a die that returns a random integer from 1 to N with results spread
90 evenly across the range.
91  
92 =item NdM, where N and M are integer, eg '2d10'
93  
94 Creates a die dM as above which is rolled N times to generate a result.
95 Note that the N is *not* just a multiplier.
96  
97 =item N and any other valid argument, where N is a number, eg (2, 'd4')
98  
99 Note that the two arguments may be in any order. Creates a die as specified,
100 and multiplies the results by N when it is rolled. Compare with NdM above.
101  
102 =back
103  
104 Leading and trailing whitespace is stripped, no other whitespace is allowed
105 in any of the above.
106  
107 =over 4
108  
109 =item SUBREF
110  
111 A reference to a subroutine, which is to be called whenever we need to generate
112 a result. It should take no parameters.
113  
114 =item HASHREF
115  
116 Use this to easily specify truly weird dice. NOT YET IMPLEMENTED, so use
117 a SUBREF for the moment.
118  
119 =back
120  
121 =cut
122  
123 sub new {
124 21 ▶ my($class, @args) = @_;
Subroutine new: called CC=12 SCAR=25.9
Pod documented
125  
126 21 ▶ @args = ('d6') unless(@args);
Branch: 1 branch
BranchTrueFalseTotal
unless @args 0 21 21
127 21 @args = map { s/(^\s+|\s+$)//; $_; } @args;
128  
129 21 my $self = '';
130  
131 21 ▶ if(@args == 1) { push @args, 1; } # multiply by 1
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1) 20 1 21
132  
133 21 ▶ if(@args == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 2) { } 21 0 21
134 21 my($recipe, $mul) = @args;
135 21 ▶ ($recipe, $mul) = ($mul, $recipe) if(ref($mul) || $mul=~ /\D/);
Branch: 1 branch
BranchTrueFalseTotal
if ref $mul or $mul =~ /\D/ 0 21 21
Condition33%1/3
ref $mul or $mul =~ /\D/
l!l&&r!l&&!r
0021
MC/DC0%0/2
ref $mul or $mul =~ /\D/
ref $mul $mul =~ /\D/
Truth table33%1/3
A or B
A= ref $mulB= $mul =~ /\D/
ABresult
1X1
011
000
136 21 ▶ die("Bad arguments to new()") if(ref($mul) || $mul=~ /\D/);
Branch: 1 branch
BranchTrueFalseTotal
if ref $mul or $mul =~ /\D/ 0 21 21
Condition33%1/3
ref $mul or $mul =~ /\D/
l!l&&r!l&&!r
0021
MC/DC0%0/2
ref $mul or $mul =~ /\D/
ref $mul $mul =~ /\D/
Truth table33%1/3
A or B
A= ref $mulB= $mul =~ /\D/
ABresult
1X1
011
000
137  
138 21 ▶ if($recipe !~ /\D/) { # constant
Branch: 4 branches
BranchTrueFalseTotal
if (not $recipe =~ /\D/) { } 17 4 21
elsif ($recipe =~ /^d(\d+)$/) { } 2 2 4
elsif ($recipe =~ /^(\d+)d(\d+)/) { } 1 1 2
elsif (ref $recipe eq 'CODE') { } 1 0 1
139 # $self = eval("sub { $recipe * $mul }");
140 17 ▶ $self = sub { $recipe * $mul };
Subroutine __ANON__: called CC=1 SCAR=0.0
141 } elsif($recipe =~ /^d(\d+)$/) { # dINT
142 # $self = eval("sub { (1 + int(rand($1))) * $mul }");
143 2 my $faces = $1;
144 2 ▶ $self = sub { (1 + int(rand($faces))) * $mul };
Subroutine __ANON__: called CC=1 SCAR=0.0
145 } elsif($recipe =~ /^(\d+)d(\d+)/) { # INTdINT
146 1 my($repeats, $faces) = ($1, $2);
147 $self = sub {
148 100000 ▶ $mul * sum(map { 1 + int(rand($faces)) } (1..$repeats))
Subroutine __ANON__: called CC=1 SCAR=0.0
149 1 };
150 } elsif(ref($recipe) eq 'CODE') {
151 10000 ▶ $self = sub { $mul * &{$recipe} }
Subroutine __ANON__: called CC=1 SCAR=0.0
152 1 } else {
153 0 die("$recipe isn't valid");
154 }
155 } else {
156 0 die("new() called incorrectly");
157 }
158  
159 21 bless($self, $class);
160 }
161  
162 =back
163  
164 =cut
165  
166 100010 ▶sub sum { foldl(sub { shift() + shift(); }, @_); }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine sum: called CC=1 SCAR=0.0
Pod undocumented
167  
168 sub foldl {
169 100015 ▶ my($f, $z, @xs) = @_;
Subroutine foldl: called CC=2 SCAR=6.9
Pod undocumented
170 100015 $z = $f->($z, $_) foreach(@xs);
171 100015 return $z;
172 }
173  
174 =head1 BUGS
175  
176 For random, read 'pseudo-random'. Patches to work with sources of true
177 randomness are welcome.
178  
179 Doesn't support dice with fractional or complex numbers of sides :-)
180  
181 =head1 FEEDBACK
182  
183 I welcome feedback about my code, including constructive criticism. And,
184 while this is free software (both free-as-in-beer and free-as-in-speech) I
185 also welcome payment. In particular, your bug reports will get moved to
186 the front of the queue if you buy me something from my wishlist, which can
187 be found at L<http://www.cantrell.org.uk/david/shopping-list/wishlist>.
188  
189 =head1 AUTHOR
190  
191 David Cantrell E<lt>F<david@cantrell.org.uk>E<gt>
192  
193 =head1 COPYRIGHT
194  
195 Copyright 2003 David Cantrell
196  
197 This module is free-as-in-speech software, and may be used, distributed,
198 and modified under the same terms as Perl itself.
199  
200 =cut
201  
202 1 1;
↑ Summary