blib/lib/Games/ABC_Path/Generator.pm

filter: Statementstmt 95.7% 156 / 163 Branchbran 80.0% 24 / 30 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 26 / 26 Podpod 100% 2 / 2 totaltotal 93.5% 216 / 231 CC 20 SCAR 14.7
CC
20
Cov
93%
calc_riddle
11.3
calc_final_layout
5.1
__ANON__
4.0
CRAP 4.4
↑ Summary Build »
1 package Games::ABC_Path::Generator;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use base 'Games::ABC_Path::Generator::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 6 ▶use Games::ABC_Path::Solver::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶use Games::ABC_Path::Solver::Board '0.1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶use Games::ABC_Path::MicrosoftRand;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 6 ▶use Games::ABC_Path::Generator::RiddleObj;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶use Games::ABC_Path::Generator::FinalLayoutObj;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 =head1 NAME
22  
23 Games::ABC_Path::Generator - a generator for ABC Path puzzle games.
24  
25 =head1 VERSION
26  
27 Version 0.1.0
28  
29 =cut
30  
31 6 our $VERSION = '0.1.0';
32  
33  
34 =head1 SYNOPSIS
35  
36 use Games::ABC_Path::Generator;
37  
38 my $gen = Games::ABC_Path::Generator->new({seed => 1});
39  
40 # Returns a Games::ABC_Path::Generator::RiddleObj object.
41 my $riddle = $gen->calc_riddle();
42  
43 =head1 DESCRIPTION
44  
45 ABC Path puzzle games are puzzle games where one is given a 5*5 grid which
46 should contain a consecutive path of the letters from 'A' to 'Y' (with vertical,
47 horizontal, diagonal, or anti-diagonal steps). The position of the letter 'A'
48 is given, and two letters are given for each of the columns, rows, main
49 diagonal and main anti-diagonal, which can appear anywhere there.
50  
51 ABC Path can be played online at L<http://www.brainbashers.com/abcpath.asp> .
52  
53 This package is a generator for ABC Path games, and it generates a different
54 layout with different clues for every random number generator seed.
55  
56 =head1 SUBROUTINES/METHODS
57  
58 =head2 my $gen = Games::ABC_Path::Generator->new({seed => $seed});
59  
60 Initialised a new generator with the random number generator seed $seed .
61  
62 =cut
63  
64 sub _init
65 {
66 4 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
67 4 my $args = shift;
68  
69 4 $self->{seed} = $args->{seed};
70  
71 4 $self->{rand} = Games::ABC_Path::MicrosoftRand->new(seed => $self->{seed});
72  
73 4 return;
74 }
75  
76  
77 sub _shuffle {
78 184 ▶ my ($self, $deck) = @_;
Subroutine _shuffle: called CC=1 SCAR=0.0
79  
80 184 return $self->{'rand'}->shuffle($deck);
81 }
82  
83 {
84 my @get_next_cells_lookup =
85 (
86 map {
87 6 my ($sy, $sx) = __PACKAGE__->_to_xy($_);
88 [ map {
89 150 my ($y,$x) = ($sy+$_->[$Y], $sx+$_->[$X]);
90 (
91 1200 ▶ (__PACKAGE__->_x_in_range($x) && __PACKAGE__->_y_in_range($y))
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->_x_in_range($x) && __PACKAGE__->_y_in_range($y) ? : 864 336 1200
Condition100%3/3
__PACKAGE__->_x_in_range($x) and __PACKAGE__->_y_in_range($y)
!ll&&!rl&&r
180156864
MC/DC100%2/2
__PACKAGE__->_x_in_range($x) and __PACKAGE__->_y_in_range($y)
__PACKAGE__->_x_in_range($x) __PACKAGE__->_y_in_range($y)
Truth table100%3/3
A and B
A= __PACKAGE__->_x_in_range($x)B= __PACKAGE__->_y_in_range($y)
ABresult
0X0
100
111
92 ? (__PACKAGE__->_xy_to_int([$y,$x])) : ()
93 )
94 }
95 ([-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1])
96 ]
97 } (0 .. $BOARD_SIZE - 1)
98 );
99  
100 sub _get_next_cells
101 {
102 1468 ▶ my ($self, $l, $init_idx) = @_;
Subroutine _get_next_cells: called CC=1 SCAR=0.0
103  
104 8004 return [ grep { vec($l, $_, 8) == 0 }
105 1468 @{$get_next_cells_lookup[$init_idx]}
106 ];
107 }
108 }
109  
110  
111 sub _add_next_state
112 6 {
113 112 ▶ my ($self, $stack, $l, $cell_int) = @_;
Subroutine _add_next_state: called CC=1 SCAR=0.0
114  
115 112 vec($l, $cell_int, 8) = 1+@$stack;
116  
117 112 push @$stack, [$l, $self->_shuffle($self->_get_next_cells($l, $cell_int))];
118  
119 112 return;
120 }
121  
122 sub _get_num_connected
123 {
124 120 ▶ my ($self, $l) = @_;
Subroutine _get_num_connected: called CC=3 SCAR=11.0
125  
126 120 my @connectivity_stack = (index($l, "\0"));
127  
128 120 my %connected;
129 120 ▶ while (@connectivity_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@connectivity_stack) 2828 120 2948
130 {
131 2828 my $int = pop(@connectivity_stack);
132 2828 ▶ if (!$connected{$int}++)
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected{$int}++) 1356 1472 2828
133 {
134 push @connectivity_stack,
135 5416 (grep { !exists($connected{$_}) }
136 1356 @{ $self->_get_next_cells($l, $int) }
137 );
138 }
139 }
140  
141 120 return scalar keys %connected;
142 }
143  
144 6 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 =head2 $gen->calc_final_layout()
147  
148 Calculates the final, solved, layout of the board based on the current
149 random seed.
150  
151 Returns the layout as a L<Games::ABC_Path::Generator::FinalLayoutObj>
152 object.
153  
154 =cut
155  
156 sub calc_final_layout
157 {
158 4 ▶ my $self = shift;
Subroutine calc_final_layout: called CC=5 SCAR=16.2
Pod documented
159  
160 4 my @dfs_stack;
161 4 $self->_add_next_state(\@dfs_stack, '', $self->{rand}->max_rand($BOARD_SIZE));
162  
163 DFS:
164 4 ▶ while (@dfs_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack) 124 0 124
165 {
166 124 my ($l, $last_cells) = @{$dfs_stack[-1]};
167  
168 124 ▶ if (@dfs_stack == $BOARD_SIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack == $Games::ABC_Path::Generator::BOARD_SIZE) 4 120 124
169 {
170 4 return Games::ABC_Path::Generator::FinalLayoutObj->new(
171 {layout_string => $l, },
172 );
173 }
174  
175 # print "Depth = " . scalar(@dfs_stack) . "\n";
176 # print "Last state = " . Dumper($last_state) . "\n";
177 # print "Layout = \n" . $self->get_layout_as_string($last_state->{layout}) . "\n";
178  
179 120 my $next_idx = shift(@$last_cells);
180  
181 120 ▶ if ( ( ! defined($next_idx) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $next_idx or $self->_get_num_connected($l) != $Games::ABC_Path::Generator::BOARD_SIZE - scalar(@dfs_stack)) { } 12 108 120
Condition66%2/3
not defined $next_idx or $self->_get_num_connected($l) != $Games::ABC_Path::Generator::BOARD_SIZE - scalar(@dfs_stack)
l!l&&r!l&&!r
012108
MC/DC50%1/2
not defined $next_idx or $self->_get_num_connected($l) != $Games::ABC_Path::Generator::BOARD_SIZE - scalar(@dfs_stack)
not defined $next_idx $self->_get_num_connected($l) != $Games::ABC_Path::Generator::BOARD_SIZE - scalar(@dfs_stack)
Truth table66%2/3
not(A) or B
A= not defined $next_idxB= $self->_get_num_connected($l) != $Games::ABC_Path::Generator::BOARD_SIZE - scalar(@dfs_stack)
ABresult
1X1
011
000
182 or
183 ($self->_get_num_connected($l) !=
184 ($BOARD_SIZE - scalar(@dfs_stack))
185 )
186 )
187 {
188 12 pop(@dfs_stack);
189 }
190 else
191 {
192 108 $self->_add_next_state(\@dfs_stack, $l, $next_idx);
193 }
194 }
195  
196 0 die "Not found!";
197 }
198  
199 =head2 $gen->calc_riddle()
200  
201 Calculates the riddle (final state + initial hints) and returns it as an object.
202  
203 =cut
204  
205 sub _gen_clue_positions {
206 72 ▶ my ($self, $cb) = @_;
Subroutine _gen_clue_positions: called CC=1 SCAR=0.0
207 72 return [map { $cb->($_) } $self->_x_indexes()];
208 };
209  
210 sub _calc_clue_positions {
211 6 ▶ my $self = shift;
Subroutine _calc_clue_positions: called CC=1 SCAR=0.0
212 return
213 [
214 map {
215 72 [map { $self->_xy_to_int($_) } @{$self->_gen_clue_positions($_)}]
216 }
217 (
218 30 ▶ sub { [$_,$_]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
219 30 ▶ sub { [$_,4-$_]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
220 30 ▶ (map { my $y = $_; sub { [$y,$_] }; } $self->_y_indexes()),
Subroutine __ANON__: called CC=1 SCAR=0.0
221 6 ▶ (map { my $x = $_; sub { [$_,$x] }; } $self->_x_indexes()),
Subroutine __ANON__: called CC=1 SCAR=0.0
222 )
223 ]
224 }
225  
226 6 my @_clues_positions = @{__PACKAGE__->_calc_clue_positions()};
227  
228 sub calc_riddle
229 {
230 3 ▶ my ($self) = @_;
Subroutine calc_riddle: called CC=11 SCAR=24.2
Pod documented
231  
232 3 my $layout = $self->calc_final_layout();
233  
234 3 my $A_pos = $layout->get_A_pos;
235  
236 my %init_state = (pos_taken => '',
237 clues =>
238 [
239 3 map { +{ num_remaining => 5, } }
240 (1 .. $NUM_CLUES),
241 ]
242 );
243  
244 my $mark = sub {
245 75 ▶ my ($state, $pos) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
246
247 75 vec($state->{pos_taken}, $pos, 1) = 1;
248
249 75 my ($y,$x) = $self->_to_xy($pos);
250 75 ▶ foreach my $clue (
Branch: 2 branches
BranchTrueFalseTotal
$y == $x ? : 15 60 75
$y == 4 - $x ? : 15 60 75
251 (($y == $x) ? 0 : ()),
252 (($y == (5-1)-$x) ? 1 : ()),
253 (2+$y),
254 ((2+5)+$x),
255 )
256 {
257 180 $state->{clues}->[$clue]->{num_remaining}--;
258 }
259 3 };
260  
261 3 $mark->(\%init_state, $A_pos);
262  
263 3 my @dfs_stack = (\%init_state);
264  
265 DFS:
266 3 ▶ while (@dfs_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack) 39 0 39
267 {
268 39 my $last_state = $dfs_stack[-1];
269  
270 39 ▶ if (! exists($last_state->{chosen_clue}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $last_state->{"chosen_clue"}) 39 0 39
271 {
272 my @clues =
273 (
274 sort {
275 ($a->[1]->{num_remaining} <=> $b->[1]->{num_remaining})
276 441 ▶ or
Branch: 1 branch
BranchTrueFalseTotal
$Games::ABC_Path::Generator::a->[1]{"num_remaining"} <=> $Games::ABC_Path::Generator::b->[1]{"num_remaining"} or $Games::ABC_Path::Generator::a->[0] <=> $Games::ABC_Path::Generator::b->[0] 234 207 441
277 ($a->[0] <=> $b->[0])
278 }
279 468 grep { !exists($_->[1]->{cells}) }
280 39 map { [$_, $last_state->{clues}->[$_]] }
281 (0 .. $NUM_CLUES-1)
282 );
283  
284 39 ▶ if (!@clues)
Branch: 1 branch
BranchTrueFalseTotal
unless (@clues) 3 36 39
285 {
286 # Yay! We found a configuration.
287 my $handle_clue = sub {
288 36 ▶ my @cells = @{shift->{cells}};
Subroutine __ANON__: called CC=1 SCAR=0.0
289 return
290 [
291 72 map { $layout->get_cell_contents($_) }
292 36 @{$self->_shuffle(\@cells)}
293 ];
294 3 };
295 my $riddle =
296 Games::ABC_Path::Generator::RiddleObj->new(
297 {
298 solution => $layout,
299 clues =>
300 [
301 3 map { $handle_clue->($_) } @{$last_state->{clues}}
302 ],
303 A_pos => [$self->_to_xy($A_pos)],
304 }
305 );
306
307 3 my $riddle_string = $riddle->get_riddle_v1_string();
308  
309 3 my $solver =
310 Games::ABC_Path::Solver::Board->input_from_v1_string(
311 $riddle_string
312 );
313
314 3 $solver->solve();
315  
316 3 ▶ if (@{$solver->get_successes_text_tables()} != 1)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$solver->get_successes_text_tables;} != 1) { } 0 3 3
317 {
318 # The solution is ambiguous
319 0 pop(@dfs_stack);
320 0 next DFS;
321 }
322 else
323 {
324 3 return $riddle;
325 }
326 }
327 # Not enough for the clues there.
328 36 ▶ if ($clues[0][1]->{num_remaining} < 2)
Branch: 1 branch
BranchTrueFalseTotal
if ($clues[0][1]{"num_remaining"} < 2) 0 36 36
329 {
330 0 pop(@dfs_stack);
331 0 next DFS;
332 }
333  
334 36 my $clue_idx = $clues[0][0];
335  
336 36 $last_state->{chosen_clue} = $clue_idx;
337  
338 my @positions =
339 (
340 180 grep { !vec($last_state->{pos_taken}, $_, 1) }
341 36 @{$_clues_positions[$clue_idx]}
342 );
343  
344 36 my @pairs;
345  
346 36 foreach my $first_idx (0 .. $#positions-1)
347 {
348 66 foreach my $second_idx ($first_idx+1 .. $#positions)
349 {
350 105 push @pairs, [@positions[$first_idx, $second_idx]];
351 }
352 }
353  
354 36 $last_state->{pos_pairs} = $self->_shuffle(\@pairs);
355 }
356  
357 36 my $chosen_clue = $last_state->{chosen_clue};
358 36 my $next_pair = shift(@{$last_state->{pos_pairs}});
359  
360 36 ▶ if (!defined($next_pair))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next_pair) 0 36 36
361 {
362 0 pop(@dfs_stack);
363 0 next DFS;
364 }
365  
366 36 my %new_state;
367 36 $new_state{pos_taken} = $last_state->{pos_taken};
368 36 $new_state{clues} = [map { +{ %{$_} } } @{$last_state->{clues}}];
369 36 foreach my $pos (@$next_pair)
370 {
371 72 $mark->(\%new_state, $pos);
372 }
373 36 $new_state{clues}->[$chosen_clue]->{cells} = [@$next_pair];
374  
375 36 push @dfs_stack, (\%new_state);
376 }
377 }
378  
379 =head1 AUTHOR
380  
381 Shlomi Fish, L<http://www.shlomifish.org/> .
382  
383 =head1 BUGS
384  
385 Please report any bugs or feature requests to C<bug-games-abc_path-generator at rt.cpan.org>, or through
386 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-ABC_Path-Generator>. I will be notified, and then you'll
387 automatically be notified of progress on your bug as I make changes.
388  
389  
390  
391  
392 =head1 SUPPORT
393  
394 You can find documentation for this module with the perldoc command.
395  
396 perldoc Games::ABC_Path::Generator
397  
398 You can also look for information at:
399  
400 =over 4
401  
402 =item * RT: CPAN's request tracker (report bugs here)
403  
404 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-ABC_Path-Generator>
405  
406 =item * AnnoCPAN: Annotated CPAN documentation
407  
408 L<http://annocpan.org/dist/Games-ABC_Path-Generator>
409  
410 =item * CPAN Ratings
411  
412 L<http://cpanratings.perl.org/d/Games-ABC_Path-Generator>
413  
414 =item * Search CPAN
415  
416 L<http://search.cpan.org/dist/Games-ABC_Path-Generator/>
417  
418 =back
419  
420 =head1 SEE ALSO
421  
422 L<Games::ABC_Path::Solver> - a solver for ABC Path games which this
423 generator uses.
424  
425 L<http://www.shlomifish.org/open-source/projects/japanese-puzzle-games/abc-path/> - the homepage of Shlomi Fish's ABC Path work.
426  
427 L<http://www.brainbashers.com/abcpath.asp> - play ABC Path online on
428 brainbashers.com .
429  
430 L<http://www.janko.at/> - Otto Janko, the creator of ABC Path.
431  
432 =head1 ACKNOWLEDGEMENTS
433  
434 Otto Janko (L<http://www.janko.at/>) for creating ABC Path.
435  
436 L<http://www.brainbashers.com/> for introducing them to me.
437  
438 Altreus ( L<http://search.cpan.org/~altreus/> ) for suggesting an improvement
439 to the documentation.
440  
441 =head1 LICENSE AND COPYRIGHT
442  
443 Copyright 2011 Shlomi Fish.
444  
445 This program is distributed under the MIT (X11) License:
446 L<http://www.opensource.org/licenses/mit-license.php>
447  
448 Permission is hereby granted, free of charge, to any person
449 obtaining a copy of this software and associated documentation
450 files (the "Software"), to deal in the Software without
451 restriction, including without limitation the rights to use,
452 copy, modify, merge, publish, distribute, sublicense, and/or sell
453 copies of the Software, and to permit persons to whom the
454 Software is furnished to do so, subject to the following
455 conditions:
456  
457 The above copyright notice and this permission notice shall be
458 included in all copies or substantial portions of the Software.
459  
460 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
461 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
462 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
463 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
464 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
465 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
466 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
467 OTHER DEALINGS IN THE SOFTWARE.
468  
469  
470 =cut
471  
472 6 1; # End of Games::ABC_Path::Generator
↑ Summary Build »