ex/dice-roll.pl

filter: Statementstmt 100% 36 / 36 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 100% 42 / 42 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Q/S/L.pm ↑ Summary ex/factors.pl »
1 1 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Q::S::L;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use List::Util qw(sum0);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 sub roll_dice
8 3 ▶{
Subroutine roll_dice: called CC=1 SCAR=0.0
9 3 my ($dnd) = @_;
10  
11 3 my ($number, $faces) = $dnd =~ m{ \A (\d+) k (\d+) \z }x;
12  
13 # each individual dice
14 3 my @dice = map { superpos(1 .. $faces) } 1 .. $number;
15  
16 # a cup of dice
17 3 my $set = sum0 @dice;
18  
19 3 return ($set, @dice);
20 }
21  
22 1 my @throws = (
23 "2k6",
24 "8k12",
25 "3k20",
26 );
27  
28 1 for my $dnd (@throws) {
29  
30 3 note "Rolling $dnd...";
31 3 my ($set, @dice) = roll_dice $dnd;
32  
33 # we roll them all at once
34 3 my $result = $set->collapse;
35  
36 # this is how we can get each individual roll
37 3 my @rolls = map { $_->collapse } @dice;
38  
39 3 note "we got $result, which consisted of rolls: " . join ", ", @rolls;
40 3 is $result, (sum0 @rolls), "result ok";
41  
42 # ... and lets roll again
43 3 $set->reset;
44 3 $result = $set->collapse;
45 3 @rolls = map { $_->collapse } @dice;
46  
47 3 note "this time we got $result, which consisted of rolls: " . join ", ", @rolls;
48 3 is $result, (sum0 @rolls), "result ok";
49 }
50  
51 1 done_testing;
52  
53 __END__
« blib/lib/Q/S/L.pm ↑ Summary ex/factors.pl »