Cinderblock/Scorable.pm

filter: Statementstmt 86.9% 107 / 123 Branchbran 72.7% 32 / 44 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 66.6% 6 / 9 totaltotal 83.2% 164 / 197 CC 24 SCAR 13.2
CC
24
Cov
83%
reanimate
7.0
winner
6.0
deanimate
5.0
CRAP 3.7
« Cinderblock/Rulemap/Rect.pm ↑ Summary Cinderblock/NodeSet.pm »
1 package Games::Go::Cinderblock::Scorable;
2 6 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 # Games::Go::Cinderblock::Scorable,
5 # * original Basilisk had death masks & territory masks.
6 # * B::Scorable should encapsulate that,
7 # & have methods to derive score from caps, deads, & territory
8  
9 # Rulemap has komi. State has captures. This has dead & territory
10 # so this module should use those to calc final score.
11 6 has rulemap => (
12 isa => 'Games::Go::Cinderblock::Rulemap',
13 is => 'ro', #shouldn't change.
14 required => 1,
15 );
16 6 has state => (
17 isa => 'Games::Go::Cinderblock::State',
18 is => 'ro', #shouldn't change.
19 required => 1,
20 );
21  
22 6 has _alive => (
23 isa => 'HashRef[Games::Go::Cinderblock::NodeSet]',
24 is => 'ro',
25 lazy => 1,
26 builder => '_initially_generous_with_life',
27 );
28 6 has _dead => (
29 isa => 'HashRef[Games::Go::Cinderblock::NodeSet]',
30 is => 'ro',
31 lazy => 1,
32 builder => '_empty_nodesets',
33 );
34 6 has _known_terr => (
35 isa => 'HashRef[Games::Go::Cinderblock::NodeSet]',
36 is => 'ro',
37 lazy => 1,
38 builder => '_empty_nodesets',
39 );
40 6 has _derived_terr => (
41 isa => 'HashRef[Games::Go::Cinderblock::NodeSet]',
42 is => 'ro',
43 lazy => 1,
44 builder => '_initial_derived_terr',
45 );
46 6 has _dame => (
47 isa => 'Games::Go::Cinderblock::NodeSet',
48 is => 'ro',
49 lazy => 1,
50 builder => '_initial_dame',
51 );
52  
53 #has _seki => (); #
54  
55 #based on state, has dame, derived, & alive. that is everything initially.
56 #this is an attribute so we can initialize these 3 categories at the same time, if needed.
57 6 has _initial_cats => (
58 is => 'ro',
59 isa => 'HashRef', #dame isn't color-separated; the others are.
60 lazy => 1,
61 builder => '_build_initial_cats',
62 );
63  
64 #crawl entire board, putting each node into one of these 3 categories.
65 3 ▶sub _build_initial_cats{
Subroutine _build_initial_cats: called CC=4 SCAR=13.9
66 3 my $self = shift;
67  
68 3 my $remaining_nodes = $self->rulemap->all_nodes_nodeset;
69 3 my %initial_cats = (
70 dame => $self->rulemap->nodeset,
71 alive => {
72 w => $self->rulemap->nodeset,
73 b => $self->rulemap->nodeset,
74 },
75 derived_terr => {
76 w => $self->rulemap->nodeset,
77 b => $self->rulemap->nodeset,
78 },
79 );
80  
81 3 my $board = $self->state->board;
82 3 ▶ while($remaining_nodes->count){
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining_nodes->count) 15 3 18
83 15 my $node = $remaining_nodes->choose;
84 15 my $stone = $self->state->at_node($node);
85 15 ▶ if($stone){
Branch: 1 branch
BranchTrueFalseTotal
if ($stone) { } 9 6 15
86 9 ▶ my $cond = sub{ $_ eq $stone};
Subroutine __ANON__: called CC=1 SCAR=0.0
87 9 my $contiguous = $self->state->floodfill ($cond, $node);
88 9 $remaining_nodes->remove($contiguous);
89 9 $initial_cats{alive}{$stone}->add($contiguous);
90 }
91 else { #space.
92 6 ▶ my $cond = sub{ ! $_ };
Subroutine __ANON__: called CC=1 SCAR=0.0
93 6 my $contiguous = $self->state->floodfill ($cond, $node);
94 6 my $adjacent = $contiguous->adjacent;
95 6 my @adj_colors = $self->state->colors_in_nodeset($adjacent);;
96 6 ▶ if(@adj_colors != 1){
Branch: 1 branch
BranchTrueFalseTotal
if (@adj_colors != 1) { } 3 3 6
97 3 $initial_cats{dame}->add($contiguous);
98 }
99 else {
100 3 $initial_cats{derived_terr}{$adj_colors[0]}->add($contiguous);
101 }
102 6 $remaining_nodes->remove($contiguous);
103 }
104 }
105 3 return \%initial_cats;
106 }
107 3 ▶sub _initial_dame{
Subroutine _initial_dame: called CC=1 SCAR=0.0
108 3 my $self = shift;
109 return $self->_initial_cats->{dame}
110 3 }
111 3 ▶sub _initial_derived_terr {
Subroutine _initial_derived_terr: called CC=1 SCAR=0.0
112 3 my $self = shift;
113 return $self->_initial_cats->{derived_terr}
114 3 }
115 3 ▶sub _initially_generous_with_life{
Subroutine _initially_generous_with_life: called CC=1 SCAR=0.0
116 3 my $self = shift;
117 return $self->_initial_cats->{alive}
118 3 }
119 6 ▶sub _empty_nodesets{
Subroutine _empty_nodesets: called CC=1 SCAR=0.0
120 6 my $self = shift;
121 return {
122 6 b => $self->rulemap->nodeset,
123 w => $self->rulemap->nodeset,
124 };
125 }
126
127 # return undef if unoccupied
128 # return 0 if dead, 1 if alive
129 14 ▶sub node_animated{
Subroutine node_animated: called CC=4 SCAR=14.5
Pod undocumented
130 14 my ($self,$node) = @_;
131 14 my $stone = $self->state->at_node($node);
132 14 ▶ return undef unless $stone;
Branch: 1 branch
BranchTrueFalseTotal
unless $stone 0 14 14
133 14 ▶ return 0 if $self->_dead->{$stone} -> has_node($node);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dead->{$stone}->has_node($node) 5 9 14
134 9 ▶ return 1 if $self->_alive->{$stone} -> has_node($node);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_alive->{$stone}->has_node($node) 9 0 9
135 0 Carp::confess;
136 }
137  
138 #transanimation -- to toggle the status of life/death, reanimate^deanimate
139 #nop if not occupied.
140 #
141 #return 1 on success, when something is actually toggled, for all *animate
142  
143 3 ▶sub transanimate{
Subroutine transanimate: called CC=3 SCAR=11.0
Pod documented
144 3 my ($self, $node) = @_;
145 3 my $stone = $self->state->at_node($node);
146 3 ▶ return unless $stone;
Branch: 1 branch
BranchTrueFalseTotal
unless $stone 0 3 3
147 3 my $deanimate = $self->node_animated($node);
148 3 ▶ if($deanimate){
Branch: 1 branch
BranchTrueFalseTotal
if ($deanimate) { } 2 1 3
149 #my $nodeset = $self->rulemap->floodfill(sub{0}, $node);
150 2 return $self->deanimate($node);
151 }
152 else{
153 1 return $self->reanimate($node);
154 }
155 };
156 4 ▶sub reanimate{ #dead -> alive
Subroutine reanimate: called CC=7 SCAR=19.5
Pod documented
157 4 my ($self,$node) = @_;
158 4 my $stone = $self->state->at_node($node);
159 4 ▶ die "no stone at @$node..." unless $stone;
Branch: 1 branch
BranchTrueFalseTotal
unless $stone 0 4 4
160
161 4 ▶ return 0 if $self->node_animated($node);
Branch: 1 branch
BranchTrueFalseTotal
if $self->node_animated($node) 1 3 4
162  
163 3 ▶ my $bounded_color = ($stone eq 'w') ? 'b' : 'w';
Branch: 1 branch
BranchTrueFalseTotal
$stone eq 'w' ? : 1 2 3
164 3 ▶ my $new_ambiguous_space = $self->state->floodfill(sub{$_ ne $bounded_color}, $node);
Subroutine __ANON__: called CC=1 SCAR=0.0
165 3 ▶ my $new_alives = $self->state->grep_nodeset(sub{$_ eq $stone}, $new_ambiguous_space);
Subroutine __ANON__: called CC=1 SCAR=0.0
166 3 $new_ambiguous_space->remove($new_alives);
167  
168 #first, put stones from dead category into alive
169 3 $self->_dead->{$stone} -> remove ($new_alives);
170 3 $self->_alive->{$stone} -> add ($new_alives);
171  
172 #put each seperate contiguous region of empty space in either 'dame' or 'derived_terr'
173 3 my @amb_spaces = $new_ambiguous_space->disjoint_split;
174 3 for my $space(@amb_spaces){
175 5 $self->_known_terr->{$bounded_color}->remove($space);
176 5 my @colors = $self->state->colors_in_nodeset($space->adjacent);
177 5 ▶ if(@colors == 2){
Branch: 2 branches
BranchTrueFalseTotal
if (@colors == 2) { } 3 2 5
elsif (@colors == 1) { } 2 0 2
178 3 $self->_dame->add($space);
179 } elsif (@colors == 1){
180 2 $self->_derived_terr->{$colors[0]}->add($space);
181 }
182 }
183
184 3 return 1;
185 }
186 7 ▶sub deanimate{ #alive -> dead
Subroutine deanimate: called CC=5 SCAR=16.1
Pod documented
187 7 my ($self,$node) = @_;
188 7 my $stone = $self->state->at_node($node);
189 7 ▶ die "no stone at @$node..." unless $stone;
Branch: 1 branch
BranchTrueFalseTotal
unless $stone 0 7 7
190  
191 7 ▶ return 0 unless $self->node_animated($node);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->node_animated($node) 1 6 7
192  
193 6 ▶ my $bounded_color = ($stone eq 'w') ? 'b' : 'w';
Branch: 1 branch
BranchTrueFalseTotal
$stone eq 'w' ? : 3 3 6
194 6 ▶ my $new_known_terr = $self->state->floodfill(sub{$_ ne $bounded_color}, $node);
Subroutine __ANON__: called CC=1 SCAR=0.0
195 6 ▶ my $new_deads = $self->state->grep_nodeset(sub{$_ eq $stone}, $new_known_terr);
Subroutine __ANON__: called CC=1 SCAR=0.0
196 6 $new_known_terr->remove($new_deads);
197  
198 # let's say, illegal if any of our new known terr is already known terr.
199 my $illegal =
200 $new_known_terr->intersect($self->_known_terr->{w})->count()
201 6 + $new_known_terr->intersect($self->_known_terr->{b})->count();
202 6 ▶ if($illegal) {return 0}
Branch: 1 branch
BranchTrueFalseTotal
if ($illegal) 2 4 6
203  
204 #wipe other cats of nodes representing space;
205 # non-known territory/space -> known
206 4 $self->_derived_terr->{w}->remove($new_known_terr);
207 4 $self->_derived_terr->{b}->remove($new_known_terr);
208 4 $self->_dame->remove($new_known_terr);
209 4 $self->_known_terr->{$bounded_color}->add($new_known_terr);
210  
211 #same for stones: alive -> dead category
212 4 $self->_alive->{$stone}->remove($new_deads);
213 4 $self->_dead->{$stone}->add($new_deads);
214  
215 4 return 1
216 }
217  
218  
219 # these 4 lines are shtoopid.
220 # a state is used, and a bunch of nodesets are derived. that is all.
221 # inc. floodfill algorithms to reanimate/deanimate certain chains / chain-groups
222 #
223 #sub territory{}
224 # sub dead_stones{}
225 #sub territory_board{}
226 #sub dead_stones_board{}
227 #
228 #->dead(color): return a nodeset of dead stones of color.
229 #->dead: return a hashref of color => nodeset pairs.
230 6 ▶sub dead{
Subroutine dead: called CC=2 SCAR=7.6
Pod documented
231 6 my ($self,$color) = @_;
232 6 ▶ unless ($color){
Branch: 1 branch
BranchTrueFalseTotal
unless ($color) 0 6 6
233 return {
234 0 b => $self->dead('b'),
235 w => $self->dead('w'),
236 };
237 }
238 #have color.
239 6 return $self->_dead->{$color};
240 }
241 9 ▶sub territory{
Subroutine territory: called CC=4 SCAR=14.8
Pod documented
242 9 my ($self,$color) = @_;
243 9 ▶ unless ($color){
Branch: 1 branch
BranchTrueFalseTotal
unless ($color) 0 9 9
244 return {
245 0 b => $self->territory('b'),
246 w => $self->territory('w'),
247 };
248 }
249 #have color.
250 # die %{$self->_known_terr};
251 9 my $known = $self->_known_terr->{$color};
252 9 ▶ die unless $known;
Branch: 1 branch
BranchTrueFalseTotal
unless $known 0 9 9
253 9 my $derived = $self->_derived_terr->{$color};
254 9 ▶ die unless $derived;
Branch: 1 branch
BranchTrueFalseTotal
unless $derived 0 9 9
255 # TODO: implications of sharing dead stones between multiple other colors.
256 # my $other_color = $color eq 'b' ? 'w' : 'b';
257 # todo: this, correctly:
258 #my $deads_as_territory = $self->_dead->{$other_color};
259 9 return $known->union($derived); #->union($deads_as_territory);
260 }
261 4 ▶sub dame{
Subroutine dame: called CC=1 SCAR=0.0
Pod documented
262 4 my $self = shift;
263 4 return $self->_dame;
264 }
265  
266  
267 0 ▶sub winner{
Subroutine winner: not called CC=2 SCAR=17.9
Pod undocumented
268 0 my $self = shift;
269 0 my $scores = $self->scores;
270 # no ties for now..
271 0 ▶ return 'w' if $scores->{w} >= $scores->{b};
Branch: 1 branch
BranchTrueFalseTotal
if $scores->{"w"} >= $scores->{"b"} 0 0 0
272 0 return 'b'
273 }
274 0 ▶sub scores{
Subroutine scores: not called CC=1 SCAR=6.9
Pod undocumented
275 0 my $self = shift;
276 0 my %scores = (
277 w=> $self->rulemap->komi,
278 b=>0,
279 );
280 0 $scores{w} -= $self->dead('w')->count;
281 0 $scores{b} -= $self->dead('b')->count;
282 0 $scores{w} += $self->territory('w')->count;
283 0 $scores{b} += $self->territory('b')->count;
284 # dead nodes == additional terr
285 0 $scores{w} += $self->dead('b')->count;
286 0 $scores{b} += $self->dead('w')->count;
287 0 return \%scores;
288 };
289  
290 6 1;
291  
292 __END__
« Cinderblock/Rulemap/Rect.pm ↑ Summary Cinderblock/NodeSet.pm »