blib/lib/Game/Fanorona.pm

filter: Statementstmt 100% 329 / 329 Branchbran 94.2% 164 / 174 Conditioncond 77.2% 34 / 44 MC/DCmcdc 62.9% 17 / 27 Subroutinesub 100% 62 / 62 Podpod 97.5% 39 / 40 totaltotal 95.4% 645 / 676 CC 111 SCAR 17.0
CC
111
Cov
96%
BUILD
14.0
continuations
12.0
__ANON__
10.1
CRAP 5.4
« blib/lib/Game/Fanorona/Result.pm ↑ Summary blib/lib/Game/Fanorona/Engine.pm »
1 package Game::Fanorona;
2  
3 25 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 25 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 25 ▶use Game::Fanorona::Engine qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
11 WHITE BLACK PAIKA APPROACH WITHDRAWAL
12 WHY_OFF_BOARD WHY_BAD_STEP WHY_NO_STONE WHY_NOT_YOURS WHY_NO_LINE WHY_OCCUPIED
13 WHY_TAKES_NOTHING WHY_NOT_A_PAIKA WHY_MUST_CAPTURE
14 WHY_NOT_THE_STONE WHY_SAME_DIRECTION WHY_REVISIT WHY_PAIKA_IN_CHAIN
15 25 );
16 25 ▶use Game::Fanorona::Rules qw(PLAY_OVER);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 25 ▶use Game::Fanorona::Move ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 25 ▶use Game::Fanorona::Variant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 25 ▶use Game::Fanorona::Error ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 25 ▶use Game::Fanorona::Result ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 25 ▶use Game::Fanorona::Notation qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
22 parse_point parse_move format_move name_of square_of
23 format_position format_game parse_game
24 25 );
25  
26 25 our $VERSION = '0.02';
27  
28 25 my @WORD = (undef, 'white', 'black');
29 25 my %SIDE = (white => WHITE, black => BLACK);
30 25 my %HOW = (PAIKA, 'paika', APPROACH, 'approach', WITHDRAWAL, 'withdrawal');
31 25 my %LETTER = (PAIKA, '', APPROACH, 'A', WITHDRAWAL, 'W');
32  
33 25 my %FROM_WHY = (
34 WHY_OFF_BOARD, 'off_board',
35 WHY_BAD_STEP, 'bad_move',
36 WHY_NO_STONE, 'no_stone',
37 WHY_NOT_YOURS, 'not_your_stone',
38 WHY_NO_LINE, 'no_line',
39 WHY_OCCUPIED, 'point_taken',
40 WHY_TAKES_NOTHING, 'takes_nothing',
41 WHY_NOT_A_PAIKA, 'bad_move',
42 WHY_MUST_CAPTURE, 'must_capture',
43 WHY_NOT_THE_STONE, 'not_the_stone',
44 WHY_SAME_DIRECTION, 'same_direction',
45 WHY_REVISIT, 'point_revisited',
46 WHY_PAIKA_IN_CHAIN, 'paika_in_chain',
47 );
48  
49 25 my %FROM_NOTATION = map { $_ => $_ } qw(off_board not_adjacent no_line paika_in_chain);
50  
51 25 has _variant_given => (is => 'ro', init_arg => 'variant', private => 1);
52  
53 25 has _start_given => (is => 'ro', init_arg => 'position', private => 1);
54  
55 25 has _first_given => (is => 'ro', init_arg => 'first', private => 1);
56  
57 25 has _script_given => (is => 'ro', init_arg => 'script', private => 1);
58  
59 25 has _variant => (is => 'rw', private => 1);
60  
61 25 has _rules => (is => 'rw', private => 1);
62  
63 966 ▶has _log => (is => 'ro', default => sub { [] }, private => 1);
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 25 has _pending => (is => 'rw', private => 1);
66  
67 25 has _ended => (is => 'rw', private => 1);
68  
69 25 has _offer => (is => 'rw', private => 1);
70  
71 25 has _error => (is => 'rw', private => 1);
72  
73 966 ▶sub BUILD {
Subroutine BUILD: called CC=14 SCAR=26.4
Pod undocumented
74 966 my ($self) = @_;
75  
76 966 my $first = $self->_first_given;
77 966 ▶ Carp::croak("Game::Fanorona: White moves first; first => '$first' is not a game of fanorona")
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and $first ne "white" 1 965 966
Condition100%3/3
defined $first and $first ne "white"
!ll&&!rl&&r
96411
MC/DC100%2/2
defined $first and $first ne "white"
defined $first $first ne "white"
Truth table100%3/3
A and B
A= defined $firstB= $first ne "white"
ABresult
0X0
100
111
78 if defined $first && $first ne 'white';
79  
80 965 my $given = $self->_variant_given;
81 965 my $variant;
82 965 ▶ if (!defined $given) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $given) { } 392 573 965
elsif (ref $given eq 'Game::Fanorona::Variant') { } 218 355 573
elsif (ref $given eq 'HASH') { } 331 24 355
elsif (not ref $given) { } 23 1 24
83 392 $variant = Game::Fanorona::Variant->named;
84 }
85 elsif (ref $given eq 'Game::Fanorona::Variant') {
86 218 $variant = $given;
87 }
88 elsif (ref $given eq 'HASH') {
89 331 $variant = Game::Fanorona::Variant->custom(%$given);
90 }
91 elsif (!ref $given) {
92 23 $variant = Game::Fanorona::Variant->from_string($given);
93 23 ▶ Carp::croak("Game::Fanorona: '$given' is not a variant") unless $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless $variant 4 19 23
94 }
95 else {
96 1 Carp::croak('Game::Fanorona: a variant is a name, a hash of fields or a Game::Fanorona::Variant');
97 }
98  
99 959 my $start = $self->_start_given;
100 959 ▶ if (defined $start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $start) 520 439 959
101 $variant = Game::Fanorona::Variant->custom(
102 520 cols => $variant->cols, rows => $variant->rows, %{ $variant->limits }, start => $start,
103 );
104 }
105 957 $self->_variant($variant);
106 957 $self->_rules(Game::Fanorona::Rules->new(position => $variant->start, variant => $variant->limits));
107 957 $self->_pending([]);
108  
109 957 my $script = $self->_script_given;
110 957 ▶ if (defined $script) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $script) 15 942 957
111 15 ▶ Carp::croak('Game::Fanorona: a script is a list of moves') unless ref $script eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $script eq "ARRAY" 1 14 15
112 14 my $n = 0;
113 14 for my $move (@$script) {
114 32 $n++;
115 32 ▶ next if $self->play($move);
Branch: 1 branch
BranchTrueFalseTotal
if $self->play($move) 31 1 32
116 1 ▶ Carp::croak("Game::Fanorona: move $n of the script, '" . ($move // 'undef') . "', was refused: "
Condition50%1/2
$move // "undef"
l!l
10
MC/DC0%0/1
$move // "undef"
$move
Truth table50%1/2
A
A= $move
Aresult
11
00
117 . $self->error->message);
118 }
119 }
120 955 return;
121 }
122  
123 64 ▶my $refuse = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
124 64 my ($self, $code, $move) = @_;
125 64 ▶ $self->_error(Game::Fanorona::Error->of($code, (defined $move ? (move => $move) : ())));
Branch: 1 branch
BranchTrueFalseTotal
defined $move ? : 51 13 64
126 64 return;
127 25 };
128  
129 31390 ▶my $describe = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
130 31390 my ($s) = @_;
131 31390 my ($from, $to) = (name_of($s->{from}), name_of($s->{to}));
132 return {
133 from => $from,
134 to => $to,
135 how => $HOW{ $s->{kind} },
136 31390 takes => [ map { name_of($_) } @{ $s->{taken} } ],
137 string => "$from-$to$LETTER{ $s->{kind} }",
138 };
139 25 };
140  
141 36426 ▶my $read = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
142 36426 my ($self, $move) = @_;
143 36426 ▶ my $text = ref $move ? format_move($move) : $move;
Branch: 1 branch
BranchTrueFalseTotal
ref $move ? : 1 36425 36426
144 36426 my $variant = $self->_variant;
145 36426 my ($parsed, $why) = parse_move($text, cols => $variant->cols, rows => $variant->rows);
146 36426 ▶ return ($parsed, $text) if $parsed;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed 36409 17 36426
147 17 ▶ $refuse->($self, $FROM_NOTATION{ $why // '' } // 'bad_move', $text);
Condition75%3/4
$FROM_NOTATION{$why // ""} // "bad_move"
l!l
125
$why // ""
l!l
170
MC/DC100%1/1
$FROM_NOTATION{$why // ""} // "bad_move"
$FROM_NOTATION{$why // ""}
MC/DC0%0/1
$why // ""
$why
Truth table100%2/2
A
A= $FROM_NOTATION{$why // ""}
Aresult
11
00
Truth table50%1/2
A
A= $why
Aresult
11
00
148 17 return;
149 25 };
150  
151 59560 ▶my $take = sub {
Subroutine __ANON__: called CC=10 SCAR=23.2
152 59560 my ($self, $rules, $wanted, $text) = @_;
153 59560 my ($from, $dir, $kind) = @{$wanted}{qw(from dir kind)};
154 59560 ▶ my @there = grep { $_->{from} == $from && $_->{dir} == $dir } $rules->steps;
Condition100%3/3
$_->{'from'} == $from && $_->{'dir'} == $dir
!ll&&!rl&&r
2245498200765874
MC/DC100%2/2
$_->{'from'} == $from && $_->{'dir'} == $dir
$_->{'from'} == $from $_->{'dir'} == $dir
Truth table100%3/3
A && B
A= $_->{'from'} == $fromB= $_->{'dir'} == $dir
ABresult
0X0
100
111
155 59560 ▶ if ($kind == PAIKA) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind == 0) 15448 44112 59560
156 15448 my @takes = grep { $_->{kind} != PAIKA } @there;
157 15448 ▶ return $refuse->($self, 'choose_capture', $text) if @takes > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @takes > 1 4 15444 15448
158 15444 ▶ $kind = $takes[0]{kind} if @takes == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @takes == 1 2 15442 15444
159 }
160 59556 my ($step) = grep { $_->{kind} == $kind } @there;
161 59556 ▶ return $step if $step && $rules->step($from, $dir, $kind);
Branch: 1 branch
BranchTrueFalseTotal
if $step and $rules->step($from, $dir, $kind) 59533 23 59556
Condition66%2/3
$step and $rules->step($from, $dir, $kind)
!ll&&!rl&&r
23059533
MC/DC50%1/2
$step and $rules->step($from, $dir, $kind)
$step $rules->step($from, $dir, $kind)
Truth table66%2/3
A and B
A= $stepB= $rules->step($from, $dir, $kind)
ABresult
0X0
100
111
162 23 ▶ $rules->step($from, $dir, $kind) unless $step;
Branch: 1 branch
BranchTrueFalseTotal
unless $step 23 0 23
163 23 ▶ my $code = $rules->refused == PLAY_OVER ? 'game_over' : ($FROM_WHY{ $rules->why } // 'bad_move');
Branch: 1 branch
BranchTrueFalseTotal
$rules->refused == 1 ? : 0 23 23
Condition50%1/2
$FROM_WHY{$rules->why} // "bad_move"
l!l
230
MC/DC0%0/1
$FROM_WHY{$rules->why} // "bad_move"
$FROM_WHY{$rules->why}
Truth table50%1/2
A
A= $FROM_WHY{$rules->why}
Aresult
11
00
164 23 return $refuse->($self, $code, $text);
165 25 };
166  
167 10674 ▶my $run = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
168 10674 my ($self, $rules, $parsed, $text) = @_;
169 10674 my @made;
170 10674 for my $wanted (@{ $parsed->{steps} }) {
171 13679 my $step = $take->($self, $rules, $wanted, $text);
172 13679 ▶ if (!$step) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($step) 15 13664 13679
173 15 $rules->undo for @made;
174 15 return;
175 }
176 13664 push @made, $step;
177 }
178 10659 ▶ $rules->stop if $rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
if $rules->in_chain 1390 9269 10659
179 10659 return @made;
180 25 };
181  
182 9491 ▶my $finish = sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
183 9491 my ($self) = @_;
184 9491 my $steps = $self->_pending;
185 9491 ▶ my $mover = $WORD[ $steps->[0] ? $self->_rules->at($steps->[-1]{to}) : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$steps->[0] ? : 9491 0 9491
186 9491 push @{ $self->_log }, format_move({ from => $steps->[0]{from}, steps => $steps });
187 9491 $self->_pending([]);
188 9491 my $offer = $self->_offer;
189 9491 ▶ $self->_offer(undef) if defined $offer && defined $mover && $offer eq $mover;
Branch: 1 branch
BranchTrueFalseTotal
if defined $offer and defined $mover and $offer eq $mover 2 9489 9491
Condition83%5/6
defined $offer and defined $mover and $offer eq $mover
!ll&&!rl&&r
948812
defined $offer and defined $mover
!ll&&!rl&&r
948803
MC/DC66%2/3
defined $offer and defined $mover and $offer eq $mover
defined $offer defined $mover $offer eq $mover
Truth table75%3/4
A and B and C
A= defined $offerB= defined $moverC= $offer eq $mover
ABCresult
0XX0
10X0
1100
1111
190 9491 return;
191 25 };
192  
193 1514 ▶sub variant { $_[0]->_variant }
Subroutine variant: called CC=1 SCAR=0.0
Pod documented
194  
195 48983 ▶sub over { ($_[0]->_ended || $_[0]->_rules->over) ? 1 : 0 }
Subroutine over: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_ended || $_[0]->_rules->over ? : 203 48780 48983
Condition100%3/3
$_[0]->_ended or $_[0]->_rules->over
l!l&&r!l&&!r
2817548780
MC/DC100%2/2
$_[0]->_ended or $_[0]->_rules->over
$_[0]->_ended $_[0]->_rules->over
Truth table100%3/3
A or B
A= $_[0]->_endedB= $_[0]->_rules->over
ABresult
1X1
011
000
196  
197 1309 ▶sub turn {
Subroutine turn: called CC=2 SCAR=6.9
Pod documented
198 1309 my ($self) = @_;
199 1309 ▶ return undef if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 2 1307 1309
200 1307 return $WORD[ $self->_rules->side ];
201 }
202  
203 647 ▶sub result {
Subroutine result: called CC=3 SCAR=11.0
Pod documented
204 647 my ($self) = @_;
205 647 my $ended = $self->_ended;
206 647 ▶ return $ended if $ended;
Branch: 1 branch
BranchTrueFalseTotal
if $ended 17 630 647
207 630 my $rules = $self->_rules;
208 630 ▶ return undef unless $rules->over;
Branch: 1 branch
BranchTrueFalseTotal
unless $rules->over 291 339 630
209 339 return Game::Fanorona::Result->new(
210 how => $rules->how,
211 winner => $WORD[ $rules->winner ],
212 position => $rules->position,
213 moves => $rules->ply,
214 );
215 }
216  
217 6 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
218 6 my ($self) = @_;
219 6 ▶ my $result = $self->result or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $result = $self->result or return undef 5 1 6
220 5 return $result->winner;
221 }
222  
223 69 ▶sub error { $_[0]->_error }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
224  
225 2738 ▶sub position { $_[0]->_rules->position }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
226  
227 2 ▶sub position_counted {
Subroutine position_counted: called CC=1 SCAR=0.0
Pod documented
228 2 my ($self) = @_;
229 2 my $rules = $self->_rules;
230 2 return format_position($rules->position, $rules->quiet, $rules->ply + 1);
231 }
232  
233 345 ▶sub signature { $_[0]->_rules->key_hex }
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
234 4589 ▶sub ply { $_[0]->_rules->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
235 323 ▶sub quiet { $_[0]->_rules->quiet }
Subroutine quiet: called CC=1 SCAR=0.0
Pod documented
236 345 ▶sub quiet_limit { $_[0]->_rules->quiet_limit }
Subroutine quiet_limit: called CC=1 SCAR=0.0
Pod documented
237 1 ▶sub repeats { $_[0]->_rules->repeats }
Subroutine repeats: called CC=1 SCAR=0.0
Pod documented
238 813 ▶sub in_chain { $_[0]->_rules->in_chain }
Subroutine in_chain: called CC=1 SCAR=0.0
Pod documented
239  
240 2 ▶sub chain_at {
Subroutine chain_at: called CC=2 SCAR=6.9
Pod documented
241 2 my ($self) = @_;
242 2 my $at = $self->_rules->chain_at;
243 2 ▶ return $at < 0 ? undef : name_of($at);
Branch: 1 branch
BranchTrueFalseTotal
$at < 0 ? : 1 1 2
244 }
245  
246 99 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
247 99 my ($self, $name) = @_;
248 99 my $variant = $self->_variant;
249 99 my @point = parse_point($name, cols => $variant->cols, rows => $variant->rows);
250 99 ▶ return undef unless @point;
Branch: 1 branch
BranchTrueFalseTotal
unless @point 8 91 99
251 91 return $WORD[ $self->_rules->at(square_of($name)) ];
252 }
253  
254 10 ▶sub stones {
Subroutine stones: called CC=3 SCAR=11.0
Pod documented
255 10 my ($self, $side) = @_;
256 10 ▶ return undef unless defined $side && $SIDE{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and $SIDE{$side} 1 9 10
Condition66%2/3
defined $side and $SIDE{$side}
!ll&&!rl&&r
019
MC/DC50%1/2
defined $side and $SIDE{$side}
defined $side $SIDE{$side}
Truth table66%2/3
A and B
A= defined $sideB= $SIDE{$side}
ABresult
0X0
100
111
257 9 return $self->_rules->count($SIDE{$side});
258 }
259  
260 529 ▶sub moves { @{ $_[0]->_log } }
Subroutine moves: called CC=1 SCAR=0.0
Pod documented
261  
262 173 ▶sub in_progress {
Subroutine in_progress: called CC=2 SCAR=6.9
Pod documented
263 173 my ($self) = @_;
264 173 my $steps = $self->_pending;
265 173 ▶ return undef unless @$steps;
Branch: 1 branch
BranchTrueFalseTotal
unless @$steps 169 4 173
266 4 return format_move({ from => $steps->[0]{from}, steps => $steps });
267 }
268  
269 659 ▶sub steps {
Subroutine steps: called CC=2 SCAR=6.9
Pod documented
270 659 my ($self) = @_;
271 659 ▶ return if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 4 655 659
272 655 return map { $describe->($_) } $self->_rules->steps;
273 }
274  
275 6 ▶sub can_stop {
Subroutine can_stop: called CC=3 SCAR=11.0
Pod documented
276 6 my ($self) = @_;
277 6 ▶ return 0 if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 5 6
278 5 ▶ return $self->_rules->can_stop ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_rules->can_stop ? : 2 3 5
279 }
280  
281 472 ▶sub starts {
Subroutine starts: called CC=1 SCAR=0.0
Pod documented
282 472 my ($self) = @_;
283 472 my %seen;
284 472 return grep { !$seen{$_}++ } map { $_->{from} } $self->steps;
285 }
286  
287 2758 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
288 2758 my ($self, $limit) = @_;
289 2758 ▶ return if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 2 2756 2758
290 2756 return $self->_rules->moves($limit);
291 }
292  
293 2737 ▶sub legal_strings {
Subroutine legal_strings: called CC=1 SCAR=0.0
Pod documented
294 2737 my ($self, $limit) = @_;
295 2737 return sort map { format_move($_) } $self->legal($limit);
296 }
297  
298 519 ▶sub step {
Subroutine step: called CC=6 SCAR=17.9
Pod documented
299 519 my ($self, $string) = @_;
300 519 $self->_error(undef);
301 519 ▶ return $refuse->($self, 'game_over', $string) if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 518 519
302 518 ▶ my ($parsed, $text) = $read->($self, $string) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &$read($self, $string) or return 518 0 518
303 518 ▶ return $refuse->($self, 'bad_move', $text) unless @{ $parsed->{steps} } == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$parsed->{"steps"};} == 1 1 517 518
304 517 ▶ my $step = $take->($self, $self->_rules, $parsed->{steps}[0], $text) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $step = &$take($self, $self->_rules, $parsed->{"steps"}[0], $text) or return 513 4 517
305 513 push @{ $self->_pending }, $step;
306 513 ▶ $finish->($self) unless $self->_rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_rules->in_chain 349 164 513
307 513 return $describe->($step);
308 }
309  
310 21 ▶sub stop {
Subroutine stop: called CC=3 SCAR=11.0
Pod documented
311 21 my ($self) = @_;
312 21 $self->_error(undef);
313 21 ▶ return $refuse->($self, 'game_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 20 21
314 20 ▶ return $refuse->($self, 'nothing_to_stop') unless $self->_rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_rules->in_chain 1 19 20
315 19 $self->_rules->stop;
316 19 $finish->($self);
317 19 return 1;
318 }
319  
320 9152 ▶sub play {
Subroutine play: called CC=6 SCAR=17.9
Pod documented
321 9152 my ($self, $move) = @_;
322 9152 $self->_error(undef);
323 9152 ▶ my $shown = ref $move ? format_move($move) : $move;
Branch: 1 branch
BranchTrueFalseTotal
ref $move ? : 1 9151 9152
324 9152 ▶ return $refuse->($self, 'game_over', $shown) if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 2 9150 9152
325 9150 ▶ return $refuse->($self, 'chain_open', $shown) if $self->_rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_rules->in_chain 1 9149 9150
326 9149 ▶ my ($parsed, $text) = $read->($self, $move) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &$read($self, $move) or return 9137 12 9149
327 9137 ▶ my @made = $run->($self, $self->_rules, $parsed, $text) or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@made) = &$run($self, $self->_rules, $parsed, $text) or return 9123 14 9137
328 9123 $self->_pending(\@made);
329 9123 $finish->($self);
330 9123 return Game::Fanorona::Move->new(from => $made[0]{from}, steps => [ @made ]);
331 }
332  
333 1539 ▶sub preview {
Subroutine preview: called CC=6 SCAR=18.0
Pod documented
334 1539 my ($self, $move) = @_;
335 1539 $self->_error(undef);
336 1539 ▶ my $shown = ref $move ? format_move($move) : $move;
Branch: 1 branch
BranchTrueFalseTotal
ref $move ? : 0 1539 1539
337 1539 ▶ return $refuse->($self, 'game_over', $shown) if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 1538 1539
338 1538 ▶ return $refuse->($self, 'chain_open', $shown) if $self->_rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_rules->in_chain 1 1537 1538
339 1537 ▶ my ($parsed, $text) = $read->($self, $move) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &$read($self, $move) or return 1537 0 1537
340 1537 my $rules = $self->_rules->clone;
341 1537 ▶ $run->($self, $rules, $parsed, $text) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$run($self, $rules, $parsed, $text) or return 1536 1 1537
342 1536 return $rules->position;
343 }
344  
345 27113 ▶sub continuations {
Subroutine continuations: called CC=12 SCAR=24.8
Pod documented
346 27113 my ($self, $prefix) = @_;
347 27113 $self->_error(undef);
348 27113 ▶ return $refuse->($self, 'game_over', $prefix) if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 27112 27113
349 27112 my $rules = $self->_rules->clone;
350 27112 my $complete = 0;
351 27112 ▶ if (defined $prefix && length $prefix) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and length $prefix) 25222 1890 27112
Condition100%3/3
defined $prefix and length $prefix
!ll&&!rl&&r
174114925222
MC/DC100%2/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table100%3/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
352 25222 ▶ my ($parsed, $text) = $read->($self, $prefix) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = &$read($self, $prefix) or return 25217 5 25222
353 25217 for my $wanted (@{ $parsed->{steps} }) {
354 45365 ▶ return $refuse->($self, 'bad_move', $text) if $complete;
Branch: 1 branch
BranchTrueFalseTotal
if $complete 1 45364 45365
355 45364 ▶ $take->($self, $rules, $wanted, $text) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$take($self, $rules, $wanted, $text) or return 45356 8 45364
356 45356 ▶ $complete = 1 unless $rules->in_chain;
Branch: 1 branch
BranchTrueFalseTotal
unless $rules->in_chain 15384 29972 45356
357 }
358 }
359 return {
360 complete => $complete,
361 stop => (!$complete && $rules->can_stop) ? 1 : 0,
362 27098 ▶ steps => [ $complete ? () : map { $describe->($_) } $rules->steps ],
Branch: 2 branches
BranchTrueFalseTotal
!$complete && $rules->can_stop ? : 9826 17272 27098
$complete ? : 15383 11715 27098
Condition100%3/3
not $complete and $rules->can_stop
!ll&&!rl&&r
1538318899826
MC/DC100%2/2
not $complete and $rules->can_stop
not $complete $rules->can_stop
Truth table100%3/3
not(A) and B
A= not $completeB= $rules->can_stop
ABresult
0X0
100
111
363 };
364 }
365  
366 21 ▶my $end = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
367 21 my ($self, $how, $winner) = @_;
368 21 my $rules = $self->_rules;
369 21 ▶ $self->_ended(Game::Fanorona::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 16 5 21
370 how => $how,
371 (defined $winner ? (winner => $winner) : ()),
372 position => $rules->position,
373 moves => $rules->ply,
374 ));
375 21 $self->_offer(undef);
376 21 return 1;
377 25 };
378  
379 16 ▶my $other = sub { $_[0] eq 'white' ? 'black' : 'white' };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'white' ? : 5 11 16
380  
381 18 ▶sub resign {
Subroutine resign: called CC=4 SCAR=13.9
Pod documented
382 18 my ($self, $side) = @_;
383 18 $self->_error(undef);
384 18 ▶ return $refuse->($self, 'game_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 17 18
385 17 ▶ return $refuse->($self, 'not_a_side') unless defined $side && $SIDE{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and $SIDE{$side} 1 16 17
Condition66%2/3
defined $side and $SIDE{$side}
!ll&&!rl&&r
0116
MC/DC50%1/2
defined $side and $SIDE{$side}
defined $side $SIDE{$side}
Truth table66%2/3
A and B
A= defined $sideB= $SIDE{$side}
ABresult
0X0
100
111
386 16 return $end->($self, 'resign', $other->($side));
387 }
388  
389 20 ▶sub offer_draw {
Subroutine offer_draw: called CC=5 SCAR=16.1
Pod documented
390 20 my ($self, $side) = @_;
391 20 $self->_error(undef);
392 20 ▶ return $refuse->($self, 'game_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 19 20
393 19 ▶ return $refuse->($self, 'not_a_side') unless defined $side && $SIDE{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and $SIDE{$side} 1 18 19
Condition66%2/3
defined $side and $SIDE{$side}
!ll&&!rl&&r
1018
MC/DC50%1/2
defined $side and $SIDE{$side}
defined $side $SIDE{$side}
Truth table66%2/3
A and B
A= defined $sideB= $SIDE{$side}
ABresult
0X0
100
111
394 18 ▶ return $refuse->($self, 'offer_standing') if defined $self->_offer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_offer 1 17 18
395 17 $self->_offer($side);
396 17 return 1;
397 }
398  
399 12 ▶my $answer = sub {
Subroutine __ANON__: called CC=6 SCAR=18.2
400 12 my ($self, $side) = @_;
401 12 $self->_error(undef);
402 12 ▶ return $refuse->($self, 'game_over') if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 1 11 12
403 11 ▶ return $refuse->($self, 'not_a_side') unless defined $side && $SIDE{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and $SIDE{$side} 0 11 11
Condition33%1/3
defined $side and $SIDE{$side}
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $side and $SIDE{$side}
defined $side $SIDE{$side}
Truth table33%1/3
A and B
A= defined $sideB= $SIDE{$side}
ABresult
0X0
100
111
404 11 ▶ return $refuse->($self, 'no_offer') unless defined $self->_offer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_offer 2 9 11
405 9 ▶ return $refuse->($self, 'own_offer') if $self->_offer eq $side;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_offer eq $side 1 8 9
406 8 return 1;
407 25 };
408  
409 8 ▶sub accept_draw {
Subroutine accept_draw: called CC=2 SCAR=6.9
Pod documented
410 8 my ($self, $side) = @_;
411 8 ▶ $answer->($self, $side) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$answer($self, $side) or return 5 3 8
412 5 return $end->($self, 'agreement');
413 }
414  
415 4 ▶sub decline_draw {
Subroutine decline_draw: called CC=2 SCAR=6.9
Pod documented
416 4 my ($self, $side) = @_;
417 4 ▶ $answer->($self, $side) or return;
Branch: 1 branch
BranchTrueFalseTotal
&$answer($self, $side) or return 3 1 4
418 3 $self->_offer(undef);
419 3 return 1;
420 }
421  
422 305 ▶sub draw_offered_by { $_[0]->_offer }
Subroutine draw_offered_by: called CC=1 SCAR=0.0
Pod documented
423  
424 253 ▶sub search {
Subroutine search: called CC=4 SCAR=13.9
Pod documented
425 253 my ($self, %with) = @_;
426 253 ▶ return undef if $self->over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->over 3 250 253
427 250 ▶ my $found = $self->_rules->search(%with) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $found = $self->_rules->search(%with) or return undef 250 0 250
428 250 my $steps = delete $found->{steps};
429 250 $found->{steps} = [ map { $describe->($_) } @$steps ];
430 250 ▶ $found->{move} = @$steps ? format_move({ from => $steps->[0]{from}, steps => $steps }) : '';
Branch: 1 branch
BranchTrueFalseTotal
@$steps ? : 248 2 250
431 250 return $found;
432 }
433  
434 18 ▶sub evaluate {
Subroutine evaluate: called CC=1 SCAR=0.0
Pod documented
435 18 my ($self, $weights) = @_;
436 18 return $self->_rules->evaluate($weights);
437 }
438  
439 136 ▶sub replay {
Subroutine replay: called CC=4 SCAR=13.9
Pod documented
440 136 my ($class, $moves, %arg) = @_;
441 136 ▶ Carp::croak('Game::Fanorona: replay takes a list of moves') unless ref $moves eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $moves eq "ARRAY" 1 135 136
442 135 my $game = $class->new(%arg);
443 135 my $n = 0;
444 135 for my $move (@$moves) {
445 5088 $n++;
446 5088 ▶ return ($game, $game->error, $n) unless $game->play($move);
Branch: 1 branch
BranchTrueFalseTotal
unless $game->play($move) 2 5086 5088
447 }
448 133 return ($game, undef, undef);
449 }
450  
451 63 ▶sub as_text {
Subroutine as_text: called CC=8 SCAR=20.8
Pod documented
452 63 my ($self, %extra) = @_;
453 63 my $variant = $self->_variant;
454 63 my $default = Game::Fanorona::Variant->named('telo')->limits;
455 63 my %game = (variant => $variant->name, moves => [ $self->moves ]);
456 63 ▶ $game{start} = $variant->start unless $variant->is_setup;
Branch: 1 branch
BranchTrueFalseTotal
unless $variant->is_setup 0 63 63
457 63 for my $limit (qw(quiet_limit repeat ply_cap)) {
458 189 ▶ $game{$limit} = $variant->$limit unless $variant->$limit == $default->{$limit};
Branch: 1 branch
BranchTrueFalseTotal
unless $variant->$limit == $default->{$limit} 1 188 189
459 }
460 63 for my $name (qw(white black)) {
461 126 ▶ $game{$name} = $extra{$name} if defined $extra{$name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $extra{$name} 62 64 126
462 }
463 63 my $result = $self->result;
464 63 ▶ $game{result} = $result->how . (defined $result->winner ? ' ' . $result->winner : '') if $result;
Branch: 2 branches
BranchTrueFalseTotal
if $result 61 2 63
defined $result->winner ? : 58 3 61
465 63 return format_game(\%game);
466 }
467  
468 68 ▶sub from_text {
Subroutine from_text: called CC=4 SCAR=13.9
Pod documented
469 68 my ($class, $text) = @_;
470 68 my ($parsed, $why, $line) = parse_game($text);
471 68 ▶ return (undef, $why, $line) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 4 64 68
472 64 my $variant = eval {
473 Game::Fanorona::Variant->custom(
474 cols => $parsed->{cols}, rows => $parsed->{rows},
475 64 map { $_ => $parsed->{$_} } grep { defined $parsed->{$_} } qw(quiet_limit repeat ply_cap start),
476 );
477 };
478 64 ▶ return (undef, 'bad_variant', 2) unless $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless $variant 0 64 64
479 64 my ($game, $error, $n) = $class->replay($parsed->{moves}, variant => $variant);
480 64 ▶ return (undef, $error->code, $parsed->{moves_at} + $n - 1) if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 1 63 64
481 63 return ($game, undef, undef);
482 }
483  
484 25 1;
485  
486 __END__
« blib/lib/Game/Fanorona/Result.pm ↑ Summary blib/lib/Game/Fanorona/Engine.pm »