blib/lib/Game/Backgammon/Dice.pm

filter: Statementstmt 97.6% 41 / 42 Branchbran 80.0% 16 / 20 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 8 / 8 Podpod 100% 3 / 3 totaltotal 84.3% 70 / 83 CC 14 SCAR 15.1
CC
14
Cov
87%
roll_for
7.0
_faces
6.0
opening_for
4.0
CRAP 4.5
« blib/lib/Game/Backgammon/Bot.pm ↑ Summary blib/lib/Game/Backgammon/Turn.pm »
1 package Game::Backgammon::Dice;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 our $VERSION = '0.05';
10 28 our @EXPORT_OK = qw(roll_for opening_for);
11  
12 19723 ▶sub _faces {
Subroutine _faces: called CC=6 SCAR=17.9
13 19723 my ($seed, $n) = @_;
14 19723 my @out;
15 19723 my $counter = 0;
16 19723 ▶ while (@out < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < 2) 19723 19723 39446
17 19723 my $bytes = Digest::SHA::sha256($seed . "roll:$n:$counter");
18 19723 for my $byte (unpack 'C*', $bytes) {
19 40082 ▶ next if $byte >= 252;
Branch: 1 branch
BranchTrueFalseTotal
if $byte >= 252 636 39446 40082
20 39446 push @out, ($byte % 6) + 1;
21 39446 ▶ last if @out == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @out == 2 19723 19723 39446
22 }
23 19723 $counter++;
24 19723 ▶ die 'Game::Backgammon::Dice: no usable bytes in 256 rounds' if $counter > 256;
Branch: 1 branch
BranchTrueFalseTotal
if $counter > 256 0 19723 19723
25 }
26 19723 return @out;
27 }
28  
29 19723 ▶sub roll_for {
Subroutine roll_for: called CC=5 SCAR=19.4
Pod documented
30 19723 my ($seed, $n) = @_;
31 19723 ▶ die 'Game::Backgammon::Dice: roll_for wants a seed' unless defined $seed && length $seed;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and length $seed 0 19723 19723
Condition33%1/3
defined $seed and length $seed
!ll&&!rl&&r
0019723
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
32 19723 ▶ die 'Game::Backgammon::Dice: a roll number is a non-negative integer'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A\d+\z/ 0 19723 19723
Condition33%1/3
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
0019723
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
33 unless defined $n && $n =~ /\A\d+\z/;
34 19723 return _faces($seed, $n);
35 }
36  
37 15907 ▶sub dice_for {
Subroutine dice_for: called CC=2 SCAR=6.9
Pod documented
38 15907 my ($seed, $n) = @_;
39 15907 my ($a, $b) = roll_for($seed, $n);
40 15907 ▶ return $a == $b ? ($a) x 4 : ($a, $b);
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 2573 13334 15907
41 }
42  
43 402 ▶sub opening_for {
Subroutine opening_for: called CC=4 SCAR=14.0
Pod documented
44 402 my ($seed) = @_;
45 402 my $n = 0;
46 402 ▶ while ($n < 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 64) 465 0 465
47 465 my ($white, $black) = roll_for($seed, $n);
48 465 $n++;
49 465 ▶ next if $white == $black;
Branch: 1 branch
BranchTrueFalseTotal
if $white == $black 63 402 465
50 402 ▶ return ($white > $black ? 'white' : 'black'), [ $white, $black ], $n;
Branch: 1 branch
BranchTrueFalseTotal
$white > $black ? : 178 224 402
51 }
52 0 die 'Game::Backgammon::Dice: the opening roll tied 64 times, which is not chance';
53 }
54  
55 28 1;
56  
57 __END__
« blib/lib/Game/Backgammon/Bot.pm ↑ Summary blib/lib/Game/Backgammon/Turn.pm »