blib/lib/Game/Go/SGF.pm

filter: Statementstmt 33.4% 80 / 239 Branchbran 17.7% 27 / 152 Conditioncond 3.9% 2 / 51 MC/DCmcdc 0.0% 0 / 32 Subroutinesub 45.4% 10 / 22 Podpod 100% 4 / 4 totaltotal 24.6% 123 / 500 CC 108 SCAR 64.9
CC
108
Cov
25%
read
1892.0
parse_result
182.0
write
32.1
CRAP 660.2
↑ Summary blib/lib/Game/Go/GTP.pm »
1 package Game::Go::SGF;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Game::Go;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Game::Go::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Game::Go::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = '0.03';
14  
15 2 my $B = Game::Go::Rules::BLACK;
16 2 my $W = Game::Go::Rules::WHITE;
17  
18 0 ▶sub _skip_ws { $_[1] += 1 while substr($_[0], $_[1], 1) =~ /\A\s\z/ }
Subroutine _skip_ws: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
substr($_[0], $_[1], 1) =~ /\A\s\z/ and do { $_[1] += 1 } 0 0 0
19  
20 0 ▶sub _parse_value {
Subroutine _parse_value: not called CC=4 SCAR=30.0
21 0 my ($text, $p) = @_;
22 0 $$p++;
23 0 my $v = '';
24 0 ▶ while ($$p < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$p < length $text) 0 0 0
25 0 my $c = substr($text, $$p, 1);
26 0 ▶ if ($c eq "\\") {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq "\\") 0 0 0
27 0 $$p++;
28 0 $v .= substr($text, $$p, 1);
29 0 $$p++;
30 0 next;
31 }
32 0 ▶ last if $c eq ']';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "]" 0 0 0
33 0 $v .= $c;
34 0 $$p++;
35 }
36 0 $$p++;
37 0 return $v;
38 }
39  
40 0 ▶sub _parse_node {
Subroutine _parse_node: not called CC=5 SCAR=34.0
41 0 my ($text, $p) = @_;
42 0 $$p++;
43 0 my %props;
44 0 ▶ while ($$p < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$p < length $text) 0 0 0
45 0 _skip_ws($text, $$p);
46 0 my ($ident) = substr($text, $$p) =~ /\A([A-Za-z]+)/;
47 0 ▶ last unless defined $ident;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ident 0 0 0
48 0 $$p += length $ident;
49  
50 0 (my $key = $ident) =~ s/[a-z]//g;
51  
52 0 my @values;
53 0 while (1) {
54 0 _skip_ws($text, $$p);
55 0 ▶ last unless substr($text, $$p, 1) eq '[';
Branch: 1 branch
BranchTrueFalseTotal
unless substr($text, $$p, 1) eq "[" 0 0 0
56 0 push @values, _parse_value($text, $p);
57 }
58 0 ▶ $props{$key} = \@values if length $key;
Branch: 1 branch
BranchTrueFalseTotal
if length $key 0 0 0
59 }
60 0 return \%props;
61 }
62  
63 0 ▶sub _parse_tree {
Subroutine _parse_tree: not called CC=5 SCAR=34.0
64 0 my ($text, $p) = @_;
65 0 $$p++;
66 0 my (@nodes, @children);
67 0 ▶ while ($$p < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$p < length $text) 0 0 0
68 0 _skip_ws($text, $$p);
69 0 my $c = substr($text, $$p, 1);
70 0 ▶ if ($c eq ';') { push @nodes, _parse_node($text, $p) }
Branch: 3 branches
BranchTrueFalseTotal
if ($c eq ';') { } 0 0 0
elsif ($c eq '(') { } 0 0 0
elsif ($c eq ')') { } 0 0 0
71 0 elsif ($c eq '(') { push @children, _parse_tree($text, $p) }
72 0 elsif ($c eq ')') { $$p++; last }
73 0 else { $$p++ }
74 }
75 0 return { nodes => \@nodes, children => \@children };
76 }
77  
78 0 ▶sub _main_line {
Subroutine _main_line: not called CC=2 SCAR=17.9
79 0 my ($tree) = @_;
80 0 my @nodes = @{ $tree->{nodes} };
81 0 my $t = $tree;
82 0 ▶ while (@{ $t->{children} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$t->{"children"};}) 0 0 0
83 0 $t = $t->{children}[0];
84 0 push @nodes, @{ $t->{nodes} };
85 }
86 0 return \@nodes;
87 }
88  
89  
90 0 ▶sub read {
Subroutine read: not called CC=43 SCAR=75.5
Pod documented
91 0 my ($text, %o) = @_;
92 0 ▶ Carp::croak('Game::Go::SGF: nothing to read') unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 0 0
Condition0%0/3
defined $text and length $text
!ll&&!rl&&r
000
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table0%0/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
93  
94 0 my $pos = index($text, '(');
95 0 ▶ Carp::croak('Game::Go::SGF: no game tree here') if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 0 0
96  
97 0 my $nodes = _main_line(_parse_tree($text, \$pos));
98 0 ▶ Carp::croak('Game::Go::SGF: the game tree is empty') unless @$nodes;
Branch: 1 branch
BranchTrueFalseTotal
unless @$nodes 0 0 0
99  
100 0 my $root = $nodes->[0];
101 0 ▶ my $one = sub { my $v = $root->{ $_[0] }; return $v && @$v ? $v->[0] : undef };
Subroutine __ANON__: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
$v && @$v ? : 0 0 0
Condition0%0/3
$v and @$v
!ll&&!rl&&r
000
MC/DC0%0/2
$v and @$v
$v @$v
Truth table0%0/3
A and B
A= $vB= @$v
ABresult
0X0
100
111
102  
103 0 my $gm = $one->('GM');
104 0 ▶ Carp::croak("Game::Go::SGF: GM[$gm] is not Go") if defined $gm && $gm ne '1';
Branch: 1 branch
BranchTrueFalseTotal
if defined $gm and $gm ne 1 0 0 0
Condition0%0/3
defined $gm and $gm ne 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $gm and $gm ne 1
defined $gm $gm ne 1
Truth table0%0/3
A and B
A= defined $gmB= $gm ne 1
ABresult
0X0
100
111
105  
106 0 my $size = $one->('SZ');
107 0 ▶ $size = 19 unless defined $size && length $size;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $size and length $size 0 0 0
Condition0%0/3
defined $size and length $size
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and length $size
defined $size length $size
Truth table0%0/3
A and B
A= defined $sizeB= length $size
ABresult
0X0
100
111
108 Carp::croak("Game::Go::SGF: SZ[$size] is not a square board this engine offers")
109 0 ▶ unless $size =~ /\A[0-9]+\z/ && grep { $_ == $size } Game::Go->sizes;
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /\A[0-9]+\z/ and grep {$_ == $size;} "Game::Go"->sizes 0 0 0
Condition0%0/3
$size =~ /\A[0-9]+\z/ and grep {$_ == $size;} "Game::Go"->sizes
!ll&&!rl&&r
000
MC/DC0%0/2
$size =~ /\A[0-9]+\z/ and grep {$_ == $size;} "Game::Go"->sizes
$size =~ /\A[0-9]+\z/ grep {$_ == $size;} "Game::Go"->sizes
Truth table0%0/3
A and B
A= $size =~ /\A[0-9]+\z/B= grep {$_ == $size;} "Game::Go"->sizes
ABresult
0X0
100
111
110  
111 0 my $komi = $one->('KM');
112 0 my $ha = $one->('HA');
113  
114 0 my %setup;
115 0 for my $pair (['AB', 'b'], ['AW', 'w']) {
116 0 my ($prop, $letter) = @$pair;
117 0 ▶ next unless $root->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless $root->{$prop} 0 0 0
118 0 for my $v (@{ $root->{$prop} }) {
119 0 my @cr = Game::Go::Notation::from_sgf($size, $v);
120 0 ▶ next unless @cr;
Branch: 1 branch
BranchTrueFalseTotal
unless @cr 0 0 0
121 0 push @{ $setup{$letter} }, \@cr;
122 }
123 }
124  
125 0 ▶ my $ab = scalar @{ $setup{b} || [] };
Condition0%0/2
$setup{'b'} || []
l!l
00
MC/DC0%0/1
$setup{'b'} || []
$setup{'b'}
Truth table0%0/2
A
A= $setup{'b'}
Aresult
11
00
126 0 ▶ if (defined $ha && $ha =~ /\A[0-9]+\z/ && $ha >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ha and $ha =~ /\A[0-9]+\z/ and $ha >= 2) 0 0 0
Condition0%0/6
defined $ha and $ha =~ /\A[0-9]+\z/ and $ha >= 2
!ll&&!rl&&r
000
defined $ha and $ha =~ /\A[0-9]+\z/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $ha and $ha =~ /\A[0-9]+\z/ and $ha >= 2
defined $ha $ha =~ /\A[0-9]+\z/ $ha >= 2
Truth table0%0/4
A and B and C
A= defined $haB= $ha =~ /\A[0-9]+\z/C= $ha >= 2
ABCresult
0XX0
10X0
1100
1111
127 0 ▶ Carp::croak("Game::Go::SGF: HA[$ha] but AB places $ab stones")
Branch: 1 branch
BranchTrueFalseTotal
unless $ab == $ha 0 0 0
128 unless $ab == $ha;
129 }
130  
131 0 my $pl = $one->('PL');
132 my $first = defined $pl && $pl =~ /\A([BWbw])\z/ ? lc($1)
133 0 ▶ : ($ab && !@{ $setup{w} || [] }) ? 'w'
Branch: 2 branches
BranchTrueFalseTotal
defined $pl && $pl =~ /\A([BWbw])\z/ ? : 0 0 0
$ab && !@{$setup{'w'} or [];} ? : 0 0 0
Condition0%0/8
defined $pl and $pl =~ /\A([BWbw])\z/
!ll&&!rl&&r
000
$ab and not @{$setup{"w"} or [];}
!ll&&!rl&&r
000
$setup{'w'} || []
l!l
00
MC/DC0%0/2
defined $pl and $pl =~ /\A([BWbw])\z/
defined $pl $pl =~ /\A([BWbw])\z/
MC/DC0%0/2
$ab and not @{$setup{"w"} or [];}
$ab not @{$setup{"w"} or [];}
MC/DC0%0/1
$setup{'w'} || []
$setup{'w'}
Truth table0%0/3
A and B
A= defined $plB= $pl =~ /\A([BWbw])\z/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $abB= not @{$setup{"w"} or [];}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $setup{'w'}
Aresult
11
00
134 : 'b';
135  
136 my $game = Game::Go->new(
137 size => $size,
138 (defined $komi && length $komi ? (komi => _round_half($komi)) : ()),
139 (%setup ? (setup => \%setup) : ()),
140 first => $first,
141 0 ▶ ko => ($o{lenient} ? 'simple' : 'positional'),
Branch: 3 branches
BranchTrueFalseTotal
defined $komi && length $komi ? : 0 0 0
%setup ? : 0 0 0
$o{'lenient'} ? : 0 0 0
Condition0%0/3
defined $komi and length $komi
!ll&&!rl&&r
000
MC/DC0%0/2
defined $komi and length $komi
defined $komi length $komi
Truth table0%0/3
A and B
A= defined $komiB= length $komi
ABresult
0X0
100
111
142 );
143  
144 0 my $moves = 0;
145 0 for my $node (@$nodes[1 .. $#$nodes]) {
146 0 for my $pair (['B', $B], ['W', $W]) {
147 0 my ($prop, $colour) = @$pair;
148 0 ▶ next unless exists $node->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $node->{$prop} 0 0 0
149 0 my $v = $node->{$prop}[0];
150 0 ▶ $v = '' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
151 0 $moves++;
152  
153 0 ▶ if ($v eq '' || $v eq 'tt') {
Branch: 1 branch
BranchTrueFalseTotal
if ($v eq "" or $v eq "tt") 0 0 0
Condition0%0/3
$v eq "" or $v eq "tt"
l!l&&r!l&&!r
000
MC/DC0%0/2
$v eq "" or $v eq "tt"
$v eq "" $v eq "tt"
Truth table0%0/3
A or B
A= $v eq ""B= $v eq "tt"
ABresult
1X1
011
000
154 0 my $out = $game->pass($colour);
155 0 _refused($out, $moves, 'pass', $o{lenient});
156 0 next;
157 }
158  
159 0 my @cr = Game::Go::Notation::from_sgf($size, $v);
160 0 ▶ Carp::croak("Game::Go::SGF: move $moves, '$v' is not a point on a ${size}x$size board")
Branch: 1 branch
BranchTrueFalseTotal
unless @cr 0 0 0
161 unless @cr;
162  
163 0 ▶ _resume($game, $moves) if $game->phase eq 'marking';
Branch: 1 branch
BranchTrueFalseTotal
if $game->phase eq "marking" 0 0 0
164  
165 0 my $played = $game->play($colour, $game->point(@cr));
166 0 _refused($played, $moves, $v, $o{lenient});
167 }
168 }
169  
170 0 my %territory;
171 0 for my $node (@$nodes) {
172 0 for my $pair (['TB', 'b'], ['TW', 'w']) {
173 0 my ($prop, $letter) = @$pair;
174 0 ▶ next unless $node->{$prop};
Branch: 1 branch
BranchTrueFalseTotal
unless $node->{$prop} 0 0 0
175 0 for my $v (@{ $node->{$prop} }) {
176 0 my @cr = Game::Go::Notation::from_sgf($size, $v);
177 0 ▶ push @{ $territory{$letter} }, \@cr if @cr;
Branch: 1 branch
BranchTrueFalseTotal
if @cr 0 0 0
178 }
179 }
180 }
181  
182 return {
183 0 ▶ game => $game,
Branch: 1 branch
BranchTrueFalseTotal
defined $ha ? : 0 0 0
184 size => $size,
185 komi => $game->komi,
186 handicap => (defined $ha ? $ha : 0),
187 moves => $moves,
188 first => $first,
189 result => parse_result($one->('RE')),
190 players => { b => $one->('PB'), w => $one->('PW') },
191 ranks => { b => $one->('BR'), w => $one->('WR') },
192 date => $one->('DT'),
193 rules => $one->('RU'),
194 territory => \%territory,
195 setup => \%setup,
196 };
197 }
198  
199 0 ▶sub _resume {
Subroutine _resume: not called CC=4 SCAR=30.0
200 0 my ($game, $n) = @_;
201 0 ▶ my $m = $game->marking or return;
Branch: 1 branch
BranchTrueFalseTotal
my $m = $game->marking or return 0 0 0
202  
203 0 my $done = $game->done($m->proposer);
204 0 ▶ Carp::croak("Game::Go::SGF: move $n resumes a stopped game and the engine refused: "
Branch: 1 branch
BranchTrueFalseTotal
if ref $done eq "Game::Go::Error" 0 0 0
205 . $done->message) if ref $done eq 'Game::Go::Error';
206  
207 0 my $out = $game->dispute($m->answerer);
208 0 ▶ Carp::croak("Game::Go::SGF: move $n resumes a stopped game and the engine refused: "
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Go::Error" 0 0 0
209 . $out->message) if ref $out eq 'Game::Go::Error';
210  
211 0 return;
212 }
213  
214 0 ▶sub _refused {
Subroutine _refused: not called CC=3 SCAR=24.8
215 0 my ($out, $n, $what, $lenient) = @_;
216 0 ▶ return unless ref $out eq 'Game::Go::Error';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out eq "Game::Go::Error" 0 0 0
217 0 ▶ Carp::croak("Game::Go::SGF: move $n ($what) was refused: " . $out->message
Branch: 1 branch
BranchTrueFalseTotal
$lenient ? : 0 0 0
218 . ($lenient ? '' : '. Try lenient => 1 if this is a record of a real game'));
219 }
220  
221 0 ▶sub _round_half {
Subroutine _round_half: not called CC=4 SCAR=30.0
222 0 my ($n) = @_;
223 0 ▶ return 0 unless defined $n && $n =~ /\A-?[0-9.]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A-?[0-9.]+\z/ 0 0 0
Condition0%0/3
defined $n and $n =~ /\A-?[0-9.]+\z/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and $n =~ /\A-?[0-9.]+\z/
defined $n $n =~ /\A-?[0-9.]+\z/
Truth table0%0/3
A and B
A= defined $nB= $n =~ /\A-?[0-9.]+\z/
ABresult
0X0
100
111
224 0 ▶ return int($n * 2 + ($n < 0 ? -0.5 : 0.5)) / 2;
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 0 0 0
225 }
226  
227  
228 0 ▶sub parse_result {
Subroutine parse_result: not called CC=13 SCAR=52.0
Pod documented
229 0 my ($re) = @_;
230 0 ▶ return undef unless defined $re && length $re;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $re and length $re 0 0 0
Condition0%0/3
defined $re and length $re
!ll&&!rl&&r
000
MC/DC0%0/2
defined $re and length $re
defined $re length $re
Truth table0%0/3
A and B
A= defined $reB= length $re
ABresult
0X0
100
111
231  
232 0 ▶ return { kind => 'draw' } if $re eq '0' || lc($re) eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $re eq 0 or lc $re eq "draw" 0 0 0
Condition0%0/3
$re eq 0 or lc $re eq "draw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$re eq 0 or lc $re eq "draw"
$re eq 0 lc $re eq "draw"
Truth table0%0/3
A or B
A= $re eq 0B= lc $re eq "draw"
ABresult
1X1
011
000
233 0 ▶ return { kind => 'void' } if lc($re) eq 'void';
Branch: 1 branch
BranchTrueFalseTotal
if lc $re eq "void" 0 0 0
234 0 ▶ return { kind => 'unknown' } if $re eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $re eq "?" 0 0 0
235  
236 0 my ($who, $rest) = $re =~ /\A([BW])\+(.*)\z/i;
237 0 ▶ return { kind => 'unknown', text => $re } unless defined $who;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $who 0 0 0
238  
239 0 ▶ my $winner = uc($who) eq 'B' ? $B : $W;
Branch: 1 branch
BranchTrueFalseTotal
uc $who eq 'B' ? : 0 0 0
240 0 ▶ return { kind => 'resign', winner => $winner } if $rest =~ /\AR(esign)?\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\AR(esign)?\z/i 0 0 0
241 0 ▶ return { kind => 'timeout', winner => $winner } if $rest =~ /\AT(ime)?\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\AT(ime)?\z/i 0 0 0
242 0 ▶ return { kind => 'forfeit', winner => $winner } if $rest =~ /\AF(orfeit)?\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\AF(orfeit)?\z/i 0 0 0
243 0 ▶ return { kind => 'score', winner => $winner, margin => $rest + 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $rest =~ /\A[0-9]+(?:\.[0-9]+)?\z/ 0 0 0
244 if $rest =~ /\A[0-9]+(?:\.[0-9]+)?\z/;
245  
246 0 return { kind => 'unknown', text => $re };
247 }
248  
249 1 ▶sub format_result {
Subroutine format_result: called CC=8 SCAR=22.9
Pod documented
250 1 my ($game) = @_;
251 1 my $r = $game->result;
252 1 ▶ return undef unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 1 1
253 1 ▶ return 'Void' if $r eq 'abandoned';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "abandoned" 0 1 1
254  
255 1 my $w = $game->winner;
256 1 ▶ return 'Void' unless defined $w;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $w 0 1 1
257 1 my $letter = uc Game::Go::Rules::letter($w);
258  
259 1 ▶ return "$letter+R" if $r eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "resign" 0 1 1
260 1 ▶ return "$letter+T" if $r eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "timeout" 0 1 1
261  
262 1 my $o = $game->outcome;
263 1 ▶ return "$letter+" . ($o ? $o->margin : '') if $r eq 'score';
Branch: 2 branches
BranchTrueFalseTotal
if $r eq "score" 1 0 1
$o ? : 1 0 1
264 0 return undef;
265 }
266  
267 1 ▶sub write {
Subroutine write: called CC=22 SCAR=34.7
Pod documented
268 1 my ($game, %o) = @_;
269  
270 1 my $size = $game->size;
271 1 my @root = (
272 'GM[1]', 'FF[4]', 'CA[UTF-8]',
273 "AP[Game::Go:$Game::Go::VERSION]",
274 "SZ[$size]",
275 'KM[' . $game->komi . ']',
276 'RU[Japanese]',
277 );
278 1 ▶ push @root, 'PB[' . _escape($o{black}) . ']' if defined $o{black};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"black"} 0 1 1
279 1 ▶ push @root, 'PW[' . _escape($o{white}) . ']' if defined $o{white};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"white"} 0 1 1
280 1 ▶ push @root, 'DT[' . _escape($o{date}) . ']' if defined $o{date};
Branch: 1 branch
BranchTrueFalseTotal
if defined $o{"date"} 0 1 1
281  
282 1 my @setup;
283 my %stones;
284 1 for my $e (@{ $game->log }) {
285 167 ▶ if ($e->{kind} eq 'handicap') {
Branch: 2 branches
BranchTrueFalseTotal
if ($e->{'kind'} eq 'handicap') { } 0 167 167
elsif ($e->{'kind'} eq 'setup') { } 0 167 167
286 0 push @{ $stones{b} }, $e->{payload}{pt};
287 }
288 elsif ($e->{kind} eq 'setup') {
289 0 for my $letter (sort keys %{ $e->{payload} }) {
290 0 push @{ $stones{$letter} }, @{ $e->{payload}{$letter} };
291 }
292 }
293 }
294 1 ▶ push @root, 'HA[' . $game->handicap . ']' if $game->handicap;
Branch: 1 branch
BranchTrueFalseTotal
if $game->handicap 0 1 1
295 1 for my $pair (['b', 'AB'], ['w', 'AW']) {
296 2 my ($letter, $prop) = @$pair;
297 2 ▶ next unless $stones{$letter};
Branch: 1 branch
BranchTrueFalseTotal
unless $stones{$letter} 2 0 2
298 push @setup, $prop . join '', map {
299 0 '[' . Game::Go::Notation::to_sgf($size, $game->col_row($_)) . ']'
300 0 } @{ $stones{$letter} };
301 }
302  
303 1 my $re = format_result($game);
304 1 ▶ push @root, "RE[$re]" if defined $re;
Branch: 1 branch
BranchTrueFalseTotal
if defined $re 1 0 1
305  
306 1 my $out = '(;' . join('', @root) . join('', @setup);
307  
308 1 for my $e (@{ $game->log }) {
309 167 ▶ my $who = Game::Go::Rules::from_letter($e->{actor}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $who = Game::Go::Rules::from_letter($e->{"actor"}) or next 164 3 167
310 164 ▶ my $prop = $who == $B ? 'B' : 'W';
Branch: 1 branch
BranchTrueFalseTotal
$who == $B ? : 82 82 164
311  
312 164 ▶ if ($e->{kind} eq 'pass') { $out .= ";${prop}[]"; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($e->{"kind"} eq "pass") 11 153 164
313 153 ▶ next unless $e->{kind} eq 'play';
Branch: 1 branch
BranchTrueFalseTotal
unless $e->{"kind"} eq "play" 3 150 153
314 150 $out .= ";${prop}[" . Game::Go::Notation::to_sgf($size, $game->col_row($e->{payload}{pt})) . ']';
315 }
316  
317 1 ▶ if ($game->scored_by && $game->outcome) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->scored_by and $game->outcome) 1 0 1
Condition33%1/3
$game->scored_by and $game->outcome
!ll&&!rl&&r
001
MC/DC0%0/2
$game->scored_by and $game->outcome
$game->scored_by $game->outcome
Truth table33%1/3
A and B
A= $game->scored_byB= $game->outcome
ABresult
0X0
100
111
318 1 my $raw = $game->raw_score;
319 1 my $marks = _territory_points($game);
320 1 for my $pair (['b', 'TB'], ['w', 'TW']) {
321 2 my ($letter, $prop) = @$pair;
322 2 ▶ next unless @{ $marks->{$letter} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$marks->{$letter} or [];} 1 1 2
Condition50%1/2
$marks->{$letter} || []
l!l
20
MC/DC0%0/1
$marks->{$letter} || []
$marks->{$letter}
Truth table50%1/2
A
A= $marks->{$letter}
Aresult
11
00
323 $out .= "\n$prop" . join '', map {
324 5 '[' . Game::Go::Notation::to_sgf($size, $game->col_row($_)) . ']'
325 1 } @{ $marks->{$letter} };
326 }
327 }
328  
329 1 return "$out\n)\n";
330 }
331  
332 1 ▶sub _territory_points {
Subroutine _territory_points: called CC=3 SCAR=11.0
333 1 my ($game) = @_;
334 1 my $map = $game->territory_map;
335 1 my %out = (b => [], w => []);
336 1 for my $pt (sort { $a <=> $b } keys %$map) {
337 5 ▶ push @{ $out{ $map->{$pt} == $B ? 'b' : 'w' } }, $pt;
Branch: 1 branch
BranchTrueFalseTotal
$map->{$pt} == $B ? : 5 0 5
338 }
339 1 return \%out;
340 }
341  
342 0 ▶sub _escape {
Subroutine _escape: not called CC=1 SCAR=6.9
343 0 my ($s) = @_;
344 0 $s =~ s/([\]\\])/\\$1/g;
345 0 return $s;
346 }
347  
348 2 1;
349  
350 __END__
↑ Summary blib/lib/Game/Go/GTP.pm »