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
« Backgammon/Bot.pm ↑ Summary Backgammon.pm »
1 package Game::Backgammon::Dice;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '0.03';
10 7 our @EXPORT_OK = qw(roll_for opening_for);
11  
12 8553 ▶sub _faces {
Subroutine _faces: called CC=6 SCAR=17.9
13 8553 my ($seed, $n) = @_;
14 8553 my @out;
15 8553 my $counter = 0;
16 8553 ▶ while (@out < 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@out < 2) 8553 8553 17106
17 8553 my $bytes = Digest::SHA::sha256($seed . "roll:$n:$counter");
18 8553 for my $byte (unpack 'C*', $bytes) {
19 17382 ▶ next if $byte >= 252;
Branch: 1 branch
BranchTrueFalseTotal
if $byte >= 252 276 17106 17382
20 17106 push @out, ($byte % 6) + 1;
21 17106 ▶ last if @out == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @out == 2 8553 8553 17106
22 }
23 8553 $counter++;
24 8553 ▶ die 'Game::Backgammon::Dice: no usable bytes in 256 rounds' if $counter > 256;
Branch: 1 branch
BranchTrueFalseTotal
if $counter > 256 0 8553 8553
25 }
26 8553 return @out;
27 }
28  
29 8553 ▶sub roll_for {
Subroutine roll_for: called CC=5 SCAR=19.4
Pod documented
30 8553 my ($seed, $n) = @_;
31 8553 ▶ 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 8553 8553
Condition33%1/3
defined $seed and length $seed
!ll&&!rl&&r
008553
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 8553 ▶ 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 8553 8553
Condition33%1/3
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
008553
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 8553 return _faces($seed, $n);
35 }
36  
37 4848 ▶sub dice_for {
Subroutine dice_for: called CC=2 SCAR=6.9
Pod documented
38 4848 my ($seed, $n) = @_;
39 4848 my ($a, $b) = roll_for($seed, $n);
40 4848 ▶ return $a == $b ? ($a) x 4 : ($a, $b);
Branch: 1 branch
BranchTrueFalseTotal
$a == $b ? : 765 4083 4848
41 }
42  
43 294 ▶sub opening_for {
Subroutine opening_for: called CC=4 SCAR=14.0
Pod documented
44 294 my ($seed) = @_;
45 294 my $n = 0;
46 294 ▶ while ($n < 64) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 64) 354 0 354
47 354 my ($white, $black) = roll_for($seed, $n);
48 354 $n++;
49 354 ▶ next if $white == $black;
Branch: 1 branch
BranchTrueFalseTotal
if $white == $black 60 294 354
50 294 ▶ return ($white > $black ? 'white' : 'black'), [ $white, $black ], $n;
Branch: 1 branch
BranchTrueFalseTotal
$white > $black ? : 151 143 294
51 }
52 0 die 'Game::Backgammon::Dice: the opening roll tied 64 times, which is not chance';
53 }
54  
55 7 1;
56  
57 __END__
« Backgammon/Bot.pm ↑ Summary Backgammon.pm »