File Coverage

blib/lib/Games/Solitaire/Verify/Foundations.pm
Criterion Covered Total %
statement 51 56 91.0
branch 4 6 66.6
condition n/a
subroutine 13 14 92.8
pod 5 5 100.0
total 73 81 90.1


line stmt bran cond sub pod time code
1             package Games::Solitaire::Verify::Foundations;
2             $Games::Solitaire::Verify::Foundations::VERSION = '0.2601';
3 19     19   368424 use warnings;
  19         38  
  19         1228  
4 19     19   101 use strict;
  19         31  
  19         508  
5              
6              
7 19     19   84 use parent 'Games::Solitaire::Verify::Base';
  19         45  
  19         119  
8              
9 19     19   1953 use Games::Solitaire::Verify::Exception ();
  19         42  
  19         363  
10 19     19   776 use Games::Solitaire::Verify::Card ();
  19         45  
  19         17384  
11              
12             # _s is the string.
13             __PACKAGE__->mk_acc_ref(
14             [
15             qw(
16             _num_decks
17             _founds
18             _s
19             )
20             ]
21             );
22              
23             # Suits sequence:
24             my @SS = ( @{ Games::Solitaire::Verify::Card->get_suits_seq() } );
25              
26             # Reverse.
27             my %RS = ( map { $SS[$_] => $_ } ( 0 .. $#SS ) );
28              
29             # Ranks
30             my @R = ( @{ Games::Solitaire::Verify::Card->get_ranks_strings() } );
31              
32              
33             sub _input_from_string
34             {
35 274     274   509 my $self = shift;
36 274         485 my $str = shift;
37              
38 274         595 my $rank_re = '[0A1-9TJQK]';
39              
40 274 50       4153 if ( $str !~
41             m{\AFoundations: H-($rank_re) C-($rank_re) D-($rank_re) S-($rank_re) *\z}ms
42             )
43             {
44 0         0 die "str=<$str>";
45 0         0 Games::Solitaire::Verify::Exception::Parse::State::Foundations->throw(
46             error => "Wrong Foundations", );
47             }
48             {
49 274         547 my @founds_strings = ( $1, $2, $3, $4 );
  274         1732  
50              
51 274         674 foreach my $suit (@SS)
52             {
53 1096         3080 $self->assign(
54             $suit, 0,
55             Games::Solitaire::Verify::Card->calc_rank_with_0(
56             shift(@founds_strings)
57             )
58             );
59             }
60             }
61             }
62              
63             sub _init
64             {
65 281     281   637 my ( $self, $args ) = @_;
66              
67 281 50       776 if ( !exists( $args->{num_decks} ) )
68             {
69 0         0 die "No number of decks were specified";
70             }
71              
72 281         848 $self->_num_decks( $args->{num_decks} );
73              
74 281         728 $self->_founds( +{ map { $_ => [ (0) x $self->_num_decks() ], } @SS } );
  1124         3920  
75              
76 281         1057 $self->_s( $self->_init_s );
77              
78 281 100       948 if ( exists( $args->{string} ) )
79             {
80 274         747 $self->_input_from_string( $args->{string} );
81             }
82              
83 281         693 return;
84             }
85              
86              
87             sub value
88             {
89 2018     2018 1 3836 my ( $self, $suit, $idx ) = @_;
90              
91 2018         7820 return $self->_founds()->{$suit}->[$idx];
92             }
93              
94              
95             sub assign
96             {
97 1124     1124 1 2421 my ( $self, $suit, $idx, $rank ) = @_;
98              
99 1124         2644 $self->_founds()->{$suit}->[$idx] = $rank;
100              
101             # Replace the rank in place.
102 1124         2940 substr( $self->{_s}, ( length('Foundations:') + 3 ) + ( $RS{$suit} << 2 ),
103             1, $R[$rank] );
104 1124         2755 return;
105             }
106              
107              
108             sub increment
109             {
110 833     833 1 1687 my ( $self, $suit, $idx ) = @_;
111              
112             substr(
113             $self->{_s}, ( length('Foundations:') + 3 ) + ( $RS{$suit} << 2 ),
114 833         3206 1, $R[ ++( $self->_founds()->{$suit}->[$idx] ) ]
115             );
116              
117 833         1558 return;
118             }
119              
120              
121             sub _foundations_strings
122             {
123 0     0   0 my $self = shift;
124              
125 0         0 return [];
126             }
127              
128             sub to_string
129             {
130 2226     2226 1 6271 return $_[0]->_s;
131             }
132              
133             sub _init_s
134             {
135 281     281   521 my $S = shift;
136              
137             return "Foundations:"
138 281         587 . join( "", map { " $_-" . $R[ $S->value( $_, 0 ) ] } @SS );
  1124         2642  
139             }
140              
141              
142             sub clone
143             {
144 7     7 1 16 my $self = shift;
145              
146 7         42 my $copy = __PACKAGE__->new(
147             {
148             num_decks => $self->_num_decks(),
149             }
150             );
151              
152 7         24 foreach my $suit (@SS)
153             {
154 28         109 foreach my $deck_idx ( 0 .. ( $self->_num_decks() - 1 ) )
155             {
156 28         68 $copy->assign( $suit, $deck_idx,
157             $self->value( $suit, $deck_idx ), );
158             }
159             }
160              
161 7         32 return $copy;
162             }
163              
164             1; # End of Games::Solitaire::Verify::Move
165              
166             __END__