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 SCAR 24.0
CC
11
Cov
100%
_from_string
5.0
_init
4.0
_card_num_normalize
2.0
CRAP 11.0
« blib/lib/Games/Solitaire/Verify/HorneAutomovePrune.pm ↑ Summary blib/lib/Games/Solitaire/Verify/App/CmdLine/From_Patsolve.pm »
1 package Games::Solitaire::Verify::Card;
2 28 $Games::Solitaire::Verify::Card::VERSION = '0.2402';
3 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 28 ▶use parent 'Games::Solitaire::Verify::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 28 ▶use Games::Solitaire::Verify::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 28 __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 23230 ▶ my ($self) = @_;
Subroutine _recalc: called CC=1 SCAR=0.0
29  
30 23230 $self->_s( $self->to_string() );
31  
32 23230 return;
33 }
34  
35 sub _card_num_normalize
36 {
37 364 ▶ my $arg = shift;
Subroutine _card_num_normalize: called CC=2 SCAR=6.9
38  
39 364 ▶ if ( ref($arg) eq "" )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $arg eq '') { } 336 28 364
40 {
41 336 return +{ map { $_ => $arg } (qw(t non_t)) };
42 }
43 else
44 {
45 28 return $arg;
46 }
47 }
48  
49 my @card_nums = (
50 28 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 28 ( map { $card_nums[$_]->{t} => ( $_ + 1 ) } ( 0 .. $#card_nums ) );
64  
65 28 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 28 my %suits_map = ( map { @$_ } @suits_map_proto );
73  
74  
75 sub get_suits_seq
76 {
77 45 ▶ my $class = shift;
Subroutine get_suits_seq: called CC=1 SCAR=0.0
Pod documented
78  
79 45 return [ map { $_->[0] } @suits_map_proto ];
80 }
81  
82  
83 sub calc_rank
84 {
85 12159 ▶ my ( $self, $s ) = @_;
Subroutine calc_rank: called CC=1 SCAR=0.0
Pod documented
86  
87 12159 return $ranks_map{$s};
88 }
89  
90  
91 sub calc_rank_with_0
92 {
93 1096 ▶ my ( $self, $str ) = @_;
Subroutine calc_rank_with_0: called CC=2 SCAR=6.9
Pod documented
94  
95 1096 ▶ if ( $str eq "0" )
Branch: 1 branch
BranchTrueFalseTotal
if ($str eq '0') { } 438 658 1096
96 {
97 438 return 0;
98 }
99 else
100 {
101 658 return $self->calc_rank($str);
102 }
103 }
104  
105 sub _from_string
106 {
107 11502 ▶ my ( $self, $str ) = @_;
Subroutine _from_string: called CC=5 SCAR=16.1
108  
109 11502 my $is_flipped = 0;
110  
111 11502 ▶ if ( $str =~ s{\A<(.*)>\z}{$1}ms )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/\A<(.*)>\z/$1/ms) 1 11501 11502
112 {
113 1 $is_flipped = 1;
114 }
115  
116 11502 ▶ if ( length($str) != 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $str != 2) 1 11501 11502
117 {
118 1 Games::Solitaire::Verify::Exception::Parse::Card->throw(
119 error => "string length is too long", );
120 }
121  
122 11501 my ( $rank, $suit ) = split( //, $str );
123  
124 11501 ▶ if ( !defined( $self->rank( $self->calc_rank($rank) ) ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->rank($self->calc_rank($rank))) 1 11500 11501
125 {
126 1 Games::Solitaire::Verify::Exception::Parse::Card::UnknownRank->throw(
127 error => "unknown rank", );
128 }
129  
130 11500 ▶ if ( exists( $suits_map{$suit} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $suits_map{$suit}) { } 11499 1 11500
131 {
132 11499 $self->suit($suit);
133 }
134 else
135 {
136 1 Games::Solitaire::Verify::Exception::Parse::Card::UnknownSuit->throw(
137 error => "unknown suit", );
138 }
139  
140 11499 $self->set_flipped($is_flipped);
141  
142 11499 return;
143 }
144  
145 sub _init
146 {
147 11733 ▶ my ( $self, $args ) = @_;
Subroutine _init: called CC=4 SCAR=13.9
148  
149 11733 ▶ if ( exists( $args->{string} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"string"}) 11502 231 11733
150 {
151 11502 $self->_from_string( $args->{string} );
152 11499 $self->_recalc();
153 }
154  
155 11730 ▶ if ( exists( $args->{id} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"id"}) 8 11722 11730
156 {
157 8 $self->id( $args->{id} );
158 }
159  
160 11730 ▶ if ( exists( $args->{data} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args->{"data"}) 7 11723 11730
161 {
162 7 $self->data( $args->{data} );
163 }
164  
165 11730 return;
166 }
167  
168  
169 sub color
170 {
171 1965 ▶ my ($self) = @_;
Subroutine color: called CC=1 SCAR=0.0
Pod documented
172  
173 1965 return $self->color_for_suit( $self->suit() );
174 }
175  
176  
177 sub color_for_suit
178 {
179 1969 ▶ my ( $self, $suit ) = @_;
Subroutine color_for_suit: called CC=1 SCAR=0.0
Pod documented
180  
181 1969 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 23865 ▶ my $self = shift;
Subroutine _to_string_without_flipped: called CC=1 SCAR=0.0
205  
206 23865 return $self->rank_to_string( $self->rank() ) . $self->suit();
207 }
208  
209 sub to_string
210 {
211 23865 ▶ my $self = shift;
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
212  
213 23865 my $s = $self->_to_string_without_flipped();
214  
215 23865 ▶ return ( $self->is_flipped ? "<$s>" : $s );
Branch: 1 branch
BranchTrueFalseTotal
$self->is_flipped ? : 3 23862 23865
216 }
217  
218  
219 sub fast_s
220 {
221 27106 ▶ return shift->_s;
Subroutine fast_s: called CC=1 SCAR=0.0
Pod documented
222 }
223  
224  
225 {
226 28 my @_t_nums = ( '0', ( map { $_->{t} } @card_nums ) );
227  
228 sub get_ranks_strings
229 {
230 19 ▶ return \@_t_nums;
Subroutine get_ranks_strings: called CC=1 SCAR=0.0
Pod documented
231 }
232  
233 sub rank_to_string
234 {
235 23865 ▶ my ( $class, $rank ) = @_;
Subroutine rank_to_string: called CC=1 SCAR=0.0
Pod documented
236  
237 23865 return $_t_nums[$rank];
238 }
239  
240 }
241  
242  
243 sub is_flipped
244 28 {
245 23866 ▶ return shift->_flipped();
Subroutine is_flipped: called CC=1 SCAR=0.0
Pod documented
246 }
247  
248  
249 sub set_flipped
250 {
251 11500 ▶ my ( $self, $v ) = @_;
Subroutine set_flipped: called CC=1 SCAR=0.0
Pod documented
252  
253 11500 $self->_flipped($v);
254  
255 11500 $self->_recalc();
256  
257 11500 return;
258 }
259  
260 28 1; # End of Games::Solitaire::Verify::Card
261  
262 __END__
« blib/lib/Games/Solitaire/Verify/HorneAutomovePrune.pm ↑ Summary blib/lib/Games/Solitaire/Verify/App/CmdLine/From_Patsolve.pm »