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.pm ↑ Summary Backgammon/Move.pm »
1 package Game::Backgammon::Board;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our $VERSION = '0.03';
9  
10 9 ▶use constant POINTS => 24;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use constant CHECKERS => 15;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use constant BAR_PIP => 25;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 99 ▶sub opening {
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
15 99 my @p = (0) x POINTS;
16 99 $p[23] = 2; $p[12] = 5; $p[7] = 3; $p[5] = 5;
17 99 $p[0] = -2; $p[11] = -5; $p[16] = -3; $p[18] = -5;
18 99 return \@p;
19 }
20  
21 has points => (
22 is => 'ro',
23 isa => ArrayRef,
24 99 ▶ default => sub { opening() }
Subroutine __ANON__: called CC=1 SCAR=0.0
25 9 );
26  
27 has bar_counts => (
28 is => 'ro',
29 isa => HashRef,
30 129 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
31 9 );
32  
33 has off_counts => (
34 is => 'ro',
35 isa => HashRef,
36 129 ▶ default => sub { { white => 0, black => 0 } }
Subroutine __ANON__: called CC=1 SCAR=0.0
37 9 );
38  
39 929290 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
40 929290 my ($self) = @_;
41 return ref($self)->new(
42 929290 points => [ @{ $self->points } ],
43 929290 bar_counts => { %{ $self->bar_counts } },
44 929290 off_counts => { %{ $self->off_counts } }
45 );
46 }
47  
48 1004217 ▶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' ? : 447614 556603 1004217
49  
50 14148464 ▶sub index_for {
Subroutine index_for: called CC=6 SCAR=22.0
Pod documented
51 14148464 my ($self, $player, $n) = @_;
52 14148464 ▶ 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 14148464 14148464
Condition33%3/9
defined $n and $n =~ /\A\d+\z/ and $n >= 1 and $n <= 24
!ll&&!rl&&r
0014148464
defined $n and $n =~ /\A\d+\z/ and $n >= 1
!ll&&!rl&&r
0014148464
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
0014148464
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 14148464 ▶ return $player eq 'white' ? $n - 1 : POINTS - $n;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 7654565 6493899 14148464
55 }
56  
57 12241183 ▶sub point_for {
Subroutine point_for: called CC=3 SCAR=11.0
Pod documented
58 12241183 my ($self, $player, $n) = @_;
59 12241183 my $v = $self->points->[ $self->index_for($player, $n) ];
60 12241183 ▶ my $mine = $player eq 'white' ? $v : -$v;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 6826266 5414917 12241183
61 12241183 ▶ return $mine >= 0 ? ($mine, 0) : (0, -$mine);
Branch: 1 branch
BranchTrueFalseTotal
$mine >= 0 ? : 9886211 2354972 12241183
62 }
63  
64 9034609 ▶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 3180053 ▶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 1698831 ▶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 ? : 128538 1570293 1698831
68  
69 872635 ▶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 ? : 93919 778716 872635
70  
71 597102 ▶sub bar { my ($self, $player) = @_; return $self->bar_counts->{$player} }
Subroutine bar: called CC=1 SCAR=0.0
Pod documented
72 117415 ▶sub off { my ($self, $player) = @_; return $self->off_counts->{$player} }
Subroutine off: called CC=1 SCAR=0.0
Pod documented
73  
74 69431 ▶sub set_point {
Subroutine set_point: called CC=2 SCAR=6.9
Pod documented
75 69431 my ($self, $player, $n, $count) = @_;
76 69431 my $i = $self->index_for($player, $n);
77 69431 ▶ $self->points->[$i] = $player eq 'white' ? $count : -$count;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 30772 38659 69431
78 69431 return $self;
79 }
80  
81 1837846 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
82 1837846 my ($self, $player, $n, $d) = @_;
83 1837846 my $i = $self->index_for($player, $n);
84 1837846 ▶ $self->points->[$i] += $player eq 'white' ? $d : -$d;
Branch: 1 branch
BranchTrueFalseTotal
$player eq 'white' ? : 797525 1040321 1837846
85 1837846 return $self;
86 }
87  
88 80807 ▶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
1156969238
MC/DC100%1/1
$d // 1
$d
Truth table100%2/2
A
A= $d
Aresult
11
00
89 9194 ▶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
199175
MC/DC100%1/1
$d // 1
$d
Truth table100%2/2
A
A= $d
Aresult
11
00
90  
91 103394 ▶sub occupied {
Subroutine occupied: called CC=3 SCAR=11.0
Pod documented
92 103394 my ($self, $player) = @_;
93 103394 my @out;
94 103394 for my $n (reverse 1 .. POINTS) {
95 2481456 my $m = $self->mine_on($player, $n);
96 2481456 ▶ push @out, [ $n, $m ] if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 595635 1885821 2481456
97 }
98 103394 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 100980 ▶sub pip_count {
Subroutine pip_count: called CC=2 SCAR=6.9
Pod documented
109 100980 my ($self, $player) = @_;
110 100980 my $pips = $self->bar($player) * BAR_PIP;
111 100980 $pips += $_->[0] * $_->[1] for @{ $self->occupied($player) };
112 100980 return $pips;
113 }
114  
115 160856 ▶sub all_home {
Subroutine all_home: called CC=4 SCAR=13.9
Pod documented
116 160856 my ($self, $player) = @_;
117 160856 ▶ return 0 if $self->bar($player);
Branch: 1 branch
BranchTrueFalseTotal
if $self->bar($player) 2 160854 160856
118 160854 for my $n (7 .. POINTS) {
119 560202 ▶ return 0 if $self->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mine_on($player, $n) 153013 407189 560202
120 }
121 7841 return 1;
122 }
123  
124 7840 ▶sub highest_occupied {
Subroutine highest_occupied: called CC=3 SCAR=11.0
Pod documented
125 7840 my ($self, $player) = @_;
126 7840 for my $n (reverse 1 .. POINTS) {
127 166795 ▶ return $n if $self->mine_on($player, $n);
Branch: 1 branch
BranchTrueFalseTotal
if $self->mine_on($player, $n) 7716 159079 166795
128 }
129 124 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 9 1;
145  
146 __END__
« Backgammon.pm ↑ Summary Backgammon/Move.pm »