blib/lib/Games/Solitaire/Verify/State.pm

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