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

filter: Statementstmt 92.6% 63 / 68 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 100% 5 / 5 totaltotal 91.4% 86 / 94 CC 7 SCAR 6.6
CC
7
Cov
91%
_input_from_string
3.1
_init
3.0
clone
3.0
CRAP 1.9
« blib/lib/Games/Solitaire/Verify/Card.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Solution/Base.pm »
1 package Games::Solitaire::Verify::Foundations;
2 16 $Games::Solitaire::Verify::Foundations::VERSION = '0.2301';
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 16 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 16 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Games::Solitaire::Verify::Card ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 16 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # _s is the string.
15 16 __PACKAGE__->mk_acc_ref(
16 [
17 qw(
18 _num_decks
19 _founds
20 _s
21 )
22 ]
23 );
24  
25 # Suits sequence:
26 16 my @SS = ( @{ Games::Solitaire::Verify::Card->get_suits_seq() } );
27  
28 # Reverse.
29 16 my %RS = ( map { $SS[$_] => $_ } ( 0 .. $#SS ) );
30  
31 # Ranks
32 16 my @R = ( @{ Games::Solitaire::Verify::Card->get_ranks_strings() } );
33  
34  
35 sub _input_from_string
36 {
37 272 ▶ my $self = shift;
Subroutine _input_from_string: called CC=3 SCAR=11.4
38 272 my $str = shift;
39  
40 272 my $rank_re = '[0A1-9TJQK]';
41  
42 272 ▶ if ( $str !~
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\AFoundations: H-($rank_re) C-($rank_re) D-($rank_re) S-($rank_re) *\z/ms) 0 272 272
43 m{\AFoundations: H-($rank_re) C-($rank_re) D-($rank_re) S-($rank_re) *\z}ms
44 )
45 {
46 0 die "str=<$str>";
47 0 Games::Solitaire::Verify::Exception::Parse::State::Foundations->throw(
48 error => "Wrong Foundations", );
49 }
50 {
51 272 my @founds_strings = ( $1, $2, $3, $4 );
52  
53 272 foreach my $suit (@SS)
54 {
55 1088 $self->assign(
56 $suit, 0,
57 Games::Solitaire::Verify::Card->calc_rank_with_0(
58 shift(@founds_strings)
59 )
60 );
61 }
62 }
63 }
64  
65 sub _init
66 {
67 279 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=3 SCAR=11.1
68  
69 279 ▶ if ( !exists( $args->{num_decks} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args->{"num_decks"}) 0 279 279
70 {
71 0 die "No number of decks were specified";
72 }
73  
74 279 $self->_num_decks( $args->{num_decks} );
75  
76 279 $self->_founds( +{ map { $_ => [ (0) x $self->_num_decks() ], } @SS } );
77  
78 279 $self->_s( $self->_init_s );
79  
80 279 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 272 7 279
81 {
82 272 $self->_input_from_string( $args->{string} );
83 }
84  
85 279 return;
86 }
87  
88  
89 sub value
90 {
91 1906 ▶ my ( $self, $suit, $idx ) = @_;
Subroutine value: called CC=1 SCAR=0.0
Pod documented
92  
93 1906 return $self->_founds()->{$suit}->[$idx];
94 }
95  
96  
97 sub assign
98 {
99 1116 ▶ my ( $self, $suit, $idx, $rank ) = @_;
Subroutine assign: called CC=1 SCAR=0.0
Pod documented
100  
101 1116 $self->_founds()->{$suit}->[$idx] = $rank;
102  
103 # Replace the rank in place.
104 1116 substr( $self->{_s}, ( length('Foundations:') + 3 ) + ( $RS{$suit} << 2 ),
105 1, $R[$rank] );
106 1116 return;
107 }
108  
109  
110 sub increment
111 {
112 729 ▶ my ( $self, $suit, $idx ) = @_;
Subroutine increment: called CC=1 SCAR=0.0
Pod documented
113  
114 substr(
115 $self->{_s}, ( length('Foundations:') + 3 ) + ( $RS{$suit} << 2 ),
116 729 1, $R[ ++( $self->_founds()->{$suit}->[$idx] ) ]
117 );
118  
119 729 return;
120 }
121  
122  
123 sub _foundations_strings
124 {
125 0 ▶ my $self = shift;
Subroutine _foundations_strings: not called CC=1 SCAR=6.9
126  
127 0 return [];
128 }
129  
130 sub to_string
131 {
132 2055 ▶ return $_[0]->_s;
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
133 }
134  
135 sub _init_s
136 {
137 279 ▶ my $S = shift;
Subroutine _init_s: called CC=1 SCAR=0.0
138  
139 return "Foundations:"
140 279 . join( "", map { " $_-" . $R[ $S->value( $_, 0 ) ] } @SS );
141 }
142  
143  
144 sub clone
145 {
146 7 ▶ my $self = shift;
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
147  
148 7 my $copy = __PACKAGE__->new(
149 {
150 num_decks => $self->_num_decks(),
151 }
152 );
153  
154 7 foreach my $suit (@SS)
155 {
156 28 foreach my $deck_idx ( 0 .. ( $self->_num_decks() - 1 ) )
157 {
158 28 $copy->assign( $suit, $deck_idx,
159 $self->value( $suit, $deck_idx ), );
160 }
161 }
162  
163 7 return $copy;
164 }
165  
166 16 1; # End of Games::Solitaire::Verify::Move
167  
168 __END__
« blib/lib/Games/Solitaire/Verify/Card.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Solution/Base.pm »