Cinderblock/Rulemap.pm

filter: Statementstmt 47.7% 125 / 262 Branchbran 45.4% 40 / 88 Conditioncond 11.7% 2 / 17 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 42.8% 18 / 42 Podpod 13.3% 4 / 30 totaltotal 42.3% 190 / 449 CC 63 SCAR 29.8
CC
63
Cov
46%
compute_score
90.0
get_empty_space
56.0
detect_basis
42.0
CRAP 19.7
↑ Summary Cinderblock/Delta.pm »
1 package Games::Go::Cinderblock::Rulemap;
2 6 ▶use 5.14.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use Games::Go::Cinderblock::NodeSet;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Games::Go::Cinderblock::Delta;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Games::Go::Cinderblock::State;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Games::Go::Cinderblock::Scorable;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Games::Go::Cinderblock::MoveAttempt;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Games::Go::Cinderblock::MoveResult;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Games::Go::Cinderblock::Rulemap::Rect;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use List::MoreUtils qw/all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # This class evaluates moves and determines new board positions.
16 # This class stores no board/position data.
17 # Also, will must be used to determine visible portions of the board if there's fog of war.
18  
19 # This class is basically here to define default behavior and
20 # to provide a mechanism to override it.
21 # However this class will not handle rendering.
22  
23 # Rulemaps are not stored in the database. They are derived from
24 # entries in the Ruleset and Extra_rule tables. Or wherever.
25 # Some extra rules could be assigned using Moose's roles.
26 # Example: 'fog of war', 'atom go' each could be assigned to several variants.
27  
28 # Also: This does not involve the ko rule. That requires a database search
29 # for a duplicate position. TODO:: B::History. Do this.
30  
31 # Note: I'm treating intersections (i.e. nodes) as scalars, which different rulemap
32 # subclasses may handle as they will. Rect nodes [$row,$col].
33  
34 #To support more than 2 players or sides, each game inherently has a sort of basis
35 # such as 'ffa', 'zen', 'team', 'perverse', or perhaps more
36  
37 # Classes to extract:
38 # Games::Go::Cinderblock::State, (DONE)
39 # Games::Go::Cinderblock::MoveResult, (DONE, + MoveAttempt)
40 # Games::Go::Cinderblock::Scorable, (DONE)
41 # Games::Go::Cinderblock::Delta,
42 # Games::Go::Cinderblock::History?
43 # Games::Go::Cinderblock::TurmDeturninator?
44 # Games::Go::Cinderblock::NodeSet, (DONE)
45  
46 6 has topology => (
47 is => 'ro',
48 isa => 'Str',
49 default => 'plane'
50 );
51 6 has phase_description => (
52 is => 'ro',
53 isa => 'Str',
54 default => '0b 1w'
55 );
56 6 has komi => (
57 is => 'ro',
58 isa => 'Num',
59 default => '6.5'
60 );
61 6 has ko_rule => (
62 is => 'ro',
63 isa => 'Str',
64 default => 'situational',
65 );
66  
67 # to be extended to fog, atom, etc
68 0 ▶sub FOO_apply_rule_role{
Subroutine FOO_apply_rule_role: not called CC=3 SCAR=24.8
Pod undocumented
69 0 my ($self, $rule, $param) = @_;
70 0 ▶ if ($rule =~ /^heisengo/){
Branch: 2 branches
BranchTrueFalseTotal
if ($rule =~ /^heisengo/u) { } 0 0 0
elsif ($rule =~ /^planckgo/u) { } 0 0 0
71 0 Games::Go::Cinderblock::Rulemap::Heisengo::apply ($self, $param);
72 }
73 elsif ($rule =~ /^planckgo/){
74 0 Games::Go::Cinderblock::Rulemap::Planckgo::apply ($self, $param);
75 }
76 0 else {die $rule}
77 }
78  
79  
80 #These must be implemented in a subclass
81 6 my $blah = 'use a topo subclass such as ::Rect instead of Games::Go::Cinderblock::Rulemap';
82 0 ▶sub move_is_valid{ die $blah}
Subroutine move_is_valid: not called CC=1 SCAR=6.9
Pod undocumented
83 0 ▶sub node_to_string{ die $blah}
Subroutine node_to_string: not called CC=1 SCAR=6.9
Pod undocumented
84 0 ▶sub node_from_string{ die $blah}
Subroutine node_from_string: not called CC=1 SCAR=6.9
Pod undocumented
85 0 ▶sub node_liberties{ die $blah}
Subroutine node_liberties: not called CC=1 SCAR=6.9
Pod undocumented
86 0 ▶sub set_stone_at_node{ die $blah}
Subroutine set_stone_at_node: not called CC=1 SCAR=6.9
Pod undocumented
87 0 ▶sub stone_at_node{ die $blah}
Subroutine stone_at_node: not called CC=1 SCAR=6.9
Pod undocumented
88 0 ▶sub all_nodes{ die $blah}
Subroutine all_nodes: not called CC=1 SCAR=6.9
Pod documented
89 0 ▶sub copy_board{ die $blah}
Subroutine copy_board: not called CC=1 SCAR=6.9
Pod undocumented
90 0 ▶sub empty_board{ die $blah}
Subroutine empty_board: not called CC=1 SCAR=6.9
Pod documented
91  
92  
93 2 ▶sub initial_state{
Subroutine initial_state: called CC=1 SCAR=0.0
Pod documented
94 2 my $self = shift;
95 2 my $state = Games::Go::Cinderblock::State->new(
96 rulemap => $self,
97 board => $self->empty_board,
98 turn => 'b',
99 );
100 2 return $state;
101 }
102  
103 0 ▶sub normalize_board_to_string{ # to hash for ko collisions..
Subroutine normalize_board_to_string: not called CC=2 SCAR=17.9
Pod undocumented
104 0 my ($self,$board) = @_;
105 0 my @all_nodes = $self->all_nodes;
106 0 ▶ my @all_stones = map {$self->stone_at_node($board, $_) || 0} @all_nodes;
Condition0%0/2
$self->stone_at_node($board, $_) || 0
l!l
00
MC/DC0%0/1
$self->stone_at_node($board, $_) || 0
$self->stone_at_node($board, $_)
Truth table0%0/2
A
A= $self->stone_at_node($board, $_)
Aresult
11
00
107 0 return join '', @all_stones;
108 }
109 # use Carp::Always;
110  
111 14 ▶sub evaluate_move_attempt{
Subroutine evaluate_move_attempt: called CC=9 SCAR=22.0
Pod undocumented
112 14 my ($self, $move_attempt) = @_;
113 14 my $basis = $move_attempt->basis_state;
114 14 my $board = $basis->board;
115 14 my $node = $move_attempt->node;
116 14 my $color = $move_attempt->color;
117 # my ($self, $board, $node, $color) = @_;
118 14 ▶ die "bad color $color" unless $color=~ /^[bw]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $color =~ /^[bw]$/u 0 14 14
119 14 ▶ die "bad node @$node" unless $self->node_is_valid($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node_is_valid($node) 4 10 14
120
121 10 my %failure_template = (
122 rulemap => $self,
123 basis_state => $basis,
124 move_attempt => $move_attempt,
125 succeeded => 0,
126 );
127 10 ▶ if ($self->stone_at_node ($board, $node)){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->stone_at_node($board, $node)) 3 7 10
128 3 return Games::Go::Cinderblock::MoveResult->new(
129 %failure_template,
130 reason => "stone exists at ". $self->node_to_string($node)
131 );
132 }
133 7 ▶ if ($color ne $basis->turn){
Branch: 1 branch
BranchTrueFalseTotal
if ($color ne $basis->turn) 2 5 7
134 2 return Games::Go::Cinderblock::MoveResult->new(
135 %failure_template,
136 reason => "color $color (not) played during turn " .$basis->turn,
137 );
138 }
139
140 #produce copy of board for evaluation -> add stone at $node
141 5 my $newboard = $self->copy_board ($board);
142 5 $self->set_stone_at_node ($newboard, $node, $color);
143 # $chain is a list of strongly connected stones,
144 # and $foes=enemies,$libs=liberties adjacent to $chain
145 5 my ($chain, $libs, $foes) = $self->get_chain($newboard, $node);
146 5 my $caps = $self->find_captured ($newboard, $foes);
147 5 ▶ if (@$libs == 0 and @$caps == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (@$libs == 0 and @$caps == 0) 1 4 5
Condition66%2/3
@$libs == 0 and @$caps == 0
!ll&&!rl&&r
401
MC/DC50%1/2
@$libs == 0 and @$caps == 0
@$libs == 0 @$caps == 0
Truth table66%2/3
A and B
A= @$libs == 0B= @$caps == 0
ABresult
0X0
100
111
148 1 return Games::Go::Cinderblock::MoveResult->new(
149 rulemap => $self,
150 basis_state => $basis,
151 move_attempt => $move_attempt,
152 succeeded => 0,
153 reason => "suicide",
154 );
155 }
156 4 for my $cap(@$caps){ # just erase captured stones
157 3 $self->set_stone_at_node ($newboard, $cap, 0);
158 }
159 4 ▶ my $other_color = (($color eq 'b') ? 'w' : 'b');
Branch: 1 branch
BranchTrueFalseTotal
$color eq 'b' ? : 3 1 4
160 my $res_stt = Games::Go::Cinderblock::State->new(
161 rulemap => $self,
162 board => $newboard,
163 turn => $other_color,
164 captures => {
165 $color => $basis->captures->{$color} + @$caps,
166 4 $other_color => $basis->captures->{$other_color},
167 },
168 );
169 #return ($newboard, '', $caps);#no err
170 4 return Games::Go::Cinderblock::MoveResult->new(
171 rulemap => $self,
172 basis_state => $basis,
173 move_attempt => $move_attempt,
174 succeeded => 1,
175 caps => $caps,
176 resulting_state => $res_stt,
177 );
178 #node is returned to make this method easier to override for heisenGo
179 }
180  
181 #uses a floodfill algorithm, #TODO: absorb. generic.
182 #returns (string, liberties, adjacent_foes)
183 11 ▶sub get_chain { #for all board types
Subroutine get_chain: called CC=6 SCAR=17.9
Pod undocumented
184 11 my ($self, $board, $node1) = @_; #start row/column
185
186 11 my %seen; #indexed by stringified nodes
187 my @found;
188 11 my @libs; #liberties
189 11 my @foes; #enemy stones adjacent to string
190 11 my $string_side = $self->stone_at_node($board, $node1);
191 11 ▶ return unless defined $string_side; #empty
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string_side 0 11 11
192 #0 has to mean empty, (b black, w white, ...)
193 11 my @nodes = ($node1); #array of adjacent intersections to consider
194
195 11 ▶ while (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 117 11 128
196 117 my $node = pop @nodes;
197 117 ▶ next if $seen {$self->node_to_string ($node)};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$self->node_to_string($node)} 34 83 117
198 83 $seen {$self->node_to_string ($node)} = 1;
199
200 83 my $here_side = $self->stone_at_node ($board, $node);
201
202 83 ▶ unless ($here_side){ #empty
Branch: 1 branch
BranchTrueFalseTotal
unless ($here_side) 16 67 83
203 16 push @libs, $node;
204 next
205 16 }
206 67 ▶ if ($here_side eq $string_side){
Branch: 1 branch
BranchTrueFalseTotal
if ($here_side eq $string_side) 32 35 67
207 32 push @found, $node;
208 32 push @nodes, $self->node_liberties ($node);
209 next
210 32 }
211 # else enemy
212 35 push @foes, $node;
213 }
214 11 return (\@found, \@libs, \@foes);
215 }
216  
217  
218 #chains are represented by a single 'delegate' node to identify chain
219 #returns chains, keyed by their delegates. a chain is a list of nodestrings
220 #also returns hash of {nodestring=>delegate}
221 #also returns hash of {delegate=>side}
222 0 ▶sub FOO_all_chains{
Subroutine FOO_all_chains: not called CC=4 SCAR=30.0
Pod undocumented
223 0 my ($self, $board) = @_;
224 0 my %delegates;
225 my %delegate_of_stone;
226 0 my %delegate_side;
227 0 for my $n ($self->all_stones($board)){
228 0 my $s = $self->node_to_string($n);
229 0 ▶ next if $delegate_of_stone{$s};
Branch: 1 branch
BranchTrueFalseTotal
if $delegate_of_stone{$s} 0 0 0
230
231 0 $delegate_side{$s} = $self->stone_at_node($board, $n);
232 0 my ($chain,$l,$f) = $self->get_chain($board, $n);
233 #push @chains, $chain;
234 #only deal with nodestrings here;
235 0 $delegates{$s} = [map {$self->node_to_string($_)} @$chain];
236 0 my @nodestrings;
237 #examine & to_string each node
238 0 for (@$chain){
239 0 my $nodestring =$self->node_to_string($_);
240 0 push @nodestrings, $nodestring;
241 0 $delegate_of_stone{$nodestring} = $s;
242 }
243 }
244 0 return (\%delegates, \%delegate_of_stone, \%delegate_side)
245 }
246  
247 175 ▶sub nodeset{ # $rm->nodeset(@nodes)
Subroutine nodeset: called CC=2 SCAR=6.9
Pod documented
248 175 my $self = shift;
249 175 my $ns = Games::Go::Cinderblock::NodeSet->new(rulemap => $self);
250 175 $ns->add($_) for @_;
251 175 return $ns;
252 }
253 # sub all_nodes, sub no_nodes.. TODO
254 4 ▶sub all_nodes_nodeset{
Subroutine all_nodes_nodeset: called CC=1 SCAR=0.0
Pod undocumented
255 4 my $self = shift;
256 4 return $self->nodeset($self->all_nodes);
257 }
258 0 ▶sub FOO_floodfill{ #in state now..
Subroutine FOO_floodfill: not called CC=4 SCAR=30.0
Pod undocumented
259 0 my ($self, $cond, $progenitor) = @_;
260 0 my $set = $self->nodeset($progenitor);
261 0 my $seen = $self->nodeset($progenitor);
262 0 my @q = $self->adjacent_nodes($progenitor);
263 0 ▶ while(@q){
Branch: 1 branch
BranchTrueFalseTotal
if (@q) 0 0 0
264 0 my $node = shift @q;
265 0 ▶ next if $seen->has($node);
Branch: 1 branch
BranchTrueFalseTotal
if $seen->has($node) 0 0 0
266 0 $seen->add($node);
267 0 ▶ next unless $cond->($node);
Branch: 1 branch
BranchTrueFalseTotal
unless &$cond($node) 0 0 0
268 0 $set->add($node);
269 0 push @q, $self->adjacent_nodes($node);
270 }
271 }
272  
273  
274 0 ▶sub all_stones {
Subroutine all_stones: not called CC=1 SCAR=6.9
Pod undocumented
275 0 my ($self, $board) = @_;
276 0 return grep {$self->stone_at_node($board, $_)} ($self->all_nodes);
277 }
278  
279 #opposite of get_chain
280 0 ▶sub get_empty_space{
Subroutine get_empty_space: not called CC=7 SCAR=40.3
Pod undocumented
281 0 my ($self, $board, $node1, $ignore_stones) = @_; #start row/column
282 0 ▶ return ([],[]) if $self->stone_at_node ($board, $node1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->stone_at_node($board, $node1) 0 0 0
283 0 ▶ $ignore_stones = {} unless $ignore_stones; #dead stones tend to be ignored when calculating territory
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore_stones 0 0 0
284
285 0 my %seen; #indexed by stringified nodes
286 my @found;
287 0 my @adjacent_stones;
288 0 my @nodes = ($node1); #array of adjacent intersections to consider
289 0 ▶ while (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 0 0 0
290 0 my $node = pop @nodes;
291 0 my $nodestring = $self->node_to_string ($node);
292 0 ▶ next if $seen {$nodestring};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$nodestring} 0 0 0
293 0 $seen {$nodestring} = 1;
294
295 0 my $here_color = $self->stone_at_node ($board, $node);
296 0 ▶ if (!$here_color or $ignore_stones->{$nodestring}){ #empty
Branch: 1 branch
BranchTrueFalseTotal
if (not $here_color or $ignore_stones->{$nodestring}) { } 0 0 0
Condition0%0/3
not $here_color or $ignore_stones->{$nodestring}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $here_color or $ignore_stones->{$nodestring}
not $here_color $ignore_stones->{$nodestring}
Truth table0%0/3
not(A) or B
A= not $here_colorB= $ignore_stones->{$nodestring}
ABresult
1X1
011
000
297 0 push @found, $node;
298 0 push @nodes, $self->node_liberties ($node)
299 }
300 else{ #stone
301 0 push @adjacent_stones, $node;
302 }
303 }
304 0 return (\@found, \@adjacent_stones);
305 }
306  
307 # TODO: absorb into generic flood fill
308 #take a list of stones, returns those which have no libs, as chains
309 5 ▶sub find_captured{
Subroutine find_captured: called CC=6 SCAR=17.9
Pod undocumented
310 5 my ($self, $board, $nodes) = @_;
311 5 my @nodes = @$nodes; #list
312 5 my %seen; #indexed by stringified node
313 my @caps; #list
314 5 ▶ while (@nodes){
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 12 5 17
315 12 my $node = pop @nodes;
316 12 ▶ next if $seen {$self->node_to_string($node)};
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$self->node_to_string($node)} 6 6 12
317 6 my ($chain, $libs, $foes) = $self->get_chain ($board, $node);
318 6 ▶ my $capture_these = scalar @$libs ? '0' : '1';
Branch: 1 branch
BranchTrueFalseTotal
scalar @$libs ? : 4 2 6
319 6 for my $n (@$chain){
320 19 $seen {$self->node_to_string($n)} = 1;
321 19 ▶ push @caps, $n if $capture_these;
Branch: 1 branch
BranchTrueFalseTotal
if $capture_these 3 16 19
322 }
323 }
324 return \@caps
325 5 }
326  
327  
328  
329 0 ▶sub side_of_entity{
Subroutine side_of_entity: not called CC=4 SCAR=30.0
Pod undocumented
330 0 my ($self, $entity) = @_;
331 0 ▶ die 'wrong score mode' unless $self->detect_basis eq 'ffa';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->detect_basis eq "ffa" 0 0 0
332 0 for my $phase (split ' ', $self->phase_description) {
333 0 ▶ if ($phase =~ m/$entity([wbr])/){
Branch: 1 branch
BranchTrueFalseTotal
if ($phase =~ /$entity([wbr])/u) 0 0 0
334 0 return $1;
335 }
336 }
337 }
338 0 ▶sub all_entities{
Subroutine all_entities: not called CC=2 SCAR=17.9
Pod undocumented
339 0 my $self = shift;
340 0 my $pd = $self->phase_description;
341 0 my %e;
342 0 ▶ while($pd=~/(\d)/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($pd =~ /(\d)/gu) 0 0 0
343 0 $e{$1}=1
344 }
345 0 return keys %e;
346 }
347 0 ▶sub all_sides{
Subroutine all_sides: not called CC=2 SCAR=17.9
Pod undocumented
348 0 my $self = shift;
349 0 my $pd = $self->phase_description;
350 0 my %s;
351 0 ▶ while($pd=~/([bw])/g){
Branch: 1 branch
BranchTrueFalseTotal
if ($pd =~ /([bw])/gu) 0 0 0
352 0 $s{$1}=1
353 }
354 0 return keys %s;
355 }
356  
357 0 ▶sub default_captures {#for before move 1
Subroutine default_captures: not called CC=1 SCAR=6.9
Pod undocumented
358 0 my $self = shift;
359 0 my @phases = split ' ', $self->phase_description;
360 0 return join ' ', map {0} (1..@phases) #'0 0'
361 }
362  
363  
364 #Necessary to decide how to describe game in /game.
365 #Score & game objectives depend.
366 #reads the phase description and
367 # returns 'ffa', 'team', 'zen', or 'perverse'? or 'other'?
368 0 ▶sub detect_basis{
Subroutine detect_basis: not called CC=6 SCAR=37.4
Pod undocumented
369 0 my $self = shift; #is it a pd or a rulemap?
370 0 ▶ my $pd = ref $self ? $self->phase_description : $self;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 0 0
371
372 #assume that this is well-formed
373 #and no entity numbers are skipped
374 0 my @phases = map {[split'',$_]} split ' ', $pd;
375 0 my %ents;
376 my %sides;
377 0 for (@phases){
378 0 $ents{$_->[0]}{$_->[1]} = 1;
379 0 $sides{$_->[1]}{$_->[0]} = 1;
380 }
381 0 ▶ return 'ffa' if @phases == keys %ents
Branch: 1 branch
BranchTrueFalseTotal
if @phases == keys %ents and @phases == keys %sides 0 0 0
Condition0%0/3
@phases == keys %ents and @phases == keys %sides
!ll&&!rl&&r
000
MC/DC0%0/2
@phases == keys %ents and @phases == keys %sides
@phases == keys %ents @phases == keys %sides
Truth table0%0/3
A and B
A= @phases == keys %entsB= @phases == keys %sides
ABresult
0X0
100
111
382 and @phases == keys %sides;
383 0 ▶ return 'zen' if all {keys %{$ents{$_}} == keys%sides} (keys%ents);
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if &all(sub { keys %{$ents{$_};} == keys %sides; } , keys %ents) 0 0 0
384
385 0 return 'other';
386 }
387  
388 0 ▶sub compute_score{
Subroutine compute_score: not called CC=9 SCAR=45.0
Pod undocumented
389 0 my ($self, $board, $caps, $death_mask) = @_;
390 0 my ($terr_mask, $terr_points) = $self->find_territory_mask($board, $death_mask);
391
392 0 my $type = $self->detect_basis;
393 0 my $pd = $self->phase_description;
394 0 my @phases = split ' ', $pd;
395 0 @phases = map {[split '', $_]} @phases;
396
397 0 my @sides = $self->all_sides;
398 0 my %side_score = map {$_=>0} @sides;
399
400 { #add up captures of each team.
401 0 my @caps = split ' ', $caps; # from latest move
402 0 for my $phase (@phases){
403 0 my $phase_caps = shift @caps;
404 0 $side_score{$phase->[1]} += $phase_caps;
405 }
406 #add up territory of each team.
407 0 for my $side (@sides){
408 0 $side_score{$side} += $terr_points->{$side};
409 }
410 #and count dead things in death_mask
411 #points in death_mask go to territory owner in terr_mask
412 0 for my $d (keys %$death_mask){
413 0 my $capturer = $terr_mask->{$d};
414 0 ▶ if ($capturer){
Branch: 1 branch
BranchTrueFalseTotal
if ($capturer) 0 0 0
415 0 $side_score{$capturer}++;
416 }
417 }
418 }
419
420 0 ▶ if ($self->phase_description eq '0b 1w'){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->phase_description eq "0b 1w") 0 0 0
421 0 $side_score{w} += $self->komi;
422 }
423
424 0 ▶ if ($type eq 'ffa' or $type eq 'zen' or $type eq 'team'){
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "ffa" or $type eq "zen" or $type eq "team") 0 0 0
Condition0%0/6
$type eq "ffa" or $type eq "zen" or $type eq "team"
l!l&&r!l&&!r
000
$type eq "ffa" or $type eq "zen"
l!l&&r!l&&!r
000
MC/DC0%0/3
$type eq "ffa" or $type eq "zen" or $type eq "team"
$type eq "ffa" $type eq "zen" $type eq "team"
Truth table0%0/4
A or B or C
A= $type eq "ffa"B= $type eq "zen"C= $type eq "team"
ABCresult
1XX1
01X1
0011
0000
425 0 return \%side_score
426 }
427 0 return 'perverse or other modes not scoring...'
428 }
429  
430 0 ▶sub num_phases{
Subroutine num_phases: not called CC=1 SCAR=6.9
Pod undocumented
431 0 my ($self) = @_;
432 0 my @phases = split ' ', $self->phase_description;
433 0 return scalar @phases;
434 }
435  
436 0 ▶sub determine_next_phase{
Subroutine determine_next_phase: not called CC=3 SCAR=24.8
Pod undocumented
437 0 my ($self, $phase, $choice_phases) = @_;
438 0 my $np = $self->num_phases;
439 0 my $next = $phase;
440 0 for (1..$np){
441 0 $next = ($next + 1) % $np;
442 0 ▶ return $next if grep {$next==$_} @$choice_phases;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$next == $_;} @$choice_phases 0 0 0
443 }
444 0 die "I was given a bad list of choice phases: " . join',',@$choice_phases;
445 }
446  
447  
448 #compare earlier state to later state.
449 # package the things which actually change.
450 # among board, turn, & captures.
451  
452 4 ▶sub delta{
Subroutine delta: called CC=5 SCAR=16.1
Pod undocumented
453 4 my ($self,$state1,$state2) = @_;
454 4 my %deltargs;
455  
456 4 my $board_changeset = $self->_compare_boards( $state1->board, $state2->board );
457 4 ▶ $deltargs{board} = $board_changeset if %$board_changeset;
Branch: 1 branch
BranchTrueFalseTotal
if %$board_changeset 4 0 4
458 4 ▶ if($state1->turn ne $state2->turn){
Branch: 1 branch
BranchTrueFalseTotal
if ($state1->turn ne $state2->turn) 4 0 4
459 4 $deltargs{turn} = {before=>$state1->turn,after=>$state2->turn};
460 }
461 4 ▶ if($state1->captures('w') != $state2->captures('w')){
Branch: 1 branch
BranchTrueFalseTotal
if ($state1->captures("w") != $state2->captures("w")) 3 1 4
462 3 $deltargs{captures}{w} = {before=>$state1->captures('w'),after=>$state2->captures('w')};
463 }
464 4 ▶ if($state1->captures('b') != $state2->captures('b')){
Branch: 1 branch
BranchTrueFalseTotal
if ($state1->captures("b") != $state2->captures("b")) 3 1 4
465 3 $deltargs{captures}{b} = {before=>$state1->captures('b'),after=>$state2->captures('b')};
466 }
467  
468 4 my $delta = Games::Go::Cinderblock::Delta->new(
469 rulemap => $self,
470 %deltargs
471 );
472 4 return $delta;
473 }
474  
475  
476 #compare earlier board to later board.
477 4 ▶sub _compare_boards{
Subroutine _compare_boards: called CC=5 SCAR=16.1
478 4 my ($self, $board1, $board2) = @_;
479
480 4 my %changeset;
481 4 for my $node ($self->all_nodes){
482 16 my $fore = $self->stone_at_node($board1, $node); #0,w,b,etc
483 16 my $afte = $self->stone_at_node($board2, $node);
484 16 ▶ next if ($fore eq $afte);
Branch: 1 branch
BranchTrueFalseTotal
if $fore eq $afte 6 10 16
485 10 ▶ if($fore){
Branch: 1 branch
BranchTrueFalseTotal
if ($fore) 3 7 10
486 # autovivify.
487 3 push @{$changeset{remove}{$fore}}, $node;
488 }
489 10 ▶ if($afte){
Branch: 1 branch
BranchTrueFalseTotal
if ($afte) 7 3 10
490 7 push @{$changeset{add}{$afte}}, $node;
491 }
492 }
493 4 return \%changeset;
494 }
495  
496 6 1;
497  
498 __END__
↑ Summary Cinderblock/Delta.pm »