blib/lib/Games/Cards/ShuffleTrack.pm

filter: Statementstmt 100% 194 / 194 Branchbran 100% 50 / 50 Conditioncond 100% 27 / 27 MC/DCmcdc 100% 14 / 14 Subroutinesub 100% 38 / 38 Podpod 100% 24 / 24 totaltotal 100% 347 / 347 CC 39 SCAR 9.8
CC
39
Cov
100%
run
5.0
_packet_transfer
4.0
cut
4.0
CRAP 2.7
↑ Summary
1 package Games::Cards::ShuffleTrack;
2  
3 11 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use List::Util qw/min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use List::MoreUtils qw/zip first_index/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Games::Cards::ShuffleTrack - Track cards through shuffles and cuts
14  
15 =head1 VERSION
16  
17 Version 0.02
18  
19 =cut
20  
21 11 our $VERSION = '0.02';
22  
23 11 my $cut_limits = {
24 normal => [0.19, 0.82], # on a 52 cards deck, cut between 10 and 43 cards
25 short => [0.09, 0.28], # on a 52 cards deck, cut between 5 and 15 cards
26 center => [0.36, 0.59], # on a 52 cards deck, cut between 19 and 31 cards
27 deep => [0.67, 0.86], # on a 52 cards deck, cut between 35 and 45 cards
28 };
29  
30 11 my $decks = {
31 empty => [],
32 new_deck_order => [qw/AH 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH
33 AC 2C 3C 4C 5C 6C 7C 8C 9C 10C JC QC KC
34 KD QD JD 10D 9D 8D 7D 6D 5D 4D 3D 2D AD
35 KS QS JS 10S 9S 8S 7S 6S 5S 4S 3S 2S AS/],
36 fournier => [qw/AS 2S 3S 4S 5S 6S 7S 8S 9S 10S JS QS KS
37 AH 2H 3H 4H 5H 6H 7H 8H 9H 10H JH QH KH
38 KD QD JD 10D 9D 8D 7D 6D 5D 4D 3D 2D AD
39 KC QC JC 10C 9C 8C 7C 6C 5C 4C 3C 2C AC/],
40 };
41  
42 11 my $shortcuts = {
43 'top' => 1,
44 'second' => 2,
45 'greek' => -2,
46 'bottom' => -1,
47 };
48  
49  
50 =head1 SYNOPSIS
51  
52 This module allows you to simulate true and false shuffles and cuts.
53  
54 use Games::Cards::ShuffleTrack;
55  
56 my $deck = Games::Cards::ShuffleTrack->new();
57  
58 $deck->overhand_shuffle( 2 );
59 $deck->riffle_shuffle();
60 $deck->cut( 'short' );
61 $deck->riffle_shuffle();
62 print $deck->get_deck();
63  
64 Or perhaps with more precision:
65  
66 my $deck = Games::Cards::ShuffleTrack->new();
67  
68 $deck->faro_in();
69 $deck->cut( 26 );
70 print $deck->get_deck();
71  
72 See the rest of the documentation for more advanced features.
73  
74  
75 =head1 INSTALLATION
76  
77 To install this module, run the following commands:
78  
79 perl Makefile.PL
80 make
81 make test
82 make install
83  
84  
85 =head1 DECK REPRESENTATION
86  
87 At the moment a deck is represented as a list of strings; each string represents a card where the first letter or digit (or digits, in the case of a 10) is the value of the card and the following letter is the suit:
88  
89 C - Clubs
90 H - Hearts
91 S - Spades
92 D - Diamonds
93  
94 As an example, some card representations:
95  
96 AC - Ace of Clubs
97 10S - Ten of Spades
98 4D - 4 of Diamonds
99 KH - King of Hearts
100  
101  
102 =head1 SUBROUTINES/METHODS
103  
104 =head2 Standard methods
105  
106 =head3 new
107  
108 Create a new deck.
109  
110 my $deck = Games::Cards::ShuffleTrack->new();
111  
112 The order of the deck is from top to bottom, which means it is the reverse of what you see when you spread a deck in your hands with the cards facing you.
113  
114 When you open most professional decks of cards you'll see the Ace of Spades (AS) in the front; this means it will actually be the 52nd card in the deck, since when you place the cards on the table facing down it will be the bottom card.
115  
116 Currently this module doesn't support specific orders or different orders other than the new deck.
117  
118 The order of the cards is as follows:
119  
120 Ace of Hearths through King of Hearts
121 Ace of Clubs through King of Clubs
122 King of Diamonds through Ace of Diamonds
123 King of Spades through Ace of Spades
124  
125 You can also specify the starting order of the deck among the following:
126  
127 =over 4
128  
129 =item * new_deck_order (the default order)
130  
131 =item * fournier
132  
133 =back
134  
135 my $deck = Games::Cards::ShuffleTrack->new( 'fournier' );
136  
137 =cut
138  
139 sub new {
140 15 ▶ my ($self, $order) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
141 15 ▶ $order ||= 'new_deck_order';
Condition100%2/2
$order ||= "new_deck_order"
l!l
312
MC/DC100%1/1
$order ||= "new_deck_order"
$order
Truth table100%2/2
A
A= $order
Aresult
11
00
142 15 my $deck = $decks->{ $order };
143 15 return bless {
144 'deck' => $deck,
145 'original' => $deck,
146 'orientation' => 'down',
147 }, $self;
148 }
149  
150 =head3 restart
151  
152 Reset the deck to its original status. The original is whatever you selected when you created the deck.
153  
154 $deck->restart;
155  
156 Do bear in mind that by doing so you are replinishing the deck of whatever cards you took out of it.
157  
158 =cut
159  
160 sub restart {
161 59 ▶ my $self = shift;
Subroutine restart: called CC=1 SCAR=0.0
Pod documented
162 59 return $self->_set_deck( @{$self->{'original'}} );
163 }
164  
165  
166 =head3 deck_size
167  
168 Returns the size of the deck.
169  
170 for ( 1 .. $deck->deck_size ) {
171 ...
172 }
173  
174 =cut
175  
176 sub deck_size {
177 440 ▶ my $self = shift;
Subroutine deck_size: called CC=1 SCAR=0.0
Pod documented
178 440 return scalar @{$self->{'deck'}};
179 }
180  
181  
182 =head3 get_deck
183  
184 Returns the deck (a reference to a list of strings).
185  
186 my $cards = $deck->get_deck();
187  
188 =cut
189  
190 sub get_deck { # TODO: use wantarray to allow for an array to be returned
191 923 ▶ my $self = shift;
Subroutine get_deck: called CC=1 SCAR=0.0
Pod documented
192 923 return $self->{'deck'};
193 }
194  
195  
196 =head3 orientation
197  
198 Return whether the deck is face up or face down:
199  
200 if ( $deck->orientation eq 'down' ) {
201 ...
202 }
203  
204 The deck's orientation is either 'up' or 'down'.
205  
206 =cut
207  
208 sub orientation {
209 5 ▶ my $self = shift;
Subroutine orientation: called CC=1 SCAR=0.0
Pod documented
210  
211 5 return $self->{'orientation'};
212 }
213  
214  
215 =head3 turn
216  
217 If the deck was face up, it is turned face down; if the deck was face down, it is turned face up.
218  
219 Turning the deck reverses its order.
220  
221 $deck->turn;
222  
223 =cut
224  
225 sub turn {
226 2 ▶ my $self = shift;
Subroutine turn: called CC=2 SCAR=6.9
Pod documented
227  
228 2 ▶ $self->{'orientation'} = $self->orientation eq 'down' ? 'up' : 'down';
Branch: 1 branch
BranchTrueFalseTotal
$self->orientation eq 'down' ? : 1 1 2
229  
230 2 $self->_set_deck( reverse @{$self->get_deck} );
231  
232 2 return $self;
233 }
234  
235  
236 =head2 Shuffling
237  
238 =head3 Overhand Shuffle
239  
240 =head4 overhand_shuffle
241  
242 In an overhand shuffle the cards are moved from hand to the other in packets, the result being similar to that of running cuts (the difference being that the packets in an overhand shuffle may be smaller than the ones in a running cut sequence).
243  
244 $deck->overhand_shuffle;
245  
246 You can specify how many times you want to go through the deck (which is basically the same thing as calling the method that many times):
247  
248 $deck->overhand_shuffle( 2 );
249  
250 =cut
251  
252 sub overhand_shuffle {
253 4 ▶ my $self = shift;
Subroutine overhand_shuffle: called CC=3 SCAR=11.0
Pod documented
254 4 ▶ my $times = shift || 1;
Condition100%2/2
shift() || 1
l!l
31
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
255  
256 4 $self->_packet_transfer( 1, 10 );
257  
258 4 ▶ return $times > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$times > 1 ? : 1 3 4
259 $self->overhand_shuffle( $times - 1 ) :
260 $self
261 }
262  
263 =head3 Hindu Shuffle
264  
265 =head4 hindu_shuffle
266  
267 In a Hindu shuffle the cards are moved from hand to the other in packets, the result being similar to that of running cuts (the difference being that the packets in an overhand shuffle may be smaller than the ones in a running cut sequence).
268  
269 $deck->hindu_shuffle;
270  
271 You can specify how many times you want to go through the deck (which is basically the same thing as calling the method that many times):
272  
273 $deck->hindu_shuffle( 2 );
274  
275 The Hindu shuffle differs in result from the Overhand shuffle in that the packets are usually thicker; the reason for this is that while in the Overhand shuffle it's the thumb that grabs the cards (and the thumb can easily carry just one or two cards) in the Hindu shuffle it's more than one finger accomplishing this task, grabbing the deck by the sides, which makes it more difficult (hence, rare) to cut just one or two cards.
276  
277 =cut
278  
279 sub hindu_shuffle {
280 3 ▶ my $self = shift;
Subroutine hindu_shuffle: called CC=3 SCAR=11.0
Pod documented
281 3 ▶ my $times = shift || 1;
Condition100%2/2
shift() || 1
l!l
21
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
282  
283 3 $self->_packet_transfer( 4, 10 );
284  
285 3 ▶ return $times > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$times > 1 ? : 1 2 3
286 $self->hindu_shuffle( $times - 1 ) :
287 $self
288 }
289  
290 sub _packet_transfer {
291 9 ▶ my $self = shift;
Subroutine _packet_transfer: called CC=4 SCAR=13.9
292 9 my $min = shift;
293 9 my $max = shift;
294  
295 9 my @deck = @{$self->get_deck};
296  
297 9 my @new_deck;
298  
299 9 ▶ while ( @deck ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@deck) 83 9 92
300 83 ▶ if (@deck < $max) { $max = scalar @deck }
Branch: 1 branch
BranchTrueFalseTotal
if (@deck < $max) 18 65 83
301 83 ▶ if (@deck < $min) { $min = scalar @deck }
Branch: 1 branch
BranchTrueFalseTotal
if (@deck < $min) 3 80 83
302 83 unshift @new_deck, splice @deck, 0, _rand( $min, $max );
303 }
304  
305 9 return $self->_set_deck( @new_deck );
306 }
307  
308  
309 =head4 run
310  
311 The act of running cards is similar to the overhand shuffle, but instead of in packets the cards are run singly.
312  
313 $deck->run( 10 );
314  
315 When running cards you can choose whether to drop those cards on the top or on the bottom of the deck. By default, the cards are moved to the bottom of the deck.
316  
317 $deck->run( 10, 'drop-top' );
318 $deck->run( 10, 'drop-bottom' );
319  
320 Running cards basically reverses their order.
321  
322 If no number is given then no cards are run.
323  
324 =cut
325  
326 sub run {
327 7 ▶ my $self = shift;
Subroutine run: called CC=5 SCAR=16.1
Pod documented
328 7 ▶ my $number_of_cards = shift || return $self;
Condition100%2/2
shift() || (return $self)
l!l
61
MC/DC100%1/1
shift() || (return $self)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
329 6 ▶ my $where_to_drop = shift || 'drop-bottom';
Condition100%2/2
shift() || 'drop-bottom'
l!l
24
MC/DC100%1/1
shift() || 'drop-bottom'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
330  
331 6 ▶ $number_of_cards > 0 or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$number_of_cards > 0 or return $self 1 5 6
332  
333  
334 # take cards from top and reverse their order
335 5 my @deck = @{$self->get_deck};
336 5 my @run = reverse splice @deck, 0, $number_of_cards;
337  
338 5 ▶ if ( $where_to_drop eq 'drop-top' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($where_to_drop eq 'drop-top') { } 2 3 5
339 2 $self->_set_deck( @run, @deck );
340 }
341 else { # drop-bottom is the default
342 3 $self->_set_deck( @deck, @run );
343 }
344  
345 5 return $self;
346 }
347  
348  
349 =head3 Riffle Shuffle
350  
351 =head4 riffle_shuffle
352  
353 Riffle shuffle the deck.
354  
355 $deck->riffle_shuffle();
356  
357 In the act of riffle shuffling a deck the deck is cut into two halves of approximately the same size; each half is riffled so that the cards of both halves interlace; these cards usually drop in groups of 1 to 5 cards.
358  
359 You can also decide where to cut the deck for the shuffle:
360  
361 $deck->riffle_shuffle( 'short' ); # closer to the top
362 $deck->riffle_shuffle( 'center' ); # near the center
363 $deck->riffle_shuffle( 'deep' ); # closer to the bottom
364 $deck->riffle_shuffle( 26 ); # precisely under the 26th card
365  
366 =cut
367  
368 # TODO: add an option for an out-shuffle
369 # TODO: add an option to control top or bottom stock
370 # TODO: when dropping cards, should we favor numbers 2 and 3?
371 sub riffle_shuffle {
372 5 ▶ my $self = shift;
Subroutine riffle_shuffle: called CC=3 SCAR=11.0
Pod documented
373 5 my $depth = shift;
374  
375 # cut the deck (left pile is the original top half)
376 5 my $cut_depth = _cut_depth( $self->deck_size, $depth );
377  
378 5 my @left_pile = @{$self->get_deck};
379 5 my @right_pile = splice @left_pile, $cut_depth;
380  
381 5 my @halves = ( \@left_pile, \@right_pile );
382  
383 # drop cards from the bottom of each half to the pile (1-5 at a time)
384 5 my @new_pile = ();
385 5 ▶ while ( @left_pile and @right_pile ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@left_pile and @right_pile) 76 5 81
Condition100%3/3
@left_pile and @right_pile
!ll&&!rl&&r
4176
MC/DC100%2/2
@left_pile and @right_pile
@left_pile @right_pile
Truth table100%3/3
A and B
A= @left_pileB= @right_pile
ABresult
0X0
100
111
386 76 my $current_half = $halves[0];
387 76 my $number_of_cards = int(rand( min(5, scalar @$current_half) ))+1;
388  
389 76 unshift @new_pile, splice @$current_half, -$number_of_cards;
390  
391 76 @halves = reverse @halves;
392 }
393  
394 # drop the balance on top and set the deck to be the result
395 5 $self->_set_deck( @left_pile, @right_pile, @new_pile );
396  
397 5 return $self;
398 }
399  
400  
401 =head3 Faro shuffle
402  
403 In a faro shuffle the deck is split in half and the two halves are interlaced perfectly so that each card from one half is inserted in between two cards from the opposite half.
404  
405 =head4 faro out
406  
407 Faro out the deck.
408  
409 $deck->faro( 'out' );
410  
411 In a "faro out" the top and bottom cards remain in their original positions.
412  
413 Considering the positions on the cards from 1 to 52 the result of the faro would be as follows:
414  
415 1, 27, 2, 28, 3, 29, 4, 30, 5, 31, 6, 32, 7, 33, ...
416  
417 =head4 faro in
418  
419 Faro in the deck.
420  
421 $deck->faro( 'in' );
422  
423 In a "faro in" the top and bottom cards do not remain in their original positions (top card becomes second from the top, bottom card becomes second from the bottom).
424  
425 Considering the positions on the cards from 1 to 52 the result of the faro would be as follows:
426  
427 27, 1, 28, 2, 29, 3, 30, 4, 31, 5, 32, 6, 33, 7, ...
428  
429 =cut
430  
431 sub faro {
432 60 ▶ my $self = shift;
Subroutine faro: called CC=2 SCAR=6.9
Pod documented
433 60 my $faro = shift; # by default we're doing a faro out
434  
435 # TODO: what happens when the deck is odd-sized?
436 60 my @first_half = @{$self->get_deck};
437 60 my @second_half = splice @first_half, $self->deck_size / 2;
438  
439 60 ▶ $self->_set_deck(
Branch: 1 branch
BranchTrueFalseTotal
$faro eq 'in' ? : 52 8 60
440 $faro eq 'in' ?
441 zip @second_half, @first_half :
442 zip @first_half, @second_half
443 );
444  
445 60 return $self;
446 }
447  
448  
449 =head2 Cutting
450  
451 =head3 cut
452  
453 Cut the deck.
454  
455 $deck->cut();
456  
457 In a 52 cards deck, this would cut somewhere between 10 and 43 cards.
458  
459 Cut at a precise position (moving X cards from top to bottom):
460  
461 $deck->cut(26);
462  
463 If you try to cut to a position that doesn't exist nothing will happen (apart from a warning that you tried to cut to a non-existing position, of course).
464  
465 You can also cut at negative positions, meaning that you're counting from the bottom of the deck and cutting above that card. For instance, to cut the bottom two cards to the top:
466  
467 $deck->cut(-2);
468  
469 Additional ways of cutting:
470  
471 $deck->cut( 'short' ); # on a 52 cards deck, somewhere between 5 and 15 cards
472 $deck->cut( 'center' ); # on a 52 cards deck, somewhere between 19 and 31 cards
473 $deck->cut( 'deep' ); # on a 52 cards deck, somewhere between 35 and 45 cards
474  
475 =head3 cut_below
476  
477 You can cut below a specific card.
478  
479 $deck->cut_below( '9D' );
480  
481 If the desired card is already on the bottom of the deck nothing will happen.
482  
483 For more information on how to cut to a specific card please refer to the L<SEE ALSO> section of this documentation.
484  
485 =head3 cut_above
486  
487 You can cut above a specific card.
488  
489 $deck->cut_above( 'JS' );
490  
491 If the desired card is already on top of the deck nothing will happen.
492  
493 For more information on how to cut to a specific card please refer to the L<SEE ALSO> section of this documentation.
494  
495 =cut
496  
497 sub cut {
498 27 ▶ my $self = shift;
Subroutine cut: called CC=4 SCAR=13.9
Pod documented
499 27 my $position = shift;
500  
501 27 ▶ if ( defined $position
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position and &looks_like_number($position) and abs $position > $self->deck_size) 2 25 27
Condition100%6/6
defined $position and &looks_like_number($position) and abs $position > $self->deck_size
!ll&&!rl&&r
4212
defined $position and &looks_like_number($position)
!ll&&!rl&&r
1323
MC/DC100%3/3
defined $position and &looks_like_number($position) and abs $position > $self->deck_size
defined $position &looks_like_number($position) abs $position > $self->deck_size
Truth table100%4/4
A and B and C
A= defined $positionB= &looks_like_number($position)C= abs $position > $self->deck_size
ABCresult
0XX0
10X0
1100
1111
502 and looks_like_number( $position )
503 and abs($position) > $self->deck_size ) {
504 2 warn "Tried to cut the deck at a non-existing position ($position).\n";
505 2 return $self;
506 }
507  
508 25 my $cut_depth = _cut_depth( $self->deck_size, $position );
509  
510 25 my @deck = @{$self->get_deck};
511 25 unshift @deck, splice @deck, $cut_depth;
512  
513 25 return $self->_set_deck( @deck );
514 }
515  
516 sub cut_below {
517 5 ▶ my $self = shift;
Subroutine cut_below: called CC=1 SCAR=0.0
Pod documented
518 5 my $card = shift;
519  
520 5 return $self->cut( $self->find( $card ) );
521 }
522  
523 sub cut_above {
524 5 ▶ my $self = shift;
Subroutine cut_above: called CC=1 SCAR=0.0
Pod documented
525 5 my $card = shift;
526  
527 5 return $self->cut( $self->find( $card ) - 1 );
528 }
529  
530 =head3 running_cuts
531  
532 Cut packets:
533  
534 $deck->running_cuts();
535  
536 To do the procedure twice:
537  
538 $deck->running_cuts( 2 );
539  
540 =cut
541  
542 sub running_cuts {
543 2 ▶ my $self = shift;
Subroutine running_cuts: called CC=3 SCAR=11.0
Pod documented
544 2 ▶ my $times = shift || 1;
Condition100%2/2
shift() || 1
l!l
11
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
545  
546 2 $self->_packet_transfer( 5, 15 );
547  
548 2 ▶ return $times > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$times > 1 ? : 1 1 2
549 $self->overhand_shuffle( $times - 1 ) :
550 $self
551 }
552  
553  
554 =head2 Handling cards
555  
556 There are a few different methods to track down cards.
557  
558 =head3 find
559  
560 Get the position of specific cards:
561  
562 my $position = $deck->find( 'AS' ); # find the position of the Ace of Spades
563  
564 my @positions = $deck->find( 'AS', 'KH' ); # find the position of two cards
565  
566 If a card is not present on the deck the position returned will be a 0.
567  
568 This method can also return the card at a specific position:
569  
570 my $card = $deck->find( 3 );
571  
572 You can also request a card in a negative position (i.e., from the bottom of the deck). To get the second to last card in the deck:
573  
574 $deck->find( -2 );
575  
576 If you're dealing five hands of poker from the top of the deck, for instance, you can easily find which cards will fall on the dealer's hand:
577  
578 $deck->find( 5, 10, 15, 20, 25 );
579  
580 =cut
581  
582 sub find {
583 358 ▶ my $self = shift;
Subroutine find: called CC=4 SCAR=13.9
Pod documented
584 358 my @cards = @_;
585  
586 358 my @results;
587  
588 358 my $deck = $self->get_deck();
589  
590 358 for my $card ( @cards ) {
591  
592 368 ▶ push @results, looks_like_number( $card )
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($card) ? : 316 52 368
593 ? $self->_find_card_by_position( $card )
594 : $self->_find_card_by_name( $card );
595  
596 }
597  
598 358 ▶ return wantarray ? @results : $results[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 32 326 358
599 }
600  
601 sub _find_card_by_position {
602 316 ▶ my $self = shift;
Subroutine _find_card_by_position: called CC=4 SCAR=13.9
603 316 my $card = shift;
604  
605 316 ▶ if ($card) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card) { } 314 2 316
606 314 ▶ if ($card > 0) { $card--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($card > 0) 297 17 314
607 314 ▶ return $card > $self->deck_size - 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$card > $self->deck_size - 1 ? : 3 311 314
608 q{} :
609 $self->get_deck->[ $card ];
610 }
611 else {
612 2 return q{};
613 }
614 }
615  
616 sub _find_card_by_name {
617 52 ▶ my $self = shift;
Subroutine _find_card_by_name: called CC=2 SCAR=6.9
618 52 my $card = shift;
619  
620 52 ▶ my $position = 1 + first_index { $_ eq $card } @{$self->get_deck};
Subroutine __ANON__: called CC=1 SCAR=0.0
621  
622 52 ▶ return $position ? $position : 0;
Branch: 1 branch
BranchTrueFalseTotal
$position ? : 51 1 52
623 }
624  
625  
626 =head3 find_card_before
627  
628 Finds the card immediately before another card:
629  
630 # return the card immediately before the Ace of Spades
631 $deck->find_card_before( 'AS' );
632  
633 If the specified card is on top of the deck you will get the card on the bottom of the deck.
634  
635 =cut
636  
637 sub find_card_before {
638 2 ▶ my $self = shift;
Subroutine find_card_before: called CC=2 SCAR=6.9
Pod documented
639 2 my $card = shift;
640  
641 2 my $position = $self->find( $card );
642  
643 2 ▶ if ($position == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position == 1) { } 1 1 2
644 1 return 0;
645 }
646 else {
647 1 return $self->find( $position - 1 );
648 }
649 }
650  
651  
652 =head3 find_card_after
653  
654 Finds the card immediately after another card:
655  
656 # return the card immediately after the King of Hearts
657 $deck->find_card_before( 'KH' );
658  
659 If the specified card is on the bottom of the deck you will get the card on the top of the deck.
660  
661 =cut
662  
663 sub find_card_after {
664 2 ▶ my $self = shift;
Subroutine find_card_after: called CC=2 SCAR=6.9
Pod documented
665 2 my $card = shift;
666  
667 2 my $position = 1 + $self->find( $card );
668
669 2 ▶ if ( $position > $self->deck_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position > $self->deck_size) 1 1 2
670 1 return 0;
671 }
672  
673 1 return $self->find( $self->find( $card ) + 1 );
674 }
675  
676  
677 =head3 distance
678  
679 Find the distance between two cards.
680  
681 To find the distance between the Ace of Spades and the King of Hearts:
682  
683 $deck->distance( 'AS', 'KH' );
684  
685 If the King of Hearts is just after the Ace of Spades, then the result is 1. If it's immediately before, the result is -1.
686  
687 =cut
688  
689 sub distance {
690 7 ▶ my $self = shift;
Subroutine distance: called CC=1 SCAR=0.0
Pod documented
691 7 my $first_card = shift;
692 7 my $second_card = shift;
693  
694 7 return $self->find( $second_card) - $self->find( $first_card );
695 }
696  
697  
698 =head3 put
699  
700 Put a card on top of the deck. This is a new card, and not a card already on the deck.
701  
702 $deck->put( $card );
703  
704 If the card was already on the deck, you now have a duplicate card.
705  
706 =cut
707  
708 sub put {
709 1 ▶ my $self = shift;
Subroutine put: called CC=1 SCAR=0.0
Pod documented
710 1 my $card = shift;
711  
712 1 $self->_set_deck( $card, @{$self->get_deck} );
713  
714 1 return $self;
715 }
716  
717  
718 =head3 deal
719  
720 Deals a card, removing it from the deck.
721  
722 my $removed_card = $deck->deal();
723  
724 Just as in regular gambling, you can deal cards from other positions:
725  
726 # deal the second card from the top
727 my $card = $deck->deal( 'second' );
728  
729 # deal the second card from the bottom
730 my $card = $deck->deal( 'greek' );
731  
732 # deal the card from the bottom of the deck
733 my $card = $deck->deal( 'bottom' );
734  
735 For more information on false dealing see the L<SEE ALSO> section.
736  
737 =cut
738  
739 sub deal {
740 5 ▶ my $self = shift;
Subroutine deal: called CC=2 SCAR=6.9
Pod documented
741 5 ▶ my $deal = shift || 'top';
Condition100%2/2
shift() || 'top'
l!l
41
MC/DC100%1/1
shift() || 'top'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
742  
743 # TODO: what if $deal is unknown?
744 5 my $position = $shortcuts->{$deal};
745  
746 5 return $self->remove( $position );
747 }
748  
749 =head3 remove
750  
751 Removes a card from the deck.
752  
753 # remove the 4th card from the top
754 my $card = $deck->remove( 4 );
755  
756 =cut
757  
758 sub remove {
759 61 ▶ my $self = shift;
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
760 61 my $position = shift;
761  
762 61 my @deck = @{$self->get_deck};
763  
764 61 ▶ if ($position > 0) { $position--; }
Branch: 1 branch
BranchTrueFalseTotal
if ($position > 0) 59 2 61
765  
766 61 my $card = splice @deck, $position, 1;
767  
768 61 $self->_set_deck( @deck );
769 61 return $card;
770 }
771  
772  
773 =head3 peek
774  
775 Peek at a position in the deck (this is essentially the same thing as &find()).
776  
777 # peek the top card
778 my $card = $deck->peek( 1 );
779  
780 You can also peek the top and bottom card by using an alias:
781  
782 # peek the top card
783 my $card = $deck->peek( 'top' );
784  
785 # peek the bottom card
786 my $card = $deck->peek( 'bottom' );
787  
788 Negative indexes are also supported:
789  
790 # peek the second from bottom card
791 my $card = $deck->peek( -2 );
792  
793 =cut
794  
795 sub peek {
796 53 ▶ my $self = shift;
Subroutine peek: called CC=3 SCAR=11.0
Pod documented
797 53 ▶ my $position = shift || 1;
Condition100%2/2
shift() || 1
l!l
521
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
798  
799 53 ▶ if (_is_shortcut( $position )) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_shortcut($position)) 4 49 53
800 4 $position = $shortcuts->{$position};
801 }
802  
803 53 return $self->find( $position );
804 }
805  
806  
807 =head3 take_random
808  
809 Remove a random card from the deck.
810  
811 my $random_card = $deck->take_random();
812  
813 You can also specify limits (if you're somehow directing the person taking the card to a particular section of the deck):
814  
815 my $random_card = $deck->take_random( 13, 39 );
816  
817 =cut
818  
819 sub take_random {
820 55 ▶ my $self = shift;
Subroutine take_random: called CC=3 SCAR=11.0
Pod documented
821  
822 55 ▶ my $lower_limit = shift || 1;
Condition100%2/2
shift() || 1
l!l
541
MC/DC100%1/1
shift() || 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
823 55 my $upper_limit = shift;
824  
825 55 ▶ $upper_limit = defined $upper_limit ?
Branch: 1 branch
BranchTrueFalseTotal
defined $upper_limit ? : 52 3 55
826 $upper_limit :
827 $self->deck_size;
828  
829 55 return $self->remove( _rand( $lower_limit, $upper_limit ) );
830 }
831  
832  
833 # subroutines
834  
835 sub _set_deck {
836 227 ▶ my $self = shift;
Subroutine _set_deck: called CC=1 SCAR=0.0
837 227 return $self->{'deck'} = [@_];
838 }
839  
840 sub _rand {
841 146 ▶ my ($lower_limit, $upper_limit) = @_;
Subroutine _rand: called CC=1 SCAR=0.0
842  
843 146 return int($lower_limit + int(rand( $upper_limit - $lower_limit )));
844 }
845  
846 sub _cut_depth {
847 30 ▶ my $deck_size = shift;
Subroutine _cut_depth: called CC=3 SCAR=11.0
848 30 my $position = shift;
849  
850 30 ▶ if (not defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $position) 5 25 30
851 5 $position = 'normal';
852 }
853  
854 30 ▶ if ($position =~ /^short|center|deep|normal$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($position =~ /^short|center|deep|normal$/) 8 22 30
855 8 my ($lower, $upper) = @{$cut_limits->{ $position }};
856 8 $position = _rand( $deck_size * $lower, $deck_size * $upper );
857 }
858  
859 30 return $position;
860 }
861  
862 sub _is_shortcut {
863 53 ▶ my $shortcut = shift;
Subroutine _is_shortcut: called CC=1 SCAR=0.0
864  
865 53 return exists $shortcuts->{$shortcut};
866 }
867  
868  
869 =head1 AUTHOR
870  
871 Jose Castro, C<< <cog at cpan.org> >>
872  
873 =head1 BUGS
874  
875 Please report any bugs or feature requests to C<bug-games-cards-shuffletrack at rt.cpan.org>, or through
876 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Cards-ShuffleTrack>. I will be notified, and then you'll
877 automatically be notified of progress on your bug as I make changes.
878  
879  
880 =head1 SUPPORT
881  
882 You can find documentation for this module with the perldoc command.
883  
884 perldoc Games::Cards::ShuffleTrack
885  
886  
887 You can also look for information at:
888  
889 =over 4
890  
891 =item * Github
892  
893 L<https://github.com/cog/Games-Cards-ShuffleTrack>
894  
895 =item * Search CPAN
896  
897 L<http://search.cpan.org/dist/Games-Cards-ShuffleTrack/>
898  
899 =back
900  
901  
902 =head1 SEE ALSO
903  
904 Recommended reading:
905  
906 =over 4
907  
908 =item * The Expert at the Card Table: The Classic Treatise on Card Manipulation, by S. W. Erdnase
909  
910 =item * The Annotated Erdnase, by Darwin Ortiz
911  
912 =back
913  
914  
915 =head1 LICENSE AND COPYRIGHT
916  
917 Copyright 2016 Jose Castro.
918  
919 This program is free software; you can redistribute it and/or modify it
920 under the terms of the the Artistic License (2.0). You may obtain a
921 copy of the full license at:
922  
923 L<http://www.perlfoundation.org/artistic_license_2_0>
924  
925 Any use, modification, and distribution of the Standard or Modified
926 Versions is governed by this Artistic License. By using, modifying or
927 distributing the Package, you accept this license. Do not use, modify,
928 or distribute the Package, if you do not accept this license.
929  
930 If your Modified Version has been derived from a Modified Version made
931 by someone other than you, you are nevertheless required to ensure that
932 your Modified Version complies with the requirements of this license.
933  
934 This license does not grant you the right to use any trademark, service
935 mark, tradename, or logo of the Copyright Holder.
936  
937 This license includes the non-exclusive, worldwide, free-of-charge
938 patent license to make, have made, use, offer to sell, sell, import and
939 otherwise transfer the Package with respect to any patent claims
940 licensable by the Copyright Holder that are necessarily infringed by the
941 Package. If you institute patent litigation (including a cross-claim or
942 counterclaim) against any party alleging that the Package constitutes
943 direct or contributory patent infringement, then this Artistic License
944 to you shall terminate on the date that such litigation is filed.
945  
946 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
947 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
948 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
949 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
950 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
951 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
952 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
953 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
954  
955  
956 =cut
957  
958 11 1; # End of Games::Cards::ShuffleTrack
↑ Summary