blib/lib/Game/Checkers.pm

filter: Statementstmt 96.5% 252 / 261 Branchbran 83.3% 115 / 138 Conditioncond 74.3% 29 / 39 MC/DCmcdc 64.0% 16 / 25 Subroutinesub 97.2% 35 / 36 Podpod 93.7% 15 / 16 totaltotal 89.7% 462 / 515 CC 86 SCAR 20.4
CC
86
Cov
90%
_parse
14.8
_diagnose
14.4
from_pdn
12.1
CRAP 7.7
« blib/lib/Game/Checkers/Board.pm ↑ Summary blib/lib/Game/Checkers/Result.pm »
1 package Game::Checkers;
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 our $VERSION = '0.04';
7  
8 30 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use Game::Checkers::Squares;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 30 ▶use Game::Checkers::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 30 ▶use Game::Checkers::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 30 ▶use Game::Checkers::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 ▶use Game::Checkers::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 30 ▶use Game::Checkers::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 30 ▶use Game::Checkers::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 30 ▶use Game::Checkers::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 30 ▶use constant NO_PROGRESS_PLIES => 80;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 30 my $STEP = \@Game::Checkers::Squares::STEP;
22 30 my $JUMP_OVER = \@Game::Checkers::Squares::JUMP_OVER;
23 30 my $JUMP_TO = \@Game::Checkers::Squares::JUMP_TO;
24  
25 30 my %DECLARED = map { $_ => 1 } qw/resign agreement timeout/;
26  
27 30 my %FORWARD = (
28 black => {
29 Game::Checkers::Squares::SE, 1,
30 Game::Checkers::Squares::SW, 1
31 },
32 white => {
33 Game::Checkers::Squares::NE, 1,
34 Game::Checkers::Squares::NW, 1
35 },
36 );
37  
38 has board => (
39 is => 'rw',
40 isa => Object
41 30 );
42  
43 has [qw/turn variant fen/] => (
44 is => 'rw',
45 isa => Str
46 30 );
47  
48 has [qw/result draw_offered_by _legal/] => (
49 is => 'rw'
50 30 );
51  
52 has [qw/history _undo/] => (
53 is => 'rw',
54 isa => ArrayRef,
55 default => []
56 30 );
57  
58 has no_progress => (
59 is => 'rw',
60 isa => Int,
61 default => 0
62 30 );
63  
64 has repetition => (
65 is => 'rw',
66 isa => HashRef,
67 default => {}
68 30 );
69  
70 261 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.0
Pod undocumented
71 261 my ($self) = @_;
72 261 ▶ $self->variant('english') unless defined $self->variant;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->variant 237 24 261
73 261 ▶ die "variant must be english, got '" . $self->variant . "'"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->variant eq "english" 0 261 261
74 unless $self->variant eq 'english';
75  
76 261 ▶ if (!$self->board) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->board) 237 24 261
77 237 ▶ if (my $fen = $self->fen) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $fen = $self->fen) { } 67 170 237
78 67 my ($board, $turn) = Game::Checkers::Board->from_fen($fen);
79 67 $self->board($board);
80 67 $self->turn($turn);
81 } else {
82 170 $self->board(Game::Checkers::Board->new);
83 }
84 }
85 261 ▶ $self->turn('black') unless defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->turn 170 91 261
86 die "turn must be black or white, got '" . $self->turn . "'"
87 261 ▶ unless $FORWARD{$self->turn};
Branch: 1 branch
BranchTrueFalseTotal
unless $FORWARD{$self->turn} 0 261 261
88  
89 261 ▶ $self->fen($self->board->to_fen($self->turn)) unless defined $self->fen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->fen 170 91 261
90 $self->repetition->{$self->board->to_fen($self->turn)}++
91 261 ▶ unless %{$self->repetition};
Branch: 1 branch
BranchTrueFalseTotal
unless %{$self->repetition;} 261 0 261
92 261 $self->_check_end;
93 261 return $self;
94 }
95  
96 1763 ▶sub status {
Subroutine status: called CC=2 SCAR=6.9
Pod documented
97 1763 ▶ return $_[0]->result ? 'finished' : 'active';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 26 1737 1763
98 }
99  
100 7160 ▶sub ply {
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
101 7160 return scalar @{$_[0]->history};
102 }
103  
104 3215 ▶sub to_fen {
Subroutine to_fen: called CC=1 SCAR=0.0
Pod documented
105 3215 my ($self) = @_;
106 3215 return $self->board->to_fen($self->turn);
107 }
108  
109 6599 ▶sub legal_moves {
Subroutine legal_moves: called CC=3 SCAR=11.0
Pod documented
110 6599 my ($self) = @_;
111 6599 my $legal = $self->_legal;
112 6599 ▶ return $legal if $legal;
Branch: 1 branch
BranchTrueFalseTotal
if $legal 3396 3203 6599
113 3203 $legal = [];
114 3203 ▶ unless ($self->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->result) 3203 0 3203
115 3203 my $turn = $self->turn;
116 $legal = [
117 22004 map { Game::Checkers::Move->from_raw($_, $turn) }
118 3203 @{Game::Checkers::Rules::generate($self->board->position, $turn)}
119 ];
120 }
121 3203 $self->_legal($legal);
122 3203 return $legal;
123 }
124  
125 0 ▶sub legal_moves_for {
Subroutine legal_moves_for: not called CC=1 SCAR=6.9
Pod documented
126 0 my ($self, $square) = @_;
127 0 return [grep { $_->from == $square } @{$self->legal_moves}];
128 }
129  
130 106 ▶sub must_capture {
Subroutine must_capture: called CC=3 SCAR=11.0
Pod documented
131 106 my $legal = $_[0]->legal_moves;
132 106 ▶ return @{$legal} && $legal->[0]->is_jump ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@{$legal;} && $legal->[0]->is_jump ? : 24 82 106
Condition66%2/3
@{$legal;} and $legal->[0]->is_jump
!ll&&!rl&&r
08224
MC/DC50%1/2
@{$legal;} and $legal->[0]->is_jump
@{$legal;} $legal->[0]->is_jump
Truth table66%2/3
A and B
A= @{$legal;}B= $legal->[0]->is_jump
ABresult
0X0
100
111
133 }
134  
135 3130 ▶sub move {
Subroutine move: called CC=10 SCAR=23.0
Pod documented
136 3130 my ($self, $move) = @_;
137 3130 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 2 3128 3130
138  
139 3128 ▶ my $parsed = $self->_parse($move)
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = $self->_parse($move) or return $self->_error("not_a_move") 3122 6 3128
140 or return $self->_error('not_a_move');
141  
142 3122 my ($from, $to) = @{$parsed}{qw/from to/};
143 3122 my $value = $self->board->at($from);
144 3122 ▶ return $self->_error('not_your_piece')
Branch: 2 branches
BranchTrueFalseTotal
unless $value and ($value > 0 ? "black" : "white") eq $self->turn 5 3117 3122
$value > 0 ? : 1596 1524 3120
Condition100%3/3
$value and ($value > 0 ? "black" : "white") eq $self->turn
!ll&&!rl&&r
233117
MC/DC100%2/2
$value and ($value > 0 ? "black" : "white") eq $self->turn
$value ($value > 0 ? "black" : "white") eq $self->turn
Truth table100%3/3
A and B
A= $valueB= ($value > 0 ? "black" : "white") eq $self->turn
ABresult
0X0
100
111
145 unless $value && ($value > 0 ? 'black' : 'white') eq $self->turn;
146  
147 3117 ▶ my @match = grep { $_->from == $from && $_->to == $to } @{$self->legal_moves};
Condition100%3/3
$_->from == $from && $_->to == $to
!ll&&!rl&&r
1531931153102
MC/DC100%2/2
$_->from == $from && $_->to == $to
$_->from == $from $_->to == $to
Truth table100%3/3
A && B
A= $_->from == $fromB= $_->to == $to
ABresult
0X0
100
111
148 3117 ▶ if (@{$parsed->{squares}} > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$parsed->{"squares"};} > 2) 33 3084 3117
149 33 my $path = join '.', @{$parsed->{squares}};
150 33 @match = grep { join('.', @{$_->path}) eq $path } @match;
151 }
152 3117 ▶ return $self->_error('ambiguous', legal => \@match) if @match > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @match > 1 2 3115 3117
153 3115 ▶ return $self->_diagnose($parsed) unless @match;
Branch: 1 branch
BranchTrueFalseTotal
unless @match 18 3097 3115
154 3097 return $self->_play($match[0]);
155 }
156  
157 37 ▶sub undo {
Subroutine undo: called CC=4 SCAR=13.9
Pod documented
158 37 my ($self) = @_;
159 37 ▶ my $record = pop @{$self->_undo}
Branch: 1 branch
BranchTrueFalseTotal
my $record = pop @{$self->_undo;} or return $self->_error("nothing_to_undo") 36 1 37
160 or return $self->_error('nothing_to_undo');
161 36 my $move = pop @{$self->history};
162  
163 36 my $fen = $self->to_fen;
164 delete $self->repetition->{$fen}
165 36 ▶ unless --$self->repetition->{$fen};
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->repetition->{$fen} 36 0 36
166  
167 36 Game::Checkers::Rules::unapply($self->board->position, $record->{raw});
168 36 ▶ $self->turn($self->turn eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$self->turn eq 'black' ? : 16 20 36
169 36 $self->no_progress($record->{no_progress});
170 36 $self->draw_offered_by($record->{draw_offered_by});
171 36 $self->result(undef);
172 36 $self->_legal(undef);
173 36 return $move;
174 }
175  
176 5 ▶sub resign {
Subroutine resign: called CC=3 SCAR=11.4
Pod documented
177 5 my ($self, $side) = @_;
178 5 $side = $self->_side($side);
179 5 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 5 5
180 5 ▶ return $self->_finish($side eq 'black' ? 'white' : 'black', 'resign');
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 5 0 5
181 }
182  
183 5 ▶sub offer_draw {
Subroutine offer_draw: called CC=2 SCAR=7.0
Pod documented
184 5 my ($self, $side) = @_;
185 5 $side = $self->_side($side);
186 5 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 5 5
187 5 $self->draw_offered_by($side);
188 5 return $side;
189 }
190  
191 6 ▶sub accept_draw {
Subroutine accept_draw: called CC=4 SCAR=13.9
Pod documented
192 6 my ($self, $side) = @_;
193 6 $side = $self->_side($side);
194 6 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 6 6
195 6 my $offered = $self->draw_offered_by;
196 6 ▶ return $self->_error('no_offer') unless $offered && $offered ne $side;
Branch: 1 branch
BranchTrueFalseTotal
unless $offered and $offered ne $side 4 2 6
Condition100%3/3
$offered and $offered ne $side
!ll&&!rl&&r
312
MC/DC100%2/2
$offered and $offered ne $side
$offered $offered ne $side
Truth table100%3/3
A and B
A= $offeredB= $offered ne $side
ABresult
0X0
100
111
197 2 return $self->_finish(undef, 'agreement');
198 }
199  
200 1 ▶sub decline_draw {
Subroutine decline_draw: called CC=3 SCAR=13.5
Pod documented
201 1 my ($self, $side) = @_;
202 1 $side = $self->_side($side);
203 1 my $offered = $self->draw_offered_by;
204 1 ▶ return $self->_error('no_offer') unless $offered && $offered ne $side;
Branch: 1 branch
BranchTrueFalseTotal
unless $offered and $offered ne $side 1 0 1
Condition33%1/3
$offered and $offered ne $side
!ll&&!rl&&r
100
MC/DC0%0/2
$offered and $offered ne $side
$offered $offered ne $side
Truth table33%1/3
A and B
A= $offeredB= $offered ne $side
ABresult
0X0
100
111
205 0 $self->draw_offered_by(undef);
206 0 return $side;
207 }
208  
209 210 ▶sub to_pdn {
Subroutine to_pdn: called CC=6 SCAR=17.9
Pod documented
210 210 my ($self, %tags) = @_;
211 210 my $opening = Game::Checkers::Board->new->to_fen('black');
212 210 ▶ if ($self->fen ne $opening) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->fen ne $opening) 13 197 210
213 13 $tags{FEN} = $self->fen;
214 13 $tags{SetUp} = '1';
215 }
216 210 my $result = $self->result;
217 210 ▶ $tags{Ending} = $result->reason if $result && $DECLARED{$result->reason};
Branch: 1 branch
BranchTrueFalseTotal
if $result and $DECLARED{$result->reason} 9 201 210
Condition100%3/3
$result and $DECLARED{$result->reason}
!ll&&!rl&&r
191109
MC/DC100%2/2
$result and $DECLARED{$result->reason}
$result $DECLARED{$result->reason}
Truth table100%3/3
A and B
A= $resultB= $DECLARED{$result->reason}
ABresult
0X0
100
111
218 210 ▶ $tags{DrawOffer} = $self->draw_offered_by if defined $self->draw_offered_by;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->draw_offered_by 9 201 210
219 return Game::Checkers::Notation::format_pdn({
220 tags => \%tags,
221 210 ▶ moves => [map { $_->notation } @{$self->history}],
Branch: 1 branch
BranchTrueFalseTotal
$self->result ? : 19 191 210
222 result => $self->result ? $self->result->pdn : '*'
223 });
224 }
225  
226 49 ▶sub from_pdn {
Subroutine from_pdn: called CC=12 SCAR=25.0
Pod documented
227 49 my ($class, $text) = @_;
228 49 my $parsed = Game::Checkers::Notation::parse_pdn($text);
229 my $self = $class->new(
230 48 ▶ $parsed->{tags}{FEN} ? (fen => $parsed->{tags}{FEN}) : ()
Branch: 1 branch
BranchTrueFalseTotal
$parsed->{'tags'}{'FEN'} ? : 4 44 48
231 );
232 48 my $number = 0;
233 48 for my $notation (@{$parsed->{moves}}) {
234 1189 $number++;
235 1189 my $move = $self->move($notation);
236 1189 ▶ next unless ref $move eq 'Game::Checkers::Error';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "Game::Checkers::Error" 1188 1 1189
237 1 die "illegal PDN: move $number, '$notation': " . $move->message . "\n";
238 }
239 47 ▶ if (defined(my $offer = $parsed->{tags}{DrawOffer})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $offer = $parsed->{"tags"}{"DrawOffer"})) 7 40 47
240 die "illegal PDN: the DrawOffer tag must be black or white, got '$offer'\n"
241 7 ▶ unless $FORWARD{$offer};
Branch: 1 branch
BranchTrueFalseTotal
unless $FORWARD{$offer} 1 6 7
242 6 $self->draw_offered_by($offer);
243 }
244 46 ▶ if (defined(my $reason = $parsed->{tags}{Ending})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $reason = $parsed->{"tags"}{"Ending"})) 7 39 46
245 die "illegal PDN: the Ending tag must be one of "
246 . join(', ', sort keys %DECLARED) . ", got '$reason'\n"
247 7 ▶ unless $DECLARED{$reason};
Branch: 1 branch
BranchTrueFalseTotal
unless $DECLARED{$reason} 1 6 7
248 6 ▶ die "illegal PDN: the Ending tag says '$reason', but the moves already end the game\n"
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 6 6
249 if $self->result;
250 6 my %winner = ('1-0' => 'white', '0-1' => 'black', '1/2-1/2' => undef);
251 6 my $result = $parsed->{result};
252 die "illegal PDN: the Ending tag says '$reason', but the result is '$result'\n"
253 unless exists $winner{$result}
254 6 ▶ && ($reason eq 'agreement' ? !defined $winner{$result} : defined $winner{$result});
Branch: 2 branches
BranchTrueFalseTotal
unless exists $winner{$result} and $reason eq "agreement" ? !defined($winner{$result}) : defined $winner{$result} 0 6 6
$reason eq 'agreement' ? : 3 3 6
Condition33%1/3
exists $winner{$result} and $reason eq "agreement" ? !defined($winner{$result}) : defined $winner{$result}
!ll&&!rl&&r
006
MC/DC0%0/2
exists $winner{$result} and $reason eq "agreement" ? !defined($winner{$result}) : defined $winner{$result}
exists $winner{$result} $reason eq "agreement" ? !defined($winner{$result}) : defined $winner{$result}
Truth table33%1/3
A and B
A= exists $winner{$result}B= $reason eq "agreement" ? !defined($winner{$result}) : defined $winner{$result}
ABresult
0X0
100
111
255 6 $self->_finish($winner{$result}, $reason);
256 }
257 45 return $self;
258 }
259  
260 24 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
261 24 my ($self) = @_;
262 24 my $clone = ref($self)->new(
263 variant => $self->variant,
264 fen => $self->fen,
265 board => $self->board->clone,
266 turn => $self->turn
267 );
268 24 $clone->history([@{$self->history}]);
269 24 $clone->_undo([@{$self->_undo}]);
270 24 $clone->no_progress($self->no_progress);
271 24 $clone->repetition({%{$self->repetition}});
272 24 $clone->draw_offered_by($self->draw_offered_by);
273 24 $clone->result($self->result);
274 24 $clone->_legal(undef);
275 24 return $clone;
276 }
277  
278 17 ▶sub _side {
Subroutine _side: called CC=3 SCAR=11.4
279 17 my ($self, $side) = @_;
280 17 ▶ $side = $self->turn unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 0 17 17
281 17 ▶ die "side must be black or white, got '$side'" unless $FORWARD{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless $FORWARD{$side} 0 17 17
282 17 return $side;
283 }
284  
285 39 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
286 39 my ($self, $flag, %extra) = @_;
287 39 return Game::Checkers::Error->throw(
288 $flag,
289 legal => $self->legal_moves,
290 %extra
291 );
292 }
293  
294 3128 ▶sub _parse {
Subroutine _parse: called CC=12 SCAR=27.0
295 3128 my ($self, $move) = @_;
296 3128 ▶ if (ref $move eq 'Game::Checkers::Move') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "Game::Checkers::Move") 1834 1294 3128
297 return {
298 from => $move->from,
299 to => $move->to,
300 1834 squares => [@{$move->path}],
301 jump => $move->is_jump
302 };
303 }
304 1294 ▶ if (ref $move eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "HASH") 2 1292 1294
305 0 ▶ my @squares = $move->{squares} ? @{$move->{squares}}
Branch: 1 branch
BranchTrueFalseTotal
$move->{'path'} ? : 0 2 2
306 0 : $move->{path} ? @{$move->{path}}
307 2 ▶ : grep { defined } $move->{from}, $move->{to};
Branch: 1 branch
BranchTrueFalseTotal
$move->{'squares'} ? : 0 2 2
308 2 ▶ return undef unless @squares > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @squares > 1 1 1 2
309 1 for my $square (@squares) {
310 2 ▶ return undef unless $square =~ m/^[0-9]+$/ && $square >= 1 && $square <= 32;
Branch: 1 branch
BranchTrueFalseTotal
unless $square =~ /^[0-9]+$/ and $square >= 1 and $square <= 32 0 2 2
Condition33%2/6
$square =~ /^[0-9]+$/ and $square >= 1 and $square <= 32
!ll&&!rl&&r
002
$square =~ /^[0-9]+$/ and $square >= 1
!ll&&!rl&&r
002
MC/DC0%0/3
$square =~ /^[0-9]+$/ and $square >= 1 and $square <= 32
$square =~ /^[0-9]+$/ $square >= 1 $square <= 32
Truth table25%1/4
A and B and C
A= $square =~ /^[0-9]+$/B= $square >= 1C= $square <= 32
ABCresult
0XX0
10X0
1100
1111
311 }
312 return {
313 from => $squares[0],
314 to => $squares[-1],
315 squares => \@squares,
316 jump => @squares > 2 ? 1 : $move->{jump}
317 1 ▶ };
Branch: 1 branch
BranchTrueFalseTotal
@squares > 2 ? : 0 1 1
318 }
319 1292 ▶ return undef if ref $move;
Branch: 1 branch
BranchTrueFalseTotal
if ref $move 1 1291 1292
320 1291 return Game::Checkers::Notation::parse_move($move);
321 }
322  
323 18 ▶sub _diagnose {
Subroutine _diagnose: called CC=14 SCAR=26.7
324 18 my ($self, $parsed) = @_;
325 18 my ($from, $to) = @{$parsed}{qw/from to/};
326 18 my $position = $self->board->position;
327 18 my $value = $position->[$from];
328 18 ▶ my $king = abs($value) == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 1 17 18
329 18 my $base = $from * 4;
330  
331 18 for my $dir (@Game::Checkers::Squares::DIRS) {
332 56 ▶ my $forward = $king || $FORWARD{$self->turn}{$dir};
Condition100%3/3
$king || $FORWARD{$self->turn}{$dir}
l!l&&r!l&&!r
32429
MC/DC100%2/2
$king || $FORWARD{$self->turn}{$dir}
$king $FORWARD{$self->turn}{$dir}
Truth table100%3/3
A || B
A= $kingB= $FORWARD{$self->turn}{$dir}
ABresult
1X1
011
000
333 56 ▶ if ($STEP->[$base + $dir] && $STEP->[$base + $dir] == $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$STEP[$base + $dir] and $$STEP[$base + $dir] == $to) 8 48 56
Condition66%2/3
$$STEP[$base + $dir] and $$STEP[$base + $dir] == $to
!ll&&!rl&&r
0488
MC/DC50%1/2
$$STEP[$base + $dir] and $$STEP[$base + $dir] == $to
$$STEP[$base + $dir] $$STEP[$base + $dir] == $to
Truth table66%2/3
A and B
A= $$STEP[$base + $dir]B= $$STEP[$base + $dir] == $to
ABresult
0X0
100
111
334 8 ▶ return $self->_error('occupied') if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 3 5 8
335 5 ▶ return $self->_error('wrong_direction') unless $forward;
Branch: 1 branch
BranchTrueFalseTotal
unless $forward 3 2 5
336 2 ▶ return $self->_error('must_capture') if $self->must_capture;
Branch: 1 branch
BranchTrueFalseTotal
if $self->must_capture 2 0 2
337 0 return $self->_error('not_legal');
338 }
339 48 ▶ if ($JUMP_TO->[$base + $dir] && $JUMP_TO->[$base + $dir] == $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$JUMP_TO[$base + $dir] and $$JUMP_TO[$base + $dir] == $to) 7 41 48
Condition100%3/3
$$JUMP_TO[$base + $dir] and $$JUMP_TO[$base + $dir] == $to
!ll&&!rl&&r
2397
MC/DC100%2/2
$$JUMP_TO[$base + $dir] and $$JUMP_TO[$base + $dir] == $to
$$JUMP_TO[$base + $dir] $$JUMP_TO[$base + $dir] == $to
Truth table100%3/3
A and B
A= $$JUMP_TO[$base + $dir]B= $$JUMP_TO[$base + $dir] == $to
ABresult
0X0
100
111
340 7 ▶ return $self->_error('occupied') if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 0 7 7
341 7 ▶ return $self->_error('wrong_direction') unless $forward;
Branch: 1 branch
BranchTrueFalseTotal
unless $forward 0 7 7
342 7 return $self->_error('not_legal');
343 }
344 }
345 3 ▶ return $self->_error('must_capture') if $self->must_capture;
Branch: 1 branch
BranchTrueFalseTotal
if $self->must_capture 0 3 3
346 3 return $self->_error('not_legal');
347 }
348  
349 3097 ▶sub _play {
Subroutine _play: called CC=8 SCAR=20.8
350 3097 my ($self, $move) = @_;
351 3097 my $raw = $move->to_raw;
352 3097 my $mover = $self->turn;
353  
354 3097 push @{$self->_undo}, {
355 raw => $raw,
356 no_progress => $self->no_progress,
357 draw_offered_by => $self->draw_offered_by
358 };
359 3097 push @{$self->history}, $move;
360  
361 3097 Game::Checkers::Rules::apply($self->board->position, $raw);
362 3097 $self->_legal(undef);
363  
364 # an offer stands until the other side answers it with a move
365 3097 my $offered = $self->draw_offered_by;
366 3097 ▶ $self->draw_offered_by(undef) if $offered && $offered ne $mover;
Branch: 1 branch
BranchTrueFalseTotal
if $offered and $offered ne $mover 3 3094 3097
Condition100%3/3
$offered and $offered ne $mover
!ll&&!rl&&r
309043
MC/DC100%2/2
$offered and $offered ne $mover
$offered $offered ne $mover
Truth table100%3/3
A and B
A= $offeredB= $offered ne $mover
ABresult
0X0
100
111
367  
368 3097 ▶ $self->no_progress(
Branch: 1 branch
BranchTrueFalseTotal
Game::Checkers::Rules::progress($raw) ? : 1922 1175 3097
369 Game::Checkers::Rules::progress($raw) ? 0 : $self->no_progress + 1
370 );
371 3097 ▶ $self->turn($mover eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$mover eq 'black' ? : 1579 1518 3097
372  
373 3097 my $count = ++$self->repetition->{$self->to_fen};
374  
375 # a player with no move has lost, which beats either drawing counter
376 3097 $self->_check_end;
377 3097 ▶ return $move if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 20 3077 3097
378  
379 3077 ▶ if ($count >= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count >= 3) { } 2 3075 3077
elsif ($self->no_progress >= 80) { } 5 3070 3075
380 2 $self->_finish(undef, 'repetition');
381 } elsif ($self->no_progress >= NO_PROGRESS_PLIES) {
382 5 $self->_finish(undef, 'no_progress');
383 }
384 3077 return $move;
385 }
386  
387 3358 ▶sub _check_end {
Subroutine _check_end: called CC=4 SCAR=13.9
388 3358 my ($self) = @_;
389 3358 ▶ return if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 3358 3358
390 3358 ▶ return if Game::Checkers::Rules::has_move($self->board->position, $self->turn);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Checkers::Rules::has_move($self->board->position, $self->turn) 3334 24 3358
391 24 ▶ return $self->_finish($self->turn eq 'black' ? 'white' : 'black', 'no_moves');
Branch: 1 branch
BranchTrueFalseTotal
$self->turn eq 'black' ? : 5 19 24
392 }
393  
394 44 ▶sub _finish {
Subroutine _finish: called CC=1 SCAR=0.0
395 44 my ($self, $winner, $reason) = @_;
396 44 $self->result(Game::Checkers::Result->new(
397 winner => $winner,
398 reason => $reason
399 ));
400 44 $self->_legal([]);
401 44 return $self->result;
402 }
403  
404 30 1;
405  
406 __END__
« blib/lib/Game/Checkers/Board.pm ↑ Summary blib/lib/Game/Checkers/Result.pm »