blib/lib/Games/LMSolve/Base.pm

filter: Statementstmt 46.8% 83 / 177 Branchbran 26.1% 11 / 42 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 45.1% 14 / 31 Podpod 100% 17 / 17 totaltotal 45.2% 127 / 281 SCAR 51.9
CC
28
Cov
42%
display_solution
56.0
main
30.0
_solve_brfs_or_dfs
15.8
CRAP 180.1
« blib/lib/Games/LMSolve/Input.pm ↑ Summary blib/lib/Games/LMSolve/Plank/Base.pm »
1 package Games::LMSolve::Base;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 13 our $VERSION = '0.8.6';
9  
10 13 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 ▶use vars qw(@ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 13 @ISA = qw(Exporter);
15  
16 13 @EXPORT_OK = qw(%cell_dirs);
17  
18 13 ▶use vars qw(%cell_dirs);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 13 %cell_dirs = (
21 'N' => [ 0, -1 ],
22 'NW' => [ -1, -1 ],
23 'NE' => [ 1, -1 ],
24 'S' => [ 0, 1 ],
25 'SE' => [ 1, 1 ],
26 'SW' => [ -1, 1 ],
27 'E' => [ 1, 0 ],
28 'W' => [ -1, 0 ],
29 );
30  
31  
32 sub new
33 {
34 1 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35  
36 1 my $self = {};
37  
38 1 bless $self, $class;
39  
40 1 $self->initialize(@_);
41  
42 1 return $self;
43 }
44  
45 sub initialize
46 {
47 1 ▶ my $self = shift;
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
48  
49 1 $self->{'state_collection'} = {};
50 1 $self->{'cmd_line'} = { 'scan' => "brfs", };
51  
52 1 $self->{'num_iters'} = 0;
53  
54 1 return 0;
55 }
56  
57  
58 my %scan_functions = (
59 'dfs' => sub {
60 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
61  
62 0 return $self->_solve_brfs_or_dfs( 1, @_ );
63 },
64 'brfs' => sub {
65 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
66  
67 1 return $self->_solve_brfs_or_dfs( 0, @_ );
68 },
69 13 );
70  
71 sub main
72 {
73 0 ▶ my $self = shift;
Subroutine main: not called CC=5 SCAR=34.0
Pod documented
74  
75 # This is a flag that specifies whether to present the moves in Run-Length
76 # Encoding.
77 0 my $to_rle = 1;
78 0 my $output_states = 0;
79 0 my $scan = "brfs";
80 0 my $run_time_states_display = 0;
81  
82 #my $p = Getopt::Long::Parser->new();
83 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
unless (&GetOptions("rle!", \$to_rle, "output-states!", \$output_states, "method=s", \$scan, "rtd!", \$run_time_states_display)) 0 0 0
84 !GetOptions(
85 'rle!' => \$to_rle,
86 'output-states!' => \$output_states,
87 'method=s' => \$scan,
88 'rtd!' => \$run_time_states_display,
89 )
90 )
91 {
92 0 die "Incorrect options passed!\n";
93 }
94  
95 0 ▶ if ( !exists( $scan_functions{$scan} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $scan_functions{$scan}) 0 0 0
96 {
97 0 die "Unknown scan \"$scan\"!\n";
98 }
99  
100 0 $self->{'cmd_line'}->{'to_rle'} = $to_rle;
101 0 $self->{'cmd_line'}->{'output_states'} = $output_states;
102 0 $self->{'cmd_line'}->{'scan'} = $scan;
103 0 ▶ $self->set_run_time_states_display( $run_time_states_display
Condition0%0/2
$run_time_states_display && \&Games::LMSolve::Base::_default_rtd_callback
!ll
00
MC/DC0%0/1
$run_time_states_display && \&Games::LMSolve::Base::_default_rtd_callback
$run_time_states_display
Truth table0%0/2
A
A= $run_time_states_display
Aresult
00
11
104 && \&_default_rtd_callback );
105  
106 0 ▶ my $filename = shift(@ARGV) || "board.txt";
Condition0%0/2
shift @ARGV || 'board.txt'
l!l
00
MC/DC0%0/1
shift @ARGV || 'board.txt'
shift @ARGV
Truth table0%0/2
A
A= shift @ARGV
Aresult
11
00
107  
108 0 my @ret = $self->solve_board($filename);
109  
110 0 $self->display_solution(@ret);
111 }
112  
113  
114 sub _die_on_abstract_function
115 {
116 my (
117 0 ▶ $package, $filename, $line, $subroutine, $hasargs,
Subroutine _die_on_abstract_function: not called CC=1 SCAR=6.9
118 $wantarray, $evaltext, $is_require, $hints, $bitmask
119 ) = caller(1);
120 0 die( "The abstract function $subroutine() was "
121 . "called, while it needs to be overrided by the derived class.\n"
122 );
123 }
124  
125  
126 sub input_board
127 {
128 0 ▶ return _die_on_abstract_function();
Subroutine input_board: not called CC=1 SCAR=6.9
Pod documented
129 }
130  
131  
132 # A function that accepts the expanded state (as an array ref)
133 # and returns an atom that represents it.
134 sub pack_state
135 {
136 0 ▶ return _die_on_abstract_function();
Subroutine pack_state: not called CC=1 SCAR=6.9
Pod documented
137 }
138  
139  
140 # A function that accepts an atom that represents a state
141 # and returns an array ref that represents it.
142 sub unpack_state
143 {
144 0 ▶ return _die_on_abstract_function();
Subroutine unpack_state: not called CC=1 SCAR=6.9
Pod documented
145 }
146  
147  
148 # Accept an atom that represents a state and output a
149 # user-readable string that describes it.
150 sub display_state
151 {
152 0 ▶ return _die_on_abstract_function();
Subroutine display_state: not called CC=1 SCAR=6.9
Pod documented
153 }
154  
155  
156 sub check_if_final_state
157 {
158 0 ▶ return _die_on_abstract_function();
Subroutine check_if_final_state: not called CC=1 SCAR=6.9
Pod documented
159 }
160  
161  
162 # This function enumerates the moves accessible to the state.
163 # If it returns a move, it still does not mean that this move is a valid
164 # one. I.e: it is possible that it is illegal to perform it.
165 sub enumerate_moves
166 {
167 0 ▶ return _die_on_abstract_function();
Subroutine enumerate_moves: not called CC=1 SCAR=6.9
Pod documented
168 }
169  
170  
171 # This function accepts a state and a move. It tries to perform the
172 # move on the state. If it is succesful, it returns the new state.
173 #
174 # Else, it returns undef to indicate that the move is not possible.
175 sub perform_move
176 {
177 0 ▶ return _die_on_abstract_function();
Subroutine perform_move: not called CC=1 SCAR=6.9
Pod documented
178 }
179  
180  
181 # This function checks if a state it receives as an argument is a
182 # dead-end one.
183 sub check_if_unsolvable
184 {
185 83 ▶ return 0;
Subroutine check_if_unsolvable: called CC=1 SCAR=0.0
Pod documented
186 }
187  
188  
189 # This is a function that should be overrided in case
190 # rendering the move into a string is non-trivial.
191 sub render_move
192 {
193 0 ▶ my $self = shift;
Subroutine render_move: not called CC=2 SCAR=17.9
Pod documented
194  
195 0 my $move = shift;
196  
197 0 ▶ return defined($move) ? $move : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $move ? : 0 0 0
198 }
199  
200  
201 sub _solve_brfs_or_dfs
202 {
203 1 ▶ my $self = shift;
Subroutine _solve_brfs_or_dfs: called CC=13 SCAR=27.6
204 1 my $state_collection = $self->{'state_collection'};
205 1 my $is_dfs = shift;
206 1 my %args = @_;
207  
208 1 my $run_time_display = $self->{'cmd_line'}->{'rt_states_display'};
209 1 my $rtd_callback = $self->{'run_time_display_callback'};
210 1 ▶ my $max_iters = $args{'max_iters'} || (-1);
Condition50%1/2
$args{'max_iters'} || -1
l!l
01
MC/DC0%0/1
$args{'max_iters'} || -1
$args{'max_iters'}
Truth table50%1/2
A
A= $args{'max_iters'}
Aresult
11
00
211 1 my $check_iters = ( $max_iters >= 0 );
212  
213 1 my ( @queue, $state, $coords, $depth, @moves, $new_state );
214  
215 1 ▶ if ( exists( $args{'initial_state'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"initial_state"}) 1 0 1
216 {
217 1 push @queue, $args{'initial_state'};
218 }
219  
220 1 my @ret;
221  
222 1 @ret = ( "unsolved", undef );
223  
224 1 ▶ while ( scalar(@queue) )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @queue) 83 0 83
225 {
226 83 ▶ if ( $check_iters && ( $max_iters <= $self->{'num_iters'} ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($check_iters and $max_iters <= $self->{"num_iters"}) 0 83 83
Condition33%1/3
$check_iters and $max_iters <= $self->{"num_iters"}
!ll&&!rl&&r
8300
MC/DC0%0/2
$check_iters and $max_iters <= $self->{"num_iters"}
$check_iters $max_iters <= $self->{"num_iters"}
Truth table33%1/3
A and B
A= $check_itersB= $max_iters <= $self->{"num_iters"}
ABresult
0X0
100
111
227 {
228 0 @ret = ( "interrupted", undef );
229 0 goto Return;
230 }
231 83 ▶ if ($is_dfs)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_dfs) { } 0 83 83
232 {
233 0 $state = pop(@queue);
234 }
235 else
236 {
237 83 $state = shift(@queue);
238 }
239 83 $coords = $self->unpack_state($state);
240 83 $depth = $state_collection->{$state}->{'d'};
241  
242 83 $self->{'num_iters'}++;
243  
244 # Output the current state to the screen, assuming this option
245 # is set.
246 83 ▶ if ($run_time_display)
Branch: 1 branch
BranchTrueFalseTotal
if ($run_time_display) 0 83 83
247 {
248 $rtd_callback->(
249 $self,
250 'depth' => $depth,
251 'state' => $coords,
252 0 'move' => $state_collection->{$state}->{'m'},
253 );
254  
255 # print ((" " x $depth) . join(",", @$coords) . " M=" . $self->render_move($state_collection->{$state}->{'m'}) ."\n");
256 }
257  
258 83 ▶ if ( $self->check_if_unsolvable($coords) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_if_unsolvable($coords)) 0 83 83
259 {
260 0 next;
261 }
262  
263 83 ▶ if ( $self->check_if_final_state($coords) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->check_if_final_state($coords)) 1 82 83
264 {
265 1 @ret = ( "solved", $state );
266 1 goto Return;
267 }
268  
269 82 @moves = $self->enumerate_moves($coords);
270  
271 82 foreach my $m (@moves)
272 {
273 238 my $new_coords = $self->perform_move( $coords, $m );
274  
275 # Check if this move leads nowhere and if so - skip to the next move.
276 238 ▶ if ( !defined($new_coords) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new_coords) 0 238 238
277 {
278 0 next;
279 }
280  
281 238 $new_state = $self->pack_state($new_coords);
282 238 ▶ if ( !exists( $state_collection->{$new_state} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $state_collection->{$new_state}) 85 153 238
283 {
284 85 $state_collection->{$new_state} = {
285 'p' => $state,
286 'm' => $m,
287 'd' => ( $depth + 1 )
288 };
289 85 push @queue, $new_state;
290 }
291 }
292 }
293  
294 Return:
295  
296 1 return @ret;
297 }
298  
299 sub _run_length_encoding
300 {
301 0 ▶ my @moves = @_;
Subroutine _run_length_encoding: not called CC=3 SCAR=24.8
302 0 my @ret = ();
303  
304 0 my $prev_m = shift(@moves);
305 0 my $count = 1;
306 0 my $m;
307 0 ▶ while ( $m = shift(@moves) )
Branch: 1 branch
BranchTrueFalseTotal
if ($m = shift @moves) 0 0 0
308 {
309 0 ▶ if ( $m eq $prev_m )
Branch: 1 branch
BranchTrueFalseTotal
if ($m eq $prev_m) { } 0 0 0
310 {
311 0 $count++;
312 }
313 else
314 {
315 0 push @ret, [ $prev_m, $count ];
316 0 $prev_m = $m;
317 0 $count = 1;
318 }
319 }
320 0 push @ret, [ $prev_m, $count ];
321  
322 0 return @ret;
323 }
324  
325 sub _solve_state
326 {
327 1 ▶ my $self = shift;
Subroutine _solve_state: called CC=1 SCAR=0.0
328  
329 1 my $initial_coords = shift;
330  
331 1 my $state = $self->pack_state($initial_coords);
332 1 $self->{'state_collection'}->{$state} = { 'p' => undef, 'd' => 0 };
333  
334 1 return $self->run_scan(
335 'initial_state' => $state,
336 @_
337 );
338 }
339  
340  
341 sub solve_board
342 {
343 1 ▶ my $self = shift;
Subroutine solve_board: called CC=1 SCAR=0.0
Pod documented
344  
345 1 my $filename = shift;
346  
347 1 my $initial_coords = $self->input_board($filename);
348  
349 1 return $self->_solve_state( $initial_coords, @_ );
350 }
351  
352  
353 sub run_scan
354 {
355 1 ▶ my $self = shift;
Subroutine run_scan: called CC=1 SCAR=0.0
Pod documented
356  
357 1 my %args = @_;
358  
359 1 return $scan_functions{ $self->{'cmd_line'}->{'scan'} }->( $self, %args );
360 }
361  
362  
363 sub get_num_iters
364 {
365 0 ▶ my $self = shift;
Subroutine get_num_iters: not called CC=1 SCAR=6.9
Pod documented
366  
367 0 return $self->{'num_iters'};
368 }
369  
370  
371 sub display_solution
372 {
373 0 ▶ my $self = shift;
Subroutine display_solution: not called CC=7 SCAR=40.3
Pod documented
374  
375 0 my @ret = @_;
376  
377 0 my $state_collection = $self->{'state_collection'};
378  
379 0 my $output_states = $self->{'cmd_line'}->{'output_states'};
380 0 my $to_rle = $self->{'cmd_line'}->{'to_rle'};
381  
382 my $echo_state = sub {
383 0 ▶ my $state = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
384 0 ▶ return $output_states
Branch: 1 branch
BranchTrueFalseTotal
$output_states ? : 0 0 0
385 ? ( $self->display_state($state) . ": Move = " )
386 : "";
387 0 };
388  
389 0 print $ret[0], "\n";
390  
391 0 ▶ if ( $ret[0] eq "solved" )
Branch: 1 branch
BranchTrueFalseTotal
if ($ret[0] eq "solved") 0 0 0
392 {
393 0 my $key = $ret[1];
394 0 my $s = $state_collection->{$key};
395 0 my @moves = ();
396 0 my @states = ($key);
397  
398 0 ▶ while ( $s->{'p'} )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"p"}) 0 0 0
399 {
400 0 push @moves, $s->{'m'};
401 0 $key = $s->{'p'};
402 0 $s = $state_collection->{$key};
403 0 push @states, $key;
404 }
405 0 @moves = reverse(@moves);
406 0 @states = reverse(@states);
407 0 my $num_state;
408 0 ▶ if ($to_rle)
Branch: 1 branch
BranchTrueFalseTotal
if ($to_rle) { } 0 0 0
409 {
410 0 my @moves_rle = _run_length_encoding(@moves);
411  
412 0 $num_state = 0;
413 0 foreach my $m (@moves_rle)
414 {
415 0 print $echo_state->( $states[$num_state] )
416 . $self->render_move( $m->[0] ) . " * "
417 . $m->[1] . "\n";
418 0 $num_state += $m->[1];
419 }
420 }
421 else
422 {
423 0 ▶ for ( $num_state = 0 ; $num_state < scalar(@moves) ; $num_state++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($num_state < scalar @moves) 0 0 0
424 {
425 0 print $echo_state->( $states[$num_state] )
426 . $self->render_move( $moves[$num_state] ) . "\n";
427 }
428 }
429 0 ▶ if ($output_states)
Branch: 1 branch
BranchTrueFalseTotal
if ($output_states) 0 0 0
430 {
431 0 print $self->display_state( $states[$num_state] ), "\n";
432 }
433 }
434 }
435  
436 sub _default_rtd_callback
437 {
438 0 ▶ my $self = shift;
Subroutine _default_rtd_callback: not called CC=1 SCAR=6.9
439  
440 0 my %args = @_;
441 print(( " " x $args{depth} )
442 0 . join( ",", @{ $args{state} } ) . " M="
443 . $self->render_move( $args{move} )
444 0 . "\n" );
445 }
446  
447  
448 sub set_run_time_states_display
449 {
450 0 ▶ my $self = shift;
Subroutine set_run_time_states_display: not called CC=2 SCAR=17.9
Pod documented
451 0 my $states_display = shift;
452  
453 0 ▶ if ( !$states_display )
Branch: 1 branch
BranchTrueFalseTotal
if (not $states_display) { } 0 0 0
454 {
455 0 $self->{'cmd_line'}->{'rt_states_display'} = undef;
456 }
457 else
458 {
459 0 $self->{'cmd_line'}->{'rt_states_display'} = 1;
460 0 $self->{'run_time_display_callback'} = $states_display;
461 }
462  
463 0 return 0;
464 }
465  
466  
467 13 1;
468  
469 __END__
« blib/lib/Games/LMSolve/Input.pm ↑ Summary blib/lib/Games/LMSolve/Plank/Base.pm »