blib/lib/Game/Go.pm

filter: Statementstmt 98.9% 479 / 484 Branchbran 87.6% 226 / 258 Conditioncond 66.6% 70 / 105 MC/DCmcdc 42.3% 25 / 59 Subroutinesub 93.5% 72 / 77 Podpod 97.5% 40 / 41 totaltotal 89.0% 912 / 1024 CC 185 SCAR 24.7
CC
185
Cov
91%
replay
32.5
_rebuild
28.1
from_text
17.0
CRAP 11.8
« blib/lib/Game/Go/Terminal.pm ↑ Summary blib/lib/Game/Go/Notation.pm »
1 package Game::Go;
2  
3 47 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 47 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 47 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 47 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 47 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 47 ▶use Game::Go::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 47 ▶use Game::Go::Engine;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 47 ▶use Game::Go::Board;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 47 ▶use Game::Go::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 47 ▶use Game::Go::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 47 ▶use Game::Go::Marking;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 47 ▶use Game::Go::Scoring;
Subroutine __ANON__: called CC=1 SCAR=0.0
18 47 ▶use Game::Go::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 47 our $VERSION = '0.05';
21  
22 47 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
23 47 EMPTY => Game::Go::Rules::EMPTY,
24 BLACK => Game::Go::Rules::BLACK,
25 WHITE => Game::Go::Rules::WHITE,
26 BORDER => Game::Go::Rules::BORDER,
27 47 };
28  
29 47 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 47 OK => Game::Go::Rules::OK,
31 ILL_OFF => Game::Go::Rules::ILL_OFF,
32 ILL_TAKEN => Game::Go::Rules::ILL_TAKEN,
33 ILL_KO => Game::Go::Rules::ILL_KO,
34 ILL_SUICIDE => Game::Go::Rules::ILL_SUICIDE,
35 ILL_REPEAT => Game::Go::Rules::ILL_REPEAT,
36 ILL_COLOUR => Game::Go::Rules::ILL_COLOUR,
37 47 };
38  
39 47 ▶use constant MAX_DISPUTES => 3;
Subroutine __ANON__: called CC=1 SCAR=0.0
40  
41 47 has size => (is => 'ro', isa => Int, default => 19);
42 47 has komi => (is => 'rw');
43 47 has handicap => (is => 'ro', isa => Int, default => 0);
44  
45 47 has ko => (is => 'ro', isa => Str, default => 'positional');
46  
47 47 has setup => (is => 'ro', private => 1);
48  
49 47 has first => (is => 'ro', private => 1);
50  
51 47 has status => (is => 'rw', isa => Str, default => 'active');
52 47 has phase => (is => 'rw', isa => Str, default => 'play');
53 47 has turn => (is => 'rw');
54  
55 47 has [qw/winner result/] => (is => 'rw');
56  
57 47 has scored_by => (is => 'rw');
58  
59 47 has outcome => (is => 'rw');
60  
61 47 has marking => (is => 'rw');
62  
63 47 has disputes => (is => 'rw', default => 0);
64  
65 47 has log => (is => 'rw', isa => ArrayRef, default => []);
66  
67 47 has _engine => (is => 'rw', private => 1);
68 47 has _passes => (is => 'rw', private => 1, default => 0);
69  
70 47 has _held_seed => (is => 'ro', init_arg => 'seed', private => 1);
71  
72 646 ▶sub BUILD {
Subroutine BUILD: called CC=16 SCAR=27.7
Pod undocumented
73 646 my ($self) = @_;
74  
75 646 my $size = $self->size;
76 Carp::croak("Game::Go: size must be 9, 13 or 19, not '$size'")
77 646 ▶ unless grep { $_ == $size } Game::Go::Rules::sizes();
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $size;} Game::Go::Rules::sizes() 9 637 646
78  
79 637 ▶ $self->komi(
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->komi 292 345 637
$self->handicap ? : 47 245 292
80 $self->handicap
81 ? Game::Go::Rules::HANDICAP_KOMI
82 : Game::Go::Rules::DEFAULT_KOMI
83 ) unless defined $self->komi;
84  
85 637 my $komi = $self->komi;
86 637 ▶ Carp::croak("Game::Go: komi must be a multiple of 0.5, not '$komi'")
Branch: 1 branch
BranchTrueFalseTotal
unless $komi =~ /\A-?[0-9]+(?:\.[05])?\z/ 1 636 637
87 unless $komi =~ /\A-?[0-9]+(?:\.[05])?\z/;
88  
89 636 my $handicap = $self->handicap;
90 636 my $most = Game::Go::Rules::max_handicap($size);
91 636 ▶ Carp::croak("Game::Go: handicap must be 0 to $most on a ${size}x$size board, not '$handicap'")
Branch: 1 branch
BranchTrueFalseTotal
if $handicap < 0 or $handicap > $most 10 626 636
Condition100%3/3
$handicap < 0 or $handicap > $most
l!l&&r!l&&!r
19626
MC/DC100%2/2
$handicap < 0 or $handicap > $most
$handicap < 0 $handicap > $most
Truth table100%3/3
A or B
A= $handicap < 0B= $handicap > $most
ABresult
1X1
011
000
92 if $handicap < 0 || $handicap > $most;
93  
94 626 my $ko = $self->ko;
95 626 ▶ Carp::croak("Game::Go: ko must be 'positional' or 'simple', not '$ko'")
Branch: 1 branch
BranchTrueFalseTotal
unless $ko eq "positional" or $ko eq "simple" 1 625 626
Condition100%3/3
$ko eq "positional" or $ko eq "simple"
l!l&&r!l&&!r
593321
MC/DC100%2/2
$ko eq "positional" or $ko eq "simple"
$ko eq "positional" $ko eq "simple"
Truth table100%3/3
A or B
A= $ko eq "positional"B= $ko eq "simple"
ABresult
1X1
011
000
96 unless $ko eq 'positional' || $ko eq 'simple';
97  
98 625 $self->_engine(Game::Go::Engine->new(size => $size));
99 625 ▶ $self->_engine->superko(0) if $ko eq 'simple';
Branch: 1 branch
BranchTrueFalseTotal
if $ko eq "simple" 32 593 625
100  
101 625 my $first = $self->first;
102 625 my $told;
103 625 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) 9 616 625
104 9 $told = Game::Go::Rules::from_letter($first);
105 9 ▶ Carp::croak("Game::Go: first must be 'b' or 'w', not '$first'") unless $told;
Branch: 1 branch
BranchTrueFalseTotal
unless $told 0 9 9
106 }
107  
108 625 $self->turn(BLACK);
109  
110 625 ▶ $self->_log('sys', 'start', {
Branch: 1 branch
BranchTrueFalseTotal
$told ? : 9 616 625
111 size => $size,
112 komi => $komi + 0,
113 handicap => $handicap,
114 ko => $ko,
115 first => Game::Go::Rules::letter($self->turn),
116 ($told ? (turn => $first) : ()),
117 });
118  
119 625 ▶ $self->_place_setup($self->setup) if $self->setup;
Branch: 1 branch
BranchTrueFalseTotal
if $self->setup 6 619 625
120 625 ▶ $self->_place_handicap($handicap) if $handicap > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $handicap > 1 40 585 625
121  
122 625 ▶ $self->turn($told) if $told;
Branch: 1 branch
BranchTrueFalseTotal
if $told 9 616 625
123  
124 625 return;
125 }
126  
127 6 ▶sub _place_setup {
Subroutine _place_setup: called CC=5 SCAR=16.2
128 6 my ($self, $setup) = @_;
129 6 my %placed;
130  
131 6 for my $letter (sort keys %$setup) {
132 12 my $colour = Game::Go::Rules::from_letter($letter);
133 12 ▶ Carp::croak("Game::Go: setup names '$letter', which is not a colour")
Branch: 1 branch
BranchTrueFalseTotal
unless $colour 0 12 12
134 unless $colour;
135 12 for my $cr (@{ $setup->{$letter} }) {
136 18 my $pt = $self->point(@$cr);
137 18 ▶ Carp::croak('Game::Go: a setup stone is off the board') if $pt < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pt < 0 0 18 18
138 18 $self->_engine->put($pt, $colour);
139 18 push @{ $placed{$letter} }, $pt;
140 }
141 }
142  
143 6 $self->_log('sys', 'setup', \%placed);
144 6 $self->_engine->push_position;
145 6 return;
146 }
147  
148 40 ▶sub _place_handicap {
Subroutine _place_handicap: called CC=2 SCAR=6.9
149 40 my ($self, $n) = @_;
150 40 my $points = Game::Go::Rules::handicap_points($self->size, $n);
151  
152 40 for my $cr (@$points) {
153 198 my $pt = $self->point(@$cr);
154  
155 198 $self->_engine->put($pt, BLACK);
156 198 $self->_log(Game::Go::Rules::letter(BLACK), 'handicap', { pt => $pt });
157 }
158  
159 40 $self->_engine->push_position;
160  
161 40 $self->turn(WHITE);
162 40 return;
163 }
164  
165 80 ▶sub seats { (BLACK, WHITE) }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
166  
167 1047 ▶sub board { Game::Go::Board->new($_[0]->_engine) }
Subroutine board: called CC=1 SCAR=0.0
Pod documented
168  
169 42109 ▶sub point { $_[0]->_engine->point_of($_[1], $_[2]) }
Subroutine point: called CC=1 SCAR=0.0
Pod documented
170  
171 687 ▶sub col_row {
Subroutine col_row: called CC=1 SCAR=0.0
Pod documented
172 687 my ($self, $pt) = @_;
173 687 my $engine = $self->_engine;
174 687 return ($engine->col_of($pt), $engine->row_of($pt));
175 }
176  
177 304 ▶sub events { [ map { { %$_ } } @{ $_[0]->log } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
178  
179 78 ▶sub raw_score {
Subroutine raw_score: called CC=4 SCAR=13.9
Pod documented
180 78 my ($self) = @_;
181 78 my $m = $self->marking;
182 78 my $p = $self->prisoners;
183 return $self->_engine->score(
184 dead => ($m ? $m->dead_points : []),
185 seki => ($m ? $m->seki_points : []),
186 prisoners_b => $p->{ +BLACK },
187 78 ▶ prisoners_w => $p->{ +WHITE },
Branch: 3 branches
BranchTrueFalseTotal
$m ? : 41 37 78
$m ? : 41 37 78
$self->komi < 0 ? : 0 78 78
188 komi_tenths => int($self->komi * 10 + ($self->komi < 0 ? -0.5 : 0.5)),
189 );
190 }
191  
192 2 ▶sub raw_area { $_[0]->_engine->area_score }
Subroutine raw_area: called CC=1 SCAR=0.0
Pod documented
193  
194 1 ▶sub territory_map {
Subroutine territory_map: called CC=3 SCAR=11.7
Pod documented
195 1 my ($self) = @_;
196 1 my $m = $self->marking;
197 1 ▶ return $self->_engine->territory_map(
Branch: 2 branches
BranchTrueFalseTotal
$m ? : 0 1 1
$m ? : 0 1 1
198 dead => ($m ? $m->dead_points : []),
199 seki => ($m ? $m->seki_points : []),
200 );
201 }
202  
203 25 ▶sub _handicap_points {
Subroutine _handicap_points: called CC=1 SCAR=0.0
204 25 my ($self) = @_;
205 101 return [ map { $self->point(@$_) }
206 25 @{ Game::Go::Rules::handicap_points($self->size, $self->handicap) } ];
207 }
208  
209 473 ▶sub star_points {
Subroutine star_points: called CC=1 SCAR=0.0
Pod documented
210 473 my ($self) = @_;
211 473 return [ map { $self->point(@$_) } @{ Game::Go::Rules::star_points($self->size) } ];
212 }
213  
214 26 ▶sub ko_point { $_[0]->_engine->ko_point }
Subroutine ko_point: called CC=1 SCAR=0.0
Pod documented
215 0 ▶sub ko_colour { $_[0]->_engine->ko_colour }
Subroutine ko_colour: not called CC=1 SCAR=6.9
Pod documented
216  
217 1183 ▶sub search_from {
Subroutine search_from: called CC=1 SCAR=0.0
Pod documented
218 1183 my ($self, %o) = @_;
219 1183 return $self->_engine->search(%o);
220 }
221  
222 49 ▶sub dead_guess_from {
Subroutine dead_guess_from: called CC=1 SCAR=0.0
Pod documented
223 49 my ($self, %o) = @_;
224 49 return $self->_engine->dead_guess(%o);
225 }
226  
227 123 ▶sub is_alive { $_[0]->_engine->is_alive($_[1]) }
Subroutine is_alive: called CC=1 SCAR=0.0
Pod documented
228  
229 121 ▶sub chain_id { $_[0]->_chain_id($_[1]) }
Subroutine chain_id: called CC=1 SCAR=0.0
Pod documented
230  
231 31 ▶sub marked_seki { $_[0]->marking ? $_[0]->marking->seki_points : [] }
Subroutine marked_seki: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->marking ? : 3 28 31
232 0 ▶sub marked_dead { $_[0]->marking ? $_[0]->marking->dead_points : [] }
Subroutine marked_dead: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->marking ? : 0 0 0
233  
234 584 ▶sub prisoners {
Subroutine prisoners: called CC=5 SCAR=16.2
Pod documented
235 584 my ($self) = @_;
236 584 my %taken = (BLACK, 0, WHITE, 0);
237 584 for my $e (@{ $self->log }) {
238 17980 ▶ next unless $e->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless $e->{"kind"} eq "play" 7859 10121 17980
239 10121 ▶ my $by = Game::Go::Rules::from_letter($e->{actor}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $by = Game::Go::Rules::from_letter($e->{"actor"}) or next 10121 0 10121
240 10121 ▶ $taken{$by} += scalar @{ $e->{payload}{caps} || [] };
Condition50%1/2
$e->{'payload'}{'caps'} || []
l!l
101210
MC/DC0%0/1
$e->{'payload'}{'caps'} || []
$e->{'payload'}{'caps'}
Truth table50%1/2
A
A= $e->{'payload'}{'caps'}
Aresult
11
00
241 }
242 584 return \%taken;
243 }
244  
245 1569 ▶sub waiting_on {
Subroutine waiting_on: called CC=3 SCAR=11.0
Pod documented
246 1569 my ($self) = @_;
247 1569 ▶ return () unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 18 1551 1569
248 1551 ▶ return ($self->marking->turn) if $self->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $self->phase eq "marking" 54 1497 1551
249 1497 return ($self->turn);
250 }
251  
252 4 ▶sub seed { $_[0]->status eq 'finished' ? $_[0]->_held_seed : undef }
Subroutine seed: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->status eq 'finished' ? : 3 1 4
253  
254  
255 7094 ▶sub _log {
Subroutine _log: called CC=2 SCAR=6.9
256 7094 my ($self, $actor, $kind, $payload) = @_;
257 7094 ▶ push @{ $self->log }, {
Condition100%2/2
$payload || {}
l!l
6161933
MC/DC100%1/1
$payload || {}
$payload
Truth table100%2/2
A
A= $payload
Aresult
11
00
258 actor => $actor,
259 kind => $kind,
260 payload => $payload || {},
261 };
262 7094 return;
263 }
264  
265 5880 ▶sub _guard {
Subroutine _guard: called CC=5 SCAR=16.1
266 5880 my ($self, $colour) = @_;
267 5880 ▶ return Game::Go::Error->throw('bad_colour')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 0 5880 5880
268 unless Game::Go::Rules::is_colour($colour);
269 5880 ▶ return Game::Go::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 5879 5880
270 unless $self->status eq 'active';
271 5879 ▶ return Game::Go::Error->throw('still_marking')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->phase eq "play" 6 5873 5879
272 unless $self->phase eq 'play';
273 5873 ▶ return Game::Go::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $self->turn 3 5870 5873
274 unless $colour == $self->turn;
275 5870 return undef;
276 }
277  
278 1331 ▶sub legal {
Subroutine legal: called CC=5 SCAR=16.1
Pod documented
279 1331 my ($self, $colour) = @_;
280 1331 ▶ return [] unless Game::Go::Rules::is_colour($colour);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 1 1330 1331
281 1330 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 14 1316 1330
282  
283 1316 ▶ return $self->_legal_marking($colour) if $self->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $self->phase eq "marking" 21 1295 1316
284  
285 1295 ▶ return [] unless $colour == $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $self->turn 13 1282 1295
286  
287 1282 my $engine = $self->_engine;
288 my @moves = map {
289 51955 Game::Go::Move->new(kind => 'play', colour => $colour, point => $_)
290 1282 } @{ $engine->legal_moves($colour) };
291  
292 1282 push @moves, Game::Go::Move->new(kind => 'pass', colour => $colour);
293  
294 1282 return \@moves;
295 }
296  
297 5183 ▶sub play {
Subroutine play: called CC=3 SCAR=11.0
Pod documented
298 5183 my ($self, $colour, $pt) = @_;
299 5183 my $stop = $self->_guard($colour);
300 5183 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 7 5176 5183
301  
302 5176 my $r = $self->_engine->play($pt, $colour);
303 5176 ▶ return Game::Go::Error->from_code($r->{code}) unless $r->{ok};
Branch: 1 branch
BranchTrueFalseTotal
unless $r->{"ok"} 20 5156 5176
304  
305 5156 $self->_passes(0);
306 $self->_log(Game::Go::Rules::letter($colour), 'play', {
307 pt => $pt,
308 caps => $r->{caps},
309 5156 });
310 5156 $self->turn(Game::Go::Rules::other($colour));
311  
312 return Game::Go::Move->new(
313 kind => 'play',
314 colour => $colour,
315 point => $pt,
316 caps => $r->{caps},
317 5156 );
318 }
319  
320 697 ▶sub pass {
Subroutine pass: called CC=3 SCAR=11.0
Pod documented
321 697 my ($self, $colour) = @_;
322 697 my $stop = $self->_guard($colour);
323 697 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 3 694 697
324  
325 694 $self->_engine->pass($colour);
326 694 $self->_log(Game::Go::Rules::letter($colour), 'pass');
327 694 $self->_passes($self->_passes + 1);
328 694 $self->turn(Game::Go::Rules::other($colour));
329  
330 694 ▶ $self->_stop if $self->_passes >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_passes >= 2 82 612 694
331  
332 694 return Game::Go::Move->new(kind => 'pass', colour => $colour);
333 }
334  
335 82 ▶sub _stop {
Subroutine _stop: called CC=2 SCAR=6.9
336 82 my ($self) = @_;
337 82 $self->_log('sys', 'stop');
338  
339 82 ▶ if ($self->disputes >= MAX_DISPUTES) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->disputes >= 3) 2 80 82
340 2 $self->_log('sys', 'area');
341 2 return $self->_score('area');
342 }
343  
344 80 $self->phase('marking');
345 80 $self->marking(Game::Go::Marking->new(
346 proposer => $self->turn,
347 answerer => Game::Go::Rules::other($self->turn),
348 ));
349 80 return;
350 }
351  
352  
353 21 ▶sub _legal_marking {
Subroutine _legal_marking: called CC=13 SCAR=25.7
354 21 my ($self, $colour) = @_;
355 21 my $m = $self->marking;
356 21 ▶ return [] unless $m && $colour == $m->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $m and $colour == $m->turn 8 13 21
Condition66%2/3
$m and $colour == $m->turn
!ll&&!rl&&r
0813
MC/DC50%1/2
$m and $colour == $m->turn
$m $colour == $m->turn
Truth table66%2/3
A and B
A= $mB= $colour == $m->turn
ABresult
0X0
100
111
357  
358 13 my @moves;
359  
360 13 ▶ if (!$m->proposed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($m->proposed) 6 7 13
361 6 my $engine = $self->_engine;
362 6 my $alive = $self->_alive_set;
363 6 my %chain;
364 6 for my $row (0 .. $self->size - 1) {
365 54 for my $col (0 .. $self->size - 1) {
366 486 my $pt = $engine->point_of($col, $row);
367 486 my $at = $engine->at($pt);
368 486 ▶ if ($at == EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($at == 0) 311 175 486
369 311 ▶ push @moves, Game::Go::Move->new(
Branch: 1 branch
BranchTrueFalseTotal
$m->is_seki($pt) ? : 0 311 311
370 kind => ($m->is_seki($pt) ? 'unseki' : 'seki'),
371 colour => $colour,
372 point => $pt,
373 );
374 311 next;
375 }
376 175 ▶ next if $alive->{$pt};
Branch: 1 branch
BranchTrueFalseTotal
if $alive->{$pt} 13 162 175
377 162 my $id = $self->_chain_id($pt);
378 162 ▶ next if !defined $id || $chain{$id}++;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $id or $chain{$id}++ 148 14 162
Condition66%2/3
not defined $id or $chain{$id}++
l!l&&r!l&&!r
014814
MC/DC50%1/2
not defined $id or $chain{$id}++
not defined $id $chain{$id}++
Truth table66%2/3
not(A) or B
A= not defined $idB= $chain{$id}++
ABresult
1X1
011
000
379 14 ▶ push @moves, Game::Go::Move->new(
Branch: 1 branch
BranchTrueFalseTotal
$m->is_dead($id) ? : 4 10 14
380 kind => ($m->is_dead($id) ? 'unmark' : 'mark'),
381 colour => $colour,
382 point => $id,
383 );
384 }
385 }
386 6 push @moves, Game::Go::Move->new(kind => 'done', colour => $colour);
387 6 return \@moves;
388 }
389  
390 7 push @moves, Game::Go::Move->new(kind => 'accept', colour => $colour);
391 7 ▶ push @moves, Game::Go::Move->new(kind => 'dispute', colour => $colour)
Branch: 1 branch
BranchTrueFalseTotal
if $self->disputes < 3 7 0 7
392 if $self->disputes < MAX_DISPUTES;
393 7 return \@moves;
394 }
395  
396 333 ▶sub _chain_id {
Subroutine _chain_id: called CC=2 SCAR=6.9
397 333 my ($self, $pt) = @_;
398 333 my $stones = $self->_engine->chain_at($pt);
399 333 ▶ return undef unless @$stones;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stones 1 332 333
400 332 my ($low) = sort { $a <=> $b } @$stones;
401 332 return $low;
402 }
403  
404 185 ▶sub _marking_guard {
Subroutine _marking_guard: called CC=7 SCAR=19.7
405 185 my ($self, $colour, $role) = @_;
406 185 ▶ return Game::Go::Error->throw('bad_colour')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 0 185 185
407 unless Game::Go::Rules::is_colour($colour);
408 185 ▶ return Game::Go::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 185 185
409 unless $self->status eq 'active';
410 185 ▶ return Game::Go::Error->throw('not_marking')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->phase eq "marking" 0 185 185
411 unless $self->phase eq 'marking';
412  
413 185 my $m = $self->marking;
414 185 ▶ my $whose = $role eq 'proposer' ? $m->proposer : $m->answerer;
Branch: 1 branch
BranchTrueFalseTotal
$role eq 'proposer' ? : 125 60 185
415 185 ▶ return Game::Go::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless $colour == $whose and $colour == $m->turn 6 179 185
Condition100%3/3
$colour == $whose and $colour == $m->turn
!ll&&!rl&&r
24179
MC/DC100%2/2
$colour == $whose and $colour == $m->turn
$colour == $whose $colour == $m->turn
Truth table100%3/3
A and B
A= $colour == $whoseB= $colour == $m->turn
ABresult
0X0
100
111
416 unless $colour == $whose && $colour == $m->turn;
417 179 return undef;
418 }
419  
420 55 ▶sub _alive_set {
Subroutine _alive_set: called CC=3 SCAR=11.0
421 55 my ($self) = @_;
422 55 my $engine = $self->_engine;
423 55 my %alive;
424 55 for my $colour ($self->seats) {
425 110 $alive{$_} = 1 for @{ $engine->alive($colour) };
426 }
427 55 return \%alive;
428 }
429  
430 52 ▶sub mark {
Subroutine mark: called CC=6 SCAR=17.9
Pod documented
431 52 my ($self, $colour, $pt) = @_;
432 52 my $stop = $self->_marking_guard($colour, 'proposer');
433 52 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 2 50 52
434  
435 50 my $id = $self->_chain_id($pt);
436 50 ▶ return Game::Go::Error->throw('not_a_chain') unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 1 49 50
437  
438 return Game::Go::Error->throw('alive_chain')
439 49 ▶ if $self->_alive_set->{$pt};
Branch: 1 branch
BranchTrueFalseTotal
if $self->_alive_set->{$pt} 1 48 49
440  
441 48 my $on = $self->marking->toggle_dead($id);
442 48 ▶ $self->_log(Game::Go::Rules::letter($colour), ($on ? 'mark' : 'unmark'), { chain => $id });
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 47 1 48
443 48 ▶ return Game::Go::Move->new(
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 47 1 48
444 kind => ($on ? 'mark' : 'unmark'),
445 colour => $colour,
446 point => $id,
447 );
448 }
449  
450 13 ▶sub mark_seki {
Subroutine mark_seki: called CC=5 SCAR=16.1
Pod documented
451 13 my ($self, $colour, $pt) = @_;
452 13 my $stop = $self->_marking_guard($colour, 'proposer');
453 13 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 0 13 13
454  
455 13 ▶ return Game::Go::Error->throw('off_board')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_engine->at($pt) == 0 1 12 13
456 unless $self->_engine->at($pt) == EMPTY;
457  
458 12 my $on = $self->marking->toggle_seki($pt);
459 12 ▶ $self->_log(Game::Go::Rules::letter($colour), ($on ? 'seki' : 'unseki'), { region => $pt });
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 11 1 12
460 12 ▶ return Game::Go::Move->new(
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 11 1 12
461 kind => ($on ? 'seki' : 'unseki'),
462 colour => $colour,
463 point => $pt,
464 );
465 }
466  
467 60 ▶sub done {
Subroutine done: called CC=2 SCAR=6.9
Pod documented
468 60 my ($self, $colour) = @_;
469 60 my $stop = $self->_marking_guard($colour, 'proposer');
470 60 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 1 59 60
471  
472 59 $self->marking->proposed(1);
473 59 $self->_log(Game::Go::Rules::letter($colour), 'done');
474 59 return Game::Go::Move->new(kind => 'done', colour => $colour);
475 }
476  
477 42 ▶sub accept {
Subroutine accept: called CC=2 SCAR=6.9
Pod documented
478 42 my ($self, $colour) = @_;
479 42 my $stop = $self->_marking_guard($colour, 'answerer');
480 42 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 2 40 42
481  
482 40 $self->_log(Game::Go::Rules::letter($colour), 'accept');
483 40 return $self->_score('territory');
484 }
485  
486 18 ▶sub dispute {
Subroutine dispute: called CC=2 SCAR=6.9
Pod documented
487 18 my ($self, $colour) = @_;
488 18 my $stop = $self->_marking_guard($colour, 'answerer');
489 18 ▶ return $stop if $stop;
Branch: 1 branch
BranchTrueFalseTotal
if $stop 1 17 18
490  
491 17 my $proposer = $self->marking->proposer;
492 17 $self->disputes($self->disputes + 1);
493 17 $self->_log(Game::Go::Rules::letter($colour), 'dispute');
494  
495 17 $self->phase('play');
496 17 $self->marking(undef);
497 17 $self->_passes(0);
498 17 $self->turn($proposer);
499 17 $self->_log('sys', 'resumed', { first => Game::Go::Rules::letter($proposer) });
500  
501 17 return Game::Go::Move->new(kind => 'dispute', colour => $colour);
502 }
503  
504 42 ▶sub _score {
Subroutine _score: called CC=2 SCAR=6.9
505 42 my ($self, $how) = @_;
506 42 $self->scored_by($how);
507  
508 42 ▶ my $scored = $how eq 'area'
Branch: 1 branch
BranchTrueFalseTotal
$how eq 'area' ? : 2 40 42
509 ? Game::Go::Scoring::score_by_area($self)
510 : Game::Go::Scoring::score($self, scored_by => 'territory');
511  
512 42 $self->phase('play');
513 42 $self->marking(undef);
514 42 $self->outcome($scored);
515 42 return $self->_finish($scored->winner, 'score');
516 }
517  
518  
519 36 ▶sub resign {
Subroutine resign: called CC=3 SCAR=11.0
Pod documented
520 36 my ($self, $colour) = @_;
521 36 ▶ return Game::Go::Error->throw('bad_colour')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 0 36 36
522 unless Game::Go::Rules::is_colour($colour);
523 36 ▶ return Game::Go::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 35 36
524 unless $self->status eq 'active';
525  
526 35 $self->_log(Game::Go::Rules::letter($colour), 'resign');
527 35 return $self->_finish(Game::Go::Rules::other($colour), 'resign');
528 }
529  
530 9 ▶sub timeout {
Subroutine timeout: called CC=3 SCAR=11.3
Pod documented
531 9 my ($self, $colour) = @_;
532 9 ▶ return Game::Go::Error->throw('bad_colour')
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Go::Rules::is_colour($colour) 0 9 9
533 unless Game::Go::Rules::is_colour($colour);
534 9 ▶ return Game::Go::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 9 9
535 unless $self->status eq 'active';
536  
537 9 $self->_log('sys', 'timeout', { p => Game::Go::Rules::letter($colour) });
538 9 return $self->_finish(Game::Go::Rules::other($colour), 'timeout');
539 }
540  
541 4 ▶sub abandon {
Subroutine abandon: called CC=2 SCAR=7.0
Pod documented
542 4 my ($self) = @_;
543 4 ▶ return Game::Go::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 4 4
544 unless $self->status eq 'active';
545  
546 4 $self->_log('sys', 'abandon');
547 4 return $self->_finish(undef, 'abandoned');
548 }
549  
550 90 ▶sub _finish {
Subroutine _finish: called CC=2 SCAR=6.9
551 90 my ($self, $winner, $result) = @_;
552 90 $self->status('finished');
553 90 $self->winner($winner);
554 90 $self->result($result);
555 90 $self->turn(undef);
556 90 ▶ $self->_log('sys', 'game_end', {
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 86 4 90
557 winner => (defined $winner ? Game::Go::Rules::letter($winner) : undef),
558 result => $result,
559 scored_by => $self->scored_by,
560 });
561 90 return $result;
562 }
563  
564  
565 47 our %SITE_OWNED;
566 47 ▶BEGIN { %SITE_OWNED = map { $_ => 1 } qw(timeout abandon) }
Subroutine BEGIN: called CC=1 SCAR=0.0
567  
568 649 ▶sub replay {
Subroutine replay: called CC=31 SCAR=34.8
Pod documented
569 649 my ($self, $events, %o) = @_;
570 649 ▶ Carp::croak('Game::Go: replay wants an arrayref of events')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $events eq "ARRAY" 1 648 649
571 unless ref $events eq 'ARRAY';
572  
573 648 ▶ return $self->_rebuild($events, %o) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 316 332 648
574  
575 332 my $seen = 0;
576 332 my $seen_handicap = 0;
577 332 for my $e (@$events) {
578 3316 my $actor = $e->{actor};
579 3316 my $kind = $e->{kind};
580 3316 ▶ my $load = $e->{payload} || {};
Condition50%1/2
$e->{'payload'} || {}
l!l
33160
MC/DC0%0/1
$e->{'payload'} || {}
$e->{'payload'}
Truth table50%1/2
A
A= $e->{'payload'}
Aresult
11
00
581 3316 $seen++;
582  
583 3316 ▶ if ($actor eq 'sys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($actor eq "sys") 384 2932 3316
584 384 ▶ if ($kind eq 'timeout') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "timeout") 4 380 384
585 4 ▶ my $who = Game::Go::Rules::from_letter($load->{p} || '');
Condition50%1/2
$load->{'p'} || ''
l!l
40
MC/DC0%0/1
$load->{'p'} || ''
$load->{'p'}
Truth table50%1/2
A
A= $load->{'p'}
Aresult
11
00
586 4 ▶ Carp::croak("Game::Go: replay: timeout names no colour") unless $who;
Branch: 1 branch
BranchTrueFalseTotal
unless $who 0 4 4
587 4 $self->timeout($who);
588 4 next;
589 }
590 380 ▶ if ($kind eq 'abandon') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "abandon") 1 379 380
591 1 $self->abandon;
592 1 next;
593 }
594  
595 379 my $mine = $self->_last_sys($kind);
596 379 ▶ Carp::croak("Game::Go: replay: the log claims a 'sys $kind' the engine did not produce")
Branch: 1 branch
BranchTrueFalseTotal
unless $mine 1 378 379
597 unless $mine;
598 378 $self->_agree($kind, $load, $mine->{payload});
599 375 next;
600 }
601  
602 2932 my $colour = Game::Go::Rules::from_letter($actor);
603 2932 ▶ Carp::croak("Game::Go: replay: '$actor' is not a player") unless $colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour 1 2931 2932
604  
605 2931 ▶ if ($kind eq 'handicap') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "handicap") 25 2906 2931
606 25 my $mine = $self->_handicap_points;
607 25 my $want = $mine->[ $seen_handicap++ ];
608 25 ▶ Carp::croak('Game::Go: replay: the log has more handicap stones than this board places')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want 0 25 25
609 unless defined $want;
610 Carp::croak("Game::Go: replay: handicap stone $seen_handicap is on the wrong point")
611 25 ▶ unless defined $load->{pt} && $load->{pt} == $want;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $load->{"pt"} and $load->{"pt"} == $want 2 23 25
Condition66%2/3
defined $load->{"pt"} and $load->{"pt"} == $want
!ll&&!rl&&r
0223
MC/DC50%1/2
defined $load->{"pt"} and $load->{"pt"} == $want
defined $load->{"pt"} $load->{"pt"} == $want
Truth table66%2/3
A and B
A= defined $load->{"pt"}B= $load->{"pt"} == $want
ABresult
0X0
100
111
612 23 next;
613 }
614  
615 2906 my $out;
616 2906 ▶ if ($kind eq 'play') { $out = $self->play($colour, $load->{pt}) }
Branch: 8 branches
BranchTrueFalseTotal
if ($kind eq 'play') { } 2612 294 2906
elsif ($kind eq 'pass') { } 231 63 294
elsif ($kind eq 'resign') { } 6 57 63
elsif ($kind eq 'mark' or $kind eq 'unmark') { } 30 27 57
elsif ($kind eq 'seki' or $kind eq 'unseki') { } 1 26 27
elsif ($kind eq 'done') { } 14 12 26
elsif ($kind eq 'accept') { } 6 6 12
elsif ($kind eq 'dispute') { } 5 1 6
617 231 elsif ($kind eq 'pass') { $out = $self->pass($colour) }
618 6 elsif ($kind eq 'resign') { $out = $self->resign($colour); next }
619  
620 ▶ elsif ($kind eq 'mark' || $kind eq 'unmark') {
Condition66%2/3
$kind eq "mark" or $kind eq "unmark"
l!l&&r!l&&!r
30027
MC/DC50%1/2
$kind eq "mark" or $kind eq "unmark"
$kind eq "mark" $kind eq "unmark"
Truth table66%2/3
A or B
A= $kind eq "mark"B= $kind eq "unmark"
ABresult
1X1
011
000
621 30 $out = $self->mark($colour, $load->{chain});
622 30 ▶ Carp::croak("Game::Go: replay: the log's '$kind' came out as a '"
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Move" and $out->kind ne $kind 0 30 30
Condition33%1/3
ref $out eq "Game::Go::Move" and $out->kind ne $kind
!ll&&!rl&&r
0300
MC/DC0%0/2
ref $out eq "Game::Go::Move" and $out->kind ne $kind
ref $out eq "Game::Go::Move" $out->kind ne $kind
Truth table33%1/3
A and B
A= ref $out eq "Game::Go::Move"B= $out->kind ne $kind
ABresult
0X0
100
111
623 . $out->kind . "'")
624 if ref $out eq 'Game::Go::Move' && $out->kind ne $kind;
625 }
626 ▶ elsif ($kind eq 'seki' || $kind eq 'unseki') {
Condition66%2/3
$kind eq "seki" or $kind eq "unseki"
l!l&&r!l&&!r
1026
MC/DC50%1/2
$kind eq "seki" or $kind eq "unseki"
$kind eq "seki" $kind eq "unseki"
Truth table66%2/3
A or B
A= $kind eq "seki"B= $kind eq "unseki"
ABresult
1X1
011
000
627 1 $out = $self->mark_seki($colour, $load->{region});
628 1 ▶ Carp::croak("Game::Go: replay: the log's '$kind' came out as a '"
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Move" and $out->kind ne $kind 0 1 1
Condition33%1/3
ref $out eq "Game::Go::Move" and $out->kind ne $kind
!ll&&!rl&&r
010
MC/DC0%0/2
ref $out eq "Game::Go::Move" and $out->kind ne $kind
ref $out eq "Game::Go::Move" $out->kind ne $kind
Truth table33%1/3
A and B
A= ref $out eq "Game::Go::Move"B= $out->kind ne $kind
ABresult
0X0
100
111
629 . $out->kind . "'")
630 if ref $out eq 'Game::Go::Move' && $out->kind ne $kind;
631 }
632 14 elsif ($kind eq 'done') { $out = $self->done($colour) }
633 6 elsif ($kind eq 'accept') { $out = $self->accept($colour); next }
634 5 elsif ($kind eq 'dispute') { $out = $self->dispute($colour) }
635  
636 1 else { Carp::croak("Game::Go: replay: no such player event '$kind'") }
637  
638 2893 ▶ Carp::croak("Game::Go: replay: the log's '$kind' was refused: " . $out->message)
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 3 2890 2893
639 if ref $out eq 'Game::Go::Error';
640 }
641  
642 321 return $seen;
643 }
644  
645 379 ▶sub _last_sys {
Subroutine _last_sys: called CC=4 SCAR=13.9
646 379 my ($self, $kind) = @_;
647 379 for my $e (reverse @{ $self->log }) {
648 418 ▶ return $e if $e->{actor} eq 'sys' && $e->{kind} eq $kind;
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"actor"} eq "sys" and $e->{"kind"} eq $kind 378 40 418
Condition100%3/3
$e->{"actor"} eq "sys" and $e->{"kind"} eq $kind
!ll&&!rl&&r
355378
MC/DC100%2/2
$e->{"actor"} eq "sys" and $e->{"kind"} eq $kind
$e->{"actor"} eq "sys" $e->{"kind"} eq $kind
Truth table100%3/3
A and B
A= $e->{"actor"} eq "sys"B= $e->{"kind"} eq $kind
ABresult
0X0
100
111
649 }
650 1 return undef;
651 }
652  
653 378 ▶sub _agree {
Subroutine _agree: called CC=3 SCAR=11.0
654 378 my ($self, $kind, $theirs, $mine) = @_;
655 378 for my $key (sort keys %$mine) {
656 Carp::croak("Game::Go: replay: 'sys $kind' disagrees about $key")
657 1719 ▶ unless _same($mine->{$key}, $theirs->{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless _same($mine->{$key}, $theirs->{$key}) 3 1716 1719
658 }
659 375 return;
660 }
661  
662 1731 ▶sub _same {
Subroutine _same: called CC=13 SCAR=28.3
663 1731 my ($a, $b) = @_;
664 1731 ▶ return 1 if !defined $a && !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $b 12 1719 1731
Condition66%2/3
defined $a or defined $b
l!l&&r!l&&!r
1719012
MC/DC50%1/2
defined $a or defined $b
defined $a defined $b
Truth table66%2/3
A or B
A= defined $aB= defined $b
ABresult
1X1
011
000
665 1719 ▶ return 0 if !defined $a || !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a and defined $b 0 1719 1719
Condition33%1/3
defined $a and defined $b
!ll&&!rl&&r
001719
MC/DC0%0/2
defined $a and defined $b
defined $a defined $b
Truth table33%1/3
A and B
A= defined $aB= defined $b
ABresult
0X0
100
111
666  
667 1719 ▶ if (ref $a eq 'ARRAY' || ref $b eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $a eq "ARRAY" or ref $b eq "ARRAY") 8 1711 1719
Condition66%2/3
ref $a eq "ARRAY" or ref $b eq "ARRAY"
l!l&&r!l&&!r
801711
MC/DC50%1/2
ref $a eq "ARRAY" or ref $b eq "ARRAY"
ref $a eq "ARRAY" ref $b eq "ARRAY"
Truth table66%2/3
A or B
A= ref $a eq "ARRAY"B= ref $b eq "ARRAY"
ABresult
1X1
011
000
668 8 ▶ return 0 unless ref $a eq 'ARRAY' && ref $b eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a eq "ARRAY" and ref $b eq "ARRAY" 0 8 8
Condition33%1/3
ref $a eq "ARRAY" and ref $b eq "ARRAY"
!ll&&!rl&&r
008
MC/DC0%0/2
ref $a eq "ARRAY" and ref $b eq "ARRAY"
ref $a eq "ARRAY" ref $b eq "ARRAY"
Truth table33%1/3
A and B
A= ref $a eq "ARRAY"B= ref $b eq "ARRAY"
ABresult
0X0
100
111
669 8 ▶ return 0 unless @$a == @$b;
Branch: 1 branch
BranchTrueFalseTotal
unless @$a == @$b 0 8 8
670 8 ▶ _same($a->[$_], $b->[$_]) or return 0 for 0 .. $#$a;
Branch: 1 branch
BranchTrueFalseTotal
_same($a->[$_], $b->[$_]) or return 0 12 0 12
671 8 return 1;
672 }
673  
674 1711 ▶ return $a eq $b ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$a eq $b ? : 1708 3 1711
675 }
676  
677 316 ▶sub _rebuild {
Subroutine _rebuild: called CC=23 SCAR=33.4
678 316 my ($class, $events, %o) = @_;
679  
680 316 my $start = $events->[0];
681 Carp::croak("Game::Go: replay: the log does not begin with a 'sys start'")
682 unless ref $start eq 'HASH'
683 && ($start->{actor} // '') eq 'sys'
684 316 ▶ && ($start->{kind} // '') eq 'start';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $start eq "HASH" and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start" 1 315 316
Condition60%6/10
ref $start eq "HASH" and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start"
!ll&&!rl&&r
10315
ref $start eq "HASH" and ($start->{"actor"} // "") eq "sys"
!ll&&!rl&&r
01315
$start->{"actor"} // ""
l!l
3160
$start->{"kind"} // ""
l!l
3150
MC/DC0%0/3
ref $start eq "HASH" and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start"
ref $start eq "HASH" $start->{"actor"} $start->{"kind"}
Truth table25%1/4
A and B and C
A= ref $start eq "HASH"B= $start->{"actor"}C= $start->{"kind"}
ABCresult
0XX0
10X0
1100
1111
685  
686 315 ▶ my $p = $start->{payload} || {};
Condition50%1/2
$start->{'payload'} || {}
l!l
3150
MC/DC0%0/1
$start->{'payload'} || {}
$start->{'payload'}
Truth table50%1/2
A
A= $start->{'payload'}
Aresult
11
00
687 315 ▶ my %args = map { defined $p->{$_} ? ($_ => $p->{$_}) : () } qw(size komi handicap ko);
Branch: 1 branch
BranchTrueFalseTotal
defined $p->{$_} ? : 1260 0 1260
688 315 ▶ $args{first} = $p->{turn} if defined $p->{turn};
Branch: 1 branch
BranchTrueFalseTotal
if defined $p->{"turn"} 6 309 315
689 315 ▶ $args{seed} = $o{seed} if defined $o{seed};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"seed"} 157 158 315
690  
691 315 ▶ my $size = $args{size} // 19;
Condition50%1/2
$args{"size"} // 19
l!l
3150
MC/DC0%0/1
$args{"size"} // 19
$args{"size"}
Truth table50%1/2
A
A= $args{"size"}
Aresult
11
00
692 Carp::croak("Game::Go: replay: the log's 'sys start' names size '$size'")
693 315 ▶ unless $size =~ /\A[0-9]+\z/ && grep { $_ == $size } Game::Go::Rules::sizes();
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /\A[0-9]+\z/ and grep {$_ == $size;} Game::Go::Rules::sizes() 1 314 315
Condition66%2/3
$size =~ /\A[0-9]+\z/ and grep {$_ == $size;} Game::Go::Rules::sizes()
!ll&&!rl&&r
01314
MC/DC50%1/2
$size =~ /\A[0-9]+\z/ and grep {$_ == $size;} Game::Go::Rules::sizes()
$size =~ /\A[0-9]+\z/ grep {$_ == $size;} Game::Go::Rules::sizes()
Truth table66%2/3
A and B
A= $size =~ /\A[0-9]+\z/B= grep {$_ == $size;} Game::Go::Rules::sizes()
ABresult
0X0
100
111
694  
695 314 my $next = $events->[1];
696 314 ▶ if (ref $next eq 'HASH' && ($next->{actor} // '') eq 'sys' && ($next->{kind} // '') eq 'setup') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $next eq "HASH" and ($next->{"actor"} // "") eq "sys" and ($next->{"kind"} // "") eq "setup") 4 310 314
Condition80%8/10
ref $next eq "HASH" and ($next->{"actor"} // "") eq "sys" and ($next->{"kind"} // "") eq "setup"
!ll&&!rl&&r
30734
ref $next eq "HASH" and ($next->{"actor"} // "") eq "sys"
!ll&&!rl&&r
1121957
$next->{"actor"} // ""
l!l
2020
$next->{"kind"} // ""
l!l
70
MC/DC33%1/3
ref $next eq "HASH" and ($next->{"actor"} // "") eq "sys" and ($next->{"kind"} // "") eq "setup"
ref $next eq "HASH" $next->{"actor"} $next->{"kind"}
Truth table50%2/4
A and B and C
A= ref $next eq "HASH"B= $next->{"actor"}C= $next->{"kind"}
ABCresult
0XX0
10X0
1100
1111
697 4 my $probe = Game::Go::Engine->new(size => $size);
698 4 my %setup;
699 4 ▶ my $placed = $next->{payload} || {};
Condition50%1/2
$next->{'payload'} || {}
l!l
40
MC/DC0%0/1
$next->{'payload'} || {}
$next->{'payload'}
Truth table50%1/2
A
A= $next->{'payload'}
Aresult
11
00
700 4 for my $letter (keys %$placed) {
701 8 my $points = $placed->{$letter};
702 Carp::croak("Game::Go: replay: the log's 'sys setup' has a bad list for '$letter'")
703 8 ▶ unless ref $points eq 'ARRAY' && !grep { !defined $_ || !/\A[0-9]+\z/ } @$points;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $points eq "ARRAY" and not grep({not defined $_ && /\A[0-9]+\z/;} @$points) 0 8 8
Condition33%2/6
ref $points eq "ARRAY" and not grep({not defined $_ && /\A[0-9]+\z/;} @$points)
!ll&&!rl&&r
008
defined $_ && /\A[0-9]+\z/
!ll&&!rl&&r
0012
MC/DC0%0/2
ref $points eq "ARRAY" and not grep({not defined $_ && /\A[0-9]+\z/;} @$points)
ref $points eq "ARRAY" not grep({not defined $_ && /\A[0-9]+\z/;} @$points)
MC/DC0%0/2
defined $_ && /\A[0-9]+\z/
defined $_ /\A[0-9]+\z/
Truth table33%1/3
A and not(B)
A= ref $points eq "ARRAY"B= not grep({not defined $_ && /\A[0-9]+\z/;} @$points)
ABresult
0X0
100
111
Truth table33%1/3
A && B
A= defined $_B= /\A[0-9]+\z/
ABresult
0X0
100
111
704 8 $setup{$letter} = [ map { [ $probe->col_of($_), $probe->row_of($_) ] } @$points ];
705 }
706 4 $args{setup} = \%setup;
707 }
708  
709 314 my $game = $class->new(%args);
710 313 $game->replay($events);
711 311 return $game;
712 }
713  
714 245 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
715 245 my ($self) = @_;
716 245 return ref($self)->replay($self->events, seed => $self->_held_seed);
717 }
718  
719 47 ▶use constant TEXT_HEAD => 'game-go 1';
Subroutine BEGIN: called CC=1 SCAR=0.0
720  
721 718 ▶sub to_text {
Subroutine to_text: called CC=5 SCAR=16.2
Pod documented
722 718 my ($self) = @_;
723 718 my @lines = (TEXT_HEAD);
724  
725 718 my $seed = $self->_held_seed;
726 718 ▶ if (defined $seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seed) 715 3 718
727 715 ▶ Carp::croak('Game::Go: to_text cannot write a seed that is not a byte string')
Branch: 1 branch
BranchTrueFalseTotal
if $seed =~ /[^\x00-\xff]/ 0 715 715
728 if $seed =~ /[^\x00-\xff]/;
729 715 push @lines, 'seed ' . unpack('H*', $seed);
730 }
731  
732 718 for my $e (@{ $self->log }) {
733 28962 ▶ my $p = $e->{payload} || {};
Condition50%1/2
$e->{'payload'} || {}
l!l
289620
MC/DC0%0/1
$e->{'payload'} || {}
$e->{'payload'}
Truth table50%1/2
A
A= $e->{'payload'}
Aresult
11
00
734 push @lines, join ' ', $e->{actor}, $e->{kind},
735 28962 map { "$_=" . _value_text($p->{$_}) } sort keys %$p;
736 }
737  
738 718 return join("\n", @lines) . "\n";
739 }
740  
741 45689 ▶sub _value_text {
Subroutine _value_text: called CC=3 SCAR=11.0
742 45689 my ($v) = @_;
743 45689 ▶ return '~' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 8 45681 45689
744 45681 ▶ return '[' . join(',', @$v) . ']' if ref $v eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 20988 24693 45681
745 24693 return $v;
746 }
747  
748 81 ▶sub from_text {
Subroutine from_text: called CC=17 SCAR=28.3
Pod documented
749 81 my ($class, $text) = @_;
750 81 ▶ Carp::croak('Game::Go: from_text wants the string to_text wrote')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 79 81
Condition100%3/3
defined $text and not ref $text
!ll&&!rl&&r
1179
MC/DC100%2/2
defined $text and not ref $text
defined $text not ref $text
Truth table100%3/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
751 unless defined $text && !ref $text;
752  
753 79 my @lines = split /\n/, $text;
754 79 ▶ Carp::croak("Game::Go: from_text: this is not a saved game, it does not begin '" . TEXT_HEAD . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless @lines and $lines[0] eq "game-go 1" 2 77 79
Condition100%3/3
@lines and $lines[0] eq "game-go 1"
!ll&&!rl&&r
1177
MC/DC100%2/2
@lines and $lines[0] eq "game-go 1"
@lines $lines[0] eq "game-go 1"
Truth table100%3/3
A and B
A= @linesB= $lines[0] eq "game-go 1"
ABresult
0X0
100
111
755 unless @lines && $lines[0] eq TEXT_HEAD;
756 77 shift @lines;
757  
758 77 my %o;
759 77 ▶ if (@lines && $lines[0] =~ /\Aseed/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines and $lines[0] =~ /\Aseed/) 74 3 77
Condition100%3/3
@lines and $lines[0] =~ /\Aseed/
!ll&&!rl&&r
1274
MC/DC100%2/2
@lines and $lines[0] =~ /\Aseed/
@lines $lines[0] =~ /\Aseed/
Truth table100%3/3
A and B
A= @linesB= $lines[0] =~ /\Aseed/
ABresult
0X0
100
111
760 74 my ($hex) = $lines[0] =~ /\Aseed ((?:[0-9a-f]{2})*)\z/;
761 74 ▶ Carp::croak('Game::Go: from_text: the seed line is not an even run of hex digits')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hex 1 73 74
762 unless defined $hex;
763 73 $o{seed} = pack 'H*', $hex;
764 73 shift @lines;
765 }
766  
767 76 my @events;
768 76 my $n = 0;
769 76 for my $line (@lines) {
770 1712 $n++;
771 1712 my ($actor, $kind, @pairs) = split / /, $line, -1;
772 1712 ▶ Carp::croak("Game::Go: from_text: event $n is not an actor and a kind")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind and $actor =~ /\A(?:sys|b|w)\z/ and $kind =~ /\A[a-z_]+\z/ 1 1711 1712
Condition66%4/6
defined $kind and $actor =~ /\A(?:sys|b|w)\z/ and $kind =~ /\A[a-z_]+\z/
!ll&&!rl&&r
101711
defined $kind and $actor =~ /\A(?:sys|b|w)\z/
!ll&&!rl&&r
011711
MC/DC33%1/3
defined $kind and $actor =~ /\A(?:sys|b|w)\z/ and $kind =~ /\A[a-z_]+\z/
defined $kind $actor =~ /\A(?:sys|b|w)\z/ $kind =~ /\A[a-z_]+\z/
Truth table50%2/4
A and B and C
A= defined $kindB= $actor =~ /\A(?:sys|b|w)\z/C= $kind =~ /\A[a-z_]+\z/
ABCresult
0XX0
10X0
1100
1111
773 unless defined $kind && $actor =~ /\A(?:sys|b|w)\z/ && $kind =~ /\A[a-z_]+\z/;
774  
775 1711 my %payload;
776 1711 for my $pair (@pairs) {
777 3104 my ($key, $raw) = $pair =~ /\A([a-z_]+)=(.*)\z/;
778 3104 ▶ Carp::croak("Game::Go: from_text: event $n has '$pair', which is not a key=value")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 3103 3104
779 unless defined $key;
780 Carp::croak("Game::Go: from_text: event $n names '$key' twice")
781 3103 ▶ if exists $payload{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $payload{$key} 1 3102 3103
782 3102 my ($ok, $value) = _value_from($raw);
783 3102 ▶ Carp::croak("Game::Go: from_text: event $n has '$raw' for '$key', which is not a value")
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 3101 3102
784 unless $ok;
785 3101 $payload{$key} = $value;
786 }
787  
788 1708 push @events, { actor => $actor, kind => $kind, payload => \%payload };
789 }
790  
791 72 ▶ Carp::croak('Game::Go: from_text: there are no events after the header') unless @events;
Branch: 1 branch
BranchTrueFalseTotal
unless @events 1 71 72
792  
793 71 return $class->replay(\@events, %o);
794 }
795  
796 3102 ▶sub _value_from {
Subroutine _value_from: called CC=6 SCAR=17.9
797 3102 my ($raw) = @_;
798 3102 my $word = qr/-?[A-Za-z0-9_.]+/;
799 3102 ▶ return (1, undef) if $raw eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $raw eq "~" 8 3094 3102
800 3094 ▶ if (my ($list) = $raw =~ /\A\[(.*)\]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($list) = $raw =~ /\A\[(.*)\]\z/) 1326 1768 3094
801 1326 my @items = split /,/, $list, -1;
802 1326 ▶ @items = () if $list eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $list eq "" 1154 172 1326
803 1326 ▶ return (0) if grep { !/\A$word\z/ } @items;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not /\A$word\z/;} @items 0 1326 1326
804 1326 return (1, \@items);
805 }
806 1768 ▶ return (0) unless $raw =~ /\A$word\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $raw =~ /\A$word\z/ 1 1767 1768
807 1767 return (1, $raw);
808 }
809  
810 100 ▶sub sizes { Game::Go::Rules::sizes() }
Subroutine sizes: called CC=1 SCAR=0.0
Pod documented
811 0 ▶sub other { Game::Go::Rules::other($_[-1]) }
Subroutine other: not called CC=1 SCAR=6.9
Pod documented
812 0 ▶sub refusal { Game::Go::Rules::refusal($_[-1]) }
Subroutine refusal: not called CC=1 SCAR=6.9
Pod documented
813 0 ▶sub refusals { Game::Go::Rules::refusals() }
Subroutine refusals: not called CC=1 SCAR=6.9
Pod documented
814 3 ▶sub abi_version { Game::Go::Engine::_abi_version() }
Subroutine abi_version: called CC=1 SCAR=0.0
Pod documented
815  
816 47 1;
817  
818 __END__
« blib/lib/Game/Go/Terminal.pm ↑ Summary blib/lib/Game/Go/Notation.pm »