| 1 | package VANAMBURG::Packet; | ||||||||||
| 2 | 4 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 4 | ▶ | use List::Util qw(shuffle); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 4 | ▶ | use List::MoreUtils qw(each_array); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 4 | ▶ | use VANAMBURG::Card; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use VANAMBURG::FaceValueSingleton; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | =head1 VANAMBURG::Packet | ||||||||||
| 9 | |||||||||||
| 10 | This class gives models packets or decks of cards. Methods include a variety | ||||||||||
| 11 | of shuffles and cuts as well as dealing cards from one packet into another. | ||||||||||
| 12 | |||||||||||
| 13 | This is the base class for all the specialized decks in this package, such as | ||||||||||
| 14 | BCS, Si Stebbins, etc. | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | =head2 cards | ||||||||||
| 19 | |||||||||||
| 20 | An array reference containing instances of VANAMBURG::Card objects. | ||||||||||
| 21 | |||||||||||
| 22 | It exposes a number of perl array functions to the main class. Developers | ||||||||||
| 23 | will want to know about this: | ||||||||||
| 24 | |||||||||||
| 25 | handles => { | ||||||||||
| 26 | add_card => 'push', | ||||||||||
| 27 | map_cards => 'map', | ||||||||||
| 28 | get_card => 'get', | ||||||||||
| 29 | card_count => 'count' | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | has 'cards' => ( | ||||||||||
| 34 | isa => 'ArrayRef[VANAMBURG::Card]', | ||||||||||
| 35 | is => 'rw', | ||||||||||
| 36 | 15 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 4 | traits => ['Array'], | |||||||||
| 38 | handles => { | ||||||||||
| 39 | add_card => 'push', | ||||||||||
| 40 | map_cards => 'map', | ||||||||||
| 41 | get_card => 'get', | ||||||||||
| 42 | card_count => 'count' | ||||||||||
| 43 | } | ||||||||||
| 44 | ); | ||||||||||
| 45 | |||||||||||
| 46 | =head2 shuffle_faro_in | ||||||||||
| 47 | |||||||||||
| 48 | Calling this reorders the packed/deck doing an "in faro". Consult your magical texts for | ||||||||||
| 49 | more information. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 0 | ▶ | sub shuffle_faro_in { | ||||||||
|
Subroutine
shuffle_faro_in: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 54 | 0 | my $self = shift; | |||||||||
| 55 | 0 | ▶ | die "can only faro packets with even number of cards" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | unless $self->card_count % 2 == 0; | ||||||||||
| 57 | 0 | my $half = $self->card_count / 2; | |||||||||
| 58 | 0 | my @top_half = splice @{ $self->cards }, 0, $half; | |||||||||
| 59 | 0 | my @bottom_half = splice @{ $self->cards }, 0, $half; | |||||||||
| 60 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 61 | 0 | my $ea = each_array( @bottom_half, @top_half ); | |||||||||
| 62 | 0 | ▶ | while ( my ( $c1, $c2 ) = $ea->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 63 | 0 | $new_packet->add_card($c1); | |||||||||
| 64 | 0 | $new_packet->add_card($c2); | |||||||||
| 65 | } | ||||||||||
| 66 | 0 | $self->cards( $new_packet->cards ); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | =head2 shuffle_faro_out | ||||||||||
| 70 | |||||||||||
| 71 | Calling this reorders the packed/deck doing an "out faro". Consult your magical texts for | ||||||||||
| 72 | more information. | ||||||||||
| 73 | |||||||||||
| 74 | =cut | ||||||||||
| 75 | |||||||||||
| 76 | 0 | ▶ | sub shuffle_faro_out { | ||||||||
|
Subroutine
shuffle_faro_out: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 77 | 0 | my $self = shift; | |||||||||
| 78 | |||||||||||
| 79 | 0 | ▶ | die "can only faro packets with even number of cards" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | unless $self->card_count % 2 == 0; | ||||||||||
| 81 | 0 | my $half = $self->card_count / 2; | |||||||||
| 82 | 0 | my @top_half = splice @{ $self->cards }, 0, $half; | |||||||||
| 83 | 0 | my @bottom_half = splice @{ $self->cards }, 0, $half; | |||||||||
| 84 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 85 | 0 | my $ea = each_array( @top_half, @bottom_half ); | |||||||||
| 86 | 0 | ▶ | while ( my ( $c1, $c2 ) = $ea->() ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 0 | $new_packet->add_card($c1); | |||||||||
| 88 | 0 | $new_packet->add_card($c2); | |||||||||
| 89 | } | ||||||||||
| 90 | 0 | $self->cards( $new_packet->cards ); | |||||||||
| 91 | } | ||||||||||
| 92 | |||||||||||
| 93 | =head2 shuffle_random | ||||||||||
| 94 | |||||||||||
| 95 | Randomizes the order of the deck much more than an ordinary shuffle would, for better or worse. | ||||||||||
| 96 | |||||||||||
| 97 | =cut | ||||||||||
| 98 | |||||||||||
| 99 | 0 | ▶ | sub shuffle_random { | ||||||||
|
Subroutine
shuffle_random: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 100 | 0 | my $self = shift; | |||||||||
| 101 | 0 | my @new_cards = shuffle @{ $self->cards }; | |||||||||
| 102 | 0 | $self->cards( \@new_cards ); | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | =head2 deal | ||||||||||
| 106 | |||||||||||
| 107 | my $new_packet = $self->deal(10); | ||||||||||
| 108 | |||||||||||
| 109 | Removes n number of cards from the top of this packet, reversing their order, as in an ordinary deal. | ||||||||||
| 110 | The resulting packet, and instance of VANAMBURG::Packet, is returned. | ||||||||||
| 111 | |||||||||||
| 112 | =cut | ||||||||||
| 113 | |||||||||||
| 114 | 0 | ▶ | sub deal { | ||||||||
|
Subroutine
deal: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 115 | 0 | my ( $self, $how_many ) = @_; | |||||||||
| 116 | |||||||||||
| 117 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 118 | 0 | my @dealt = splice @{ $self->cards }, 0, $how_many; | |||||||||
| 119 | 0 | ▶ | while (@dealt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | $new_packet->add_card( pop @dealt ); | |||||||||
| 121 | } | ||||||||||
| 122 | 0 | return $new_packet; | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | =head2 insert_packet | ||||||||||
| 126 | |||||||||||
| 127 | # Insert a packet after 5 cards from the top of this packet. | ||||||||||
| 128 | $self->insert_packet(5, $other_packet); | ||||||||||
| 129 | |||||||||||
| 130 | Inserts a packet into this packet after a specified number of cards from the top | ||||||||||
| 131 | of this packet. | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||
| 134 | |||||||||||
| 135 | 0 | ▶ | sub insert_packet { | ||||||||
|
Subroutine
insert_packet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 136 | 0 | my ( $self, $after_location, $packet ) = @_; | |||||||||
| 137 | 0 | my $top = $self->cut($after_location); | |||||||||
| 138 | 0 | my @new_cards; | |||||||||
| 139 | 0 | push @new_cards, @{ $top->cards }, @{ $packet->cards }, @{ $self->cards }; | |||||||||
| 140 | 0 | $self->cards( \@new_cards ); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | =head2 cut | ||||||||||
| 144 | |||||||||||
| 145 | my $new_packet = $self->cut(26); | ||||||||||
| 146 | |||||||||||
| 147 | Cuts off a packet from this packet/deck, resulting in fewer cards in this packet. | ||||||||||
| 148 | The new packet is returned as an instance of VANAMBURG::Packet. | ||||||||||
| 149 | |||||||||||
| 150 | =cut | ||||||||||
| 151 | |||||||||||
| 152 | 0 | ▶ | sub cut { | ||||||||
|
Subroutine
cut: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 153 | 0 | my ( $self, $how_many ) = @_; | |||||||||
| 154 | |||||||||||
| 155 | 0 | my @packet = splice @{ $self->cards }, 0, $how_many; | |||||||||
| 156 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 157 | 0 | $new_packet->add_card(@packet); | |||||||||
| 158 | 0 | return $new_packet; | |||||||||
| 159 | } | ||||||||||
| 160 | |||||||||||
| 161 | =head2 cut_and_complete | ||||||||||
| 162 | |||||||||||
| 163 | # cut a deck exactly in the middle and place top on bottom. | ||||||||||
| 164 | $self->cut_and_complete(26); | ||||||||||
| 165 | |||||||||||
| 166 | Cut of n number of cards from the top, and place this packet on the bottom. | ||||||||||
| 167 | |||||||||||
| 168 | =cut | ||||||||||
| 169 | |||||||||||
| 170 | 0 | ▶ | sub cut_and_complete { | ||||||||
|
Subroutine
cut_and_complete: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 171 | 0 | my ( $self, $how_many ) = @_; | |||||||||
| 172 | 0 | my @packet = splice @{ $self->cards }, 0, $how_many; | |||||||||
| 173 | 0 | $self->add_card(@packet); | |||||||||
| 174 | } | ||||||||||
| 175 | |||||||||||
| 176 | =head2 cut_and_take | ||||||||||
| 177 | |||||||||||
| 178 | Simulates cutting a packet from the top, completing the cut, | ||||||||||
| 179 | and taking the top card from the deck. This deck/packet now | ||||||||||
| 180 | has one less card. | ||||||||||
| 181 | |||||||||||
| 182 | The taken card is returned as an instance of VANAMBURG::Card. | ||||||||||
| 183 | |||||||||||
| 184 | =cut | ||||||||||
| 185 | |||||||||||
| 186 | 0 | ▶ | sub cut_and_take { | ||||||||
|
Subroutine
cut_and_take: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 187 | 0 | my ( $self, $cut_to_packet_location ) = @_; | |||||||||
| 188 | |||||||||||
| 189 | 0 | my $Packet_max_index = $#{ $self->cards }; | |||||||||
| 190 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 191 | 0 | my $take = $self->card_at_location($cut_to_packet_location); | |||||||||
| 192 | |||||||||||
| 193 | # put the bottm on the top | ||||||||||
| 194 | 0 | push @{ $new_packet->cards }, | |||||||||
| 195 | 0 | @{ $self->cards }[ $cut_to_packet_location .. $Packet_max_index ]; | |||||||||
| 196 | |||||||||||
| 197 | # put the top on the bottom | ||||||||||
| 198 | 0 | push @{ $new_packet->cards }, | |||||||||
| 199 | 0 | @{ $self->cards }[ 0 .. $cut_to_packet_location - 2 ]; | |||||||||
| 200 | 0 | $self->cards( $new_packet->cards ); | |||||||||
| 201 | |||||||||||
| 202 | 0 | return $take; | |||||||||
| 203 | } | ||||||||||
| 204 | |||||||||||
| 205 | =head2 cut_take_bury | ||||||||||
| 206 | |||||||||||
| 207 | This simulates cutting to a location, taking that card, placing it on the top, | ||||||||||
| 208 | and completing the cut, thus burying the taken card in the middle in a new | ||||||||||
| 209 | location. | ||||||||||
| 210 | |||||||||||
| 211 | The 'taken' card, though it remains in the deck, is returned for 'peeking'. | ||||||||||
| 212 | |||||||||||
| 213 | =cut | ||||||||||
| 214 | |||||||||||
| 215 | 0 | ▶ | sub cut_take_bury { | ||||||||
|
Subroutine
cut_take_bury: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 216 | 0 | my ( $self, $cut_to_packet_location ) = @_; | |||||||||
| 217 | |||||||||||
| 218 | 0 | my $Packet_max_index = $#{ $self->cards }; | |||||||||
| 219 | 0 | my $new_packet = VANAMBURG::Packet->new; | |||||||||
| 220 | 0 | my $take = $self->card_at_location($cut_to_packet_location); | |||||||||
| 221 | |||||||||||
| 222 | # put the bottm on the top | ||||||||||
| 223 | 0 | push @{ $new_packet->cards }, | |||||||||
| 224 | 0 | @{ $self->cards }[ $cut_to_packet_location .. $Packet_max_index ]; | |||||||||
| 225 | |||||||||||
| 226 | # put bury card next | ||||||||||
| 227 | 0 | push @{ $new_packet->cards }, $take; | |||||||||
| 228 | |||||||||||
| 229 | # put the top on the bottom | ||||||||||
| 230 | 0 | push @{ $new_packet->cards }, | |||||||||
| 231 | 0 | @{ $self->cards }[ 0 .. $cut_to_packet_location - 2 ]; | |||||||||
| 232 | 0 | $self->cards( $new_packet->cards ); | |||||||||
| 233 | |||||||||||
| 234 | 0 | return $take; | |||||||||
| 235 | } | ||||||||||
| 236 | |||||||||||
| 237 | =head2 location_of | ||||||||||
| 238 | |||||||||||
| 239 | Given a card, this method returns is location in the packet. This is a | ||||||||||
| 240 | one-based index, familiar to a magician, not a 0 baded index, familiar | ||||||||||
| 241 | to a programmer. | ||||||||||
| 242 | |||||||||||
| 243 | =cut | ||||||||||
| 244 | |||||||||||
| 245 | 0 | ▶ | sub location_of { | ||||||||
|
Subroutine
location_of: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 246 | 0 | my ( $self, $card ) = @_; | |||||||||
| 247 | 0 | for ( 0 .. $#{ $self->cards } ) { | |||||||||
| 248 | 0 | ▶ | return $_ + 1 if ( $self->get_card($_)->equals($card) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 249 | } | ||||||||||
| 250 | } | ||||||||||
| 251 | |||||||||||
| 252 | =head2 bottom_card | ||||||||||
| 253 | |||||||||||
| 254 | Returns the bottom card of the packet for 'peeking'. The card remains | ||||||||||
| 255 | in this deck. The returned card is an instance of L<VANAMBURG::Card>. | ||||||||||
| 256 | |||||||||||
| 257 | =cut | ||||||||||
| 258 | |||||||||||
| 259 | 1 | ▶ | sub bottom_card { | ||||||||
|
Subroutine
bottom_card: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 260 | 1 | my $self = shift; | |||||||||
| 261 | 1 | $self->get_card( $#{ $self->cards } ); | |||||||||
| 262 | } | ||||||||||
| 263 | |||||||||||
| 264 | =head2 top_card | ||||||||||
| 265 | |||||||||||
| 266 | |||||||||||
| 267 | Returns the top card of the packet for 'peeking'. The card remains | ||||||||||
| 268 | in this deck. The returned card is an instance of L<VANAMBURG::Card>. | ||||||||||
| 269 | |||||||||||
| 270 | =cut | ||||||||||
| 271 | |||||||||||
| 272 | 1 | ▶ | sub top_card { | ||||||||
|
Subroutine
top_card: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 273 | 1 | my $self = shift; | |||||||||
| 274 | 1 | $self->get_card(0); | |||||||||
| 275 | } | ||||||||||
| 276 | |||||||||||
| 277 | =head2 card_at_location | ||||||||||
| 278 | |||||||||||
| 279 | Returns the card at a location. This is a 1 based index, familiar to | ||||||||||
| 280 | the magician, not a 0 based index familiar to the programmer. For | ||||||||||
| 281 | 0 based access use get_card(index). | ||||||||||
| 282 | |||||||||||
| 283 | The returned card is an instance of L<VANAMBURG::Card>. | ||||||||||
| 284 | |||||||||||
| 285 | =cut | ||||||||||
| 286 | |||||||||||
| 287 | 28 | ▶ | sub card_at_location { | ||||||||
|
Subroutine
card_at_location: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 288 | 28 | my ( $self, $Packet_location ) = @_; | |||||||||
| 289 | 28 | return $self->get_card( $Packet_location - 1 ); | |||||||||
| 290 | } | ||||||||||
| 291 | |||||||||||
| 292 | =head2 print_packet | ||||||||||
| 293 | |||||||||||
| 294 | Prints the display name and location to STDOUT. Useful for console scripts only. | ||||||||||
| 295 | |||||||||||
| 296 | =cut | ||||||||||
| 297 | |||||||||||
| 298 | 0 | ▶ | sub print_packet { | ||||||||
|
Subroutine
print_packet: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 299 | 0 | my $self = shift; | |||||||||
| 300 | 0 | my $location = 1; | |||||||||
| 301 | $self->map_cards( | ||||||||||
| 302 | 0 | ▶ | sub { printf "%02d %s\n", ( $location++, $_->display_name ); } ); | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 303 | } | ||||||||||
| 304 | |||||||||||
| 305 | =head2 to_abbreviation_csv_string | ||||||||||
| 306 | |||||||||||
| 307 | Returns a representation of the packet as a comma separated list of abbreviations (e.g., "AD,JS,2H") | ||||||||||
| 308 | which can be passed to L<VANAMBURG::PacketFactory::create_packet> or L<VANAMBURG::PacketFactory::create_stack> | ||||||||||
| 309 | to later re-instantiate the packet as objects. | ||||||||||
| 310 | |||||||||||
| 311 | =cut | ||||||||||
| 312 | |||||||||||
| 313 | 1 | ▶ | sub to_abbreviation_csv_string { | ||||||||
|
Subroutine
to_abbreviation_csv_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 314 | 1 | my $self = shift; | |||||||||
| 315 | my $result = join ',', map { | ||||||||||
| 316 | 4 | $_->abbreviation | |||||||||
| 317 | 1 | } @{ $self->cards }; | |||||||||
| 318 | } | ||||||||||
| 319 | 4 | 1; | |||||||||