| 1 | package Games::Solitaire::Verify::Card; | ||||||||||
| 2 | 28 | $Games::Solitaire::Verify::Card::VERSION = '0.2601'; | |||||||||
| 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 | 23438 | ▶ | { | ||||||||
|
Subroutine
_recalc: called CC=1 SCAR=0.0
| |||||||||||
| 28 | 23438 | my ($self) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 23438 | $self->_s( $self->to_string() ); | |||||||||
| 31 | |||||||||||
| 32 | 23438 | return; | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | sub _card_num_normalize | ||||||||||
| 36 | 364 | ▶ | { | ||||||||
|
Subroutine
_card_num_normalize: called CC=2 SCAR=6.9
| |||||||||||
| 37 | 364 | my $arg = shift; | |||||||||
| 38 | |||||||||||
| 39 | 364 | ▶ | if ( ref($arg) eq "" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 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", | ||||||||||
| 59 | "K" | ||||||||||
| 60 | ) | ||||||||||
| 61 | ); | ||||||||||
| 62 | |||||||||||
| 63 | my %ranks_map = | ||||||||||
| 64 | 28 | ( map { $card_nums[$_]->{t} => ( $_ + 1 ) } ( 0 .. $#card_nums ) ); | |||||||||
| 65 | |||||||||||
| 66 | 28 | my @suits_map_proto = ( | |||||||||
| 67 | [ "H" => { name => "hearts", color => "red", }, ], | ||||||||||
| 68 | [ "C" => { name => "clubs", color => "black", }, ], | ||||||||||
| 69 | [ "D" => { name => "diamonds", color => "red", }, ], | ||||||||||
| 70 | [ "S" => { name => "spades", color => "black", }, ], | ||||||||||
| 71 | ); | ||||||||||
| 72 | |||||||||||
| 73 | 28 | my %suits_map = ( map { @$_ } @suits_map_proto ); | |||||||||
| 74 | |||||||||||
| 75 | |||||||||||
| 76 | sub get_suits_seq | ||||||||||
| 77 | 45 | ▶ | { | ||||||||
|
Subroutine
get_suits_seq: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 78 | 45 | my $class = shift; | |||||||||
| 79 | |||||||||||
| 80 | 45 | return [ map { $_->[0] } @suits_map_proto ]; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | |||||||||||
| 84 | sub calc_rank | ||||||||||
| 85 | 12263 | ▶ | { | ||||||||
|
Subroutine
calc_rank: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 86 | 12263 | my ( $self, $s ) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 12263 | return $ranks_map{$s}; | |||||||||
| 89 | } | ||||||||||
| 90 | |||||||||||
| 91 | |||||||||||
| 92 | sub calc_rank_with_0 | ||||||||||
| 93 | 1096 | ▶ | { | ||||||||
|
Subroutine
calc_rank_with_0: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 94 | 1096 | my ( $self, $str ) = @_; | |||||||||
| 95 | |||||||||||
| 96 | 1096 | ▶ | if ( $str eq "0" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 97 | { | ||||||||||
| 98 | 438 | return 0; | |||||||||
| 99 | } | ||||||||||
| 100 | else | ||||||||||
| 101 | { | ||||||||||
| 102 | 658 | return $self->calc_rank($str); | |||||||||
| 103 | } | ||||||||||
| 104 | } | ||||||||||
| 105 | |||||||||||
| 106 | sub _from_string | ||||||||||
| 107 | 11606 | ▶ | { | ||||||||
|
Subroutine
_from_string: called CC=5 SCAR=16.1
| |||||||||||
| 108 | 11606 | my ( $self, $str ) = @_; | |||||||||
| 109 | |||||||||||
| 110 | 11606 | my $is_flipped = 0; | |||||||||
| 111 | |||||||||||
| 112 | 11606 | ▶ | if ( $str =~ s{\A<(.*)>\z}{$1}ms ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 113 | { | ||||||||||
| 114 | 1 | $is_flipped = 1; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 11606 | ▶ | if ( length($str) != 2 ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 118 | { | ||||||||||
| 119 | 1 | Games::Solitaire::Verify::Exception::Parse::Card->throw( | |||||||||
| 120 | error => "string length is too long", ); | ||||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 11605 | my ( $rank, $suit ) = split( //, $str ); | |||||||||
| 124 | |||||||||||
| 125 | 11605 | ▶ | if ( !defined( $self->rank( $self->calc_rank($rank) ) ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | { | ||||||||||
| 127 | 1 | Games::Solitaire::Verify::Exception::Parse::Card::UnknownRank->throw( | |||||||||
| 128 | error => "unknown rank", ); | ||||||||||
| 129 | } | ||||||||||
| 130 | |||||||||||
| 131 | 11604 | ▶ | if ( exists( $suits_map{$suit} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | { | ||||||||||
| 133 | 11603 | $self->suit($suit); | |||||||||
| 134 | } | ||||||||||
| 135 | else | ||||||||||
| 136 | { | ||||||||||
| 137 | 1 | Games::Solitaire::Verify::Exception::Parse::Card::UnknownSuit->throw( | |||||||||
| 138 | error => "unknown suit", ); | ||||||||||
| 139 | } | ||||||||||
| 140 | |||||||||||
| 141 | 11603 | $self->set_flipped($is_flipped); | |||||||||
| 142 | |||||||||||
| 143 | 11603 | return; | |||||||||
| 144 | } | ||||||||||
| 145 | |||||||||||
| 146 | sub _init | ||||||||||
| 147 | 11837 | ▶ | { | ||||||||
|
Subroutine
_init: called CC=4 SCAR=13.9
| |||||||||||
| 148 | 11837 | my ( $self, $args ) = @_; | |||||||||
| 149 | |||||||||||
| 150 | 11837 | ▶ | if ( exists( $args->{string} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | { | ||||||||||
| 152 | 11606 | $self->_from_string( $args->{string} ); | |||||||||
| 153 | 11603 | $self->_recalc(); | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | 11834 | ▶ | if ( exists( $args->{id} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 157 | { | ||||||||||
| 158 | 8 | $self->id( $args->{id} ); | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | 11834 | ▶ | if ( exists( $args->{data} ) ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | { | ||||||||||
| 163 | 7 | $self->data( $args->{data} ); | |||||||||
| 164 | } | ||||||||||
| 165 | |||||||||||
| 166 | 11834 | return; | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | |||||||||||
| 170 | sub color | ||||||||||
| 171 | 1965 | ▶ | { | ||||||||
|
Subroutine
color: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 172 | 1965 | my ($self) = @_; | |||||||||
| 173 | |||||||||||
| 174 | 1965 | return $self->color_for_suit( $self->suit() ); | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | |||||||||||
| 178 | sub color_for_suit | ||||||||||
| 179 | 1969 | ▶ | { | ||||||||
|
Subroutine
color_for_suit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 180 | 1969 | my ( $self, $suit ) = @_; | |||||||||
| 181 | |||||||||||
| 182 | 1969 | return $suits_map{$suit}->{'color'}; | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | |||||||||||
| 186 | sub clone | ||||||||||
| 187 | 231 | ▶ | { | ||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 188 | 231 | my $self = shift; | |||||||||
| 189 | |||||||||||
| 190 | 231 | my $new_card = Games::Solitaire::Verify::Card->new(); | |||||||||
| 191 | |||||||||||
| 192 | 231 | $new_card->data( $self->data() ); | |||||||||
| 193 | 231 | $new_card->id( $self->id() ); | |||||||||
| 194 | 231 | $new_card->suit( $self->suit() ); | |||||||||
| 195 | 231 | $new_card->rank( $self->rank() ); | |||||||||
| 196 | |||||||||||
| 197 | 231 | $new_card->_recalc(); | |||||||||
| 198 | |||||||||||
| 199 | 231 | return $new_card; | |||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | |||||||||||
| 203 | sub _to_string_without_flipped | ||||||||||
| 204 | 24379 | ▶ | { | ||||||||
|
Subroutine
_to_string_without_flipped: called CC=1 SCAR=0.0
| |||||||||||
| 205 | 24379 | my $self = shift; | |||||||||
| 206 | |||||||||||
| 207 | 24379 | return $self->rank_to_string( $self->rank() ) . $self->suit(); | |||||||||
| 208 | } | ||||||||||
| 209 | |||||||||||
| 210 | sub to_string | ||||||||||
| 211 | 24379 | ▶ | { | ||||||||
|
Subroutine
to_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 212 | 24379 | my $self = shift; | |||||||||
| 213 | |||||||||||
| 214 | 24379 | my $s = $self->_to_string_without_flipped(); | |||||||||
| 215 | |||||||||||
| 216 | 24379 | ▶ | return ( $self->is_flipped ? "<$s>" : $s ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 217 | } | ||||||||||
| 218 | |||||||||||
| 219 | |||||||||||
| 220 | sub fast_s | ||||||||||
| 221 | 27567 | ▶ | { | ||||||||
|
Subroutine
fast_s: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 222 | 27567 | return shift->_s; | |||||||||
| 223 | } | ||||||||||
| 224 | |||||||||||
| 225 | |||||||||||
| 226 | { | ||||||||||
| 227 | 28 | my @_t_nums = ( '0', ( map { $_->{t} } @card_nums ) ); | |||||||||
| 228 | |||||||||||
| 229 | sub get_ranks_strings | ||||||||||
| 230 | 19 | ▶ | { | ||||||||
|
Subroutine
get_ranks_strings: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 231 | 19 | return \@_t_nums; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | sub rank_to_string | ||||||||||
| 235 | 24379 | ▶ | { | ||||||||
|
Subroutine
rank_to_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 236 | 24379 | my ( $class, $rank ) = @_; | |||||||||
| 237 | |||||||||||
| 238 | 24379 | return $_t_nums[$rank]; | |||||||||
| 239 | } | ||||||||||
| 240 | |||||||||||
| 241 | } | ||||||||||
| 242 | |||||||||||
| 243 | |||||||||||
| 244 | sub is_flipped | ||||||||||
| 245 | 28 | ▶ | { | ||||||||
|
Subroutine
is_flipped: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 246 | 24380 | return shift->_flipped(); | |||||||||
| 247 | } | ||||||||||
| 248 | |||||||||||
| 249 | |||||||||||
| 250 | sub set_flipped | ||||||||||
| 251 | 11604 | ▶ | { | ||||||||
|
Subroutine
set_flipped: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 252 | 11604 | my ( $self, $v ) = @_; | |||||||||
| 253 | |||||||||||
| 254 | 11604 | $self->_flipped($v); | |||||||||
| 255 | |||||||||||
| 256 | 11604 | $self->_recalc(); | |||||||||
| 257 | |||||||||||
| 258 | 11604 | return; | |||||||||
| 259 | } | ||||||||||
| 260 | |||||||||||
| 261 | 28 | 1; # End of Games::Solitaire::Verify::Card | |||||||||
| 262 | |||||||||||
| 263 | __END__ | ||||||||||