blib/lib/Game/RoyalUr/Dice.pm

filter: Statementstmt 81.2% 52 / 64 Branchbran 42.3% 11 / 26 Conditioncond 33.3% 5 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 63.9% 85 / 133 CC 20 SCAR 20.4
CC
20
Cov
65%
opening_for
20.0
throw_for
11.6
__ANON__
6.7
CRAP 7.7
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/arch/Game/RoyalUr/Install/Files.pm »
1 package Game::RoyalUr::Dice;
2  
3 5 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 5 our $VERSION = '0.03';
13  
14 5 our @EXPORT_OK = qw(throw_for marked roll_of roll_for opening_for);
15 5 our %EXPORT_TAGS = (all => \@EXPORT_OK);
16  
17 80000 ▶my $field = sub {
Subroutine __ANON__: called CC=5 SCAR=19.1
18 80000 my ($variant, $name) = @_;
19 80000 my $value;
20 80000 ▶ if (Scalar::Util::blessed($variant) && $variant->can($name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($variant) and $variant->can($name)) { } 0 80000 80000
elsif (ref $variant eq 'HASH') { } 80000 0 80000
Condition33%1/3
builtin::blessed($variant) and $variant->can($name)
!ll&&!rl&&r
8000000
MC/DC0%0/2
builtin::blessed($variant) and $variant->can($name)
builtin::blessed($variant) $variant->can($name)
Truth table33%1/3
A and B
A= builtin::blessed($variant)B= $variant->can($name)
ABresult
0X0
100
111
21 0 $value = $variant->$name;
22 }
23 elsif (ref $variant eq 'HASH') {
24 80000 $value = $variant->{$name};
25 }
26 else {
27 0 Carp::croak("Game::RoyalUr::Dice: a rule set is a hash reference or an object with $name");
28 }
29 80000 ▶ Carp::croak("Game::RoyalUr::Dice: the rule set has no $name") unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 80000 80000
30 80000 return $value;
31 5 };
32  
33 40256 ▶sub throw_for {
Subroutine throw_for: called CC=8 SCAR=24.5
Pod documented
34 40256 my ($seed, $n, $dice) = @_;
35 40256 ▶ Carp::croak('Game::RoyalUr::Dice: throw_for wants a seed')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed 0 40256 40256
Condition33%1/3
defined $seed and length $seed
!ll&&!rl&&r
0040256
MC/DC0%0/2
defined $seed and length $seed
defined $seed length $seed
Truth table33%1/3
A and B
A= defined $seedB= length $seed
ABresult
0X0
100
111
36 unless defined $seed && length $seed;
37 40256 ▶ Carp::croak('Game::RoyalUr::Dice: a throw number is a non-negative integer')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A\d+\z/ 0 40256 40256
Condition33%1/3
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
0040256
MC/DC0%0/2
defined $n and $n =~ /\A\d+\z/
defined $n $n =~ /\A\d+\z/
Truth table33%1/3
A and B
A= defined $nB= $n =~ /\A\d+\z/
ABresult
0X0
100
111
38 unless defined $n && $n =~ /\A\d+\z/;
39 40256 ▶ Carp::croak('Game::RoyalUr::Dice: there are three dice or four')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dice and $dice =~ /\A[34]\z/ 0 40256 40256
Condition33%1/3
defined $dice and $dice =~ /\A[34]\z/
!ll&&!rl&&r
0040256
MC/DC0%0/2
defined $dice and $dice =~ /\A[34]\z/
defined $dice $dice =~ /\A[34]\z/
Truth table33%1/3
A and B
A= defined $diceB= $dice =~ /\A[34]\z/
ABresult
0X0
100
111
40 unless defined $dice && $dice =~ /\A[34]\z/;
41 40256 my $bytes = "$seed";
42 40256 ▶ Carp::croak('Game::RoyalUr::Dice: a seed is bytes, and that one holds a character above 255')
Branch: 1 branch
BranchTrueFalseTotal
unless utf8::downgrade($bytes, 1) 0 40256 40256
43 unless utf8::downgrade($bytes, 1);
44 40256 my $byte = unpack 'C', Digest::SHA::sha256($bytes . "roll:$n");
45 40256 return [ map { ($byte >> $_) & 1 } 0 .. $dice - 1 ];
46 }
47  
48 40000 ▶sub marked {
Subroutine marked: called CC=2 SCAR=6.9
Pod documented
49 40000 my ($throw) = @_;
50 40000 my $count = 0;
51 40000 $count += $_ for @$throw;
52 40000 return $count;
53 }
54  
55 40000 ▶sub roll_of {
Subroutine roll_of: called CC=5 SCAR=17.2
Pod documented
56 40000 my ($marked, $variant) = @_;
57 40000 ▶ Carp::croak('Game::RoyalUr::Dice: a count of marked dice is 0 to 4')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $marked and $marked =~ /\A[0-4]\z/ 0 40000 40000
Condition33%1/3
defined $marked and $marked =~ /\A[0-4]\z/
!ll&&!rl&&r
0040000
MC/DC0%0/2
defined $marked and $marked =~ /\A[0-4]\z/
defined $marked $marked =~ /\A[0-4]\z/
Truth table33%1/3
A and B
A= defined $markedB= $marked =~ /\A[0-4]\z/
ABresult
0X0
100
111
58 unless defined $marked && $marked =~ /\A[0-4]\z/;
59 40000 my $zero = $field->($variant, 'zero_rolls');
60 40000 ▶ Carp::croak('Game::RoyalUr::Dice: zero_rolls is 0 or 4')
Branch: 1 branch
BranchTrueFalseTotal
unless $zero =~ /\A[04]\z/ 0 40000 40000
61 unless $zero =~ /\A[04]\z/;
62 40000 ▶ return $marked == 0 ? $zero + 0 : $marked + 0;
Branch: 1 branch
BranchTrueFalseTotal
$marked == 0 ? : 3603 36397 40000
63 }
64  
65 40000 ▶sub roll_for {
Subroutine roll_for: called CC=1 SCAR=0.0
Pod documented
66 40000 my ($seed, $n, $variant) = @_;
67 40000 my $throw = throw_for($seed, $n, $field->($variant, 'dice'));
68 40000 return roll_of(marked($throw), $variant);
69 }
70  
71 0 ▶sub opening_for {
Subroutine opening_for: not called CC=4 SCAR=30.0
Pod documented
72 0 my ($seed, $dice) = @_;
73 0 my ($n, @throws) = (0);
74 0 ▶ while ($n < 128) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 128) 0 0 0
75 0 my $light = throw_for($seed, $n++, $dice);
76 0 my $dark = throw_for($seed, $n++, $dice);
77 0 push @throws, $light, $dark;
78 0 my ($l, $d) = (marked($light), marked($dark));
79 0 ▶ next if $l == $d;
Branch: 1 branch
BranchTrueFalseTotal
if $l == $d 0 0 0
80 0 ▶ return (($l > $d ? 'light' : 'dark'), $n, \@throws);
Branch: 1 branch
BranchTrueFalseTotal
$l > $d ? : 0 0 0
81 }
82 0 Carp::croak('Game::RoyalUr::Dice: the opening throw tied 64 times, which is not chance');
83 }
84  
85 5 1;
86  
87 __END__
« blib/lib/Game/RoyalUr/Notation.pm ↑ Summary blib/arch/Game/RoyalUr/Install/Files.pm »