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 8 $Games::ABC_Path::Generator::VERSION = '0.6.0';
3 8 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 ▶use parent 'Games::ABC_Path::Generator::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 ▶use Games::ABC_Path::Solver::Constants;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Games::ABC_Path::Solver::Board '0.1.0';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 8 ▶use Games::ABC_Path::MicrosoftRand ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 8 ▶use Games::ABC_Path::Generator::RiddleObj ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶use Games::ABC_Path::Generator::FinalLayoutObj ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶use Games::ABC_Path::Generator::Coord ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19  
20 sub _init
21 {
22 4 ▶ my $self = shift;
Subroutine _init: called CC=1 SCAR=0.0
23 4 my $args = shift;
24  
25 4 $self->{seed} = $args->{seed};
26  
27 $self->{rand} =
28 4 Games::ABC_Path::MicrosoftRand->new( seed => $self->{seed} );
29  
30 4 return;
31 }
32  
33 sub _shuffle
34 {
35 184 ▶ my ( $self, $deck ) = @_;
Subroutine _shuffle: called CC=1 SCAR=0.0
36  
37 184 return $self->{'rand'}->shuffle($deck);
38 }
39  
40 {
41 my @get_next_cells_lookup = (
42 map {
43 8 my $start = Games::ABC_Path::Generator::Coord->_from_int($_);
44 [
45 map {
46 200 my ( $y, $x ) =
47 ( $start->y() + $_->[$Y], $start->x() + $_->[$X] );
48 (
49 (
50 1600 ▶ __PACKAGE__->_x_in_range($x)
Branch: 1 branch
BranchTrueFalseTotal
__PACKAGE__->_x_in_range($x) && __PACKAGE__->_y_in_range($y) ? : 1152 448 1600
Condition100%3/3
__PACKAGE__->_x_in_range($x) and __PACKAGE__->_y_in_range($y)
!ll&&!rl&&r
2402081152
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
51 && __PACKAGE__->_y_in_range($y)
52 ) ? ( __PACKAGE__->_xy_to_int( [ $y, $x ] ) ) : ()
53 )
54 } (
55 [ -1, -1 ], [ -1, 0 ], [ -1, 1 ], [ 0, -1 ],
56 [ 0, 1 ], [ 1, -1 ], [ 1, 0 ], [ 1, 1 ]
57 )
58 ]
59 } ( 0 .. $BOARD_SIZE - 1 )
60 );
61  
62 sub _get_next_cells
63 {
64 1468 ▶ my ( $self, $l, $init_idx ) = @_;
Subroutine _get_next_cells: called CC=1 SCAR=0.0
65  
66 8004 return [ grep { vec( $l, $_, 8 ) == 0 }
67 1468 @{ $get_next_cells_lookup[$init_idx] } ];
68 }
69 }
70  
71 sub _add_next_state
72 8 {
73 112 ▶ my ( $self, $stack, $l, $cell_int ) = @_;
Subroutine _add_next_state: called CC=1 SCAR=0.0
74  
75 112 vec( $l, $cell_int, 8 ) = 1 + @$stack;
76  
77 112 push @$stack,
78 [ $l, $self->_shuffle( $self->_get_next_cells( $l, $cell_int ) ) ];
79  
80 112 return;
81 }
82  
83 sub _get_num_connected
84 {
85 120 ▶ my ( $self, $l ) = @_;
Subroutine _get_num_connected: called CC=3 SCAR=11.0
86  
87 120 my @connectivity_stack = ( index( $l, "\0" ) );
88  
89 120 my %connected;
90 120 ▶ while (@connectivity_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@connectivity_stack) 2828 120 2948
91 {
92 2828 my $int = pop(@connectivity_stack);
93 2828 ▶ if ( !$connected{$int}++ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($connected{$int}++) 1356 1472 2828
94 {
95 push @connectivity_stack,
96 5416 ( grep { !exists( $connected{$_} ) }
97 1356 @{ $self->_get_next_cells( $l, $int ) } );
98 }
99 }
100  
101 120 return scalar keys %connected;
102 }
103  
104  
105 sub calc_final_layout
106 {
107 4 ▶ my $self = shift;
Subroutine calc_final_layout: called CC=5 SCAR=16.2
Pod documented
108  
109 4 my @dfs_stack;
110 $self->_add_next_state( \@dfs_stack, '',
111 4 $self->{rand}->max_rand($BOARD_SIZE) );
112  
113 DFS:
114 4 ▶ while (@dfs_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack) 124 0 124
115 {
116 124 my ( $l, $last_cells ) = @{ $dfs_stack[-1] };
117  
118 124 ▶ if ( @dfs_stack == $BOARD_SIZE )
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack == $Games::ABC_Path::Generator::BOARD_SIZE) 4 120 124
119 {
120 4 return Games::ABC_Path::Generator::FinalLayoutObj->new(
121 { layout_string => $l, }, );
122 }
123  
124 # print "Depth = " . scalar(@dfs_stack) . "\n";
125 # print "Last state = " . Dumper($last_state) . "\n";
126 # print "Layout = \n" . $self->get_layout_as_string($last_state->{layout}) . "\n";
127  
128 120 my $next_idx = shift(@$last_cells);
129  
130 120 ▶ if (
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
131 ( !defined($next_idx) )
132 or ( $self->_get_num_connected($l) !=
133 ( $BOARD_SIZE - scalar(@dfs_stack) ) )
134 )
135 {
136 12 pop(@dfs_stack);
137 }
138 else
139 {
140 108 $self->_add_next_state( \@dfs_stack, $l, $next_idx );
141 }
142 }
143  
144 0 die "Not found!";
145 }
146  
147  
148 sub _gen_clue_positions
149 {
150 96 ▶ my ( $self, $cb ) = @_;
Subroutine _gen_clue_positions: called CC=1 SCAR=0.0
151 96 return [ map { $cb->($_) } $self->_x_indexes() ];
152 }
153  
154 sub _calc_clue_positions
155 {
156 8 ▶ my $self = shift;
Subroutine _calc_clue_positions: called CC=1 SCAR=0.0
157 return [
158 map {
159 480 [ map { $self->_xy_to_int($_) }
160 96 @{ $self->_gen_clue_positions($_) } ]
161 } (
162 40 ▶ sub { [ $_, $_ ]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
163 40 ▶ sub { [ $_, 4 - $_ ]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
164 (
165 map {
166 40 my $y = $_;
167 40 ▶ sub { [ $y, $_ ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
168 } $self->_y_indexes()
169 ),
170 (
171 map {
172 8 my $x = $_;
173 40 ▶ sub { [ $_, $x ] };
Subroutine __ANON__: called CC=1 SCAR=0.0
174 } $self->_x_indexes()
175 ),
176 )
177 ];
178 }
179  
180 8 my @_clues_positions = @{ __PACKAGE__->_calc_clue_positions() };
181  
182 sub calc_riddle
183 {
184 3 ▶ my ($self) = @_;
Subroutine calc_riddle: called CC=11 SCAR=24.2
Pod documented
185  
186 3 my $layout = $self->calc_final_layout();
187  
188 3 my $A_pos = $layout->get_A_pos;
189  
190 my %init_state = (
191 pos_taken => '',
192 3 clues => [ map { +{ num_remaining => 5, } } ( 1 .. $NUM_CLUES ), ]
193 );
194  
195 my $mark = sub {
196 75 ▶ my ( $state, $pos ) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
197  
198 75 vec( $state->{pos_taken}, $pos, 1 ) = 1;
199  
200 75 my $coord = Games::ABC_Path::Generator::Coord->_from_int($pos);
201  
202 75 ▶ foreach my $clue (
Branch: 2 branches
BranchTrueFalseTotal
$coord->y == $coord->x ? : 15 60 75
$coord->y == 4 - $coord->x ? : 15 60 75
203 ( ( $coord->y == $coord->x ) ? 0 : () ),
204 ( ( $coord->y == ( 5 - 1 ) - $coord->x ) ? 1 : () ),
205 ( 2 + $coord->y ),
206 ( ( 2 + 5 ) + $coord->x ),
207 )
208 {
209 180 $state->{clues}->[$clue]->{num_remaining}--;
210 }
211 3 };
212  
213 3 $mark->( \%init_state, $A_pos );
214  
215 3 my @dfs_stack = ( \%init_state );
216  
217 DFS:
218 3 ▶ while (@dfs_stack)
Branch: 1 branch
BranchTrueFalseTotal
if (@dfs_stack) 39 0 39
219 {
220 39 my $last_state = $dfs_stack[-1];
221  
222 39 ▶ if ( !exists( $last_state->{chosen_clue} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $last_state->{"chosen_clue"}) 39 0 39
223 {
224 my @clues = (
225 sort {
226 ( $a->[1]->{num_remaining} <=> $b->[1]->{num_remaining} )
227 441 ▶ or ( $a->[0] <=> $b->[0] )
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
228 }
229 468 grep { !exists( $_->[1]->{cells} ) }
230 39 map { [ $_, $last_state->{clues}->[$_] ] }
231 ( 0 .. $NUM_CLUES - 1 )
232 );
233  
234 39 ▶ if ( !@clues )
Branch: 1 branch
BranchTrueFalseTotal
unless (@clues) 3 36 39
235 {
236 # Yay! We found a configuration.
237 my $handle_clue = sub {
238 36 ▶ my @cells = @{ shift->{cells} };
Subroutine __ANON__: called CC=1 SCAR=0.0
239 72 return [ map { $layout->get_cell_contents($_) }
240 36 @{ $self->_shuffle( \@cells ) } ];
241 3 };
242 my $riddle = Games::ABC_Path::Generator::RiddleObj->new(
243 {
244 solution => $layout,
245 clues => [
246 3 map { $handle_clue->($_) } @{ $last_state->{clues} }
247 ],
248 A_pos => Games::ABC_Path::Generator::Coord->_from_int(
249 $A_pos),
250 }
251 );
252  
253 3 my $riddle_string = $riddle->get_riddle_v1_string();
254  
255 3 my $solver =
256 Games::ABC_Path::Solver::Board->input_from_v1_string(
257 $riddle_string);
258  
259 3 $solver->solve();
260  
261 3 ▶ if ( @{ $solver->get_successes_text_tables() } != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$solver->get_successes_text_tables;} != 1) { } 0 3 3
262 {
263 # The solution is ambiguous
264 0 pop(@dfs_stack);
265 0 next DFS;
266 }
267 else
268 {
269 3 return $riddle;
270 }
271 }
272  
273 # Not enough for the clues there.
274 36 ▶ if ( $clues[0][1]->{num_remaining} < 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($clues[0][1]{"num_remaining"} < 2) 0 36 36
275 {
276 0 pop(@dfs_stack);
277 0 next DFS;
278 }
279  
280 36 my $clue_idx = $clues[0][0];
281  
282 36 $last_state->{chosen_clue} = $clue_idx;
283  
284 my @positions =
285 180 ( grep { !vec( $last_state->{pos_taken}, $_, 1 ) }
286 36 @{ $_clues_positions[$clue_idx] } );
287  
288 36 my @pairs;
289  
290 36 foreach my $first_idx ( 0 .. $#positions - 1 )
291 {
292 66 foreach my $second_idx ( $first_idx + 1 .. $#positions )
293 {
294 105 push @pairs, [ @positions[ $first_idx, $second_idx ] ];
295 }
296 }
297  
298 36 $last_state->{pos_pairs} = $self->_shuffle( \@pairs );
299 }
300  
301 36 my $chosen_clue = $last_state->{chosen_clue};
302 36 my $next_pair = shift( @{ $last_state->{pos_pairs} } );
303  
304 36 ▶ if ( !defined($next_pair) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $next_pair) 0 36 36
305 {
306 0 pop(@dfs_stack);
307 0 next DFS;
308 }
309  
310 36 my %new_state;
311 36 $new_state{pos_taken} = $last_state->{pos_taken};
312 36 $new_state{clues} = [ map { +{ %{$_} } } @{ $last_state->{clues} } ];
313 36 foreach my $pos (@$next_pair)
314 {
315 72 $mark->( \%new_state, $pos );
316 }
317 36 $new_state{clues}->[$chosen_clue]->{cells} = [@$next_pair];
318  
319 36 push @dfs_stack, ( \%new_state );
320 }
321 }
322  
323  
324 8 1; # End of Games::ABC_Path::Generator
325  
326 __END__
↑ Summary Build »