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

filter: Statementstmt 100% 91 / 91 Branchbran 100% 20 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 21 / 21 Podpod 100% 12 / 12 totaltotal 100% 144 / 144 CC 11 SCAR 8.0
CC
11
Cov
100%
_from_string
5.0
_init
4.0
_card_num_normalize
2.0
CRAP 2.2
« blib/lib/Games/Solitaire/Verify/Freecells.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Foundations.pm »
1 package Games::Solitaire::Verify::Card;
2 25 $Games::Solitaire::Verify::Card::VERSION = '0.2301';
3 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 25 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 25 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 25 __PACKAGE__->mk_acc_ref(
12 [
13 qw(
14 _flipped
15 _s
16 data
17 id
18 rank
19 suit
20 _game
21 )
22 ]
23 );
24  
25  
26 sub _recalc
27 {
28 22902 ▶ my ($self) = @_;
Subroutine _recalc: called CC=1 SCAR=0.0
29  
30 22902 $self->_s( $self->to_string() );
31  
32 22902 return;
33 }
34  
35 sub _card_num_normalize
36 {
37 325 ▶ my $arg = shift;
Subroutine _card_num_normalize: called CC=2 SCAR=6.9
38  
39 325 ▶ if ( ref($arg) eq "" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq '') { } 300 25 325
40 {
41 300 return +{ map { $_ => $arg } (qw(t non_t)) };
42 }
43 else
44 {
45 25 return $arg;
46 }
47 }
48  
49 my @card_nums = (
50 25 map { _card_num_normalize($_) } (
51 "A",
52 ( 2 .. 9 ),
53 {
54 't' => "T",
55 'non_t' => "10",
56 },
57 ,
58 "J", "Q", "K"
59 )
60 );
61  
62 my %ranks_map =
63 25 ( map { $card_nums[$_]->{t} => ( $_ + 1 ) } ( 0 .. $#card_nums ) );
64  
65 25 my @suits_map_proto = (
66 [ "H" => { name => "hearts", color => "red", }, ],
67 [ "C" => { name => "clubs", color => "black", }, ],
68 [ "D" => { name => "diamonds", color => "red", }, ],
69 [ "S" => { name => "spades", color => "black", }, ],
70 );
71  
72 25 my %suits_map = ( map { @$_ } @suits_map_proto );
73  
74  
75 sub get_suits_seq
76 {
77 39 ▶ my $class = shift;
Subroutine get_suits_seq: called CC=1 SCAR=0.0
Pod documented
78  
79 39 return [ map { $_->[0] } @suits_map_proto ];
80 }
81  
82  
83 sub calc_rank
84 {
85 11995 ▶ my ( $self, $s ) = @_;
Subroutine calc_rank: called CC=1 SCAR=0.0
Pod documented
86  
87 11995 return $ranks_map{$s};
88 }
89  
90  
91 sub calc_rank_with_0
92 {
93 1088 ▶ my ( $self, $str ) = @_;
Subroutine calc_rank_with_0: called CC=2 SCAR=6.9
Pod documented
94  
95 1088 ▶ if ( $str eq "0" )
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq '0') { } 430 658 1088
96 {
97 430 return 0;
98 }
99 else
100 {
101 658 return $self->calc_rank($str);
102 }
103 }
104  
105 sub _from_string
106 {
107 11338 ▶ my ( $self, $str ) = @_;
Subroutine _from_string: called CC=5 SCAR=16.1
108  
109 11338 my $is_flipped = 0;
110  
111 11338 ▶ if ( $str =~ s{\A<(.*)>\z}{$1}ms )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/\A<(.*)>\z/$1/ms) 1 11337 11338
112 {
113 1 $is_flipped = 1;
114 }
115  
116 11338 ▶ if ( length($str) != 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $str != 2) 1 11337 11338
117 {
118 1 Games::Solitaire::Verify::Exception::Parse::Card->throw(
119 error => "string length is too long", );
120 }
121  
122 11337 my ( $rank, $suit ) = split( //, $str );
123  
124 11337 ▶ if ( !defined( $self->rank( $self->calc_rank($rank) ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->rank($self->calc_rank($rank))) 1 11336 11337
125 {
126 1 Games::Solitaire::Verify::Exception::Parse::Card::UnknownRank->throw(
127 error => "unknown rank", );
128 }
129  
130 11336 ▶ if ( exists( $suits_map{$suit} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $suits_map{$suit}) { } 11335 1 11336
131 {
132 11335 $self->suit($suit);
133 }
134 else
135 {
136 1 Games::Solitaire::Verify::Exception::Parse::Card::UnknownSuit->throw(
137 error => "unknown suit", );
138 }
139  
140 11335 $self->set_flipped($is_flipped);
141  
142 11335 return;
143 }
144  
145 sub _init
146 {
147 11569 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=4 SCAR=13.9
148  
149 11569 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 11338 231 11569
150 {
151 11338 $self->_from_string( $args->{string} );
152 11335 $self->_recalc();
153 }
154  
155 11566 ▶ if ( exists( $args->{id} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"id"}) 8 11558 11566
156 {
157 8 $self->id( $args->{id} );
158 }
159  
160 11566 ▶ if ( exists( $args->{data} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"data"}) 7 11559 11566
161 {
162 7 $self->data( $args->{data} );
163 }
164  
165 11566 return;
166 }
167  
168  
169 sub color
170 {
171 1829 ▶ my ($self) = @_;
Subroutine color: called CC=1 SCAR=0.0
Pod documented
172  
173 1829 return $self->color_for_suit( $self->suit() );
174 }
175  
176  
177 sub color_for_suit
178 {
179 1833 ▶ my ( $self, $suit ) = @_;
Subroutine color_for_suit: called CC=1 SCAR=0.0
Pod documented
180  
181 1833 return $suits_map{$suit}->{'color'};
182 }
183  
184  
185 sub clone
186 {
187 231 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
188  
189 231 my $new_card = Games::Solitaire::Verify::Card->new();
190  
191 231 $new_card->data( $self->data() );
192 231 $new_card->id( $self->id() );
193 231 $new_card->suit( $self->suit() );
194 231 $new_card->rank( $self->rank() );
195  
196 231 $new_card->_recalc();
197  
198 231 return $new_card;
199 }
200  
201  
202 sub _to_string_without_flipped
203 {
204 23160 ▶ my $self = shift;
Subroutine _to_string_without_flipped: called CC=1 SCAR=0.0
205  
206 23160 return $self->rank_to_string( $self->rank() ) . $self->suit();
207 }
208  
209 sub to_string
210 {
211 23160 ▶ my $self = shift;
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
212  
213 23160 my $s = $self->_to_string_without_flipped();
214  
215 23160 ▶ return ( $self->is_flipped ? "<$s>" : $s );
Branch: 1 branch
BranchTrueFalseTotal
$self->is_flipped ? : 3 23157 23160
216 }
217  
218  
219 sub fast_s
220 {
221 25684 ▶ return shift->_s;
Subroutine fast_s: called CC=1 SCAR=0.0
Pod documented
222 }
223  
224  
225 {
226 25 my @_t_nums = ( '0', ( map { $_->{t} } @card_nums ) );
227  
228 sub get_ranks_strings
229 {
230 16 ▶ return \@_t_nums;
Subroutine get_ranks_strings: called CC=1 SCAR=0.0
Pod documented
231 }
232  
233 sub rank_to_string
234 {
235 23160 ▶ my ( $class, $rank ) = @_;
Subroutine rank_to_string: called CC=1 SCAR=0.0
Pod documented
236  
237 23160 return $_t_nums[$rank];
238 }
239  
240 }
241  
242  
243 sub is_flipped
244 25 {
245 23161 ▶ return shift->_flipped();
Subroutine is_flipped: called CC=1 SCAR=0.0
Pod documented
246 }
247  
248  
249 sub set_flipped
250 {
251 11336 ▶ my ( $self, $v ) = @_;
Subroutine set_flipped: called CC=1 SCAR=0.0
Pod documented
252  
253 11336 $self->_flipped($v);
254  
255 11336 $self->_recalc();
256  
257 11336 return;
258 }
259  
260 25 1; # End of Games::Solitaire::Verify::Card
261  
262 __END__
« blib/lib/Games/Solitaire/Verify/Freecells.pm ↑ Summary blib/lib/Games/Solitaire/Verify/Foundations.pm »