blib/lib/Game/Backgammon/Board.pm

filter: Statementstmt 100% 110 / 110 Branchbran 90.6% 29 / 32 Conditioncond 53.8% 7 / 13 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 100% 30 / 30 Podpod 100% 21 / 21 totaltotal 93.8% 199 / 212 CC 28 SCAR 11.1
CC
28
Cov
94%
index_for
9.0
consistent
5.1
all_home
4.0
CRAP 3.0
« blib/lib/Game/Backgammon/Error.pm ↑ Summary blib/lib/Game/Backgammon/Move.pm »
1 package Game::Backgammon::Board;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 30 our $VERSION = '0.05';
9  
10 30 ▶use constant POINTS => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use constant CHECKERS => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use constant BAR_PIP => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 207 ▶sub opening {
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
15 207 my @p = (0) x POINTS;
16 207 $p[23] = 2; $p[12] = 5; $p[7] = 3; $p[5] = 5;
17 207 $p[0] = -2; $p[11] = -5; $p[16] = -3; $p[18] = -5;
18 207 return \@p;
19 }
20  
21 has points => (
22 is => 'ro',
23 isa => ArrayRef,
24 207 ▶ default => sub { opening() }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 30 );
26  
27 has bar_counts => (
28 is => 'ro',
29 isa => HashRef,
30 237 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
31 30 );
32  
33 has off_counts => (
34 is => 'ro',
35 isa => HashRef,
36 237 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 30 );
38  
39 2256654 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
40 2256654 my ($self) = @_;
41 return ref($self)->new(
42 2256654 points => [ @{ $self->points } ],
43 2256654 bar_counts => { %{ $self->bar_counts } },
44 2256654 off_counts => { %{ $self->off_counts } }
45 );
46 }
47  
48 2525845 ▶sub other { return $_[0] eq 'white' ? 'black' : 'white' }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'white' ? : 1205120 1320725 2525845
49  
50 36465840 ▶sub index_for {
Subroutine index_for: called CC=6 SCAR=22.0
Pod documented
51 36465840 my ($self, $player, $n) = @_;
52 36465840 ▶ die "Game::Backgammon::Board: point $n is not 1 to 24"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A\d+\z/ and $n >= 1 and $n <= 24 0 36465840 36465840
Condition33%3/9
defined $n and $n =~ /\A\d+\z/ and $n >= 1 and $n <= 24
!ll&&!rl&&r
0036465840
defined $n and $n =~ /\A\d+\z/ and $n >= 1
!ll&&!rl&&r
0036465840
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
0036465840
MC/DC0%0/4
defined $n and $n =~ /\A\d+\z/ and $n >= 1 and $n <= 24
defined $n $n =~ /\A\d+\z/ $n >= 1 $n <= 24
Truth table20%1/5
A and B and C and D
A= defined $nB= $n =~ /\A\d+\z/C= $n >= 1D= $n <= 24
ABCDresult
0XXX0
10XX0
110X0
11100
11111
53 unless defined $n && $n =~ /\A\d+\z/ && $n >= 1 && $n <= POINTS;
54 36465840 ▶ return $player eq 'white' ? $n - 1 : POINTS - $n;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 18885032 17580808 36465840
55 }
56  
57 31900937 ▶sub point_for {
Subroutine point_for: called CC=3 SCAR=11.0
Pod documented
58 31900937 my ($self, $player, $n) = @_;
59 31900937 my $v = $self->points->[ $self->index_for($player, $n) ];
60 31900937 ▶ my $mine = $player eq 'white' ? $v : -$v;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 16726830 15174107 31900937
61 31900937 ▶ return $mine >= 0 ? ($mine, 0) : (0, -$mine);
Branch: 1 branch
BranchTrueFalseTotal
$mine >= 0 ? : 26728086 5172851 31900937
62 }
63  
64 24378581 ▶sub mine_on { my ($self, $p, $n) = @_; my ($m) = $self->point_for($p, $n); return $m }
Subroutine mine_on: called CC=1 SCAR=0.0
Pod documented
65 6263555 ▶sub theirs_on { my ($self, $p, $n) = @_; my (undef, $t) = $self->point_for($p, $n); return $t }
Subroutine theirs_on: called CC=1 SCAR=0.0
Pod documented
66  
67 3890835 ▶sub is_blot { my ($self, $player, $n) = @_; return $self->theirs_on($player, $n) == 1 ? 1 : 0 }
Subroutine is_blot: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->theirs_on($player, $n) == 1 ? : 272956 3617879 3890835
68  
69 1739240 ▶sub is_blocked { my ($self, $player, $n) = @_; return $self->theirs_on($player, $n) >= 2 ? 1 : 0 }
Subroutine is_blocked: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$self->theirs_on($player, $n) >= 2 ? : 95811 1643429 1739240
70  
71 1827978 ▶sub bar { my ($self, $player) = @_; return $self->bar_counts->{$player} }
Subroutine bar: called CC=1 SCAR=0.0
Pod documented
72 567178 ▶sub off { my ($self, $player) = @_; return $self->off_counts->{$player} }
Subroutine off: called CC=1 SCAR=0.0
Pod documented
73  
74 158733 ▶sub set_point {
Subroutine set_point: called CC=2 SCAR=6.9
Pod documented
75 158733 my ($self, $player, $n, $count) = @_;
76 158733 my $i = $self->index_for($player, $n);
77 158733 ▶ $self->points->[$i] = $player eq 'white' ? $count : -$count;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 77753 80980 158733
78 158733 return $self;
79 }
80  
81 4406166 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
82 4406166 my ($self, $player, $n, $d) = @_;
83 4406166 my $i = $self->index_for($player, $n);
84 4406166 ▶ $self->points->[$i] += $player eq 'white' ? $d : -$d;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 2080447 2325719 4406166
85 4406166 return $self;
86 }
87  
88 256444 ▶sub to_bar { my ($self, $player, $d) = @_; $self->bar_counts->{$player} += $d // 1; return $self }
Subroutine to_bar: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$d // 1
l!l
97904158540
MC/DC100%1/1
$d // 1
$d
Truth table100%2/2
A
A= $d
Aresult
11
00
89 9267 ▶sub to_off { my ($self, $player, $d) = @_; $self->off_counts->{$player} += $d // 1; return $self }
Subroutine to_off: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$d // 1
l!l
199248
MC/DC100%1/1
$d // 1
$d
Truth table100%2/2
A
A= $d
Aresult
11
00
90  
91 448800 ▶sub occupied {
Subroutine occupied: called CC=3 SCAR=11.0
Pod documented
92 448800 my ($self, $player) = @_;
93 448800 my @out;
94 448800 for my $n (reverse 1 .. POINTS) {
95 10771200 my $m = $self->mine_on($player, $n);
96 10771200 ▶ push @out, [ $n, $m ] if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 2075166 8696034 10771200
97 }
98 448800 return \@out;
99 }
100  
101 2414 ▶sub checkers_on_points {
Subroutine checkers_on_points: called CC=2 SCAR=6.9
Pod documented
102 2414 my ($self, $player) = @_;
103 2414 my $n = 0;
104 2414 $n += $_->[1] for @{ $self->occupied($player) };
105 2414 return $n;
106 }
107  
108 446386 ▶sub pip_count {
Subroutine pip_count: called CC=2 SCAR=6.9
Pod documented
109 446386 my ($self, $player) = @_;
110 446386 my $pips = $self->bar($player) * BAR_PIP;
111 446386 $pips += $_->[0] * $_->[1] for @{ $self->occupied($player) };
112 446386 return $pips;
113 }
114  
115 372106 ▶sub all_home {
Subroutine all_home: called CC=4 SCAR=13.9
Pod documented
116 372106 my ($self, $player) = @_;
117 372106 ▶ return 0 if $self->bar($player);
Branch: 1 branch
BranchTrueFalseTotal
if $self->bar($player) 2 372104 372106
118 372104 for my $n (7 .. POINTS) {
119 2465830 ▶ return 0 if $self->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mine_on($player, $n) 364394 2101436 2465830
120 }
121 7710 return 1;
122 }
123  
124 7709 ▶sub highest_occupied {
Subroutine highest_occupied: called CC=3 SCAR=11.0
Pod documented
125 7709 my ($self, $player) = @_;
126 7709 for my $n (reverse 1 .. POINTS) {
127 165613 ▶ return $n if $self->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mine_on($player, $n) 7572 158041 165613
128 }
129 137 return 0;
130 }
131  
132 1206 ▶sub consistent {
Subroutine consistent: called CC=5 SCAR=16.2
Pod documented
133 1206 my ($self) = @_;
134 1206 my @wrong;
135 1206 for my $player (qw(white black)) {
136 2412 my $n = $self->checkers_on_points($player) + $self->bar($player) + $self->off($player);
137 2412 ▶ push @wrong, "$player has $n checkers, not " . CHECKERS unless $n == CHECKERS;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == 15 1 2411 2412
138 2412 ▶ push @wrong, "$player has a negative bar" if $self->bar($player) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->bar($player) < 0 0 2412 2412
139 2412 ▶ push @wrong, "$player has a negative tray" if $self->off($player) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->off($player) < 0 0 2412 2412
140 }
141 1206 return @wrong;
142 }
143  
144 30 1;
145  
146 __END__
« blib/lib/Game/Backgammon/Error.pm ↑ Summary blib/lib/Game/Backgammon/Move.pm »