blib/lib/Games/JackThief.pm

filter: Statementstmt 9.8% 8 / 81 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 33.3% 3 / 9 Podpod n/a totaltotal 6.7% 11 / 163 SCAR 65.3
CC
28
Cov
6%
JackThief_DiscardCards
182.0
new
56.0
JackThief_Hand
42.0
CRAP 685.1
↑ Summary
1 ##########################################################################################################################################
2 ### Games::JackThief -- 2015, Kapil Rathore <kapil.rthr@gmail.com>
3 ###
4 ### What is JackThief Game ?
5 ###
6 ### This game is played with 51 * (Decks-1) cards. The jokers have no role here and out of 4 jacks,
7 ### one jack is removed which can be of any suit. This game is usually played by 2 players or more.
8 ### After the shuffle, it would be very tedious for the dealer to give one card to each and every player
9 ### since the entire 51 cards have to be distributed amongst all the players equally or nearly equally.
10 ###
11 ### After both the players get their respective bunch of cards, the process of separation begins.
12 ### All paired cards are discarded. For example, if the player 1 has two aces, two Kings, two 10s, two 5s,
13 ### then he will remove these cards. Similarly for cards such as four 7s, four 4s and so on,
14 ### because these form two pairs.
15 ### Once all the cards discarded, player will fetch a random card from neighbouring player,
16 ### he will again discard from his set of cards if match is found. Process continues until the
17 ### player with jack left and became the looser of the game
18 ###
19 ###
20 ### After the pairs are weeded out, both the players will be left with odd number of cards usually one of each,
21 ### since if there are three cards of six say, then two of them form pairs and are discarded and only one six is left.
22 ###
23 ### This would go on till the all the cards are not discarded except one jack and the player who will be having the Jack
24 ### in the end would be called as JackThief.
25 ###
26 ### How to Run the Games::JackThief ?
27 #####################################################################
28 ### use warnings;
29 ### use strict;
30 ### use Games::JackThief;
31 ### #use Data::Dumper; # To see the process of card Fetching enable this
32 ###
33 ### my $Decks_and_Players = {
34 ### no_of_Decks => 1,
35 ### no_of_Players => 3,
36 ### };
37 ###
38 ### my $JackThief = Games::JackThief->new($Decks_and_Players); #my $JackThief = Games::JackThief->new(); # Still Works and Takes default values
39 ### $JackThief->JackThief_Hand;
40 ### $JackThief->CreateFetchSeq;
41 ### do
42 ### {
43 ### #print Dumper($JackThief); # To see the process of card Fetching enable this
44 ### #print "\n\n#####\n\n"; # To see the process of card Fetching enable this
45 ### $JackThief->JackThief_RunFetchRound;
46 ### #<STDIN>; # To see the process of card Fetching enable this
47 ### } while($JackThief->LooserFound);
48 ###
49 ### print "Looser is Player $JackThief->{FetchSeq}[0]\n";
50 ### ########## END #############
51 ###
52 ####
53 ######################################################################
54 ### How to Run the Games::JackThief ?
55 ### 1) Defining the hash with information of no_of_Decks and no_of_Players.
56 ### 2) Taking the JackThief object --> generating the valid Deck Sequence of 51*(Decks-1) cards ---> Randomizing the sequence
57 ### 3) Now Distributing the cards to each players --> the process of separation begins and players discards the cards of same type
58 ### 4) create a circular fetch sequence --> after fetch player will discard the card --> Rotations goes until the loose with Jack is found
59 ###############################################################################################################################################
60 package Games::JackThief;
61
62 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 =head1 NAME
67
68 Games::JackThief - The great new Games::JackThief!
69
70 =head1 VERSION
71
72 Version 0.01
73
74 =cut
75
76 our $VERSION = '0.01';
77
78
79 =head1 SYNOPSIS
80
81 Quick summary of what the module does.
82
83 Perhaps a little code snippet.
84
85 use Games::JackThief;
86
87 my $foo = Games::JackThief->new();
88 ...
89
90 =head1 EXPORT
91
92 A list of functions that can be exported. You can delete this section
93 if you don't export anything, such as for a purely object-oriented module.
94
95 =head1 SUBROUTINES/METHODS
96
97 =head2 function1
98
99 =cut
100
101
102 #################################
103 ## Constructor -> GenerateDeckSequence -> Shuffle
104 sub new {
105 #################################
106 0 ▶ my $Class = shift;
Subroutine new: not called CC=7 SCAR=40.3
107 0 my $Data = shift;
108
109 0 my $Self = {};
110 0 bless $Self, $Class;
111
112 0 ▶ $Self->{no_of_Decks} = (defined $Data->{no_of_Decks} && $Data->{no_of_Decks} > 0 && $Data->{no_of_Decks} <= 5) ? $Data->{no_of_Decks} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $Data->{'no_of_Decks'} && $Data->{'no_of_Decks'} > 0 && $Data->{'no_of_Decks'} <= 5 ? : 0 0 0
Condition0%0/6
defined $Data->{"no_of_Decks"} and $Data->{"no_of_Decks"} > 0 and $Data->{"no_of_Decks"} <= 5
!ll&&!rl&&r
000
defined $Data->{"no_of_Decks"} and $Data->{"no_of_Decks"} > 0
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Data->{"no_of_Decks"} and $Data->{"no_of_Decks"} > 0 and $Data->{"no_of_Decks"} <= 5
defined $Data->{"no_of_Decks"} $Data->{"no_of_Decks"} > 0 $Data->{"no_of_Decks"} <= 5
Truth table0%0/4
A and B and C
A= defined $Data->{"no_of_Decks"}B= $Data->{"no_of_Decks"} > 0C= $Data->{"no_of_Decks"} <= 5
ABCresult
0XX0
10X0
1100
1111
113 0 ▶ $Self->{no_of_Players} = (defined $Data->{no_of_Players} && $Data->{no_of_Players} > 2 && $Data->{no_of_Players} <= 10) ? $Data->{no_of_Players} : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $Data->{'no_of_Players'} && $Data->{'no_of_Players'} > 2 && $Data->{'no_of_Players'} <= 10 ? : 0 0 0
Condition0%0/6
defined $Data->{"no_of_Players"} and $Data->{"no_of_Players"} > 2 and $Data->{"no_of_Players"} <= 10
!ll&&!rl&&r
000
defined $Data->{"no_of_Players"} and $Data->{"no_of_Players"} > 2
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Data->{"no_of_Players"} and $Data->{"no_of_Players"} > 2 and $Data->{"no_of_Players"} <= 10
defined $Data->{"no_of_Players"} $Data->{"no_of_Players"} > 2 $Data->{"no_of_Players"} <= 10
Truth table0%0/4
A and B and C
A= defined $Data->{"no_of_Players"}B= $Data->{"no_of_Players"} > 2C= $Data->{"no_of_Players"} <= 10
ABCresult
0XX0
10X0
1100
1111
114
115 0 $Self->_GenerateDeckSequence;
116
117 0 return $Self;
118 }
119
120 #################################
121 ## Creates Hand and Discards the pair
122 sub JackThief_Hand {
123 #################################
124 0 ▶ my $Self = shift;
Subroutine JackThief_Hand: not called CC=6 SCAR=37.4
125 0 ▶ while (scalar @{$Self->{AllCards}} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$Self->{"AllCards"};} > 0) 0 0 0
126 {
127 0 ▶ for (my $a = 1; $a <= $Self->{no_of_Players}; $a++)
Branch: 1 branch
BranchTrueFalseTotal
if ($a <= $Self->{"no_of_Players"}) 0 0 0
128 {
129 0 ▶ push(@{$Self->{'Player'.$a}}, pop @{$Self->{AllCards}}) if (scalar @{$Self->{AllCards}} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Self->{"AllCards"};} > 0 0 0 0
130 }
131 }
132 0 $Self->JackThief_DiscardCards($Self->{no_of_Players});
133 # valid hand to start
134 0 my $ValidHand = 1;
135 0 foreach(1..$Self->{no_of_Players})
136 {
137 0 ▶ if (scalar (@{$Self->{'Player'.$_}}) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$$Self{'Player' . $_};} == 0) 0 0 0
138 {
139 0 print "## Not a valid hand Try again ##\n";
140 0 exit;
141 }
142 }
143 #
144 0 return $Self;
145 }
146
147 #################################
148 # Run the Fetch Sequence until looser found
149 sub JackThief_RunFetchRound {
150 #################################
151 0 ▶ my $Self = shift;
Subroutine JackThief_RunFetchRound: not called CC=3 SCAR=24.8
152 0 my $fetch_by = $Self->{FetchSeq}[0];
153 0 my $fetch_from = $Self->{FetchSeq}[1];
154
155 0 my $randSel = int rand @{$Self->{'Player'. $fetch_from}};
156 0 my $ftchd_card = splice(@{$Self->{'Player'. $fetch_from}}, $randSel , 1);
157 0 push (@{$Self->{'Player'. $fetch_by}}, $ftchd_card);
158 0 $Self->JackThief_DiscardCards(1, $Self->{FetchSeq}[0]);
159
160 0 ▶ if (scalar @{$Self->{'Player'. $fetch_by}} == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$$Self{'Player' . $fetch_by};} == 0) { } 0 0 0
elsif (scalar @{$$Self{'Player' . $fetch_from};} == 0) { } 0 0 0
161 {
162 # $fetch_by player wins here;
163 0 $Self->_UpdateFetchSeq(0);
164 }
165 0 elsif (scalar @{$Self->{'Player'. $fetch_from }} == 0)
166 {
167 # $fetch_from player wins here;
168 0 $Self->_UpdateFetchSeq(1);
169 }
170 else
171 {
172 # non of the player wins;
173 0 $Self->_UpdateFetchSeq(2);
174 }
175
176 0 return $Self;
177 }
178
179 #################################
180 # Check looser found or not
181 sub LooserFound {
182 #################################
183 0 ▶ my $Self = shift;
Subroutine LooserFound: not called CC=3 SCAR=24.8
184 0 ▶ return 1 if (scalar (@{$Self->{FetchSeq}}) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Self->{"FetchSeq"};} > 1 0 0 0
185 0 ▶ return 0 if (scalar (@{$Self->{FetchSeq}}) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Self->{"FetchSeq"};} == 1 0 0 0
186 }
187
188 #################################
189 # it actually creates the Fetch Seq
190 sub CreateFetchSeq {
191 #################################
192 0 ▶ my $Self = shift;
Subroutine CreateFetchSeq: not called CC=1 SCAR=6.9
193 0 $Self->{FetchSeq} = [1..$Self->{no_of_Players}];
194 0 return $Self;
195 }
196
197 #################################
198 ## Discards the pair
199 sub JackThief_DiscardCards {
200 #################################
201 0 ▶ my $Self = shift;
Subroutine JackThief_DiscardCards: not called CC=13 SCAR=52.0
202 0 my $Players = shift;
203 0 my $a=shift;
204 0 ▶ $Players = (defined $Players) ? $Players : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $Players ? : 0 0 0
205 0 ▶ $a = (defined $a) ? $a : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $a ? : 0 0 0
206 0 foreach(1..$Players)
207 #for (my $a = 1; $a <= $Players; $a++)
208 {
209 0 my $plyr = 'Player'.$a;
210 0 @{$Self->{$plyr}} = sort (@{$Self->{$plyr}});
211 0 ▶ for (my $b = 0; $b < @{$Self->{$plyr}}; $b++)
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @{$Self->{$plyr};}) 0 0 0
212 {
213 0 ▶ if ((defined $Self->{$plyr}[$b]) && (defined $Self->{$plyr}[$b+1]))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Self->{$plyr}[$b] and defined $Self->{$plyr}[$b + 1]) 0 0 0
Condition0%0/3
defined $Self->{$plyr}[$b] and defined $Self->{$plyr}[$b + 1]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Self->{$plyr}[$b] and defined $Self->{$plyr}[$b + 1]
defined $Self->{$plyr}[$b] defined $Self->{$plyr}[$b + 1]
Truth table0%0/3
A and B
A= defined $Self->{$plyr}[$b]B= defined $Self->{$plyr}[$b + 1]
ABresult
0X0
100
111
214 {
215 0 ▶ if ((($Self->{$plyr}[$b] =~ m/\d+/) && ($Self->{$plyr}[$b+1] =~ m/\d+/)) && ($Self->{$plyr}[$b] == $Self->{$plyr}[$b+1]))
Branch: 2 branches
BranchTrueFalseTotal
if ($Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/ and $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]) { } 0 0 0
elsif ($Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i and $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]) { } 0 0 0
Condition0%0/6
$Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/ and $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]
!ll&&!rl&&r
000
$Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/
!ll&&!rl&&r
000
MC/DC0%0/3
$Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/ and $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]
$Self->{$plyr}[$b] =~ /\d+/ $Self->{$plyr}[$b + 1] =~ /\d+/ $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]
Truth table0%0/4
A and B and C
A= $Self->{$plyr}[$b] =~ /\d+/B= $Self->{$plyr}[$b + 1] =~ /\d+/C= $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]
ABCresult
0XX0
10X0
1100
1111
216 {
217 0 $Self->{$plyr}[$b] = $Self->{$plyr}[$b+1] = "";
218 0 $b++;
219 }
220 ▶ elsif((($Self->{$plyr}[$b] =~ m/\w+/i) && ($Self->{$plyr}[$b+1] =~ m/\w+/i)) && ($Self->{$plyr}[$b] eq $Self->{$plyr}[$b+1]))
Condition0%0/6
$Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i and $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]
!ll&&!rl&&r
000
$Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i
!ll&&!rl&&r
000
MC/DC0%0/3
$Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i and $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]
$Self->{$plyr}[$b] =~ /\w+/i $Self->{$plyr}[$b + 1] =~ /\w+/i $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]
Truth table0%0/4
A and B and C
A= $Self->{$plyr}[$b] =~ /\w+/iB= $Self->{$plyr}[$b + 1] =~ /\w+/iC= $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]
ABCresult
0XX0
10X0
1100
1111
221 {
222 0 $Self->{$plyr}[$b] = $Self->{$plyr}[$b+1] = "";
223 0 $b++;
224 }
225 }
226 }
227 0 @{$Self->{$plyr}} = grep { $_ ne '' } @{$Self->{$plyr}};
228 0 $a++
229 }
230 0 return $Self;
231 }
232
233 ##Private methods
234
235 #################################
236 # rotate the Fetch Seq
237 sub _UpdateFetchSeq {
238 #################################
239 my $Self = shift;
240 my $arg = shift;
241
242 if (!$arg)
243 {
244 shift $Self->{FetchSeq};
245 }
246 elsif($arg == 1)
247 {
248 my $tmp = shift $Self->{FetchSeq};
249 shift $Self->{FetchSeq};
250 push (@{$Self->{FetchSeq}}, $tmp);
251 }
252 elsif($arg == 2)
253 {
254 my $tmp = shift $Self->{FetchSeq};
255 push (@{$Self->{FetchSeq}}, $tmp);
256 }
257 return $Self;
258 }
259
260 #################################
261 ## generates the cards sequences and drops one jack
262 sub _GenerateDeckSequence {
263 #################################
264 my $Self = shift;
265 my @DeckTypes = ('Heart', 'Diamond', 'Spade', 'Club');
266 my @DeckNumbers = ('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K');
267 my $i = 1;
268 my $DropOneJack = 0;
269 foreach(1..$Self->{no_of_Decks})
270 {
271 foreach(@DeckTypes) {
272 my $D = $_;
273 foreach(@DeckNumbers) {
274 my $N = $_;
275 if (($N eq "J") && (!$DropOneJack)) { $DropOneJack++; }
276 else {
277 #$Self->{'AllCards'}->{$i}->{$D} = $N;
278 push(@{$Self->{AllCards}}, $N);
279 $i++;
280 }
281 }
282 }
283 }
284 $Self->_shuffle;
285 return $Self;
286 }
287
288 #################################
289 ## randomize the sequence of cards
290 sub _shuffle {
291 #################################
292 my $Self = shift;
293 my $i;
294 for ($i = @{$Self->{AllCards}}; --$i; ) {
295 my $j = int rand ($i+1);
296 next if $i == $j;
297 @{$Self->{AllCards}}[$i,$j] = @{$Self->{AllCards}}[$j,$i];
298 }
299 }
300
301 #########################################################################################
302
303 =head1 AUTHOR
304
305 Kapil Rathore, C<< <kapil.rthr at gmail.com> >>
306
307 =head1 BUGS
308
309 Please report any bugs or feature requests to C<bug-games-jackthief at rt.cpan.org>, or through
310 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-JackThief>. I will be notified, and then you'll
311 automatically be notified of progress on your bug as I make changes.
312
313
314
315
316 =head1 SUPPORT
317
318 You can find documentation for this module with the perldoc command.
319
320 perldoc Games::JackThief
321
322
323 You can also look for information at:
324
325 =over 4
326
327 =item * RT: CPAN's request tracker (report bugs here)
328
329 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-JackThief>
330
331 =item * AnnoCPAN: Annotated CPAN documentation
332
333 L<http://annocpan.org/dist/Games-JackThief>
334
335 =item * CPAN Ratings
336
337 L<http://cpanratings.perl.org/d/Games-JackThief>
338
339 =item * Search CPAN
340
341 L<http://search.cpan.org/dist/Games-JackThief/>
342
343 =back
344
345
346 =head1 ACKNOWLEDGEMENTS
347
348
349 =head1 LICENSE AND COPYRIGHT
350
351 Copyright 2015 Kapil Rathore.
352
353 This program is free software; you can redistribute it and/or modify it
354 under the terms of the the Artistic License (1.0). You may obtain a
355 copy of the full license at:
356
357 L<http://www.perlfoundation.org/artistic_license_1_0>
358
359 Aggregation of this Package with a commercial distribution is always
360 permitted provided that the use of this Package is embedded; that is,
361 when no overt attempt is made to make this Package's interfaces visible
362 to the end user of the commercial distribution. Such use shall not be
363 construed as a distribution of this Package.
364
365 The name of the Copyright Holder may not be used to endorse or promote
366 products derived from this software without specific prior written
367 permission.
368
369 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
370 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
371 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
372
373
374 =cut
375
376 1; # End of Games::JackThief
↑ Summary