SGF2misc.pm

filter: Statementstmt 58.8% 427 / 726 Branchbran 42.4% 164 / 386 Conditioncond 28.4% 45 / 158 MC/DCmcdc 10.9% 10 / 91 Subroutinesub 74.4% 32 / 43 Podpod 5.8% 1 / 17 totaltotal 47.7% 679 / 1421 CC 293 SCAR 53.0
CC
293
Cov
50%
_crazy_moku_alg
756.0
as_html
702.0
as_js
506.0
CRAP 200.7
↑ Summary SGF2misc/GD.pm »
1 package Games::Go::SGF2misc;
2  
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Parse::Lex;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Compress::Zlib;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use CGI qw(escapeHTML);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 our $VERSION = 0.9781;
13  
14 6 1;
15  
16 # main calls
17 # new {{{
18 sub new {
19 6 ▶ my $this = shift;
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
20 6 $this = bless {}, $this;
21  
22 6 ▶ if( $ENV{DEBUG} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"} > 0) 0 6 6
23 6 ▶ use Number::Format;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶ use Devel::Size qw(total_size);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶ use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 $this->{frm} = new Number::Format;
28 }
29  
30 6 return $this;
31 }
32 # }}}
33 # parse {{{
34 sub parse {
35 14 ▶ my $this = shift;
Subroutine parse: called CC=3 SCAR=11.8
Pod undocumented
36 14 my $file = shift;
37  
38 14 ▶ if( -f $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $file) 14 0 14
39 14 local $/; # Enable local "slurp" ... ie, by unsetting $/ for this local scope, it will not end lines on \n
40 14 ▶ open SGFIN, $file or die "couldn't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open SGFIN, $file or die "couldn't open ${file}: $!" 0 14 14
41  
42 14 our $FILENAME = $file;
43  
44 14 return $this->parse_internal(\*SGFIN);
45 }
46  
47 0 $this->{error} = "Parse Error reading $file: unknown";
48 0 return 0;
49 }
50 # }}}
51 # parse_string {{{
52 sub parse_string {
53 0 ▶ my $this = shift;
Subroutine parse_string: not called CC=1 SCAR=6.9
Pod undocumented
54 0 my $string = shift;
55  
56 0 our $FILENAME = "STRING";
57  
58 0 return $this->parse_internal($string);
59 }
60 # }}}
61 # parse_internal {{{
62 sub parse_internal {
63 14 ▶ my $this = shift;
Subroutine parse_internal: called CC=29 SCAR=34.6
Pod undocumented
64 14 my $file = shift;
65  
66 14 our $FILENAME;
67  
68 14 for my $k (keys %$this) {
69 50 ▶ delete $this->{$k} unless {Time=>1, frm=>1}->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless {"Time", 1, "frm", 1}->{$k} 50 0 50
70 }
71 14 $global::lex_error = undef;
72  
73 14 $this->_time("parse");
74  
75 my @rules = (
76 VALUE => '(?:\[\]|\[(?s:.*?[^\x5c])\])',
77  
78 BCOL => '\(', # begin collection
79 ECOL => '\)', # end collection
80 PID => '(?:CoPyright|[A-Z]+)', # property identifier (CoPyright is the spurious IGS tag, assholes)
81 NODE => ';', # new node
82 WSPACE => '[\s\r\n]',
83  
84 qw(ERROR .*), sub {
85 0 ▶ $global::lex_error = "Parse Error reading $FILENAME: $_[1]\n";
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 }
87 14 );
88  
89 14 ▶ Parse::Lex->trace if $ENV{DEBUG} > 30;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 30 0 14 14
90  
91 6 ▶ my $lex = Parse::Lex->new(@rules); $^W = 0; no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
92 14 $lex->from($file);
93  
94 14 $this->{parse} = { p => undef, n => [], c=>[] }; # p => parent, n => nodes, c => child Collections
95  
96 14 my $ref = $this->{parse}; # our current position
97  
98 # parse rules:
99 14 my $nos = -1; # the current node (array position). -1 when we're not in a node
100 14 my $pid = 0; # 0 unless we just got a pid; otherwise, node array position
101  
102 14 TOKEN: while (1) {
103 20400 my $token = $lex->next;
104  
105 20400 ▶ if (not $lex->eoi) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $lex->eoi) { } 20387 13 20400
106 20387 my $C = $token->name;
107 20387 my $V = $token->text;
108  
109 20387 ▶ if( $C eq "ERROR" or defined $global::lex_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($C eq "ERROR" or defined $global::lex_error) 0 20387 20387
Condition33%1/3
$C eq "ERROR" or defined $global::lex_error
l!l&&r!l&&!r
0020387
MC/DC0%0/2
$C eq "ERROR" or defined $global::lex_error
$C eq "ERROR" defined $global::lex_error
Truth table33%1/3
A or B
A= $C eq "ERROR"B= defined $global::lex_error
ABresult
1X1
011
000
110 0 $global::lex_error = "Parse Error reading $FILENAME: unknown"; # TODO: this $file should be ... the name of it instead
111 0 $this->{error} = $global::lex_error;
112 0 return 0;
113 }
114  
115 20387 ▶ if( $C eq "BCOL" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($C eq 'BCOL') { } 368 20019 20387
elsif ($C eq 'ECOL') { } 368 19651 20019
elsif ($C eq 'NODE') { } 2667 16984 19651
116 368 push @{ $ref->{c} }, { p=>$ref, n=>[], c=>[] };
117 368 $ref = $ref->{c}[$#{ $ref->{c} }];
118 368 $nos = -1;
119  
120 } elsif( $C eq "ECOL" ) {
121 368 $ref = $ref->{p};
122 368 $nos = -1;
123  
124 } elsif( $C eq "NODE" ) {
125 2667 push @{ $ref->{n} }, [];
126 2667 $nos = $#{ $ref->{n} };
127  
128 }
129
130 # this get's it's own if block for the $pid
131 20387 ▶ if( $C eq "PID" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($C eq 'PID') { } 6425 13962 20387
elsif ($C eq 'VALUE') { } 8189 5773 13962
elsif ($C eq 'WSPACE') { } 2370 3403 5773
132 6425 ▶ if( $nos == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nos == -1) 1 6424 6425
133 1 $this->{error} = "Parse Error reading $FILENAME: property identifier ($V) in strange place";
134 1 return 0;
135 }
136 6424 push @{ $ref->{n}[$nos] }, {P=>$V};
137 6424 $pid = $#{ $ref->{n}[$nos] };
138  
139 } elsif( $C eq "VALUE" ) {
140 8189 $V =~ s/^\[//ms; $V =~ s/\]$//ms;
141 8189 $V =~ s/\\(.)/$1/msg;
142  
143 8189 ▶ if( $nos == -1 or $pid == -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nos == -1 or $pid == -1) 0 8189 8189
Condition33%1/3
$nos == -1 or $pid == -1
l!l&&r!l&&!r
008189
MC/DC0%0/2
$nos == -1 or $pid == -1
$nos == -1 $pid == -1
Truth table33%1/3
A or B
A= $nos == -1B= $pid == -1
ABresult
1X1
011
000
144 0 $this->{error} = "Parse Error reading $FILENAME: property value ($V) in strange place";
145 0 return 0;
146 }
147 8189 ▶ if( defined $ref->{n}[$nos][$pid]{V} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref->{"n"}[$nos][$pid]{"V"}) 1765 6424 8189
148 1765 push @{ $ref->{n}[$nos] }, {P=>$ref->{n}[$nos][$pid]{P}};
149 1765 $pid = $#{ $ref->{n}[$nos] };
150 }
151  
152 8189 $ref->{n}[$nos][$pid]{V} = $V;
153  
154 } elsif( $C eq "WSPACE" ) {
155 # don't set pid to -1 here (2006-5-12)
156  
157 } else {
158 3403 $pid = -1;
159 }
160  
161 } else {
162 13 last TOKEN;
163 }
164 }
165  
166 13 $this->_time("parse");
167  
168 13 ▶ print STDERR "SGF Parsed! Calling internal _parse() routine\n" if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
169 13 ▶ print STDERR "\$this size (before _parse)= ", $this->{frm}->format_bytes(total_size( $this )), "\n" if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
170  
171 13 $this->_time("_parse");
172  
173 13 my $r = $this->_parse(0, $this->{parse});
174  
175 13 $this->_time("_parse");
176  
177 13 ▶ print STDERR "\$this size (after _parse)= ", $this->{frm}->format_bytes(total_size( $this )), "\n" if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
178  
179 13 ▶ print STDERR "rebuilding {refdb} (for ref2id/id2ref)\n" if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
180  
181 13 $this->_time("_nodelist");
182  
183 13 $this->{nodelist} = { map {$this->_ref2id($_) => $this->_nodelist([], $_)} @{$this->{gametree}} };
184  
185 13 $this->_time("_nodelist");
186  
187 13 ▶ print STDERR "\$this size (after _nodelist())= ", $this->{frm}->format_bytes(total_size( $this )), "\n" if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
188  
189 13 $this->_time("nuke(gametree and parse)");
190 13 my @to_nuke;
191
192 13 ▶ push @to_nuke, (@{$this->{gametree}}) if ref($this->{gametree}) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
if ref $this->{"gametree"} eq "ARRAY" 13 0 13
193 13 ▶ push @to_nuke, $this->{parse} if ref($this->{parse}) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
if ref $this->{"parse"} eq "HASH" 13 0 13
194  
195 13 ▶ while( @to_nuke ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@to_nuke) 3145 13 3158
196 3145 my $ref = shift @to_nuke;
197 3145 for my $k (qw(p c kids parent)) {
198 12580 ▶ if( my $v = $ref->{$k} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = $ref->{$k}) 3513 9067 12580
199 3513 ▶ if( ref($v) eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq "ARRAY") 3145 368 3513
200 3145 push @to_nuke, @$v;
201 }
202  
203 3513 delete $ref->{$k};
204 }
205 }
206 }
207 13 $this->_time("nuke(gametree and parse)");
208  
209 13 ▶ $this->_show_timings if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 13 13
210  
211 13 return $r;
212 }
213 # }}}
214 # freeze {{{
215 sub freeze {
216 1 ▶ my $this = shift;
Subroutine freeze: called CC=2 SCAR=6.9
Pod undocumented
217  
218 1 local $Data::Dumper::Indent = 0;
219 1 local $Data::Dumper::Purity = 1;
220  
221 1 my $fm = {};
222 1 for my $k (qw(nodelist refdb)) {
223 2 $fm->{$k} = $this->{$k};
224 }
225  
226 1 $this->_time("freeze Dumper");
227 1 my $buf = Dumper( $fm );
228 1 $this->_time("freeze Dumper");
229  
230 1 return Compress::Zlib::memGzip( $buf );
231 }
232 # }}}
233 # thaw {{{
234 sub thaw {
235 1 ▶ my $this = shift;
Subroutine thaw: called CC=6 SCAR=23.9
Pod undocumented
236 1 my $frz = shift;
237 1 my ($VAR1);
238  
239 1 ▶ if( ref($frz) eq "GLOB" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $frz eq 'GLOB') { } 0 1 1
240 0 $this->_time("gzreads");
241 0 my $gz = gzopen($frz, "r");
242  
243 0 $frz = "";
244  
245 0 my $x;
246 0 ▶ while( my $r = $gz->gzread($x, 32768) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $gz->gzread($x, 32768)) 0 0 0
247 0 $frz .= $x;
248 }
249 0 $gz->gzclose;
250  
251 0 $this->_time("gzreads");
252  
253 0 $this->_time("eval");
254 0 eval $frz;
255 0 $this->_time("eval");
256 } else {
257 1 $this->_time("memgunzip/eval");
258 1 eval Compress::Zlib::memGunzip( $frz );
259 1 $this->_time("memgunzip/eval");
260 1 ▶ if( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
261 0 $this->{error} = $@;
262 0 return 0;
263 }
264 }
265  
266 1 $this->_time("assign refs");
267 1 for my $k (keys %$VAR1) {
268 2 $this->{$k} = $VAR1->{$k};
269 }
270 1 $this->_time("assign refs");
271  
272 1 ▶ $this->_show_timings if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 1 1
273  
274 1 return 1;
275 }
276 # }}}
277 # errstr {{{
278 sub errstr {
279 0 ▶ my $this = shift;
Subroutine errstr: not called CC=1 SCAR=6.9
Pod undocumented
280  
281 0 $this->{error} =~ s/[\r\n\s]$//msg;
282  
283 0 return $this->{error};
284 }
285 # }}}
286  
287 # tools -- can croak()!
288 # sgfco2numco {{{
289 sub sgfco2numco {
290 5419 ▶ my $this = shift;
Subroutine sgfco2numco: called CC=13 SCAR=29.3
Pod undocumented
291 5419 my $gref = shift;
292 5419 my $co = shift;
293  
294 5419 my ($sz, $ff);
295 5419 ▶ if( ref($gref) eq "HASH" and ref($gref->{game_properties}) eq "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $gref eq 'HASH' and ref $gref->{'game_properties'} eq 'HASH') { } 5419 0 5419
Condition33%1/3
ref $gref eq "HASH" and ref $gref->{"game_properties"} eq "HASH"
!ll&&!rl&&r
005419
MC/DC0%0/2
ref $gref eq "HASH" and ref $gref->{"game_properties"} eq "HASH"
ref $gref eq "HASH" ref $gref->{"game_properties"} eq "HASH"
Truth table33%1/3
A and B
A= ref $gref eq "HASH"B= ref $gref->{"game_properties"} eq "HASH"
ABresult
0X0
100
111
296 5419 $sz = $gref->{game_properties}{SZ};
297 5419 $ff = $gref->{game_properties}{FF};
298  
299 5419 ▶ unless( $sz and $ff ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sz and $ff) 0 5419 5419
Condition33%1/3
$sz and $ff
!ll&&!rl&&r
005419
MC/DC0%0/2
$sz and $ff
$sz $ff
Truth table33%1/3
A and B
A= $szB= $ff
ABresult
0X0
100
111
300 0 croak "Error: sgfco2numco needs FF and SZ properties to function, sorry.\n";
301 }
302 } else {
303 0 croak "Syntax Error: You must pass a game reference to sgfco2numco because it needs the FF and SZ properties.\n";
304 }
305  
306 5419 ▶ if( $co =~ m/\w{2}\:\w{2}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($co =~ /\w{2}\:\w{2}/) 0 5419 5419
307 0 croak "Parsed Stupidly: SGF2misc.pm doesn't handle compressed co-ordinates ($co) yet... *sigh*\n";
308 }
309  
310 my $inty = sub {
311 10824 ▶ my $x = -1;
Subroutine __ANON__: called CC=4 SCAR=14.6
312  
313 10824 ▶ $x = int(hex(unpack("H*", $_[0]))) - 97 if $_[0] =~ m/[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[a-z]/ 10824 0 10824
314 10824 ▶ $x = int(hex(unpack("H*", $_[0]))) - 65 if $_[0] =~ m/[A-Z]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /[A-Z]/ 0 10824 10824
315  
316 10824 ▶ die "unexpected error reading column identifier" unless $x > -1;
Branch: 1 branch
BranchTrueFalseTotal
unless $x > -1 0 10824 10824
317  
318 10824 return $x;
319 5419 };
320  
321 5419 ▶ if( not $co or ($co eq "tt" and ($ff == 3 or $sz<=19)) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $co or $co eq "tt" and $ff == 3 || $sz <= 19) 7 5412 5419
Condition77%7/9
not $co or $co eq "tt" and $ff == 3 || $sz <= 19
l!l&&r!l&&!r
210444373
$co eq "tt" and $ff == 3 || $sz <= 19
!ll&&!rl&&r
541205
$ff == 3 or $sz <= 19
l!l&&r!l&&!r
230
MC/DC0%0/4
not $co or $co eq "tt" and $ff == 3 || $sz <= 19
not $co $co eq "tt" $ff == 3 $sz <= 19
Truth table40%2/5
not(A) or B and C or D
A= not $coB= $co eq "tt"C= $ff == 3D= $sz <= 19
ABCDresult
1XXX1
011X1
01011
00XX0
01000
322 7 ▶ return (wantarray ? (qw(PASS PASS)) : [qw(PASS PASS)]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
323 }
324  
325 5412 ▶ if( $co =~ m/^([a-zA-Z])([a-zA-Z])$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($co =~ /^([a-zA-Z])([a-zA-Z])$/) 5412 0 5412
326 5412 my ($row, $col) = ($1, $2);
327  
328 5412 ▶ return (wantarray ? ($inty->($col), $inty->($row)) : [ $inty->($col), $inty->($row) ]);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5412 0 5412
329 }
330  
331 0 croak "Parse Error: co-ordinate not understood ($co)\n";
332 }
333 # }}}
334  
335 # outputers
336 # parse_hash {{{
337 sub parse_hash {
338 0 ▶ my $this = shift;
Subroutine parse_hash: not called CC=1 SCAR=6.9
Pod undocumented
339  
340 0 return $this->{parse};
341 }
342 # }}}
343 # nodelist {{{
344 sub nodelist {
345 4 ▶ my $this = shift;
Subroutine nodelist: called CC=1 SCAR=0.0
Pod undocumented
346  
347 4 return $this->{nodelist};
348 }
349 # }}}
350 # is_node {{{
351 sub is_node {
352 0 ▶ my $this = shift;
Subroutine is_node: not called CC=2 SCAR=17.9
Pod documented
353 0 my $node = shift;
354  
355 0 ▶ return ($this->{refdb}{$node} ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
$this->{'refdb'}{$node} ? : 0 0 0
356 }
357 # }}}
358 # as_perl {{{
359 sub as_perl {
360 375 ▶ my $this = shift;
Subroutine as_perl: called CC=4 SCAR=17.3
Pod undocumented
361 375 my $node = shift;
362 375 my $soft = shift;
363  
364 375 ▶ if( $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node) 375 0 375
365 375 ▶ if( my $ref = $this->{refdb}{$node} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = $this->{"refdb"}{$node}) 375 0 375
366 375 return $ref;
367 }
368 }
369  
370 0 $this->{error} = "no such node: $node";
371 0 ▶ return 0 if $soft;
Branch: 1 branch
BranchTrueFalseTotal
if $soft 0 0 0
372  
373 0 croak $this->{error};
374 }
375 # }}}
376 # as_text {{{
377 sub as_text {
378 373 ▶ my $this = shift;
Subroutine as_text: called CC=4 SCAR=13.9
Pod undocumented
379 373 my $node = shift;
380  
381 373 ▶ $node = $this->as_perl( $node, 1 ) or croak $this->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$node = $this->as_perl($node, 1) or croak($this->errstr) 0 373 373
382  
383 373 my $board = $node->{board};
384  
385 373 my $x = "";
386 373 for my $i (0..$#{ $board }) {
387 7087 for my $j (0..$#{ $board->[$i] }) {
388 134653 $x .= " " . { ' '=>'.', 'W'=>'O', 'B'=>'X' }->{$board->[$i][$j]};
389 }
390 7087 $x .= "\n";
391 }
392  
393 373 return $x;
394 }
395 # }}}
396 # _mark_alg {{{
397 sub _mark_alg {
398 0 ▶ my $this = shift;
Subroutine _mark_alg: not called CC=16 SCAR=56.1
399 0 my ($mark, $img) = @_;
400  
401 0 ▶ return "bt.gif" if $mark eq "TR" and $img eq "b.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "TR" and $img eq "b.gif" 0 0 0
Condition0%0/3
$mark eq "TR" and $img eq "b.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "TR" and $img eq "b.gif"
$mark eq "TR" $img eq "b.gif"
Truth table0%0/3
A and B
A= $mark eq "TR"B= $img eq "b.gif"
ABresult
0X0
100
111
402 0 ▶ return "wt.gif" if $mark eq "TR" and $img eq "w.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "TR" and $img eq "w.gif" 0 0 0
Condition0%0/3
$mark eq "TR" and $img eq "w.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "TR" and $img eq "w.gif"
$mark eq "TR" $img eq "w.gif"
Truth table0%0/3
A and B
A= $mark eq "TR"B= $img eq "w.gif"
ABresult
0X0
100
111
403 0 ▶ return "bc.gif" if $mark eq "CR" and $img eq "b.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "CR" and $img eq "b.gif" 0 0 0
Condition0%0/3
$mark eq "CR" and $img eq "b.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "CR" and $img eq "b.gif"
$mark eq "CR" $img eq "b.gif"
Truth table0%0/3
A and B
A= $mark eq "CR"B= $img eq "b.gif"
ABresult
0X0
100
111
404 0 ▶ return "wc.gif" if $mark eq "CR" and $img eq "w.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "CR" and $img eq "w.gif" 0 0 0
Condition0%0/3
$mark eq "CR" and $img eq "w.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "CR" and $img eq "w.gif"
$mark eq "CR" $img eq "w.gif"
Truth table0%0/3
A and B
A= $mark eq "CR"B= $img eq "w.gif"
ABresult
0X0
100
111
405 0 ▶ return "bq.gif" if $mark eq "SQ" and $img eq "b.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "SQ" and $img eq "b.gif" 0 0 0
Condition0%0/3
$mark eq "SQ" and $img eq "b.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "SQ" and $img eq "b.gif"
$mark eq "SQ" $img eq "b.gif"
Truth table0%0/3
A and B
A= $mark eq "SQ"B= $img eq "b.gif"
ABresult
0X0
100
111
406 0 ▶ return "wq.gif" if $mark eq "SQ" and $img eq "w.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $mark eq "SQ" and $img eq "w.gif" 0 0 0
Condition0%0/3
$mark eq "SQ" and $img eq "w.gif"
!ll&&!rl&&r
000
MC/DC0%0/2
$mark eq "SQ" and $img eq "w.gif"
$mark eq "SQ" $img eq "w.gif"
Truth table0%0/3
A and B
A= $mark eq "SQ"B= $img eq "w.gif"
ABresult
0X0
100
111
407  
408 0 ▶ if( ($mark = int($mark)) > 0 and $mark <= 100 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($mark = int $mark) > 0 and $mark <= 100) 0 0 0
Condition0%0/3
($mark = int $mark) > 0 and $mark <= 100
!ll&&!rl&&r
000
MC/DC0%0/2
($mark = int $mark) > 0 and $mark <= 100
($mark = int $mark) > 0 $mark <= 100
Truth table0%0/3
A and B
A= ($mark = int $mark) > 0B= $mark <= 100
ABresult
0X0
100
111
409 0 ▶ return "b$mark.gif" if $img eq "b.gif";
Branch: 1 branch
BranchTrueFalseTotal
if $img eq "b.gif" 0 0 0
410 0 return "w$mark.gif"
411 }
412  
413 0 return $img;
414 }
415 # }}}
416 # _crazy_moku_alg {{{
417 sub _crazy_moku_alg {
418 0 ▶ my $this = shift;
Subroutine _crazy_moku_alg: not called CC=27 SCAR=66.3
419 0 my ($i, $j, $size) = @_;
420  
421 0 our $cma_size;
422 0 our $hoshi;
423  
424 0 ▶ if( $size != $cma_size or not $hoshi ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size != $Games::Go::SGF2misc::cma_size or not $Games::Go::SGF2misc::hoshi) 0 0 0
Condition0%0/3
$size != $Games::Go::SGF2misc::cma_size or not $Games::Go::SGF2misc::hoshi
l!l&&r!l&&!r
000
MC/DC0%0/2
$size != $Games::Go::SGF2misc::cma_size or not $Games::Go::SGF2misc::hoshi
$size != $Games::Go::SGF2misc::cma_size not $Games::Go::SGF2misc::hoshi
Truth table0%0/3
A or not(B)
A= $size != $Games::Go::SGF2misc::cma_sizeB= not $Games::Go::SGF2misc::hoshi
ABresult
1X1
011
000
425 0 $hoshi = {};
426 0 ▶ if( $size == 19 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($size == 19) { } 0 0 0
elsif ($size == 13) { } 0 0 0
elsif ($size == 9) { } 0 0 0
427 0 $hoshi = { "3 3" => 1, "3 15" => 1, "15 3" => 1, "15 15" => 1,
428 "9 3" => 1, "9 15" => 1, "3 9" => 1, "15 9" => 1, "9 9" => 1, };
429 } elsif( $size == 13 ) {
430 0 $hoshi = { "3 3" => 1, "9 9" => 1, "3 9" => 1, "9 3" => 1,
431 "6 3" => 1, "3 6" => 1, "9 6" => 1, "6 9" => 1, "6 6" => 1, };
432 } elsif( $size == 9 ) {
433 0 $hoshi = { "2 2" => 1, "2 6" => 1, "6 6" => 1, "6 2" => 1, "4 4" => 1, };
434 }
435 }
436  
437 0 ▶ return "ulc.gif" if $i == 0 and $j == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $j == 0 0 0 0
Condition0%0/3
$i == 0 and $j == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$i == 0 and $j == 0
$i == 0 $j == 0
Truth table0%0/3
A and B
A= $i == 0B= $j == 0
ABresult
0X0
100
111
438 0 ▶ return "urc.gif" if $i == 0 and $j == $size;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $j == $size 0 0 0
Condition0%0/3
$i == 0 and $j == $size
!ll&&!rl&&r
000
MC/DC0%0/2
$i == 0 and $j == $size
$i == 0 $j == $size
Truth table0%0/3
A and B
A= $i == 0B= $j == $size
ABresult
0X0
100
111
439 0 ▶ return "llc.gif" if $i == $size and $j == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $size and $j == 0 0 0 0
Condition0%0/3
$i == $size and $j == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$i == $size and $j == 0
$i == $size $j == 0
Truth table0%0/3
A and B
A= $i == $sizeB= $j == 0
ABresult
0X0
100
111
440 0 ▶ return "lrc.gif" if $i == $size and $j == $size;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $size and $j == $size 0 0 0
Condition0%0/3
$i == $size and $j == $size
!ll&&!rl&&r
000
MC/DC0%0/2
$i == $size and $j == $size
$i == $size $j == $size
Truth table0%0/3
A and B
A= $i == $sizeB= $j == $size
ABresult
0X0
100
111
441 0 ▶ return "ts.gif" if $i == 0 and $j != 0 and $j != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $i == 0 and $j != 0 and $j != $size 0 0 0
Condition0%0/6
$i == 0 and $j != 0 and $j != $size
!ll&&!rl&&r
000
$i == 0 and $j != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$i == 0 and $j != 0 and $j != $size
$i == 0 $j != 0 $j != $size
Truth table0%0/4
A and B and C
A= $i == 0B= $j != 0C= $j != $size
ABCresult
0XX0
10X0
1100
1111
442 0 ▶ return "bs.gif" if $i == $size and $j != 0 and $j != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $size and $j != 0 and $j != $size 0 0 0
Condition0%0/6
$i == $size and $j != 0 and $j != $size
!ll&&!rl&&r
000
$i == $size and $j != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$i == $size and $j != 0 and $j != $size
$i == $size $j != 0 $j != $size
Truth table0%0/4
A and B and C
A= $i == $sizeB= $j != 0C= $j != $size
ABCresult
0XX0
10X0
1100
1111
443 0 ▶ return "ls.gif" if $j == 0 and $i != 0 and $i != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $j == 0 and $i != 0 and $i != $size 0 0 0
Condition0%0/6
$j == 0 and $i != 0 and $i != $size
!ll&&!rl&&r
000
$j == 0 and $i != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$j == 0 and $i != 0 and $i != $size
$j == 0 $i != 0 $i != $size
Truth table0%0/4
A and B and C
A= $j == 0B= $i != 0C= $i != $size
ABCresult
0XX0
10X0
1100
1111
444 0 ▶ return "rs.gif" if $j == $size and $i != 0 and $i != $size;
Branch: 1 branch
BranchTrueFalseTotal
if $j == $size and $i != 0 and $i != $size 0 0 0
Condition0%0/6
$j == $size and $i != 0 and $i != $size
!ll&&!rl&&r
000
$j == $size and $i != 0
!ll&&!rl&&r
000
MC/DC0%0/3
$j == $size and $i != 0 and $i != $size
$j == $size $i != 0 $i != $size
Truth table0%0/4
A and B and C
A= $j == $sizeB= $i != 0C= $i != $size
ABCresult
0XX0
10X0
1100
1111
445  
446 0 ▶ return "h.gif" if $hoshi->{"$i $j"};
Branch: 1 branch
BranchTrueFalseTotal
if $$Games::Go::SGF2misc::hoshi{"$i $j"} 0 0 0
447 0 return "p.gif",
448 }
449 # }}}
450 # as_html {{{
451 sub as_html {
452 0 ▶ my $this = shift;
Subroutine as_html: not called CC=26 SCAR=65.5
Pod undocumented
453 0 my $node = shift;
454 0 my $dir = shift;
455 0 ▶ $dir = "./img" unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
456 0 my $id = shift;
457 0 my $onode = $node;
458  
459 0 ▶ $node = $this->as_perl( $node, 1 ) or croak $this->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$node = $this->as_perl($node, 1) or croak($this->errstr) 0 0 0
460  
461 # use Data::Dumper; $Data::Dumper::Indent = 0;
462 # warn Dumper( $node );
463  
464 0 my $gref = $this->as_perl(1);
465 0 my $game_info = $gref->{game_properties};
466
467 =cut
468 game_properties' => {'FF' => 4,'PB' => 'Orien Vandenbergh
469 (nichus)','GM' => 1,'KM' => '6.5','SZ' => 19,'PC' => 'Dragon Go Server: http://www.dragongoserver.net','RE' => 'W+29.5','RU' =>
470 'Japanese','BR' => '13 kyu','GN' => 'jettero-nichus-20041229.sgf','GC' => 'Game ID: 85389','DT' => '2004-10-29,2004-12-29','PW' =>
471 'Jettero Heller (jettero)','WR' => '13 kyu','OT' => '30 days + 1 day/10 periods Japanese byoyomi'}
472 =cut
473  
474 0 my $board = $node->{board};
475 0 my $size = @{$board->[0]}; # inaccurate?
476 0 $size--;
477  
478 0 my %marks = ();
479 0 for my $m (@{ $node->{marks} }) {
480 0 ▶ $marks{"$m->[1] $m->[2]"} = ($m->[0] eq "LB" ? $m->[4] : $m->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$m->[0] eq 'LB' ? : 0 0 0
481 }
482  
483 0 my $arow = "<tr align='center'><td>" . join("", map("<td>$_", qw(A B C D E F G H J K L M N O P Q R S T))) . "<td>";
484  
485 0 my $x = "<table class='sgf2miscboard' cellpadding=0 cellspacing=0>$arow\n";
486  
487 0 for my $i (0..$#{ $board }) {
488 0 $x .= "<tr><td>". (19-$i);
489 0 for my $j (0 .. $#{ $board->[$i] }) {
490 0 my $iid = "";
491 0 ▶ $iid = " id='$id.$i.$j'" if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
492  
493 my $c = {
494 'B' => "b.gif",
495 'W' => "w.gif",
496 0 }->{$board->[$i][$j]};
497  
498 0 ▶ $c = "wc.gif" if $c eq "w.gif" and $node->{moves}[0][1] == $i and $node->{moves}[0][2] == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j 0 0 0
Condition0%0/6
$c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
!ll&&!rl&&r
000
$c eq "w.gif" and $node->{"moves"}[0][1] == $i
!ll&&!rl&&r
000
MC/DC0%0/3
$c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
$c eq "w.gif" $node->{"moves"}[0][1] == $i $node->{"moves"}[0][2] == $j
Truth table0%0/4
A and B and C
A= $c eq "w.gif"B= $node->{"moves"}[0][1] == $iC= $node->{"moves"}[0][2] == $j
ABCresult
0XX0
10X0
1100
1111
499 0 ▶ $c = "bc.gif" if $c eq "b.gif" and $node->{moves}[0][1] == $i and $node->{moves}[0][2] == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j 0 0 0
Condition0%0/6
$c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
!ll&&!rl&&r
000
$c eq "b.gif" and $node->{"moves"}[0][1] == $i
!ll&&!rl&&r
000
MC/DC0%0/3
$c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
$c eq "b.gif" $node->{"moves"}[0][1] == $i $node->{"moves"}[0][2] == $j
Truth table0%0/4
A and B and C
A= $c eq "b.gif"B= $node->{"moves"}[0][1] == $iC= $node->{"moves"}[0][2] == $j
ABCresult
0XX0
10X0
1100
1111
500  
501 0 ▶ $c = $this->_crazy_moku_alg($i, $j, $size) unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 0 0
502 0 $c = $this->_mark_alg($marks{"$i $j"}, $c);
503  
504 0 $c = "$dir/$c";
505 0 $x .= "<td><img$iid src=\"$c\">";
506 }
507 0 $x .= "<td align='right'>". (19-$i) . "\n";
508 }
509  
510 0 my $cpid = "";
511 0 ▶ $cpid = " id='$id.state'" if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
512  
513 0 my $p = "<tr><td$cpid colspan='21' class='sgf2misccaps'><br/>W.Caps: $node->{captures}->{W}\&nbsp; \&nbsp;B.Caps: $node->{captures}->{B}";
514  
515 0 ▶ if( $node->{other} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"other"}) 0 0 0
516 0 my $TB = $node->{other}->{TB};
517 0 my $TW = $node->{other}->{TW};
518  
519 0 ▶ if ($TB and $TW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TB and $TW) 0 0 0
Condition0%0/3
$TB and $TW
!ll&&!rl&&r
000
MC/DC0%0/2
$TB and $TW
$TB $TW
Truth table0%0/3
A and B
A= $TBB= $TW
ABresult
0X0
100
111
520 0 my $cb = $node->{captures}->{B};
521 0 my $cw = $node->{captures}->{W};
522 0 my $km = $game_info->{KM};
523  
524 0 my ($tb, $tw) = (0, 0);
525 0 for my $r (@$TB) {
526 0 my @a = $this->sgfco2numco($gref, $r);
527  
528 0 $tb ++;
529 0 ▶ $cb ++ if $board->[$a[0]][$a[1]] eq "W";
Branch: 1 branch
BranchTrueFalseTotal
if $$board[$a[0]][$a[1]] eq "W" 0 0 0
530 }
531  
532 0 for my $r (@$TW) {
533 0 my @a = $this->sgfco2numco($gref, $r);
534  
535 0 $tw ++;
536 0 ▶ $cw ++ if $board->[$a[0]][$a[1]] eq "B";
Branch: 1 branch
BranchTrueFalseTotal
if $$board[$a[0]][$a[1]] eq "B" 0 0 0
537 }
538  
539 0 my $f = ($tw + $cw + $km) - ($tb + $cb);
540 0 ▶ $f = ($f<0 ? "B+".abs($f) : "W+$f");
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 0 0 0
541  
542 0 $p = "<tr><td$cpid colspan='21' class='sgf2miscresult'><br/>W($tw t + $cw c + $km k), B($tb t + $cb c): $f";
543 }
544 }
545  
546 0 my $cmid = "";
547 0 ▶ $cmid = " id='$id.comment'" if $id;
Branch: 1 branch
BranchTrueFalseTotal
if $id 0 0 0
548  
549 0 my $comments = "";
550 0 $comments .= escapeHTML($_) for @{$node->{comments}};
551 0 $comments =~ s/[\r\n]/<br\/>/sg;
552  
553 0 return "$x$arow$p</table><!--MATCHME--><div$cmid class='sgf2misccomment'>$comments</div>";
554 }
555 # }}}
556 # as_js {{{
557 sub as_js {
558 0 ▶ my $this = shift;
Subroutine as_js: not called CC=22 SCAR=62.3
Pod undocumented
559 0 my $node = shift;
560  
561 0 ▶ $node = $this->as_perl( $node, 1 ) or croak $this->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$node = $this->as_perl($node, 1) or croak($this->errstr) 0 0 0
562  
563 0 my $gref = $this->as_perl(1);
564 0 my $game_info = $gref->{game_properties};
565  
566 0 my $board = $node->{board};
567 0 my $size = @{$board->[0]}; # inaccurate?
568 0 $size--;
569  
570 0 my %marks = ();
571 0 for my $m (@{ $node->{marks} }) {
572 0 ▶ $marks{"$m->[1] $m->[2]"} = ($m->[0] eq "LB" ? $m->[4] : $m->[0]);
Branch: 1 branch
BranchTrueFalseTotal
$m->[0] eq 'LB' ? : 0 0 0
573 }
574  
575 0 my @board = ();
576 0 for my $i (0..$#{ $board }) {
577 0 my $row = [];
578 0 for my $j (0 .. $#{ $board->[$i] }) {
579 my $c = {
580 'B' => "b.gif",
581 'W' => "w.gif",
582 0 }->{$board->[$i][$j]};
583  
584 0 ▶ $c = "wc.gif" if $c eq "w.gif" and $node->{moves}[0][1] == $i and $node->{moves}[0][2] == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j 0 0 0
Condition0%0/6
$c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
!ll&&!rl&&r
000
$c eq "w.gif" and $node->{"moves"}[0][1] == $i
!ll&&!rl&&r
000
MC/DC0%0/3
$c eq "w.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
$c eq "w.gif" $node->{"moves"}[0][1] == $i $node->{"moves"}[0][2] == $j
Truth table0%0/4
A and B and C
A= $c eq "w.gif"B= $node->{"moves"}[0][1] == $iC= $node->{"moves"}[0][2] == $j
ABCresult
0XX0
10X0
1100
1111
585 0 ▶ $c = "bc.gif" if $c eq "b.gif" and $node->{moves}[0][1] == $i and $node->{moves}[0][2] == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j 0 0 0
Condition0%0/6
$c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
!ll&&!rl&&r
000
$c eq "b.gif" and $node->{"moves"}[0][1] == $i
!ll&&!rl&&r
000
MC/DC0%0/3
$c eq "b.gif" and $node->{"moves"}[0][1] == $i and $node->{"moves"}[0][2] == $j
$c eq "b.gif" $node->{"moves"}[0][1] == $i $node->{"moves"}[0][2] == $j
Truth table0%0/4
A and B and C
A= $c eq "b.gif"B= $node->{"moves"}[0][1] == $iC= $node->{"moves"}[0][2] == $j
ABCresult
0XX0
10X0
1100
1111
586  
587 0 ▶ $c = $this->_crazy_moku_alg($i, $j, $size) unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 0 0 0
588 0 $c = $this->_mark_alg($marks{"$i $j"}, $c);
589  
590 0 push @$row, $c;
591 }
592  
593 0 push @board, $row;
594 }
595  
596 0 local $Data::Dumper::Indent = 0;
597 0 my $board = Dumper(\@board);
598 0 $board =~ s/^\$VAR1\s*=\s*//s;
599 0 $board =~ s/\s*\;\s*$//s;
600 0 $board =~ s/\.gif//sg;
601  
602 0 my $p = "<br/>W.Caps: $node->{captures}->{W}\&nbsp; \&nbsp;B.Caps: $node->{captures}->{B}";
603  
604 0 ▶ if( $node->{other} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"other"}) 0 0 0
605 0 my $TB = $node->{other}->{TB};
606 0 my $TW = $node->{other}->{TW};
607  
608 0 ▶ if ($TB and $TW) {
Branch: 1 branch
BranchTrueFalseTotal
if ($TB and $TW) 0 0 0
Condition0%0/3
$TB and $TW
!ll&&!rl&&r
000
MC/DC0%0/2
$TB and $TW
$TB $TW
Truth table0%0/3
A and B
A= $TBB= $TW
ABresult
0X0
100
111
609 0 my $cb = $node->{captures}->{B};
610 0 my $cw = $node->{captures}->{W};
611 0 my $km = $game_info->{KM};
612  
613 0 my ($tb, $tw) = (0, 0);
614 0 for my $r (@$TB) {
615 0 my @a = $this->sgfco2numco($gref, $r);
616  
617 0 $tb ++;
618 0 ▶ $cb ++ if $board->[$a[0]][$a[1]] eq "W";
Branch: 1 branch
BranchTrueFalseTotal
if $$board[$a[0]][$a[1]] eq "W" 0 0 0
619 }
620  
621 0 for my $r (@$TW) {
622 0 my @a = $this->sgfco2numco($gref, $r);
623  
624 0 $tw ++;
625 0 ▶ $cw ++ if $board->[$a[0]][$a[1]] eq "B";
Branch: 1 branch
BranchTrueFalseTotal
if $$board[$a[0]][$a[1]] eq "B" 0 0 0
626 }
627  
628 0 my $f = ($tw + $cw + $km) - ($tb + $cb);
629 0 ▶ $f = ($f<0 ? "B+".abs($f) : "W+$f");
Branch: 1 branch
BranchTrueFalseTotal
$f < 0 ? : 0 0 0
630  
631 0 $p = "<br/>W($tw t + $cw c + $km k), B($tb t + $cb c): $f";
632 }
633 }
634  
635 0 my $comments = "";
636 0 $comments .= escapeHTML($_) for @{$node->{comments}};
637 0 $comments =~ s/[\r\n]/<br\/>/sg;
638  
639 0 $p =~ s/"/\\"/sg;
640 0 $comments =~ s/"/\\"/sg;
641  
642 0 return "{ board: $board, status: \"$p\", comment: \"$comments\" }";
643 }
644 # }}}
645 # as_image {{{
646 sub as_image {
647 1 ▶ my $this = shift;
Subroutine as_image: called CC=23 SCAR=54.2
Pod undocumented
648 1 my $node = shift; my $nm = $node;
649 1 my $argu = shift;
650 1 my %opts = (imagesize=>256, antialias=>0);
651  
652 1 ▶ $node = $this->as_perl( $node, 1 ) or croak $this->errstr;
Branch: 1 branch
BranchTrueFalseTotal
$node = $this->as_perl($node, 1) or croak($this->errstr) 0 1 1
653  
654 1 my $board = $node->{board};
655 1 my $size = @{$board->[0]}; # inaccurate?
656  
657 1 ▶ if( ref($argu) ne "HASH" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $argu ne "HASH") 0 1 1
658 0 croak
659 "as_image() takes a hashref argument... e.g., {imagesize=>256, etc=>1} or nothing at all.";
660 }
661  
662 1 ▶ my $package = $argu->{'use'} || 'Games::Go::SGF2misc::GD';
Condition50%1/2
$argu->{'use'} || 'Games::Go::SGF2misc::GD'
l!l
01
MC/DC0%0/1
$argu->{'use'} || 'Games::Go::SGF2misc::GD'
$argu->{'use'}
Truth table50%1/2
A
A= $argu->{'use'}
Aresult
11
00
663 1 ▶ if ($package =~ /svg/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /svg/i) 0 1 1
664 0 $opts{'imagesize'} = '256px';
665 }
666  
667 1 @opts{keys %$argu} = (values %$argu);
668 1 $opts{boardsize} = $size;
669 1 ▶ $opts{filename} = "$nm.png" unless $opts{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"filename"} 0 1 1
670  
671 1 my $image;
672 1 ▶ eval qq( use $package; \$image = $package->new(%opts); );
Subroutine BEGIN: called CC=1 SCAR=1.2
673  
674 1 $image->drawGoban();
675  
676 # draw moves
677 0 for my $i (0..$#{ $board }) {
678 0 for my $j (0..$#{ $board->[$i] }) {
679 0 ▶ if( $board->[$i][$j] =~ m/([WB])/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($board->[$i][$j] =~ /([WB])/) 0 0 0
680 0 ▶ if( $ENV{DEBUG} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"} > 0) 0 0 0
681 0 print STDERR "placeStone($1, [$i, $j])\n";
682 }
683  
684 # SGFs are $y, $x, the matrix is $x, $y ...
685 0 $image->placeStone(lc($1), [reverse( $i, $j )]);
686 }
687 }
688 }
689  
690 0 my $marks = 0;
691 # draw marks
692 0 for my $m (@{ $node->{marks} }) {
693 0 ▶ $image->addCircle($m->[3]) if $m->[0] eq "CR";
Branch: 1 branch
BranchTrueFalseTotal
if $m->[0] eq "CR" 0 0 0
694 0 ▶ $image->addSquare($m->[3]) if $m->[0] eq "SQ";
Branch: 1 branch
BranchTrueFalseTotal
if $m->[0] eq "SQ" 0 0 0
695 0 ▶ $image->addTriangle($m->[3]) if $m->[0] eq "TR";
Branch: 1 branch
BranchTrueFalseTotal
if $m->[0] eq "TR" 0 0 0
696  
697 0 ▶ $image->addLetter($m->[3], 'X', "./times.ttf") if $m->[0] eq "MA";
Branch: 1 branch
BranchTrueFalseTotal
if $m->[0] eq "MA" 0 0 0
698 0 ▶ $image->addLetter($m->[3], $m->[4], "./times.ttf") if $m->[0] eq "LB";
Branch: 1 branch
BranchTrueFalseTotal
if $m->[0] eq "LB" 0 0 0
699 0 $marks++;
700 }
701  
702 0 ▶ if ($argu->{'automark'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argu->{"automark"}) 0 0 0
703 0 ▶ unless ($marks) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($marks) 0 0 0
704 0 my $moves = $node->{moves};
705 0 foreach my $m (@$moves) {
706 0 ▶ $image->addCircle($m->[3]) unless $m->[3];
Branch: 1 branch
BranchTrueFalseTotal
unless $m->[3] 0 0 0
707 }
708 }
709 }
710  
711 0 ▶ if ($package =~ /svg/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package =~ /svg/i) { } 0 0 0
712 0 ▶ if( $opts{filename} =~ m/.png$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'filename'} =~ /.png$/) { } 0 0 0
713 0 $image->export($opts{'filename'});
714 } else {
715 0 $image->save($opts{filename});
716 }
717 } else {
718 0 ▶ if( $opts{filename} =~ m/^\-\.(\w+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"filename"} =~ /^\-\.(\w+)$/) 0 0 0
719 0 return $image->dump($1);
720 }
721  
722 0 $image->save($opts{filename});
723 }
724 }
725 # }}}
726 # as_freezerbag {{{
727 sub as_freezerbag {
728 0 ▶ my $this = shift;
Subroutine as_freezerbag: not called CC=9 SCAR=45.0
Pod undocumented
729 0 ▶ my $file = shift or croak "You must name your freezerbag.";
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or croak("You must name your freezerbag.") 0 0 0
730 0 my $code = shift;
731 0 ▶ $code = "# your code here\n" unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 0 0
732 0 my $perl = shift;
733  
734 0 ▶ if( not $perl ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($perl) 0 0 0
735 0 for my $try (qw{ /usr/bin/perl /usr/local/bin/perl }) {
736 0 ▶ $perl = $try if -x $try;
Branch: 1 branch
BranchTrueFalseTotal
if -x $try 0 0 0
737 }
738 0 ▶ croak "couldn't find perl" unless -x $perl;
Branch: 1 branch
BranchTrueFalseTotal
unless -x $perl 0 0 0
739 }
740
741 0 ▶ open OUTMF, ">$file" or croak "Couldn't open freezerbag ($file) for output: $!";
Branch: 1 branch
BranchTrueFalseTotal
open OUTMF, ">$file" or croak("Couldn't open freezerbag ($file) for output: $!") 0 0 0
742 0 print OUTMF "#!$perl\n# vi:fdm=marker fdl=0:\n\nuse strict;\nno warnings;\nuse Games::Go::SGF2misc;\n\n";
743 0 print OUTMF "my \$sgf = new Games::Go::SGF2misc;\n";
744 0 print OUTMF " \$sgf->thaw(\\*DATA);\n\n$code\n\n# freezer DATA {\{\{\n__DATA__\n";
745  
746 0 $this->_time("print freeze");
747 0 print OUTMF $this->freeze;
748 0 $this->_time("print freeze");
749  
750 0 close OUTMF;
751  
752 0 ▶ $this->_show_timings if $ENV{DEBUG} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 0 0 0 0
753 }
754 # }}}
755  
756 # internals
757 # _show_timings {{{
758 sub _show_timings {
759 0 ▶ my $this = shift;
Subroutine _show_timings: not called CC=5 SCAR=34.0
760  
761 0 my @times = ();
762 0 for my $k (keys %{ $this->{Time} }) {
763 0 ▶ my $x = $this->{Time}{$k}{diffs}; next unless ref($x) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x eq "ARRAY" 0 0 0
764 0 my $n = int @$x;
765 0 my $sum = 0;
766 0 $sum += $_ for @$x;
767  
768 0 push @times, [ $k, $sum, $n, ($sum/$n) ];
769 }
770  
771 0 for my $x (sort {$b->[1] <=> $a->[1]} @times) {
772 0 printf('%-35s: sum=%3.4fs cardinality=%5d avg=%3.2fs%s', @$x, "\n");
773 }
774  
775 0 delete $this->{Time};
776 }
777 # }}}
778 # _time {{{
779 sub _time {
780 155441 ▶ return unless $ENV{DEBUG} > 0;
Subroutine _time: called CC=6 SCAR=35.0
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"DEBUG"} > 0 155441 0 155441
781  
782 0 my $this = shift;
783 0 my $tag = shift;
784  
785 0 ▶ if( $ENV{DEBUG} == 1.2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"} == "1.2") 0 0 0
786 0 my @a;
787  
788 0 for (sort keys %{ $this->{Time} }) {
789 0 ▶ push @a, $_ if $this->{Time}{$_}{start};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"Time"}{$_}{"start"} 0 0 0
790 }
791  
792 0 print STDERR "clocks: @a\n";
793 }
794  
795 0 ▶ if( defined $this->{Time}{$tag}{start} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{'Time'}{$tag}{'start'}) { } 0 0 0
796 0 push @{ $this->{Time}{$tag}{diffs} }, (time - $this->{Time}{$tag}{start});
797 0 delete $this->{Time}{$tag}{start};
798 } else {
799 0 $this->{Time}{$tag}{start} = time;
800 }
801 }
802 # }}}
803 # _nodelist {{{
804 sub _nodelist {
805 2764 ▶ my $this = shift;
Subroutine _nodelist: called CC=5 SCAR=16.1
806 2764 my $list = shift;
807 2764 my $cur = shift;
808  
809 # $this->{nodelist} = { map {$this->_ref2id($_) => $this->_nodelist([], $_)} @{$this->{gametree}} };
810  
811 2764 for my $kid (@{ $cur->{kids} }) {
812 2667 my $id = $this->_ref2id( $kid );
813  
814 2667 ▶ die "problem parsing node id" unless $id =~ m/(\d+)\.(\d+)\-(.+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $id =~ /(\d+)\.(\d+)\-(.+)/ 0 2667 2667
815  
816 2667 my ($g, $v, $m) = ($1, $2, $3);
817  
818 2667 ▶ if( $v > @{ $list } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v > @{$list;}) 251 2416 2667
819 251 my $x = [];
820 251 push @$list, $x;
821 251 for (1..$m) {
822 453 push @$x, undef;
823 }
824 }
825  
826 2667 push @{ $list->[$v-1] }, $id;
827  
828 2667 $this->_nodelist($list, $kid);
829 }
830  
831 2764 return $list;
832 }
833 # }}}
834 # _parse (aka, the internal parse) {{{
835 sub _parse {
836 478 ▶ my $this = shift;
Subroutine _parse: called CC=49 SCAR=45.0
837 478 my $level = shift;
838 478 my $pref = shift;
839 478 my $gref = shift;
840 478 my $parent = shift;
841  
842 478 ▶ if( $ENV{DEBUG} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"} > 1) 0 478 478
843 0 print STDERR "\t_parse($level)";
844 0 ▶ print STDERR " ... variation = $gref->{variations} " if ref($gref) and defined $gref->{variations};
Branch: 1 branch
BranchTrueFalseTotal
if ref $gref and defined $gref->{"variations"} 0 0 0
Condition0%0/3
ref $gref and defined $gref->{"variations"}
!ll&&!rl&&r
000
MC/DC0%0/2
ref $gref and defined $gref->{"variations"}
ref $gref defined $gref->{"variations"}
Truth table0%0/3
A and B
A= ref $grefB= defined $gref->{"variations"}
ABresult
0X0
100
111
845 0 print STDERR "\n";
846 }
847  
848 478 my $gm_pr_reg = qr{^(?:GM|SZ|CA|AP|RU|KM|HA|FF|PW|PB|RE|TM|OT|BR|WR|DT|PC|AN|BT|CP|EV|GN|GC|ON|RO|SO|US)$};
849  
850 478 ▶ if( $level == 0 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($level == 0) { } 13 465 478
elsif ($level == 1) { } 97 368 465
elsif (defined $gref) { } 368 0 368
851 # The file level... $gref is most certainly undefined...
852 # We're also starting the gametree from scratch here
853  
854 13 $this->{gametree} = [];
855  
856 13 ▶ if( int(@{ $this->{parse}{n} }) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (int @{$this->{"parse"}{"n"};}) 0 13 13
857 0 $this->{error} = "Parse Error: nodes found at top level... very strange.";
858 0 return 0;
859 }
860  
861 13 for my $c (@{ $this->{parse}{c} }) {
862 97 ▶ $this->_parse($level+1, $c, undef) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->_parse($level + 1, $c, undef) or return 0 0 97 97
863 }
864  
865 13 return 1;
866  
867 } elsif( $level == 1 ) {
868 # Every collection should be a new game
869 # At this $level, all we do is make a game and look for game properties.
870 # Then we re _parse() at our current position
871  
872 97 $gref = { variations=>1, kids=>[] }; push @{ $this->{gametree} }, $gref;
873 97 $gref->{gnum} = int @{ $this->{gametree} };
874  
875 97 my $pnode = $pref->{n}[0];
876 97 for my $p (@$pnode) {
877 433 ▶ if( $p->{P} =~ m/$gm_pr_reg/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"P"} =~ /$gm_pr_reg/) 416 17 433
878 416 $gref->{game_properties}{$p->{P}} = $p->{V};
879 }
880 433 ▶ if( $p->{P} eq "CoPyright" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->{"P"} eq "CoPyright") 0 433 433
881 0 $gref->{game_properties}{FF} = 4;
882 }
883 }
884  
885 97 ▶ unless( $gref->{game_properties}{GM} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gref->{"game_properties"}{"GM"} == 1) 0 97 97
886 0 $this->{error} = "Parse Error: Need GM[1] property in the first node of the game... not found.";
887 0 return 0;
888 }
889  
890 97 ▶ unless( $gref->{game_properties}{FF} == 3 or $gref->{game_properties}{FF} == 4 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gref->{"game_properties"}{"FF"} == 3 or $gref->{"game_properties"}{"FF"} == 4) 0 97 97
Condition66%2/3
$gref->{"game_properties"}{"FF"} == 3 or $gref->{"game_properties"}{"FF"} == 4
l!l&&r!l&&!r
9070
MC/DC0%0/2
$gref->{"game_properties"}{"FF"} == 3 or $gref->{"game_properties"}{"FF"} == 4
$gref->{"game_properties"}{"FF"} == 3 $gref->{"game_properties"}{"FF"} == 4
Truth table66%2/3
A or B
A= $gref->{"game_properties"}{"FF"} == 3B= $gref->{"game_properties"}{"FF"} == 4
ABresult
1X1
011
000
891 0 ▶ unless( $ENV{ALLOW_STRANGE_FFs} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"ALLOW_STRANGE_FFs"}) 0 0 0
892 0 $this->{error} = "Parse Error: Need FF[3] or FF[4] property in the first node of the game... not found.";
893 0 return 0;
894 }
895 }
896  
897 97 ▶ if( $gref->{game_properties}{SZ} < 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gref->{"game_properties"}{"SZ"} < 3) 0 97 97
898 0 $this->{error} = "Parse Error: SZ must be set and be greater than 2 (SZ was $gref->{game_properties}{SZ})";
899 0 return 0;
900 }
901  
902 97 ▶ if( $gref->{game_properties}{FF} == 3 and $gref->{game_properties}{SZ} > 19 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gref->{"game_properties"}{"FF"} == 3 and $gref->{"game_properties"}{"SZ"} > 19) 0 97 97
Condition66%2/3
$gref->{"game_properties"}{"FF"} == 3 and $gref->{"game_properties"}{"SZ"} > 19
!ll&&!rl&&r
7900
MC/DC0%0/2
$gref->{"game_properties"}{"FF"} == 3 and $gref->{"game_properties"}{"SZ"} > 19
$gref->{"game_properties"}{"FF"} == 3 $gref->{"game_properties"}{"SZ"} > 19
Truth table66%2/3
A and B
A= $gref->{"game_properties"}{"FF"} == 3B= $gref->{"game_properties"}{"SZ"} > 19
ABresult
0X0
100
111
903 0 $this->{error} = "Parse Error: In FF[3] a move of B[tt] is a pass and therefore, SZ must be less than 20 " .
904 "(SZ was $gref->{game_properties}{SZ}).";
905 0 return 0;
906 }
907  
908 97 ▶ if( $gref->{game_properties}{FF} == 4 and $gref->{game_properties}{SZ} > 52 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gref->{"game_properties"}{"FF"} == 4 and $gref->{"game_properties"}{"SZ"} > 52) 0 97 97
Condition66%2/3
$gref->{"game_properties"}{"FF"} == 4 and $gref->{"game_properties"}{"SZ"} > 52
!ll&&!rl&&r
9070
MC/DC0%0/2
$gref->{"game_properties"}{"FF"} == 4 and $gref->{"game_properties"}{"SZ"} > 52
$gref->{"game_properties"}{"FF"} == 4 $gref->{"game_properties"}{"SZ"} > 52
Truth table66%2/3
A and B
A= $gref->{"game_properties"}{"FF"} == 4B= $gref->{"game_properties"}{"SZ"} > 52
ABresult
0X0
100
111
909 0 $this->{error} = "Parse Error: In FF[4] the size of the board must be no greater than 52" .
910 "(SZ was $gref->{game_properties}{SZ})";
911 0 return 0;
912 }
913  
914 97 ▶ $this->_parse($level+1, $pref, $gref) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->_parse($level + 1, $pref, $gref) or return 0 0 97 97
915  
916 97 return 1;
917  
918 } elsif( defined $gref ) {
919 # OK, now we're getting into some serious parsing.
920  
921 368 my $gnode; # this has the effect of forking the variations off the last node in the collection.
922 # is that correct?
923  
924 368 for my $i (0..$#{ $pref->{n} }) {
925 2667 my $pnode = $pref->{n}[$i];
926  
927 2667 ▶ $parent = ($gnode ? $gnode : $parent ? $parent : $gref);
Branch: 2 branches
BranchTrueFalseTotal
$gnode ? : 2299 368 2667
$parent ? : 271 97 368
928  
929 2667 $gnode = { variation=>$gref->{variations}, kids=>[] };
930 2667 push @{ $parent->{kids} }, $gnode;
931  
932 2667 ▶ $gnode->{board} = $this->_copy_board_matrix( $parent->{board} ) if $parent->{board};
Branch: 1 branch
BranchTrueFalseTotal
if $parent->{"board"} 2570 97 2667
933 2667 ▶ $gnode->{board} = $this->_new_board_matrix( $gref ) unless $gnode->{board};
Branch: 1 branch
BranchTrueFalseTotal
unless $gnode->{"board"} 97 2570 2667
934  
935 2667 $gnode->{captures} = { B=>0, W=>0 };
936 2667 ▶ if( ref($parent) and ref(my $pc = $parent->{captures}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $parent and ref(my $pc = $parent->{"captures"})) 2570 97 2667
Condition66%2/3
ref $parent and ref(my $pc = $parent->{"captures"})
!ll&&!rl&&r
0972570
MC/DC50%1/2
ref $parent and ref(my $pc = $parent->{"captures"})
ref $parent ref(my $pc = $parent->{"captures"})
Truth table66%2/3
A and B
A= ref $parentB= ref(my $pc = $parent->{"captures"})
ABresult
0X0
100
111
937 2570 $gnode->{captures}{B} += $pc->{B};
938 2570 $gnode->{captures}{W} += $pc->{W};
939 }
940  
941 2667 for my $p (@$pnode) {
942 8189 ▶ if( $p->{P} =~ m/^([BW])$/) {
Branch: 6 branches
BranchTrueFalseTotal
if ($p->{'P'} =~ /^([BW])$/) { } 2483 5706 8189
elsif ($p->{'P'} =~ /^A([WBE])$/) { } 1494 4212 5706
elsif ($p->{'P'} =~ /^C$/) { } 533 3679 4212
elsif ($p->{'P'} =~ /^(?:CR|TR|SQ)$/) { } 1434 2245 3679
elsif ($p->{'P'} =~ /^(?:LB)$/ and $p->{'V'} =~ /^(..)\:(.+)$/) { } 8 2237 2245
elsif (not $p->{'P'} =~ /$gm_pr_reg/) { } 1821 416 2237
943 2483 my $c = $1;
944 2483 my @c = $this->sgfco2numco($gref, $p->{V});
945  
946 2483 ▶ print STDERR "\t\tmove: $c($p->{V}) == [@c]\n" if $ENV{DEBUG} >= 4;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} >= 4 0 2483 2483
947  
948 2483 push @{ $gnode->{moves} }, [ $c, @c, $p->{V} ];
949  
950 2483 ▶ unless( $c[0] eq "PASS" ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c[0] eq "PASS") 2476 7 2483
951 # fix up board
952 2476 $gnode->{board}[$c[0]][$c[1]] = $c;
953  
954 # check for captures
955 2476 $this->_check_for_captures($gref->{game_properties}{SZ}, $gnode, @c );
956 }
957  
958 } elsif( $p->{P} =~ m/^A([WBE])$/ ) {
959 1494 my $c = $1;
960 1494 my @c = $this->sgfco2numco($gref, $p->{V});
961  
962 1494 push @{ $gnode->{edits} }, [ $c, @c, $p->{V} ];
963  
964 # fix up board
965 # do NOT check for captures
966 1494 ▶ if( $c eq "E" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c eq 'E') { } 0 1494 1494
967 0 $gnode->{board}[$c[0]][$c[1]] = ' ';
968 } else {
969 1494 $gnode->{board}[$c[0]][$c[1]] = $c;
970 }
971 } elsif( $p->{P} =~ m/^C$/ ) {
972 533 push @{ $gnode->{comments} }, $p->{V};
973  
974 } elsif( $p->{P} =~ m/^(?:CR|TR|SQ)$/ ) {
975 1434 my @c = $this->sgfco2numco($gref, $p->{V});
976  
977 1434 push @{ $gnode->{marks} }, [ $p->{P}, @c, $p->{V} ];
978  
979 # It's tempting to put the marks ON THE BOARD Do not do
980 # this. They'd need to get handled in _copy, and also,
981 # whosoever get's the $board out of the $gnode, can
982 # also get the $marks!
983  
984 ▶ } elsif( $p->{P} =~ m/^(?:LB)$/ and $p->{V} =~ m/^(..)\:(.+)$/ ) {
Condition66%2/3
$p->{"P"} =~ /^(?:LB)$/ and $p->{"V"} =~ /^(..)\:(.+)$/
!ll&&!rl&&r
223708
MC/DC50%1/2
$p->{"P"} =~ /^(?:LB)$/ and $p->{"V"} =~ /^(..)\:(.+)$/
$p->{"P"} =~ /^(?:LB)$/ $p->{"V"} =~ /^(..)\:(.+)$/
Truth table66%2/3
A and B
A= $p->{"P"} =~ /^(?:LB)$/B= $p->{"V"} =~ /^(..)\:(.+)$/
ABresult
0X0
100
111
985 8 push @{ $gnode->{marks} }, [ "LB", $this->sgfco2numco($gref, $1), $1, $2 ];
986  
987 } elsif( not $p->{P} =~ m/$gm_pr_reg/ ) {
988 1821 push @{ $gnode->{other}{$p->{P}} }, $p->{V};
989 }
990 }
991  
992 2667 $gnode->{gnum} = $parent->{gnum};
993 $gnode->{move_no} =
994 2483 ▶ (ref($gnode->{moves}) ? int(@{ $gnode->{moves} }) : 0)
Branch: 1 branch
BranchTrueFalseTotal
defined $parent->{'move_no'} ? : 2570 97 2667
Condition66%2/3
ref $parent && (defined $parent->{'move_no'} ? $parent->{'move_no'} : 0)
!ll&&!rl&&r
03552312
MC/DC50%1/2
ref $parent && (defined $parent->{'move_no'} ? $parent->{'move_no'} : 0)
ref $parent (defined $parent->{'move_no'} ? $parent->{'move_no'} : 0)
Truth table66%2/3
A && B
A= ref $parentB= (defined $parent->{'move_no'} ? $parent->{'move_no'} : 0)
ABresult
0X0
100
111
995 2667 ▶ + (ref($parent) and defined $parent->{move_no} ? $parent->{move_no} : 0);
Branch: 1 branch
BranchTrueFalseTotal
ref $gnode->{'moves'} ? : 2483 184 2667
996 }
997  
998 368 my $j = @{ $pref->{c} };
999 368 ▶ if( $j > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($j > 1) { } 117 251 368
elsif ($j == 1) { } 0 251 251
1000 # pretend we're in the node with move #12
1001
1002 # The first fork is still this variation, and contains move #13
1003 117 ▶ $this->_parse($level+1, $pref->{c}[0], $gref, $gnode) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->_parse($level + 1, $pref->{"c"}[0], $gref, $gnode) or return 0 0 117 117
1004  
1005 # Every other fork is an alternate move #13
1006 117 for my $i (1..$#{ $pref->{c} }) {
1007 154 $gref->{variations}++;
1008 154 ▶ $this->_parse($level+1, $pref->{c}[$i], $gref, $gnode) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$this->_parse($level + 1, $pref->{"c"}[$i], $gref, $gnode) or return 0 0 154 154
1009 }
1010 } elsif( $j == 1 ) {
1011 0 $this->{error} = "Parse Error: the author didn't think this condition could come up ... ";
1012 0 return 0;
1013 }
1014  
1015 368 return 1;
1016 }
1017  
1018 0 $this->{error} = "Parse Error: unknown parse depth ($level) or broken reference(s) ($pref, $gref)... error unknown";
1019 0 return 0;
1020 }
1021 # }}}
1022 # _ref2id {{{
1023 sub _ref2id {
1024 2764 ▶ my $this = shift;
Subroutine _ref2id: called CC=12 SCAR=25.2
1025 2764 my $ref = shift;
1026  
1027 2764 ▶ croak "invalid ref given to _ref2id()" unless ref($ref) eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $ref eq "HASH" 0 2764 2764
1028  
1029 2764 ▶ unless( defined $this->{refdb2}{$ref} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $this->{"refdb2"}{$ref}) 2764 0 2764
1030 2764 my $id;
1031 2764 my $c = 2;
1032 2764 ▶ if( defined($ref->{variation}) and defined($ref->{move_no}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ref->{'variation'} and defined $ref->{'move_no'}) { } 2667 97 2764
Condition66%2/3
defined $ref->{"variation"} and defined $ref->{"move_no"}
!ll&&!rl&&r
9702667
MC/DC50%1/2
defined $ref->{"variation"} and defined $ref->{"move_no"}
defined $ref->{"variation"} defined $ref->{"move_no"}
Truth table66%2/3
A and B
A= defined $ref->{"variation"}B= defined $ref->{"move_no"}
ABresult
0X0
100
111
1033 $id = "$ref->{gnum}." .
1034 2667 ▶ $ref->{variation} . "-" . ($ref->{move_no} ? $ref->{move_no} : "root");
Branch: 1 branch
BranchTrueFalseTotal
$ref->{'move_no'} ? : 2484 183 2667
1035 2667 my $cur = $id;
1036 2667 ▶ while( defined $this->{refdb}{$cur} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this->{"refdb"}{$cur}) 88 2667 2755
1037 88 $cur = $id . "-" . $c++;
1038 }
1039 2667 $id = $cur;
1040 } else {
1041 97 $id = ++$this->{games};
1042 }
1043  
1044 2764 ▶ print STDERR "$ref 2 id: $id\n" if $ENV{DEBUG} >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} >= 10 0 2764 2764
1045  
1046 2764 $this->{refdb2}{$ref} = $id;
1047  
1048 2764 for my $k (qw(comments board marks moves other captures game_properties variations)) {
1049 22112 ▶ $this->{refdb}{$id}{$k} = $ref->{$k} if defined $ref->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if defined $ref->{$k} 11353 10759 22112
1050 }
1051  
1052 2764 for my $k (qw(gnum kids)) {
1053 5528 delete $this->{refdb}{$id}{$k};
1054 }
1055  
1056 2764 ▶ if( $ENV{DEBUG} > 20 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"DEBUG"} > 20) 0 2764 2764
1057 0 print STDERR "\$this\->\{refdb\}\{\$ref($ref)\} = $this->{refdb2}{$ref} ",
1058 "/ \$this\-\>\{refdb\}\{\$id($id)\} = $this->{refdb}{$id}\n";
1059 }
1060 }
1061  
1062 2764 return $this->{refdb2}{$ref};
1063 }
1064 # }}}
1065 # _new_board_matrix {{{
1066 sub _new_board_matrix {
1067 97 ▶ my $this = shift;
Subroutine _new_board_matrix: called CC=4 SCAR=13.9
1068 97 my $gref = shift;
1069  
1070 97 $this->_time("_new_board_matrix");
1071  
1072 97 my $board = [];
1073  
1074 97 my $size = $gref->{game_properties}{SZ};
1075 97 ▶ croak "Syntax Error: You must pass a game reference to sgfco2numco because it needs the FF and SZ properties.\n" unless $size;
Branch: 1 branch
BranchTrueFalseTotal
unless $size 0 97 97
1076  
1077 97 for my $i (1..$size) {
1078 1781 my $row = [];
1079 1781 for my $j (1..$size) {
1080 33505 push @$row, ' ';
1081 }
1082 1781 push @$board, $row;
1083 }
1084  
1085 97 $this->_time("_new_board_matrix");
1086  
1087 97 return $board;
1088 }
1089 # }}}
1090 # _copy_board_matrix {{{
1091 sub _copy_board_matrix {
1092 2570 ▶ my $this = shift;
Subroutine _copy_board_matrix: called CC=3 SCAR=11.0
1093 2570 my $tocp = shift;
1094  
1095 2570 $this->_time("_copy_board_matrix");
1096  
1097 2570 my $board = [];
1098  
1099 2570 my $double_check = int @$tocp;
1100 2570 for (@$tocp) {
1101 47594 my @a = @{ $_ };
1102 47594 push @$board, \@a;
1103  
1104 47594 ▶ die "Problem copying board (" . (int @a) . " vs $double_check)!" unless int @a == $double_check;
Branch: 1 branch
BranchTrueFalseTotal
unless int @a == $double_check 0 47594 47594
1105 }
1106  
1107 2570 $this->_time("_copy_board_matrix");
1108  
1109 2570 return $board;
1110 }
1111 # }}}
1112  
1113 # _check_for_captures {{{
1114 sub _check_for_captures {
1115 2476 ▶ my ($this, $SZ, $node, @p) = @_;
Subroutine _check_for_captures: called CC=20 SCAR=32.3
1116 2476 my $board = $node->{board};
1117 2476 my $caps = $node->{captures};
1118  
1119 2476 $this->_time("_check_for_captures");
1120  
1121 2476 my $tc = $board->[$p[0]][$p[1]];
1122  
1123 2476 ▶ croak "crazy unexpected error: checking for caps, and current pos doesn't have a stone. Two times double odd, and fatal"
Branch: 1 branch
BranchTrueFalseTotal
unless $tc =~ /^[WB]$/ 0 2476 2476
1124 unless $tc =~ m/^[WB]$/;
1125  
1126 2476 ▶ my $oc = ($tc eq "W" ? "B" : "W");
Branch: 1 branch
BranchTrueFalseTotal
$tc eq 'W' ? : 1234 1242 2476
1127  
1128 # 1. Find groups for all adjacent stones.
1129  
1130 2476 $this->_time("for(_find_group)");
1131  
1132 2476 my %checked = ();
1133 2476 my @groups = ();
1134 2476 for my $p ( [$p[0]-1, $p[1]+0], [$p[0]+1, $p[1]+0], [$p[0]+0, $p[1]-1], [$p[0]+0, $p[1]+1] ) {
1135 9904 my @g = $this->_find_group( \%checked, $SZ, $oc, $board, @$p );
1136  
1137 9904 ▶ push @groups, [ @g ] if @g;
Branch: 1 branch
BranchTrueFalseTotal
if @g 2094 7810 9904
1138 }
1139  
1140 2476 $this->_time("for(_find_group)");
1141 2476 $this->_time("for(\@groups), _count_liberties");
1142  
1143 2476 ▶ if( @groups ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@groups) 1582 894 2476
1144 # 2. Any groups without liberties are toast!
1145 1582 ▶ print STDERR "_check_for_captures() found ", int(@groups), " neighboring groups:" if $ENV{DEBUG} > 3 and int(@groups);
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 and int @groups 0 1582 1582
Condition33%1/3
$ENV{"DEBUG"} > 3 and int @groups
!ll&&!rl&&r
158200
MC/DC0%0/2
$ENV{"DEBUG"} > 3 and int @groups
$ENV{"DEBUG"} > 3 int @groups
Truth table33%1/3
A and B
A= $ENV{"DEBUG"} > 3B= int @groups
ABresult
0X0
100
111
1146  
1147 1582 for my $group (@groups) {
1148 2094 my $l = $this->_count_liberties( $SZ, $board, @$group );
1149  
1150 2094 ▶ print STDERR " liberties($l)" if $ENV{DEBUG}>3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 2094 2094
1151 2094 ▶ if( $l < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l < 1) 183 1911 2094
1152 183 ▶ print STDERR "-killed! " if $ENV{DEBUG}>3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 183 183
1153 183 for my $p (@$group) {
1154 279 $caps->{$tc}++;
1155 279 $board->[$p->[0]][$p->[1]] = ' ';
1156 }
1157 }
1158 }
1159  
1160 1582 ▶ print STDERR "\n" if $ENV{DEBUG} > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 1582 1582
1161 }
1162  
1163 2476 $this->_time("for(\@groups), _count_liberties");
1164 2476 $this->_time("_find_group/_count_liberties of me");
1165  
1166 # 3. Check my own liberties, I may be toast
1167 2476 %checked = ();
1168 2476 my @me_group = $this->_find_group( \%checked, $SZ, $tc, $board, @p );
1169 2476 my $me_lifec = $this->_count_liberties( $SZ, $board, @me_group );
1170 2476 ▶ print STDERR "_check_for_captures() me_group ", int(@me_group), " stones: " if $ENV{DEBUG} > 3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 2476 2476
1171 2476 ▶ print STDERR " me liberties($me_lifec)" if $ENV{DEBUG}>3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 2476 2476
1172 2476 ▶ if( $me_lifec < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($me_lifec < 1) 0 2476 2476
1173 0 ▶ print STDERR "-killed! " if $ENV{DEBUG}>3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 0 0
1174 0 for my $p (@me_group) {
1175 0 $caps->{$oc}++;
1176 0 $board->[$p->[0]][$p->[1]] = ' ';
1177 }
1178 }
1179 2476 ▶ print STDERR "\n" if $ENV{DEBUG}>3;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 3 0 2476 2476
1180  
1181 2476 $this->_time("_find_group/_count_liberties of me");
1182 2476 $this->_time("_check_for_captures");
1183 }
1184 # }}}
1185 # _count_liberties {{{
1186 sub _count_liberties {
1187 4570 ▶ my ($this, $SZ, $board, @group) = @_;
Subroutine _count_liberties: called CC=9 SCAR=22.0
1188  
1189 4570 $this->_time("_count_liberties");
1190  
1191 4570 my %checked = ();
1192 4570 my $count = 0;
1193  
1194 4570 for my $g (@group) {
1195 12036 for my $p ( [$g->[0]-1, $g->[1]+0], [$g->[0]+1, $g->[1]+0], [$g->[0]+0, $g->[1]-1], [$g->[0]+0, $g->[1]+1] ) {
1196 48144 ▶ if( not $checked{"@$p"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($checked{join $", @$p}) 39402 8742 48144
1197 39402 $checked{"@$p"} = 1;
1198 39402 ▶ unless( ($p->[0] < 0 or $p->[0] > ($SZ-1)) or ($p->[1] < 0 or $p->[1] > ($SZ-1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->[0] < 0 or $p->[0] > $SZ - 1 or ($p->[1] < 0 or $p->[1] > $SZ - 1)) 37252 2150 39402
Condition100%9/9
$p->[0] < 0 or $p->[0] > $SZ - 1 or ($p->[1] < 0 or $p->[1] > $SZ - 1)
l!l&&r!l&&!r
131929637787
$p->[0] < 0 or $p->[0] > $SZ - 1
l!l&&r!l&&!r
46085938083
$p->[1] < 0 or $p->[1] > $SZ - 1
l!l&&r!l&&!r
53529637252
MC/DC75%3/4
$p->[0] < 0 or $p->[0] > $SZ - 1 or ($p->[1] < 0 or $p->[1] > $SZ - 1)
$p->[0] < 0 $p->[0] > $SZ - 1 $p->[1] < 0 $p->[1] > $SZ - 1
Truth table80%4/5
A or B or C or D
A= $p->[0] < 0B= $p->[0] > $SZ - 1C= $p->[1] < 0D= $p->[1] > $SZ - 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1199 37252 ▶ if( $board->[$p->[0]][$p->[1]] eq ' ' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$board[$p->[0]][$p->[1]] eq " ") 13705 23547 37252
1200 13705 $count++;
1201 }
1202 }
1203 }
1204 }
1205 }
1206  
1207 4570 $this->_time("_count_liberties");
1208  
1209 4570 return $count;
1210 }
1211 # }}}
1212 # _find_group {{{
1213 sub _find_group {
1214 60524 ▶ my ($this, $checked, $SZ, $oc, $board, @p) = @_;
Subroutine _find_group: called CC=13 SCAR=25.9
1215  
1216 60524 $this->_time("_find_group");
1217  
1218 60524 ▶ print STDERR "\t_find_group(@p)" if $ENV{DEBUG}>12;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 12 0 60524 60524
1219 60524 my @g;
1220  
1221 60524 ▶ if( not $checked->{"@p"} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$checked{join $", @p}) 47631 12893 60524
1222 47631 $checked->{"@p"} = 1;
1223 47631 ▶ print STDERR "." if $ENV{DEBUG}>12;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 12 0 47631 47631
1224 47631 ▶ unless( ($p[0] < 0 or $p[0] > ($SZ-1)) or ($p[1] < 0 or $p[1] > ($SZ-1)) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p[0] < 0 or $p[0] > $SZ - 1 or ($p[1] < 0 or $p[1] > $SZ - 1)) 44594 3037 47631
Condition100%9/9
$p[0] < 0 or $p[0] > $SZ - 1 or ($p[1] < 0 or $p[1] > $SZ - 1)
l!l&&r!l&&!r
187441845339
$p[0] < 0 or $p[0] > $SZ - 1
l!l&&r!l&&!r
677119745757
$p[1] < 0 or $p[1] > $SZ - 1
l!l&&r!l&&!r
74541844594
MC/DC75%3/4
$p[0] < 0 or $p[0] > $SZ - 1 or ($p[1] < 0 or $p[1] > $SZ - 1)
$p[0] < 0 $p[0] > $SZ - 1 $p[1] < 0 $p[1] > $SZ - 1
Truth table80%4/5
A or B or C or D
A= $p[0] < 0B= $p[0] > $SZ - 1C= $p[1] < 0D= $p[1] > $SZ - 1
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1225 44594 ▶ print STDERR ".." if $ENV{DEBUG}>12;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 12 0 44594 44594
1226 44594 ▶ if( $board->[$p[0]][$p[1]] eq $oc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$board[$p[0]][$p[1]] eq $oc) 12036 32558 44594
1227 12036 ▶ print STDERR " !" if $ENV{DEBUG}>12;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 12 0 12036 12036
1228 12036 push @g, [ @p ];
1229 12036 for my $p ( [$p[0]-1, $p[1]+0], [$p[0]+1, $p[1]+0], [$p[0]+0, $p[1]-1], [$p[0]+0, $p[1]+1] ) {
1230 48144 push @g, $this->_find_group( $checked, $SZ, $oc, $board, @$p );
1231 }
1232 }
1233 }
1234 }
1235 60524 ▶ print STDERR "\n" if $ENV{DEBUG}>12;
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"DEBUG"} > 12 0 60524 60524
1236  
1237 60524 $this->_time("_find_group");
1238  
1239 60524 return @g;
1240 }
1241 # }}}
1242  
↑ Summary SGF2misc/GD.pm »