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