Verify/State.pm

filter: Statementstmt 96.0% 290 / 302 Branchbran 87.2% 75 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 98.1% 54 / 55 Podpod 100% 18 / 18 totaltotal 94.7% 437 / 461 CC 54 SCAR 11.8
CC
54
Cov
94%
verify_contents
8.6
_mv_stack_seq_to_foundation
6.1
_from_string
5.2
CRAP 3.2
« Verify/FromOtherSolversBase.pm ↑ Summary Verify/Freecells.pm »
1 package Games::Solitaire::Verify::State;
2 18 $Games::Solitaire::Verify::State::VERSION = '0.2601';
3 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 18 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 18 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 18 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 18 ▶use Games::Solitaire::Verify::Column ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 18 ▶use Games::Solitaire::Verify::Move ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 18 ▶use Games::Solitaire::Verify::Freecells ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 18 ▶use Games::Solitaire::Verify::Foundations ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 18 ▶use Games::Solitaire::Verify::VariantParams ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶use Games::Solitaire::Verify::VariantsMap ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use POSIX qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 __PACKAGE__->mk_acc_ref(
22 [
23 qw(
24 _columns
25 _freecells
26 _foundations
27 _variant
28 _variant_params
29 _temp_move
30 )
31 ]
32 );
33  
34  
35 sub set_freecells
36 274 ▶{
Subroutine set_freecells: called CC=1 SCAR=0.0
Pod documented
37 274 my ( $self, $freecells ) = @_;
38  
39 274 $self->_freecells($freecells);
40  
41 274 return ();
42 }
43  
44 sub _assign_freecells_from_string
45 272 ▶{
Subroutine _assign_freecells_from_string: called CC=1 SCAR=0.0
46 272 my $self = shift;
47 272 my $string = shift;
48  
49 272 $self->set_freecells(
50 Games::Solitaire::Verify::Freecells->new(
51 {
52 count => $self->num_freecells(),
53 string => $string,
54 }
55 )
56 );
57  
58 272 return ();
59 }
60  
61  
62 sub add_column
63 2246 ▶{
Subroutine add_column: called CC=1 SCAR=0.0
Pod documented
64 2246 my ( $self, $col ) = @_;
65  
66 2246 push @{ $self->_columns() }, $col;
67  
68 2246 return ();
69 }
70  
71  
72 sub set_foundations
73 281 ▶{
Subroutine set_foundations: called CC=1 SCAR=0.0
Pod documented
74 281 my ( $self, $foundations ) = @_;
75  
76 281 $self->_foundations($foundations);
77  
78 281 return ();
79 }
80  
81 sub _get_suits_seq
82 0 ▶{
Subroutine _get_suits_seq: not called CC=1 SCAR=6.9
83 0 my $class = shift;
84  
85 0 return Games::Solitaire::Verify::Card->get_suits_seq();
86 }
87  
88 sub _from_string
89 266 ▶{
Subroutine _from_string: called CC=5 SCAR=16.4
90 266 my ( $self, $str ) = @_;
91  
92 266 my $rank_re = '[0A1-9TJQK]';
93  
94 266 ▶ if ( $str !~ m{\A(Foundations:[^\n]*)\n}gms )
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\A(Foundations:[^\n]*)\n/gms) 0 266 266
95 {
96 0 Games::Solitaire::Verify::Exception::Parse::State::Foundations->throw(
97 error => "Wrong Foundations", );
98 }
99 266 my $founds_s = $1;
100  
101 266 $self->set_foundations(
102 Games::Solitaire::Verify::Foundations->new(
103 {
104 num_decks => $self->num_decks(),
105 string => $founds_s,
106 }
107 )
108 );
109  
110 266 ▶ if ( $str !~ m{\G(Freecells:[^\n]*)\n}gms )
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\G(Freecells:[^\n]*)\n/gms) 0 266 266
111 {
112 0 Games::Solitaire::Verify::Exception::Parse::State::Freecells->throw(
113 error => "Wrong Freecell String", );
114 }
115 266 $self->_assign_freecells_from_string($1);
116  
117 266 foreach my $col_idx ( 0 .. ( $self->num_columns() - 1 ) )
118 {
119 2128 ▶ if ( $str !~ m{\G(:[^\n]*)\n}msg )
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\G(:[^\n]*)\n/gms) 2 2126 2128
120 {
121 2 Games::Solitaire::Verify::Exception::Parse::State::Column->throw(
122 error => "Cannot parse column",
123 index => $col_idx,
124 );
125 }
126 2126 my $column_str = $1;
127  
128 2126 $self->add_column(
129 Games::Solitaire::Verify::Column->new(
130 {
131 string => $column_str,
132 }
133 )
134 );
135 }
136  
137 264 return ();
138 }
139  
140 sub _fill_non_custom_variant
141 272 ▶{
Subroutine _fill_non_custom_variant: called CC=2 SCAR=7.1
142 272 my $self = shift;
143 272 my $variant = shift;
144  
145 272 my $variants_map = Games::Solitaire::Verify::VariantsMap->new();
146  
147 272 my $params = $variants_map->get_variant_by_id($variant);
148  
149 272 ▶ if ( !defined($params) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params) 0 272 272
150 {
151 0 Games::Solitaire::Verify::Exception::Variant::Unknown->throw(
152 error => "Unknown/Unsupported Variant",
153 variant => $variant,
154 );
155 }
156 272 $self->_variant_params($params);
157 272 $self->_variant($variant);
158  
159 272 return ();
160 }
161  
162 sub _set_variant
163 281 ▶{
Subroutine _set_variant: called CC=2 SCAR=6.9
164 281 my $self = shift;
165 281 my $args = shift;
166  
167 281 my $variant = $args->{variant};
168  
169 281 ▶ if ( $variant eq "custom" )
Branch: 1 branch
BranchTrueFalseTotal
if ($variant eq 'custom') { } 9 272 281
170 {
171 9 $self->_variant($variant);
172 9 $self->_variant_params( $args->{variant_params} );
173 }
174 else
175 {
176 272 $self->_fill_non_custom_variant($variant);
177 }
178  
179 281 return ();
180 }
181  
182 sub _init
183 281 ▶{
Subroutine _init: called CC=2 SCAR=6.9
184 281 my ( $self, $args ) = @_;
185  
186 # Set the variant
187 281 $self->_set_variant($args);
188  
189 281 $self->_columns( [] );
190  
191 281 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 272 9 281
192 {
193 272 return $self->_from_string( $args->{string} );
194 }
195 9 return ();
196 }
197  
198  
199 sub get_freecell
200 1939 ▶{
Subroutine get_freecell: called CC=1 SCAR=0.0
Pod documented
201 1939 my ( $self, $index ) = @_;
202  
203 1939 return $self->_freecells()->cell($index);
204 }
205  
206  
207 sub set_freecell
208 590 ▶{
Subroutine set_freecell: called CC=1 SCAR=0.0
Pod documented
209 590 my ( $self, $index, $card ) = @_;
210  
211 590 return $self->_freecells->assign( $index, $card );
212 }
213  
214  
215 sub get_foundation_value
216 866 ▶{
Subroutine get_foundation_value: called CC=1 SCAR=0.0
Pod documented
217 866 my ( $self, $suit, $idx ) = @_;
218  
219 866 return $self->_foundations()->value( $suit, $idx );
220 }
221  
222  
223 sub increment_foundation_value
224 833 ▶{
Subroutine increment_foundation_value: called CC=1 SCAR=0.0
Pod documented
225 833 my ( $self, $suit, $idx ) = @_;
226  
227 833 $self->_foundations()->increment( $suit, $idx );
228  
229 833 return ();
230 }
231  
232  
233 sub num_decks
234 1066 ▶{
Subroutine num_decks: called CC=1 SCAR=0.0
Pod documented
235 1066 my $self = shift;
236  
237 1066 return $self->_variant_params->num_decks();
238 }
239  
240  
241 sub num_freecells
242 487 ▶{
Subroutine num_freecells: called CC=1 SCAR=0.0
Pod documented
243 487 my $self = shift;
244  
245 487 return $self->_variant_params->num_freecells();
246 }
247  
248  
249 sub num_empty_freecells
250 489 ▶{
Subroutine num_empty_freecells: called CC=1 SCAR=0.0
Pod documented
251 489 my $self = shift;
252  
253 489 return $self->_freecells->num_empty();
254 }
255  
256  
257 sub num_columns
258 1168 ▶{
Subroutine num_columns: called CC=1 SCAR=0.0
Pod documented
259 1168 my $self = shift;
260  
261 1168 return $self->_variant_params->num_columns();
262 }
263  
264  
265 sub get_column
266 13203 ▶{
Subroutine get_column: called CC=1 SCAR=0.0
Pod documented
267 13203 my $self = shift;
268 13203 my $index = shift;
269  
270 13203 return $self->_columns->[$index];
271 }
272  
273  
274 sub num_empty_columns
275 452 ▶{
Subroutine num_empty_columns: called CC=3 SCAR=11.0
Pod documented
276 452 my $self = shift;
277  
278 452 my $count = 0;
279  
280 452 foreach my $idx ( 0 .. ( $self->num_columns() - 1 ) )
281 {
282 3776 ▶ if ( !$self->get_column($idx)->len() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_column($idx)->len) 328 3448 3776
283 {
284 328 ++$count;
285 }
286 }
287 452 return $count;
288 }
289  
290  
291 sub clone
292 7 ▶{
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
293 7 my $self = shift;
294  
295 7 my $variant = $self->_variant;
296 7 ▶ my $copy = Games::Solitaire::Verify::State->new(
Branch: 1 branch
BranchTrueFalseTotal
$variant eq 'custom' ? : 1 6 7
297 {
298 variant => $variant,
299 (
300 ( $variant eq "custom" )
301 ? ( variant_params => $self->_variant_params() )
302 : ()
303 ),
304 }
305 );
306  
307 7 foreach my $idx ( 0 .. ( $self->num_columns() - 1 ) )
308 {
309 56 $copy->add_column( $self->get_column($idx)->clone() );
310 }
311  
312 7 $copy->set_foundations( $self->_foundations()->clone() );
313  
314 7 $copy->_freecells( $self->_freecells()->clone() );
315  
316 7 return $copy;
317 }
318  
319  
320 # Returns 0 on success, else the error.
321 sub _can_put_into_empty_column
322 225 ▶{
Subroutine _can_put_into_empty_column: called CC=3 SCAR=11.0
323 225 my ( $self, $card ) = @_;
324  
325 225 ▶ if ( $self->_variant_params->empty_stacks_filled_by() eq "kings" )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_variant_params->empty_stacks_filled_by eq "kings") 13 212 225
326 {
327 13 ▶ if ( $card->rank() != 13 )
Branch: 1 branch
BranchTrueFalseTotal
if ($card->rank != 13) 2 11 13
328 {
329 return
330 2 Games::Solitaire::Verify::Exception::Move::Dest::Col::OnlyKingsCanFillEmpty
331 ->new(
332 error => "Non-king on an empty stack",
333 move => $self->_temp_move(),
334 );
335 }
336 }
337 223 return 0;
338 }
339  
340 sub _is_matching_color
341 1422 ▶{
Subroutine _is_matching_color: called CC=5 SCAR=16.1
342 1422 my ( $self, $parent, $child ) = @_;
343  
344 1422 my $rules = $self->_variant_params()->rules();
345 1422 my $sbb = $self->_variant_params()->seq_build_by();
346  
347 1422 ▶ my $verdict = (
Branch: 3 branches
BranchTrueFalseTotal
$rules eq 'simple_simon' ? : 317 1105 1422
$sbb eq 'alt_color' ? : 977 128 1105
$sbb eq 'suit' ? : 128 0 128
348 ( $rules eq "simple_simon" ) ? 0
349 : ( $sbb eq "alt_color" ) ? ( $parent->color() eq $child->color() )
350 : ( $sbb eq "suit" ) ? ( $parent->suit() ne $child->suit() )
351 : 0
352 );
353  
354 1422 ▶ if ($verdict)
Branch: 1 branch
BranchTrueFalseTotal
if ($verdict) 3 1419 1422
355 {
356 return
357 3 Games::Solitaire::Verify::Exception::Move::Dest::Col::NonMatchSuits
358 ->new(
359 seq_build_by => $sbb,
360 move => $self->_temp_move(),
361 );
362 }
363  
364 1419 return 0;
365 }
366  
367 sub _can_put_on_top
368 1425 ▶{
Subroutine _can_put_on_top: called CC=3 SCAR=11.0
369 1425 my ( $self, $parent, $child ) = @_;
370  
371 1425 ▶ if ( $parent->rank() != $child->rank() + 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->rank != $child->rank + 1) 3 1422 1425
372 {
373 return
374 3 Games::Solitaire::Verify::Exception::Move::Dest::Col::RankMismatch
375 ->new(
376 error => "Rank mismatch between parent and child cards",
377 move => $self->_temp_move(),
378 );
379 }
380  
381 1422 ▶ if ( my $ret = $self->_is_matching_color( $parent, $child ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $ret = $self->_is_matching_color($parent, $child)) 3 1419 1422
382 {
383 3 return $ret;
384 }
385  
386 1419 return 0;
387 }
388  
389 sub _can_put_on_column
390 1088 ▶{
Subroutine _can_put_on_column: called CC=2 SCAR=6.9
391 1088 my ( $self, $col_idx, $card ) = @_;
392  
393 return (
394 1088 ▶ ( $self->get_column($col_idx)->len() == 0 )
Branch: 1 branch
BranchTrueFalseTotal
$self->get_column($col_idx)->len == 0 ? : 225 863 1088
395 ? $self->_can_put_into_empty_column($card)
396 : $self->_can_put_on_top( $self->get_column($col_idx)->top(), $card )
397 );
398 }
399  
400 sub _calc_freecell_max_seq_move
401 451 ▶{
Subroutine _calc_freecell_max_seq_move: called CC=2 SCAR=7.1
402 451 my ( $self, $args ) = @_;
403 451 ▶ my $to_empty = ( defined( $args->{to_empty} ) ? $args->{to_empty} : 0 );
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{'to_empty'} ? : 451 0 451
404  
405 451 return ( ( $self->num_empty_freecells() + 1 )
406 << ( $self->num_empty_columns() - $to_empty ) );
407 }
408  
409 sub _calc_empty_stacks_filled_by_any_card_max_seq_move
410 451 ▶{
Subroutine _calc_empty_stacks_filled_by_any_card_max_seq_move: called CC=1 SCAR=0.0
411 451 my ( $self, $args ) = @_;
412  
413 451 return $self->_calc_freecell_max_seq_move($args);
414 }
415  
416 sub _calc_max_sequence_move
417 615 ▶{
Subroutine _calc_max_sequence_move: called CC=3 SCAR=11.0
418 615 my ( $self, $args ) = @_;
419  
420 615 ▶ return +( $self->_variant_params->sequence_move() eq "unlimited" )
Branch: 2 branches
BranchTrueFalseTotal
$self->_variant_params->sequence_move eq 'unlimited' ? : 127 488 615
$self->_variant_params->empty_stacks_filled_by eq 'any' ? : 451 37 488
421 ? POSIX::INT_MAX()
422 : ( $self->_variant_params->empty_stacks_filled_by() eq "any" )
423 ? $self->_calc_empty_stacks_filled_by_any_card_max_seq_move($args)
424 : ( $self->num_empty_freecells() + 1 );
425 }
426  
427 sub _is_sequence_in_column
428 625 ▶{
Subroutine _is_sequence_in_column: called CC=5 SCAR=16.1
429 625 my ( $self, $source_idx, $num_cards, $num_seq_components_ref ) = @_;
430  
431 625 my $col = $self->get_column($source_idx);
432 625 my $len = $col->len();
433  
434 625 my $rules = $self->_variant_params()->rules();
435  
436 625 my $num_comps = 1;
437  
438 625 foreach my $card_idx ( 0 .. ( $num_cards - 2 ) )
439 {
440 562 my $parent = $col->pos( $len - 1 - $card_idx - 1 );
441 562 my $child = $col->pos( $len - 1 - $card_idx );
442  
443 562 ▶ if ( $self->_can_put_on_top( $parent, $child ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can_put_on_top($parent, $child)) 1 561 562
444 {
445 return
446 1 Games::Solitaire::Verify::Exception::Move::Src::Col::NonSequence
447 ->new(
448 move => $self->_temp_move(),
449 pos => $card_idx,
450 );
451 }
452  
453 $num_comps += (
454 561 ▶ ( $rules eq "simple_simon" )
Branch: 2 branches
BranchTrueFalseTotal
$rules eq 'simple_simon' ? : 254 307 561
$parent->suit ne $child->suit ? : 14 240 254
455 ? ( ( $parent->suit() ne $child->suit() ) ? 1 : 0 )
456 : 1
457 );
458 }
459  
460 624 ${$num_seq_components_ref} = $num_comps;
461  
462 624 return 0;
463 }
464  
465  
466 sub clear_freecell
467 578 ▶{
Subroutine clear_freecell: called CC=1 SCAR=0.0
Pod documented
468 578 my ( $self, $index ) = @_;
469  
470 578 return $self->_freecells->clear($index);
471 }
472  
473 sub verify_and_perform_move
474 2458 ▶{
Subroutine verify_and_perform_move: called CC=2 SCAR=7.1
Pod documented
475 2458 my ( $self, $move ) = @_;
476  
477 2458 ▶ if ( my $method =
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $self->can('_mv_' . $move->source_type . '_to_' . $move->dest_type)) { } 2458 0 2458
478 $self->can( "_mv_" . $move->source_type . "_to_" . $move->dest_type ) )
479 {
480 2458 $self->_temp_move($move);
481 2458 my $ret = $method->($self);
482 2458 $self->_temp_move( undef() );
483 2458 return $ret;
484 }
485 else
486 {
487 0 die "Cannot handle this move type";
488 }
489 }
490  
491 sub _mv_stack_to_foundation
492 657 ▶{
Subroutine _mv_stack_to_foundation: called CC=2 SCAR=6.9
493 657 my $self = shift;
494  
495 657 my $move = $self->_temp_move();
496  
497 657 my $col_idx = $move->source();
498 657 my $card = $self->get_column($col_idx)->top();
499  
500 657 my $rank = $card->rank();
501 657 my $suit = $card->suit();
502  
503 657 ▶ my $f_idx = first { $self->get_foundation_value( $suit, $_ ) == $rank - 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
504 657 ( 0 .. ( $self->num_decks() - 1 ) );
505  
506 657 ▶ if ( defined($f_idx) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f_idx) { } 656 1 657
507 {
508 656 $self->get_column($col_idx)->pop();
509 656 $self->increment_foundation_value( $suit, $f_idx );
510 656 return 0;
511 }
512 else
513 {
514 return
515 1 Games::Solitaire::Verify::Exception::Move::Dest::Foundation->new(
516 move => $move );
517 }
518 }
519  
520 sub _mv_stack_seq_to_foundation
521 7 ▶{
Subroutine _mv_stack_seq_to_foundation: called CC=6 SCAR=18.1
522 7 my $self = shift;
523  
524 7 my $move = $self->_temp_move();
525  
526 7 my $rules = $self->_variant_params()->rules();
527  
528 7 ▶ if ( $rules ne "simple_simon" )
Branch: 1 branch
BranchTrueFalseTotal
if ($rules ne "simple_simon") 1 6 7
529 {
530 return
531 1 Games::Solitaire::Verify::Exception::Move::Variant::Unsupported
532 ->new( move => $move );
533 }
534  
535 6 my $col_idx = $move->source();
536  
537 6 my $num_seq_components;
538 6 my $verdict =
539 $self->_is_sequence_in_column( $col_idx, 13, \$num_seq_components, );
540  
541 6 ▶ if ($verdict)
Branch: 1 branch
BranchTrueFalseTotal
if ($verdict) 0 6 6
542 {
543 0 return $verdict;
544 }
545  
546 6 ▶ if ( $num_seq_components != 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($num_seq_components != 1) 1 5 6
547 {
548 return
549 1 Games::Solitaire::Verify::Exception::Move::Src::Col::NotTrueSeq
550 ->new( move => $move );
551 }
552  
553 5 my $card = $self->get_column($col_idx)->top();
554  
555 5 my $suit = $card->suit();
556  
557 5 ▶ my $f_idx = first { $self->get_foundation_value( $suit, $_ ) == 0 }
Subroutine __ANON__: called CC=1 SCAR=0.0
558 5 ( 0 .. ( $self->num_decks() - 1 ) );
559  
560 5 ▶ if ( defined($f_idx) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f_idx) { } 5 0 5
561 {
562 5 foreach my $card_idx ( 1 .. 13 )
563 {
564 65 $self->get_column($col_idx)->pop();
565 65 $self->increment_foundation_value( $suit, $f_idx );
566 }
567 5 return 0;
568 }
569 else
570 {
571 return
572 0 Games::Solitaire::Verify::Exception::Move::Dest::Foundation->new(
573 move => $move );
574 }
575 }
576  
577 sub _mv_stack_to_freecell
578 589 ▶{
Subroutine _mv_stack_to_freecell: called CC=3 SCAR=11.0
579 589 my $self = shift;
580 589 my $move = $self->_temp_move();
581  
582 589 my $col_idx = $move->source();
583 589 my $fc_idx = $move->dest();
584  
585 589 ▶ if ( !$self->get_column($col_idx)->len() )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->get_column($col_idx)->len) 1 588 589
586 {
587 return
588 1 Games::Solitaire::Verify::Exception::Move::Src::Col::NoCards->new(
589 move => $move, );
590 }
591  
592 588 ▶ if ( defined( $self->get_freecell($fc_idx) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->get_freecell($fc_idx)) 1 587 588
593 {
594 1 return Games::Solitaire::Verify::Exception::Move::Dest::Freecell->new(
595 move => $move, );
596 }
597  
598 587 $self->set_freecell( $fc_idx, $self->get_column($col_idx)->pop() );
599  
600 587 return 0;
601 }
602  
603 sub _mv_stack_to_stack
604 620 ▶{
Subroutine _mv_stack_to_stack: called CC=5 SCAR=16.1
605 620 my $self = shift;
606 620 my $move = $self->_temp_move();
607  
608 620 my $source = $move->source();
609 620 my $dest = $move->dest();
610 620 my $num_cards = $move->num_cards();
611  
612 # Source column
613 620 my $sc = $self->get_column($source);
614 620 my $dc = $self->get_column($dest);
615  
616 620 my $source_len = $sc->len();
617  
618 620 ▶ if ( $source_len < $num_cards )
Branch: 1 branch
BranchTrueFalseTotal
if ($source_len < $num_cards) 1 619 620
619 {
620 return
621 1 Games::Solitaire::Verify::Exception::Move::Src::Col::NotEnoughCards
622 ->new( move => $move, );
623 }
624  
625 619 my $num_seq_components;
626 619 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $verdict = $self->_is_sequence_in_column($source, $num_cards, \$num_seq_components)) 1 618 619
627 my $verdict = $self->_is_sequence_in_column(
628 $source, $num_cards, \$num_seq_components,
629 )
630 )
631 {
632 1 return $verdict;
633 }
634  
635 618 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (my $verdict = $self->_can_put_on_column($dest, $sc->pos($source_len - $num_cards))) 3 615 618
636 my $verdict = $self->_can_put_on_column(
637 $dest, $sc->pos( $source_len - $num_cards )
638 )
639 )
640 {
641 3 return $verdict;
642 }
643  
644 # Now let's see if we have enough resources
645 # to move the cards.
646  
647 615 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($num_seq_components > $self->_calc_max_sequence_move({"to_empty", $dc->len == 0})) 3 612 615
648 $num_seq_components > $self->_calc_max_sequence_move(
649 {
650 to_empty => ( $dc->len() == 0 ),
651 }
652 )
653 )
654 {
655 return
656 3 Games::Solitaire::Verify::Exception::Move::NotEnoughEmpties->new(
657 move => $move, );
658 }
659  
660 # Now let's actually move them.
661 612 $dc->append_cards( $sc->popN($num_cards) );
662  
663 612 return 0;
664 }
665  
666 sub _mv_freecell_to_foundation
667 114 ▶{
Subroutine _mv_freecell_to_foundation: called CC=3 SCAR=11.0
668 114 my $self = shift;
669 114 my $move = $self->_temp_move();
670  
671 114 my $fc_idx = $move->source();
672 114 my $card = $self->get_freecell($fc_idx);
673  
674 114 ▶ if ( !defined($card) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card) 1 113 114
675 {
676 return
677 1 Games::Solitaire::Verify::Exception::Move::Src::Freecell::Empty
678 ->new( move => $move, );
679 }
680  
681 113 my $rank = $card->rank();
682 113 my $suit = $card->suit();
683  
684 113 ▶ my $f_idx = first { $self->get_foundation_value( $suit, $_ ) == $rank - 1 }
Subroutine __ANON__: called CC=1 SCAR=0.0
685 113 ( 0 .. ( $self->num_decks() - 1 ) );
686  
687 113 ▶ if ( defined($f_idx) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $f_idx) { } 112 1 113
688 {
689 112 $self->clear_freecell($fc_idx);
690 112 $self->increment_foundation_value( $suit, $f_idx );
691 112 return 0;
692 }
693 else
694 {
695 return
696 1 Games::Solitaire::Verify::Exception::Move::Dest::Foundation->new(
697 move => $move );
698 }
699 }
700  
701 sub _mv_freecell_to_stack
702 471 ▶{
Subroutine _mv_freecell_to_stack: called CC=3 SCAR=11.0
703 471 my $self = shift;
704 471 my $move = $self->_temp_move();
705  
706 471 my $fc_idx = $move->source();
707 471 my $col_idx = $move->dest();
708  
709 471 my $card = $self->get_freecell($fc_idx);
710  
711 471 ▶ if ( !defined($card) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $card) 1 470 471
712 {
713 return
714 1 Games::Solitaire::Verify::Exception::Move::Src::Freecell::Empty
715 ->new( move => $move, );
716 }
717  
718 470 ▶ if ( my $verdict = $self->_can_put_on_column( $col_idx, $card ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $verdict = $self->_can_put_on_column($col_idx, $card)) 4 466 470
719 {
720 4 return $verdict;
721 }
722  
723 466 $self->get_column($col_idx)->push($card);
724 466 $self->clear_freecell($fc_idx);
725  
726 466 return 0;
727 }
728  
729  
730 18 my @SS = ( @{ Games::Solitaire::Verify::Card->get_suits_seq() } );
731  
732 sub verify_contents
733 16 ▶{
Subroutine verify_contents: called CC=8 SCAR=21.5
Pod documented
734 16 my ( $self, $args ) = @_;
735  
736 16 my $MAX_RANK = $args->{max_rank};
737 16 my $found = {};
738 832 ▶ my $register = sub {
Subroutine __ANON__: called CC=3 SCAR=12.3
739 832 my $card = shift;
740 832 ▶ if ( $card->rank > $MAX_RANK )
Branch: 1 branch
BranchTrueFalseTotal
if ($card->rank > $MAX_RANK) 0 832 832
741 {
742 0 die Games::Solitaire::Verify::Exception::State::TooHighRank->new(
743 cards => [$card], );
744 }
745 832 my $s = $card->fast_s;
746 832 ▶ if ( ( ++$found->{$s} ) > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (++$found->{$s} > 1) 0 832 832
747 {
748 0 die Games::Solitaire::Verify::Exception::State::ExtraCards->new(
749 cards => [$card], );
750 }
751 832 return ();
752 16 };
753 16 for my $fc ( 0 .. $self->num_freecells - 1 )
754 {
755 64 my $card = $self->get_freecell($fc);
756 64 ▶ if ( defined $card )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $card) 14 50 64
757 {
758 14 $register->($card);
759 }
760 }
761 16 foreach my $suit (@SS)
762 {
763 64 for my $rank ( 1 .. $self->get_foundation_value( $suit, 0 ) )
764 {
765 0 $register->(
766 Games::Solitaire::Verify::Card->new(
767 {
768 string => (
769 Games::Solitaire::Verify::Card->rank_to_string(
770 $rank)
771 . $suit
772 )
773 }
774 ),
775 );
776 }
777 }
778  
779 16 foreach my $idx ( 0 .. ( $self->num_columns() - 1 ) )
780 {
781 132 my $col = $self->get_column($idx);
782 132 for my $pos ( 0 .. $col->len - 1 )
783 {
784 818 $register->( $col->pos($pos) );
785 }
786 }
787  
788 16 ▶ if ( scalar( keys %$found ) != $MAX_RANK * 4 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %$found != $MAX_RANK * 4) 0 16 16
789 {
790 0 die Games::Solitaire::Verify::Exception::State::MissingCards->new(
791 cards => [], );
792 }
793  
794 16 return ();
795 }
796  
797  
798 sub to_string
799 2226 ▶{
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
800 2226 my $self = shift;
801  
802 return join(
803 "\n",
804 (
805 22606 map { $_->to_string() } $self->_foundations(),
806 $self->_freecells(),
807 2226 @{ $self->_columns() }
808 ),
809 ""
810 );
811 }
812  
813 18 1; # End of Games::Solitaire::Verify::State
814  
815 __END__
« Verify/FromOtherSolversBase.pm ↑ Summary Verify/Freecells.pm »