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
« Backgammon/Error.pm ↑ Summary Backgammon/Move.pm »
1 package Game::Backgammon::Board;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 12 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 12 our $VERSION = '0.04';
9  
10 12 ▶use constant POINTS => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use constant CHECKERS => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use constant BAR_PIP => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 189 ▶sub opening {
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
15 189 my @p = (0) x POINTS;
16 189 $p[23] = 2; $p[12] = 5; $p[7] = 3; $p[5] = 5;
17 189 $p[0] = -2; $p[11] = -5; $p[16] = -3; $p[18] = -5;
18 189 return \@p;
19 }
20  
21 has points => (
22 is => 'ro',
23 isa => ArrayRef,
24 189 ▶ default => sub { opening() }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 12 );
26  
27 has bar_counts => (
28 is => 'ro',
29 isa => HashRef,
30 219 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
31 12 );
32  
33 has off_counts => (
34 is => 'ro',
35 isa => HashRef,
36 219 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 12 );
38  
39 2245681 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
40 2245681 my ($self) = @_;
41 return ref($self)->new(
42 2245681 points => [ @{ $self->points } ],
43 2245681 bar_counts => { %{ $self->bar_counts } },
44 2245681 off_counts => { %{ $self->off_counts } }
45 );
46 }
47  
48 2514284 ▶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' ? : 1200991 1313293 2514284
49  
50 36333546 ▶sub index_for {
Subroutine index_for: called CC=6 SCAR=22.0
Pod documented
51 36333546 my ($self, $player, $n) = @_;
52 36333546 ▶ 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 36333546 36333546
Condition33%3/9
defined $n and $n =~ /\A\d+\z/ and $n >= 1 and $n <= 24
!ll&&!rl&&r
0036333546
defined $n and $n =~ /\A\d+\z/ and $n >= 1
!ll&&!rl&&r
0036333546
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
0036333546
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 36333546 ▶ return $player eq 'white' ? $n - 1 : POINTS - $n;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 18826045 17507501 36333546
55 }
56  
57 31790942 ▶sub point_for {
Subroutine point_for: called CC=3 SCAR=11.0
Pod documented
58 31790942 my ($self, $player, $n) = @_;
59 31790942 my $v = $self->points->[ $self->index_for($player, $n) ];
60 31790942 ▶ my $mine = $player eq 'white' ? $v : -$v;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 16676211 15114731 31790942
61 31790942 ▶ return $mine >= 0 ? ($mine, 0) : (0, -$mine);
Branch: 1 branch
BranchTrueFalseTotal
$mine >= 0 ? : 26636389 5154553 31790942
62 }
63  
64 24305415 ▶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 6233326 ▶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 3870429 ▶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 ? : 272146 3598283 3870429
68  
69 1729417 ▶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 ? : 95421 1633996 1729417
70  
71 1821741 ▶sub bar { my ($self, $player) = @_; return $self->bar_counts->{$player} }
Subroutine bar: called CC=1 SCAR=0.0
Pod documented
72 565678 ▶sub off { my ($self, $player) = @_; return $self->off_counts->{$player} }
Subroutine off: called CC=1 SCAR=0.0
Pod documented
73  
74 158281 ▶sub set_point {
Subroutine set_point: called CC=2 SCAR=6.9
Pod documented
75 158281 my ($self, $player, $n, $count) = @_;
76 158281 my $i = $self->index_for($player, $n);
77 158281 ▶ $self->points->[$i] = $player eq 'white' ? $count : -$count;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 77371 80910 158281
78 158281 return $self;
79 }
80  
81 4384319 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
82 4384319 my ($self, $player, $n, $d) = @_;
83 4384319 my $i = $self->index_for($player, $n);
84 4384319 ▶ $self->points->[$i] += $player eq 'white' ? $d : -$d;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 2072461 2311858 4384319
85 4384319 return $self;
86 }
87  
88 255893 ▶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
97805158088
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 447844 ▶sub occupied {
Subroutine occupied: called CC=3 SCAR=11.0
Pod documented
92 447844 my ($self, $player) = @_;
93 447844 my @out;
94 447844 for my $n (reverse 1 .. POINTS) {
95 10748256 my $m = $self->mine_on($player, $n);
96 10748256 ▶ push @out, [ $n, $m ] if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 2069747 8678509 10748256
97 }
98 447844 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 445430 ▶sub pip_count {
Subroutine pip_count: called CC=2 SCAR=6.9
Pod documented
109 445430 my ($self, $player) = @_;
110 445430 my $pips = $self->bar($player) * BAR_PIP;
111 445430 $pips += $_->[0] * $_->[1] for @{ $self->occupied($player) };
112 445430 return $pips;
113 }
114  
115 370172 ▶sub all_home {
Subroutine all_home: called CC=4 SCAR=13.9
Pod documented
116 370172 my ($self, $player) = @_;
117 370172 ▶ return 0 if $self->bar($player);
Branch: 1 branch
BranchTrueFalseTotal
if $self->bar($player) 2 370170 370172
118 370170 for my $n (7 .. POINTS) {
119 2462024 ▶ return 0 if $self->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mine_on($player, $n) 362460 2099564 2462024
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 12 1;
145  
146 __END__
« Backgammon/Error.pm ↑ Summary Backgammon/Move.pm »