Verify/Solution/ExpandMultiCardMoves.pm

filter: Statementstmt 93.6% 163 / 174 Branchbran 71.7% 33 / 46 Conditioncond 85.7% 18 / 21 MC/DCmcdc 72.7% 8 / 11 Subroutinesub 100% 25 / 25 Podpod 100% 1 / 1 totaltotal 89.2% 248 / 278 CC 36 SCAR 17.5
CC
36
Cov
89%
_apply_move
13.1
verify
8.0
_read_state
7.4
CRAP 5.7
« Verify/App/CmdLine/From_Patsolve.pm ↑ Summary Verify/Solution.pm »
1 package Games::Solitaire::Verify::Solution::ExpandMultiCardMoves;
2 5 $Games::Solitaire::Verify::Solution::ExpandMultiCardMoves::VERSION = '0.2601';
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 5 ▶use parent 'Games::Solitaire::Verify::Solution::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # TODO : Merge with lib/Games/Solitaire/Verify/Solution.pm
11  
12 5 ▶use POSIX qw( ceil );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 5 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 5 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 5 ▶use Games::Solitaire::Verify::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Games::Solitaire::Verify::Move ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶use Games::Solitaire::Verify::State ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 5 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 5 __PACKAGE__->mk_acc_ref(
23 [
24 qw(
25 _move_line
26 _output_fh
27 )
28 ]
29 );
30  
31  
32 sub _init
33 2 ▶{
Subroutine _init: called CC=1 SCAR=0.0
34 2 my ( $self, $args ) = @_;
35  
36 2 $self->SUPER::_init($args);
37  
38 2 $self->_st(undef);
39 2 $self->_reached_end(0);
40 2 $self->_output_fh( $args->{output_fh} );
41  
42 2 return 0;
43 }
44  
45 sub _out
46 3748 ▶{
Subroutine _out: called CC=1 SCAR=0.0
47 3748 my ( $self, $text ) = @_;
48  
49 3748 $self->_output_fh()->print($text);
50  
51 3748 return ();
52 }
53  
54 sub _out_line
55 2962 ▶{
Subroutine _out_line: called CC=1 SCAR=0.0
56 2962 my ( $self, $line ) = @_;
57  
58 2962 return $self->_out($line);
59 }
60  
61 sub _assign_read_new_state
62 217 ▶{
Subroutine _assign_read_new_state: called CC=3 SCAR=11.1
63 217 my ( $self, $str ) = @_;
64  
65 my $new_state = Games::Solitaire::Verify::State->new(
66 {
67 string => $str,
68 217 @{ $self->_V },
69 }
70 );
71  
72 217 ▶ if ( !defined( $self->_st() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->_st) { } 1 216 217
73 {
74 # Do nothing.
75  
76 }
77 else
78 {
79 216 ▶ if ( $self->_st()->to_string() ne $str )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_st->to_string ne $str) 0 216 216
80 {
81 0 die "States don't match";
82 }
83 }
84 217 $self->_st($new_state);
85  
86 217 return ();
87 }
88  
89 sub _read_state
90 434 ▶{
Subroutine _read_state: called CC=7 SCAR=20.0
91 434 my $self = shift;
92  
93 434 my $line = $self->_l();
94  
95 434 ▶ if ( $line ne "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "\n") 0 434 434
96 {
97 0 die "Non empty line before state";
98 }
99  
100 434 $self->_out_line($line);
101  
102 434 my $str = "";
103  
104 434 ▶ while ( ( $line = $self->_l() ) && ( $line ne "\n" ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($line = $self->_l and $line ne "\n") 4340 434 4774
Condition66%2/3
$line = $self->_l and $line ne "\n"
!ll&&!rl&&r
04344340
MC/DC50%1/2
$line = $self->_l and $line ne "\n"
$line = $self->_l $line ne "\n"
Truth table66%2/3
A and B
A= $line = $self->_lB= $line ne "\n"
ABresult
0X0
100
111
105 {
106 4340 $str .= $line;
107 }
108  
109 434 $self->_assign_read_new_state($str);
110  
111 434 $self->_out($str);
112  
113 434 $self->_out_line("\n");
114 434 ▶ while ( defined( $line = $self->_l() ) && ( $line eq "\n" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->_l) and $line eq "\n") 434 434 868
Condition66%2/3
defined($line = $self->_l) and $line eq "\n"
!ll&&!rl&&r
0434434
MC/DC50%1/2
defined($line = $self->_l) and $line eq "\n"
defined($line = $self->_l) $line eq "\n"
Truth table66%2/3
A and B
A= defined($line = $self->_l)B= $line eq "\n"
ABresult
0X0
100
111
115 {
116 434 $self->_out_line($line);
117 }
118  
119 434 ▶ if ( $line !~ m{\A={3,}\n\z} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\A={3,}\n\z/u) 0 434 434
120 {
121 0 die "No ======== separator";
122 }
123 434 $self->_out_line($line);
124  
125 434 return ();
126 }
127  
128 sub _read_move
129 434 ▶{
Subroutine _read_move: called CC=4 SCAR=13.9
130 434 my $self = shift;
131  
132 434 my $line = $self->_l();
133  
134 434 ▶ if ( $line ne "\n" )
Branch: 1 branch
BranchTrueFalseTotal
if ($line ne "\n") 0 434 434
135 {
136 0 die "No empty line before move";
137 }
138  
139 434 $self->_out_line($line);
140  
141 434 $line = $self->_l();
142  
143 434 ▶ if ( $line eq "This game is solveable.\n" )
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "This game is solveable.\n") 2 432 434
144 {
145 2 $self->_reached_end(1);
146 2 $self->_out_line($line);
147  
148 2 ▶ while ( defined( $line = $self->_l() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = $self->_l)) 4 2 6
149 {
150 4 $self->_out_line($line);
151 }
152  
153 2 return "END";
154 }
155  
156 432 chomp($line);
157  
158 432 $self->_move_line($line);
159  
160 432 $self->_move(
161 Games::Solitaire::Verify::Move->new(
162 {
163 fcs_string => $line,
164 game => $self->_variant(),
165 }
166 )
167 );
168  
169 432 return ();
170 }
171  
172  
173 sub _find_max_step
174 32 ▶{
Subroutine _find_max_step: called CC=2 SCAR=6.9
175 32 my ( $self, $n ) = @_;
176  
177 32 my $x = 1;
178  
179 32 ▶ while ( ( $x << 1 ) < $n )
Branch: 1 branch
BranchTrueFalseTotal
if ($x << 1 < $n) 6 32 38
180 {
181 6 $x <<= 1;
182 }
183  
184 32 return $x;
185 }
186  
187 sub _apply_move
188 432 ▶{
Subroutine _apply_move: called CC=13 SCAR=25.7
189 432 my $self = shift;
190  
191 432 ▶ if ( ( $self->_move->source_type eq "stack" )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_move->source_type eq 'stack' and $self->_move->dest_type eq 'stack' and $self->_move->num_cards > 1 and $self->_variant_params->sequence_move eq 'limited') { } 80 352 432
Condition88%8/9
$self->_move->source_type eq "stack" and $self->_move->dest_type eq "stack" and $self->_move->num_cards > 1 and $self->_variant_params->sequence_move eq "limited"
!ll&&!rl&&r
352080
$self->_move->source_type eq "stack" and $self->_move->dest_type eq "stack" and $self->_move->num_cards > 1
!ll&&!rl&&r
3104280
$self->_move->source_type eq "stack" and $self->_move->dest_type eq "stack"
!ll&&!rl&&r
110200122
MC/DC75%3/4
$self->_move->source_type eq "stack" and $self->_move->dest_type eq "stack" and $self->_move->num_cards > 1 and $self->_variant_params->sequence_move eq "limited"
$self->_move->source_type eq "stack" $self->_move->dest_type eq "stack" $self->_move->num_cards > 1 $self->_variant_params->sequence_move eq "limited"
Truth table80%4/5
A and B and C and D
A= $self->_move->source_type eq "stack"B= $self->_move->dest_type eq "stack"C= $self->_move->num_cards > 1D= $self->_variant_params->sequence_move eq "limited"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
192 && ( $self->_move->dest_type eq "stack" )
193 && ( $self->_move->num_cards > 1 )
194 && ( $self->_variant_params->sequence_move() eq "limited" ) )
195 {
196 80 my $ultimate_num_cards = $self->_move->num_cards;
197 80 my $ultimate_source = $self->_move->source;
198 80 my $ultimate_dest = $self->_move->dest;
199  
200 # Need to process this move.
201 80 my @empty_fc_indexes;
202 my @empty_stack_indexes;
203  
204 80 foreach my $idx ( 0 .. ( $self->_st->num_freecells() - 1 ) )
205 {
206 320 ▶ if ( !defined( $self->_st->get_freecell($idx) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->_st->get_freecell($idx)) 150 170 320
207 {
208 150 push @empty_fc_indexes, $idx;
209 }
210 }
211  
212 80 foreach my $idx ( 0 .. ( $self->_st->num_columns() - 1 ) )
213 {
214 640 ▶ if ( ( $idx != $ultimate_dest )
Branch: 1 branch
BranchTrueFalseTotal
if ($idx != $ultimate_dest and $idx != $ultimate_source and not $self->_st->get_column($idx)->len) 32 608 640
Condition100%6/6
$idx != $ultimate_dest and $idx != $ultimate_source and not $self->_st->get_column($idx)->len
!ll&&!rl&&r
16044832
$idx != $ultimate_dest and $idx != $ultimate_source
!ll&&!rl&&r
8080480
MC/DC100%3/3
$idx != $ultimate_dest and $idx != $ultimate_source and not $self->_st->get_column($idx)->len
$idx != $ultimate_dest $idx != $ultimate_source not $self->_st->get_column($idx)->len
Truth table100%4/4
A and B and not(C)
A= $idx != $ultimate_destB= $idx != $ultimate_sourceC= not $self->_st->get_column($idx)->len
ABCresult
0XX0
10X0
1100
1111
215 && ( $idx != $ultimate_source )
216 && ( !$self->_st->get_column($idx)->len() ) )
217 {
218 32 push @empty_stack_indexes, $idx;
219 }
220 }
221  
222 80 my @num_cards_moved_at_each_stage;
223  
224 80 my $num_cards = 0;
225 80 push @num_cards_moved_at_each_stage, $num_cards;
226 80 my $step_width = 1 + @empty_fc_indexes;
227 80 ▶ while (
Branch: 1 branch
BranchTrueFalseTotal
if (($num_cards = &min($num_cards + $step_width, $ultimate_num_cards)) < $ultimate_num_cards) 26 80 106
228 (
229 $num_cards =
230 min( $num_cards + $step_width, $ultimate_num_cards )
231 ) < $ultimate_num_cards
232 )
233 {
234 26 push @num_cards_moved_at_each_stage, $num_cards;
235 }
236 80 push @num_cards_moved_at_each_stage, $num_cards;
237  
238 # Initialised to the null sub.
239 80 ▶ my $output_state_promise = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
240 80 return ();
241 80 };
242  
243 352 ▶ my $past_first_output_state_promise = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
244 352 $self->_out(
245 "\n" . $self->_st->to_string . "\n\n====================\n\n" );
246  
247 352 return ();
248 80 };
249  
250 432 ▶ my $add_move = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
251 432 my ($move_line) = @_;
252  
253 432 $output_state_promise->();
254  
255 432 $self->_out_line( $move_line . "\n" );
256  
257 432 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $verdict = $self->_st->verify_and_perform_move("Games::Solitaire::Verify::Move"->new({"fcs_string", $move_line, "game", $self->_variant}))) 0 432 432
258 my $verdict = $self->_st()->verify_and_perform_move(
259 Games::Solitaire::Verify::Move->new(
260 {
261 fcs_string => $move_line,
262 game => $self->_variant(),
263 }
264 )
265 )
266 )
267 {
268 0 Games::Solitaire::Verify::Exception::VerifyMove->throw(
269 error => "Wrong Move",
270 problem => $verdict,
271 );
272 }
273  
274 432 $output_state_promise = $past_first_output_state_promise;
275  
276 432 return ();
277 80 };
278  
279 144 ▶ my $move_using_freecells = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
280 144 my ( $source, $dest, $count ) = @_;
281  
282 144 my $num_cards_thru_freecell = $count - 1;
283 144 for my $i ( 0 .. $num_cards_thru_freecell - 1 )
284 {
285 144 $add_move->(
286 "Move a card from stack $source to freecell $empty_fc_indexes[$i]"
287 );
288 }
289 144 $add_move->("Move 1 cards from stack $source to stack $dest");
290  
291 144 for my $i ( reverse( 0 .. $num_cards_thru_freecell - 1 ) )
292 {
293 144 $add_move->(
294 "Move a card from freecell $empty_fc_indexes[$i] to stack $dest"
295 );
296 }
297  
298 144 return ();
299 80 };
300  
301 80 my $recursive_move;
302 144 ▶ $recursive_move = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
303 144 my ( $source, $dest, $num_cards, $empty_cols ) = @_;
304  
305 144 ▶ if ( $num_cards <= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($num_cards <= 0) { } 0 144 144
306 {
307 # Do nothing - the no-op.
308 #$move_using_freecells->($source, $dest,
309 # $num_cards_moved_at_each_stage[$depth] -
310 # $num_cards_moved_at_each_stage[$depth-1]
311 #);
312 0 return ();
313 }
314 else
315 {
316 144 my @running_empty_cols = @$empty_cols;
317 144 my @steps;
318  
319 144 ▶ while ( ceil( $num_cards / $step_width ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (ceil($num_cards / $step_width) > 1) 32 144 176
320 {
321 # Top power of two in $num_steps
322 32 my $rec_num_steps = $self->_find_max_step(
323 ceil( $num_cards / $step_width ) );
324 32 my $count_cards = $rec_num_steps * $step_width;
325 32 my $temp_dest = shift(@running_empty_cols);
326 32 $recursive_move->(
327 $source, $temp_dest, $count_cards,
328 [@running_empty_cols],
329 );
330  
331 32 push @steps,
332 +{
333 'source' => $source,
334 'dest' => $temp_dest,
335 count => $count_cards
336 };
337 32 $num_cards -= $count_cards;
338 }
339 144 $move_using_freecells->( $source, $dest, $num_cards );
340  
341 144 foreach my $s ( reverse(@steps) )
342 {
343 $recursive_move->(
344 32 $s->{dest}, $dest, $s->{count}, [@running_empty_cols]
345 );
346 @running_empty_cols =
347 32 ( sort { $a <=> $b } @running_empty_cols, $s->{dest} );
348 }
349 144 return ();
350 }
351 80 };
352  
353 80 $recursive_move->(
354 $ultimate_source, $ultimate_dest,
355 $ultimate_num_cards, [@empty_stack_indexes],
356 );
357 }
358 else
359 {
360 352 $self->_out_line( $self->_move_line . "\n" );
361 352 ▶ if ( my $verdict =
Branch: 1 branch
BranchTrueFalseTotal
if (my $verdict = $self->_st->verify_and_perform_move($self->_move)) 0 352 352
362 $self->_st()->verify_and_perform_move( $self->_move() ) )
363 {
364 0 Games::Solitaire::Verify::Exception::VerifyMove->throw(
365 error => "Wrong Move",
366 problem => $verdict,
367 );
368 }
369 }
370  
371 432 return ();
372 }
373  
374  
375 sub verify
376 2 ▶{
Subroutine verify: called CC=6 SCAR=20.9
Pod documented
377 2 my $self = shift;
378  
379 2 eval {
380  
381 2 my $line = $self->_l();
382  
383 2 ▶ if ( $line !~ m{\A(-=)+-\n\z} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /\A(-=)+-\n\z/u) 0 2 2
384 {
385 0 die "Incorrect start";
386 }
387 2 $self->_out_line($line);
388  
389 2 $self->_read_state();
390  
391 2 ▶ while ( !defined( scalar( $self->_read_move() ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined scalar $self->_read_move) 432 2 434
392 {
393 432 $self->_apply_move();
394 432 $self->_read_state();
395 }
396 };
397  
398 2 my $err;
399 2 ▶ if ( !$@ )
Branch: 2 branches
BranchTrueFalseTotal
if (not $@) { } 2 0 2
elsif ($err = 'Exception::Class'->caught('Games::Solitaire::Verify::Exception::VerifyMove')) { } 0 0 0
400 {
401 # Do nothing - no exception was thrown.
402 }
403 elsif (
404 $err = Exception::Class->caught(
405 'Games::Solitaire::Verify::Exception::VerifyMove')
406 )
407 {
408 0 return { error => $err, line_num => $self->_ln(), };
409 }
410 else
411 {
412 0 $err = Exception::Class->caught();
413 0 ▶ ref $err ? $err->rethrow : die $err;
Branch: 1 branch
BranchTrueFalseTotal
ref $err ? : 0 0 0
414 }
415  
416 2 return ();
417 }
418  
419 5 1; # End of Games::Solitaire::Verify::Solution::ExpandMultiCardMoves
420  
421 __END__
« Verify/App/CmdLine/From_Patsolve.pm ↑ Summary Verify/Solution.pm »