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