blib/lib/Games/Solitaire/BlackHole/Solver/App/Base.pm

filter: Statementstmt 83.6% 266 / 318 Branchbran 58.7% 54 / 92 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 83.7% 31 / 37 Podpod 100% 1 / 1 totaltotal 78.7% 363 / 461 SCAR 44.3
CC
54
Cov
78%
_process_cmd_line
16.0
_shuffle
12.0
__ANON__
12.0
CRAP 84.1
↑ Summary blib/lib/Games/Solitaire/BlackHole/Solver/Golf/App.pm »
1 package Games::Solitaire::BlackHole::Solver::App::Base;
2 8 $Games::Solitaire::BlackHole::Solver::App::Base::VERSION = '0.6.0';
3 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Getopt::Long qw/ GetOptions /;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Pod::Usage qw/ pod2usage /;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Math::Random::MT ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use List::Util 1.34 qw/ any max /;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 extends('Exporter');
10  
11 8 has [
12 '_active_record',
13 '_active_task',
14 '_board_cards',
15 '_board_lines',
16 '_board_values',
17 '_init_foundation',
18 '_init_queue',
19 '_init_tasks_configs',
20 '_is_good_diff',
21 '_maximal_num_played_cards__from_all_tasks',
22 '_prelude',
23 '_prelude_iter',
24 '_prelude_string',
25 '_talon_cards',
26 '_positions',
27 '_quiet',
28 '_output_handle',
29 '_output_fn',
30 '_should_show_maximal_num_played_cards',
31 '_tasks',
32 '_tasks_by_names',
33 '_task_idx',
34 ] => ( is => 'rw' );
35 8 our %EXPORT_TAGS = ( 'all' => [qw($card_re)] );
36 8 our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
37  
38 8 my @ranks = ( "A", 2 .. 9, qw(T J Q K) );
39 8 my %ranks_to_n = ( map { $ranks[$_] => $_ } 0 .. $#ranks );
40  
41 sub _RANK_KING
42 {
43 5 ▶ return $ranks_to_n{'K'};
Subroutine _RANK_KING: called CC=1 SCAR=0.0
44 }
45  
46 8 my $card_re_str = '[' . join( "", @ranks ) . '][HSCD]';
47 8 our $card_re = qr{$card_re_str};
48  
49 sub _get_rank
50 {
51 260 ▶ shift;
Subroutine _get_rank: called CC=1 SCAR=0.0
52 260 return $ranks_to_n{ substr( shift(), 0, 1 ) };
53 }
54  
55 sub _calc_lines
56 {
57 5 ▶ my $self = shift;
Subroutine _calc_lines: called CC=3 SCAR=11.2
58 5 my $filename = shift;
59  
60 5 my @lines;
61 5 ▶ if ( $filename eq "-" )
Branch: 1 branch
BranchTrueFalseTotal
if ($filename eq '-') { } 0 5 5
62 {
63 0 @lines = <STDIN>;
64 }
65 else
66 {
67 5 ▶ open my $in, "<", $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<", $filename or die "Could not open $filename for inputting the board lines - $!" 0 5 5
68 or die
69 "Could not open $filename for inputting the board lines - $!";
70 5 @lines = <$in>;
71 5 close($in);
72 }
73 5 chomp @lines;
74 5 $self->_board_lines( \@lines );
75 5 return;
76 }
77  
78 sub _update_max_num_played_cards
79 {
80 6 ▶ my $self = shift;
Subroutine _update_max_num_played_cards: called CC=2 SCAR=6.9
81  
82 6 foreach my $task ( @{ $self->_tasks } )
83 {
84 6 $self->_update_max_reached_depths_stack_len($task);
85 }
86  
87 6 return;
88 }
89  
90 sub _trace_solution
91 {
92 3 ▶ my ( $self, $final_state ) = @_;
Subroutine _trace_solution: called CC=5 SCAR=16.1
93 3 $self->_update_max_num_played_cards();
94 3 my $output_handle = $self->_output_handle;
95 3 $output_handle->print("Solved!\n");
96  
97 3 ▶ return if $self->_quiet;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_quiet 0 3 3
98  
99 3 my $state = $final_state;
100 3 my ( $prev_state, $col_idx );
101  
102 3 my @moves;
103 LOOP:
104 3 ▶ while ( ( $prev_state, $col_idx ) = @{ $self->_positions->{$state} } )
Branch: 1 branch
BranchTrueFalseTotal
if (() = @{$self->_positions->{$state};}) 151 0 151
105 {
106 151 ▶ last LOOP if not defined $prev_state;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prev_state 3 148 151
107 push @moves,
108 (
109 148 ▶ ( $col_idx == @{ $self->_board_cards } )
Branch: 1 branch
BranchTrueFalseTotal
$col_idx == @{$self->_board_cards;} ? : 11 137 148
110 ? "Deal talon " . $self->_talon_cards->[ vec( $prev_state, 1, 8 ) ]
111 : $self->_board_cards->[$col_idx]
112 [ vec( $prev_state, 4 + $col_idx, 4 ) - 1 ]
113 );
114 }
115 continue
116 {
117 148 $state = $prev_state;
118 }
119 3 print {$output_handle} map { "$_\n" } reverse(@moves);
120  
121 3 return;
122 }
123  
124 sub get_max_num_played_cards
125 {
126 3 ▶ my $self = shift;
Subroutine get_max_num_played_cards: called CC=1 SCAR=0.0
Pod documented
127  
128 3 $self->_update_max_num_played_cards();
129  
130 3 return $self->_maximal_num_played_cards__from_all_tasks() - 1;
131 }
132  
133 sub _my_exit
134 {
135 5 ▶ my ( $self, $verdict, ) = @_;
Subroutine _my_exit: called CC=4 SCAR=13.9
136 5 my $output_handle = $self->_output_handle;
137  
138 5 ▶ if ( !$verdict )
Branch: 1 branch
BranchTrueFalseTotal
unless ($verdict) 2 3 5
139 {
140 2 $output_handle->print("Unsolved!\n");
141 }
142 5 ▶ if ( $self->_should_show_maximal_num_played_cards() )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_should_show_maximal_num_played_cards) 3 2 5
143 {
144 3 $output_handle->printf(
145 "At most %u cards could be played.\n",
146 $self->get_max_num_played_cards()
147 );
148 }
149  
150 5 ▶ if ( defined( $self->_output_fn ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_output_fn) 5 0 5
151 {
152 5 close($output_handle);
153 }
154  
155 5 exit( !$verdict );
156 }
157  
158 sub _parse_board
159 {
160 5 ▶ my ($self) = @_;
Subroutine _parse_board: called CC=2 SCAR=7.0
161 5 my $lines = $self->_board_lines;
162  
163 5 my $found_line = shift(@$lines);
164  
165 5 my $init_foundation;
166 5 ▶ if ( my ($card) = $found_line =~ m{\AFoundations: ($card_re)\z} )
Branch: 1 branch
BranchTrueFalseTotal
if (my($card) = $found_line =~ /\AFoundations: ($Games::Solitaire::BlackHole::Solver::App::Base::card_re)\z/) { } 5 0 5
167 {
168 5 $init_foundation = $self->_get_rank($card);
169 }
170 else
171 {
172 0 die "Could not match first foundation line!";
173 }
174 5 $self->_init_foundation($init_foundation);
175  
176 5 $self->_board_cards( [ map { [ split /\s+/, $_ ] } @$lines ] );
177 $self->_board_values(
178 [
179 map {
180 75 [ map { $self->_get_rank($_) } @$_ ]
181 5 } @{ $self->_board_cards }
182 ]
183 );
184 5 return;
185 }
186  
187 sub _set_up_initial_position
188 {
189 5 ▶ my ( $self, $talon_ptr ) = @_;
Subroutine _set_up_initial_position: called CC=2 SCAR=6.9
190  
191 5 my $init_state = "";
192  
193 5 vec( $init_state, 0, 8 ) = $self->_init_foundation;
194 5 vec( $init_state, 1, 8 ) = $talon_ptr;
195  
196 5 my $board_values = $self->_board_values;
197 5 foreach my $col_idx ( keys @$board_values )
198 {
199 vec( $init_state, 4 + $col_idx, 4 ) =
200 75 scalar( @{ $board_values->[$col_idx] } );
201 }
202  
203 # The values of $positions is an array reference with the 0th key being the
204 # previous state, and the 1th key being the column of the move.
205 5 $self->_positions( +{ $init_state => [ undef, undef, 1, 0, ], } );
206  
207 5 $self->_init_queue( [$init_state] );
208  
209 5 return;
210 }
211  
212 sub _shuffle
213 {
214 0 ▶ my ( $self, $gen, $arr ) = @_;
Subroutine _shuffle: not called CC=3 SCAR=24.8
215  
216 0 my $i = $#$arr;
217 0 ▶ while ( $i > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 0 0 0
218 {
219 0 my $j = int( $gen->rand( $i + 1 ) );
220 0 ▶ if ( $i != $j )
Branch: 1 branch
BranchTrueFalseTotal
if ($i != $j) 0 0 0
221 {
222 0 @$arr[ $i, $j ] = @$arr[ $j, $i ];
223 }
224 0 --$i;
225 }
226 0 return;
227 }
228  
229 8 my $TASK_NAME_RE = qr/[A-Za-z0-9_]+/;
230 8 my $TASK_ALLOC_RE = qr/[0-9]+\@$TASK_NAME_RE/;
231  
232 sub _process_cmd_line
233 {
234 5 ▶ my ( $self, $args ) = @_;
Subroutine _process_cmd_line: called CC=9 SCAR=27.7
235  
236 5 $self->_should_show_maximal_num_played_cards(0);
237 5 my $quiet = '';
238 5 my $output_fn;
239 5 my ( $help, $man, $version );
240 5 my @tasks;
241  
242 my $push_task = sub {
243 5 ▶ push @tasks,
Subroutine __ANON__: called CC=1 SCAR=0.0
244 +{
245 name => undef(),
246 seed => 0,
247 };
248 5 return;
249 5 };
250 5 $push_task->();
251 GetOptions(
252 "o|output=s" => \$output_fn,
253 "quiet!" => \$quiet,
254 "next-task" => sub {
255 0 ▶ $push_task->();
Subroutine __ANON__: not called CC=1 SCAR=6.9
256 0 return;
257 },
258 "prelude=s" => sub {
259 0 ▶ my ( undef, $val ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
260 0 ▶ if ( $val !~ /\A$TASK_ALLOC_RE(?:,$TASK_ALLOC_RE)*\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /\A$TASK_ALLOC_RE(?:,$TASK_ALLOC_RE)*\z/) 0 0 0
261 {
262 0 die "Invalid prelude string '$val' !";
263 }
264 0 $self->_prelude_string($val);
265 0 return;
266 },
267 "task-name=s" => sub {
268 0 ▶ my ( undef, $val ) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
269 0 ▶ if ( $val !~ /\A$TASK_NAME_RE\z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($val =~ /\A$TASK_NAME_RE\z/) 0 0 0
270 {
271 0 die "Invalid task name '$val' - must be alphanumeric!";
272 }
273 0 $tasks[-1]->{name} = $val;
274 0 return;
275 },
276 "seed=i" => sub {
277 0 ▶ my ( undef, $val ) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 0 $tasks[-1]->{seed} = $val;
279 0 return;
280 },
281 "show-max-num-played-cards!" => sub {
282 3 ▶ my ( undef, $val ) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
283 3 $self->_should_show_maximal_num_played_cards($val);
284 3 return;
285 },
286 'help|h|?' => \$help,
287 'man' => \$man,
288 'version' => \$version,
289 5 ▶ %{ $args->{extra_flags} },
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("o|output=s", \$output_fn, "quiet!", \$quiet, "next-task", sub { &$push_task(); return; } , "prelude=s", sub { my(undef, $val) = @_; unless ($val =~ /\A$TASK_ALLOC_RE(?:,$TASK_ALLOC_RE)*\z/) { die "Invalid prelude string '${val}' !"; } ; $self->_prelude_string($val); return; } , "task-name=s", sub { my(undef, $val) = @_; unless ($val =~ /\A$TASK_NAME_RE\z/) { die "Invalid task name '${val}' - must be alphanumeric!"; } ; $tasks[-1]{"name"} = $val; return; } , "seed=i", sub { my(undef, $val) = @_; $tasks[-1]{"seed"} = $val; return; } , "show-max-num-played-cards!", sub { my(undef, $val) = @_; $self->_should_show_maximal_num_played_cards($val); return; } , "help|h|?", \$help, "man", \$man, "version", \$version, %{$$args{"extra_flags"};}) or pod2usage(2) 0 5 5
290 ) or pod2usage(2);
291 5 ▶ if ( @tasks == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@tasks == 1) 5 0 5
292 {
293 5 $tasks[-1]{name} = 'default';
294 }
295 5 ▶ if ( any { !defined $_->{name} } @tasks )
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { not defined $_->{"name"}; } , @tasks)) 0 5 5
296 {
297 0 die "You did not specify the task-names for some tasks";
298 }
299 5 $self->_init_tasks_configs( \@tasks );
300  
301 5 ▶ pod2usage(1) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 5 5
302 5 ▶ pod2usage( -exitstatus => 0, -verbose => 2 ) if $man;
Branch: 1 branch
BranchTrueFalseTotal
if $man 0 5 5
303  
304 5 ▶ if ($version)
Branch: 1 branch
BranchTrueFalseTotal
if ($version) 0 5 5
305 {
306 0 print
307 "black-hole-solve version $Games::Solitaire::BlackHole::Solver::App::Base::VERSION\n";
308 0 exit(0);
309 }
310  
311 5 $self->_quiet($quiet);
312 5 my $output_handle;
313  
314 5 ▶ if ( defined($output_fn) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $output_fn) { } 5 0 5
315 {
316 5 ▶ open( $output_handle, ">", $output_fn )
Branch: 1 branch
BranchTrueFalseTotal
open $output_handle, ">", $output_fn or die "Could not open '${output_fn}' for writing" 0 5 5
317 or die "Could not open '$output_fn' for writing";
318 }
319 else
320 {
321 0 open( $output_handle, ">&STDOUT" );
322 }
323 5 $self->_output_fn($output_fn);
324 5 $self->_output_handle($output_handle);
325 5 $self->_calc_lines( shift(@ARGV) );
326  
327 5 return;
328 }
329  
330 sub _set_up_tasks
331 {
332 5 ▶ my ($self) = @_;
Subroutine _set_up_tasks: called CC=6 SCAR=21.3
333 5 $self->_maximal_num_played_cards__from_all_tasks(0);
334  
335 5 my @tasks;
336 my %tasks_by_names;
337 5 foreach my $task_rec ( @{ $self->_init_tasks_configs } )
338 {
339 5 my $iseed = $task_rec->{seed};
340 5 my $name = $task_rec->{name};
341 5 my $_task_idx = @tasks;
342 5 ▶ my $task_obj =
Condition50%1/2
$iseed || 1
l!l
05
MC/DC0%0/1
$iseed || 1
$iseed
Truth table50%1/2
A
A= $iseed
Aresult
11
00
343 Games::Solitaire::BlackHole::Solver::App::Base::Task->new(
344 {
345 _name => $name,
346 _seed => $iseed,
347 _gen => Math::Random::MT->new( $iseed || 1 ),
348 _remaining_iters => 100,
349 _task_idx => $_task_idx,
350 }
351 );
352 5 $task_obj->_push_to_queue( $self->_init_queue );
353 5 push @tasks, $task_obj;
354 5 ▶ if ( exists $tasks_by_names{$name} )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tasks_by_names{$name}) 0 5 5
355 {
356 0 die "Duplicate task-name '$name'!";
357 }
358 5 $tasks_by_names{$name} = $task_obj;
359 }
360 5 $self->_task_idx(0);
361 5 $self->_tasks( \@tasks );
362 5 $self->_tasks_by_names( \%tasks_by_names );
363 5 my @prelude;
364 my $process_item = sub {
365 0 ▶ my $s = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
366 0 ▶ if ( my ( $quota, $name ) = $s =~ /\A([0-9]+)\@($TASK_NAME_RE)\z/ )
Branch: 1 branch
BranchTrueFalseTotal
if (() = $s =~ /\A([0-9]+)\@($TASK_NAME_RE)\z/) { } 0 0 0
367 {
368 0 ▶ if ( not exists $self->_tasks_by_names->{$name} )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->_tasks_by_names->{$name}) 0 0 0
369 {
370 0 die "Unknown task name $name in prelude!";
371 }
372 0 my $task_obj = $self->_tasks_by_names->{$name};
373 0 return Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem
374 ->new(
375 {
376 _quota => $quota,
377 _task => $task_obj,
378 _task_idx => $task_obj->_task_idx,
379 _task_name => $task_obj->_name,
380 }
381 );
382 }
383 else
384 {
385 0 die "foo";
386 }
387 5 };
388 5 ▶ if ( my $_prelude_string = $self->_prelude_string )
Branch: 1 branch
BranchTrueFalseTotal
if (my $_prelude_string = $self->_prelude_string) 0 5 5
389 {
390 push @prelude,
391 (
392 0 map { $process_item->($_) }
393 split /,/, $_prelude_string
394 );
395 }
396 5 $self->_prelude( \@prelude );
397 5 $self->_prelude_iter(0);
398 5 ▶ if ( @{ $self->_prelude } )
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->_prelude;}) 0 5 5
399 {
400 0 $self->_next_task;
401 }
402 5 return;
403 }
404  
405 sub _update_max_reached_depths_stack_len
406 {
407 6 ▶ my ( $self, $task ) = @_;
Subroutine _update_max_reached_depths_stack_len: called CC=1 SCAR=0.0
408  
409 6 $self->_maximal_num_played_cards__from_all_tasks(
410 max(
411 $self->_maximal_num_played_cards__from_all_tasks,
412 $task->_max_reached_depths_stack_len
413 )
414 );
415  
416 6 return;
417 }
418  
419 sub _next_task
420 {
421 186 ▶ my ($self) = @_;
Subroutine _next_task: called CC=5 SCAR=21.3
422 186 ▶ if ( $self->_prelude_iter < @{ $self->_prelude } )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_prelude_iter < @{$self->_prelude;}) 0 186 186
423 {
424 0 my $alloc = $self->_prelude->[ $self->{_prelude_iter}++ ];
425 0 my $task = $alloc->_task;
426 0 ▶ if ( !@{ $task->_queue } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$task->_queue;}) 0 0 0
427 {
428 0 $self->_update_max_reached_depths_stack_len($task);
429 0 return $self->_next_task;
430 }
431 0 $task->_remaining_iters( $alloc->_quota );
432 0 $self->_active_task($task);
433 0 return 1;
434 }
435 186 my $tasks = $self->_tasks;
436 186 ▶ return if !@$tasks;
Branch: 1 branch
BranchTrueFalseTotal
unless @$tasks 0 186 186
437 186 ▶ if ( !@{ $tasks->[ $self->_task_idx ]->_queue } )
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$$tasks[$self->_task_idx]->_queue;}) 0 186 186
438 {
439 0 splice @$tasks, $self->_task_idx, 1;
440 0 return $self->_next_task;
441 }
442 186 my $task = $tasks->[ $self->_task_idx ];
443 186 $self->_task_idx( ( $self->_task_idx + 1 ) % @$tasks );
444 186 $task->_remaining_iters(100);
445 186 $self->_active_task($task);
446  
447 186 return 1;
448 }
449  
450 sub _get_next_state
451 {
452 18236 ▶ my ($self) = @_;
Subroutine _get_next_state: called CC=3 SCAR=11.0
453  
454 18236 my $l = @{ $self->_active_task->_queue };
455 18236 my $ret = pop( @{ $self->_active_task->_queue } );
456 18236 my $stack = $self->_active_task->_depths_stack;
457 18236 ▶ while ( @$stack and ( $stack->[-1] == $l ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@$stack and $stack->[-1] == $l) 30712 18236 48948
Condition100%3/3
@$stack and $stack->[-1] == $l
!ll&&!rl&&r
71822930712
MC/DC100%2/2
@$stack and $stack->[-1] == $l
@$stack $stack->[-1] == $l
Truth table100%3/3
A and B
A= @$stackB= $stack->[-1] == $l
ABresult
0X0
100
111
458 {
459 30712 pop @$stack;
460 }
461 18236 push @$stack, ( $l - 1 );
462 18236 return $ret;
463 }
464  
465 sub _get_next_state_wrapper
466 {
467 18236 ▶ my ($self) = @_;
Subroutine _get_next_state_wrapper: called CC=3 SCAR=11.0
468  
469 18236 my $positions = $self->_positions;
470  
471 18236 ▶ while ( my $state = $self->_get_next_state )
Branch: 1 branch
BranchTrueFalseTotal
if (my $state = $self->_get_next_state) 18234 2 18236
472 {
473 18234 my $rec = $positions->{$state};
474 18234 $self->_active_record($rec);
475 18234 ▶ return $state if $rec->[2];
Branch: 1 branch
BranchTrueFalseTotal
if $rec->[2] 18234 0 18234
476 }
477 2 return;
478 }
479  
480 sub _process_pending_items
481 {
482 18231 ▶ my ( $self, $_pending, $state ) = @_;
Subroutine _process_pending_items: called CC=7 SCAR=19.5
483  
484 18231 my $rec = $self->_active_record;
485 18231 my $task = $self->_active_task;
486  
487 18231 ▶ if (@$_pending)
Branch: 1 branch
BranchTrueFalseTotal
if (@$_pending) { } 12624 5607 18231
488 {
489 12624 ▶ $self->_shuffle( $task->_gen, $_pending ) if $task->_seed;
Branch: 1 branch
BranchTrueFalseTotal
if $task->_seed 0 12624 12624
490 12624 $task->_push_to_queue( [ map { $_->[0] } @$_pending ] );
491 12624 $rec->[3] += ( scalar grep { !$_->[1] } @$_pending );
492 }
493 else
494 {
495 5607 my $parent = $state;
496 5607 my $parent_rec = $rec;
497 5607 my $positions = $self->_positions;
498  
499 PARENT:
500 5607 ▶ while ( ( !$parent_rec->[3] ) or ( ! --$parent_rec->[3] ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent_rec->[3] and --$parent_rec->[3]) 18083 5605 23688
Condition100%3/3
$parent_rec->[3] and --$parent_rec->[3]
!ll&&!rl&&r
5607124765605
MC/DC100%2/2
$parent_rec->[3] and --$parent_rec->[3]
$parent_rec->[3] --$parent_rec->[3]
Truth table100%3/3
A and B
A= $parent_rec->[3]B= --$parent_rec->[3]
ABresult
0X0
100
111
501 {
502 18083 $parent_rec->[2] = 0;
503 18083 $parent = $parent_rec->[0];
504 18083 ▶ last PARENT if not defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 2 18081 18083
505 18081 $parent_rec = $positions->{$parent};
506 }
507 }
508 18231 ▶ if ( not --$task->{_remaining_iters} )
Branch: 1 branch
BranchTrueFalseTotal
unless (--$task->{"_remaining_iters"}) 181 18050 18231
509 {
510 181 return $self->_next_task;
511 }
512 18050 return 1;
513 }
514  
515 sub _find_moves
516 {
517 18234 ▶ my ( $self, $_pending, $state, $no_cards ) = @_;
Subroutine _find_moves: called CC=7 SCAR=19.5
518 18234 my $board_values = $self->_board_values;
519 18234 my $fnd = vec( $state, 0, 8 );
520 18234 my $positions = $self->_positions;
521 18234 my $_is_good_diff = $self->_is_good_diff;
522 18234 foreach my $col_idx ( keys @$board_values )
523 {
524 300448 my $pos = vec( $state, 4 + $col_idx, 4 );
525  
526 300448 ▶ if ($pos)
Branch: 1 branch
BranchTrueFalseTotal
if ($pos) 186314 114134 300448
527 {
528 186314 $$no_cards = 0;
529  
530 186314 my $card = $board_values->[$col_idx][ $pos - 1 ];
531 186314 ▶ if ( exists( $_is_good_diff->{ $card - $fnd } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$_is_good_diff{$card - $fnd}) 26301 160013 186314
532 {
533 26301 my $next_s = $state;
534 26301 vec( $next_s, 0, 8 ) = $card;
535 26301 --vec( $next_s, 4 + $col_idx, 4 );
536 26301 my $exists = exists( $positions->{$next_s} );
537 26301 my $to_add = 0;
538 26301 ▶ if ( !$exists )
Branch: 2 branches
BranchTrueFalseTotal
if (not $exists) { } 17762 8539 26301
elsif ($positions->{$next_s}[2]) { } 0 8539 8539
539 {
540 17762 $positions->{$next_s} = [ $state, $col_idx, 1, 0 ];
541 17762 $to_add = 1;
542 }
543 elsif ( $positions->{$next_s}->[2] )
544 {
545 0 $to_add = 1;
546 }
547 26301 ▶ if ($to_add)
Branch: 1 branch
BranchTrueFalseTotal
if ($to_add) 17762 8539 26301
548 {
549 17762 push( @$_pending, [ $next_s, $exists ] );
550 }
551 }
552 }
553 }
554  
555 18234 return;
556 }
557  
558 sub _set_up_solver
559 {
560 5 ▶ my ( $self, $talon_ptr, $diffs ) = @_;
Subroutine _set_up_solver: called CC=1 SCAR=0.0
561  
562 5 $self->_parse_board;
563 5 $self->_set_up_initial_position($talon_ptr);
564 5 $self->_set_up_tasks;
565 5 $self->_is_good_diff( +{ map { $_ => 1 } map { $_, -$_ } @$diffs, } );
566  
567 5 return;
568 }
569  
570 package Games::Solitaire::BlackHole::Solver::App::Base::Task;
571 8 $Games::Solitaire::BlackHole::Solver::App::Base::Task::VERSION = '0.6.0';
572 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
573  
574 5 ▶has '_queue' => ( is => 'ro', default => sub { return []; }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
575 5 ▶has '_depths_stack' => ( is => 'ro', default => sub { return []; }, );
Subroutine __ANON__: called CC=1 SCAR=0.0
576 8 has '_max_reached_depths_stack_len' => ( is => 'rw', default => 0 );
577 8 has [ '_gen', '_task_idx', '_name', '_remaining_iters', '_seed', ] =>
578 ( is => 'rw' );
579  
580 sub _push_to_queue
581 {
582 12629 ▶ my ( $self, $items ) = @_;
Subroutine _push_to_queue: called CC=3 SCAR=11.0
583 12629 ▶ die if not @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless @$items 0 12629 12629
584 12629 push @{ $self->_queue }, @$items;
585 12629 push @{ $self->_depths_stack }, scalar( @{ $self->_queue } );
586 12629 my $l = @{ $self->_depths_stack };
587 12629 ▶ if ( $l > $self->_max_reached_depths_stack_len() )
Branch: 1 branch
BranchTrueFalseTotal
if ($l > $self->_max_reached_depths_stack_len) 160 12469 12629
588 {
589 160 $self->_max_reached_depths_stack_len($l);
590 }
591  
592 12629 return;
593 }
594  
595 package Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem;
596 8 $Games::Solitaire::BlackHole::Solver::App::Base::PreludeItem::VERSION = '0.6.0';
597 8 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
598  
599 8 has [ '_quota', '_task', '_task_idx', '_task_name', ] => ( is => 'rw' );
600  
601 8 1;
602  
603 __END__
↑ Summary blib/lib/Games/Solitaire/BlackHole/Solver/Golf/App.pm »