blib/lib/Games/JackThief.pm

filter: Statementstmt 89.0% 114 / 128 Branchbran 77.2% 34 / 44 Conditioncond 73.3% 22 / 30 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 100% 13 / 13 Podpod 0.0% 0 / 6 totaltotal 81.0% 192 / 237 CC 38 SCAR 19.6
CC
38
Cov
84%
JackThief_DiscardCards
13.2
new
7.2
JackThief_Hand
6.1
CRAP 7.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 4 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
65 4 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
66 =head1 NAME
67
68 Games::JackThief - The great new Games::JackThief!
69
70 =head1 VERSION
71
72 Version 0.02
73
74 =cut
75
76 4 our $VERSION = '0.02';
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 5 ▶sub new {
Subroutine new: called CC=7 SCAR=19.8
Pod undocumented
105 #################################
106 5 my $Class = shift;
107 5 my $Data = shift;
108
109 5 my $Self = {};
110 5 bless $Self, $Class;
111
112 5 ▶ $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 ? : 2 3 5
Condition66%4/6
defined $Data->{"no_of_Decks"} and $Data->{"no_of_Decks"} > 0 and $Data->{"no_of_Decks"} <= 5
!ll&&!rl&&r
302
defined $Data->{"no_of_Decks"} and $Data->{"no_of_Decks"} > 0
!ll&&!rl&&r
302
MC/DC33%1/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 table50%2/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 5 ▶ $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 ? : 2 3 5
Condition66%4/6
defined $Data->{"no_of_Players"} and $Data->{"no_of_Players"} > 2 and $Data->{"no_of_Players"} <= 10
!ll&&!rl&&r
302
defined $Data->{"no_of_Players"} and $Data->{"no_of_Players"} > 2
!ll&&!rl&&r
302
MC/DC33%1/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 table50%2/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 5 $Self->_GenerateDeckSequence;
116
117 5 return $Self;
118 }
119
120 #################################
121 ## Creates Hand and Discards the pair
122 1 ▶sub JackThief_Hand {
Subroutine JackThief_Hand: called CC=6 SCAR=18.0
Pod undocumented
123 #################################
124 1 my $Self = shift;
125 1 ▶ while (scalar @{$Self->{AllCards}} > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$Self->{"AllCards"};} > 0) 26 1 27
126 {
127 26 ▶ for (my $a = 1; $a <= $Self->{no_of_Players}; $a++)
Branch: 1 branch
BranchTrueFalseTotal
if ($a <= $Self->{"no_of_Players"}) 260 26 286
128 {
129 260 ▶ push(@{$Self->{'Player'.$a}}, pop @{$Self->{AllCards}}) if (scalar @{$Self->{AllCards}} > 0);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Self->{"AllCards"};} > 0 259 1 260
130 }
131 }
132 1 $Self->JackThief_DiscardCards($Self->{no_of_Players});
133 # valid hand to start
134 1 my $ValidHand = 1;
135 1 foreach(1..$Self->{no_of_Players})
136 {
137 10 ▶ if (scalar (@{$Self->{'Player'.$_}}) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$$Self{'Player' . $_};} == 0) 0 10 10
138 {
139 0 print "## Not a valid hand Try again ##\n";
140 0 exit;
141 }
142 }
143 #
144 1 return $Self;
145 }
146
147 #################################
148 # Run the Fetch Sequence until looser found
149 1 ▶sub JackThief_RunFetchRound {
Subroutine JackThief_RunFetchRound: called CC=3 SCAR=11.2
Pod undocumented
150 #################################
151 1 my $Self = shift;
152 1 my $fetch_by = $Self->{FetchSeq}[0];
153 1 my $fetch_from = $Self->{FetchSeq}[1];
154
155 1 my $randSel = int rand @{$Self->{'Player'. $fetch_from}};
156 1 my $ftchd_card = splice(@{$Self->{'Player'. $fetch_from}}, $randSel , 1);
157 1 push (@{$Self->{'Player'. $fetch_by}}, $ftchd_card);
158 1 $Self->JackThief_DiscardCards(1, $Self->{FetchSeq}[0]);
159
160 1 ▶ if (scalar @{$Self->{'Player'. $fetch_by}} == 0)
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @{$$Self{'Player' . $fetch_by};} == 0) { } 0 1 1
elsif (scalar @{$$Self{'Player' . $fetch_from};} == 0) { } 0 1 1
161 {
162 # $fetch_by player wins here;
163 0 $Self->_UpdateFetchSeq(0);
164 }
165 1 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 1 $Self->_UpdateFetchSeq(2);
174 }
175
176 1 return $Self;
177 }
178
179 #################################
180 # Check looser found or not
181 1 ▶sub LooserFound {
Subroutine LooserFound: called CC=3 SCAR=15.1
Pod undocumented
182 #################################
183 1 my $Self = shift;
184 1 ▶ return 1 if (scalar (@{$Self->{FetchSeq}}) > 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$Self->{"FetchSeq"};} > 1 1 0 1
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 1 ▶sub CreateFetchSeq {
Subroutine CreateFetchSeq: called CC=1 SCAR=0.0
Pod undocumented
191 #################################
192 1 my $Self = shift;
193 1 $Self->{FetchSeq} = [1..$Self->{no_of_Players}];
194 1 return $Self;
195 }
196
197 #################################
198 ## Discards the pair
199 2 ▶sub JackThief_DiscardCards {
Subroutine JackThief_DiscardCards: called CC=13 SCAR=25.8
Pod undocumented
200 #################################
201 2 my $Self = shift;
202 2 my $Players = shift;
203 2 my $a=shift;
204 2 ▶ $Players = (defined $Players) ? $Players : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $Players ? : 2 0 2
205 2 ▶ $a = (defined $a) ? $a : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $a ? : 1 1 2
206 2 foreach(1..$Players)
207 #for (my $a = 1; $a <= $Players; $a++)
208 {
209 11 my $plyr = 'Player'.$a;
210 11 @{$Self->{$plyr}} = sort (@{$Self->{$plyr}});
211 11 ▶ for (my $b = 0; $b < @{$Self->{$plyr}}; $b++)
Branch: 1 branch
BranchTrueFalseTotal
if ($b < @{$Self->{$plyr};}) 166 11 177
212 {
213 166 ▶ 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]) 159 7 166
Condition66%2/3
defined $Self->{$plyr}[$b] and defined $Self->{$plyr}[$b + 1]
!ll&&!rl&&r
07159
MC/DC50%1/2
defined $Self->{$plyr}[$b] and defined $Self->{$plyr}[$b + 1]
defined $Self->{$plyr}[$b] defined $Self->{$plyr}[$b + 1]
Truth table66%2/3
A and B
A= defined $Self->{$plyr}[$b]B= defined $Self->{$plyr}[$b + 1]
ABresult
0X0
100
111
214 {
215 159 ▶ 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]) { } 68 91 159
elsif ($Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i and $Self->{$plyr}[$b] eq $Self->{$plyr}[$b + 1]) { } 30 61 91
Condition100%6/6
$Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/ and $Self->{$plyr}[$b] == $Self->{$plyr}[$b + 1]
!ll&&!rl&&r
494268
$Self->{$plyr}[$b] =~ /\d+/ and $Self->{$plyr}[$b + 1] =~ /\d+/
!ll&&!rl&&r
436110
MC/DC100%3/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 table100%4/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 68 $Self->{$plyr}[$b] = $Self->{$plyr}[$b+1] = "";
218 68 $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]))
Condition50%3/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
06130
$Self->{$plyr}[$b] =~ /\w+/i and $Self->{$plyr}[$b + 1] =~ /\w+/i
!ll&&!rl&&r
0091
MC/DC33%1/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 table50%2/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 30 $Self->{$plyr}[$b] = $Self->{$plyr}[$b+1] = "";
223 30 $b++;
224 }
225 }
226 }
227 11 @{$Self->{$plyr}} = grep { $_ ne '' } @{$Self->{$plyr}};
228 11 $a++
229 }
230 2 return $Self;
231 }
232
233 ##Private methods
234
235 #################################
236 # rotate the Fetch Seq
237 1 ▶sub _UpdateFetchSeq {
Subroutine _UpdateFetchSeq: called CC=4 SCAR=17.9
238 #################################
239 1 my $Self = shift;
240 1 my $arg = shift;
241 1 ▶ if (!$arg)
Branch: 3 branches
BranchTrueFalseTotal
if (not $arg) { } 0 1 1
elsif ($arg == 1) { } 0 1 1
elsif ($arg == 2) { } 1 0 1
242 {
243 0 shift @{$Self->{FetchSeq}};
244 }
245 elsif($arg == 1)
246 {
247 0 my $tmp = shift @{$Self->{FetchSeq}};
248 0 shift @{$Self->{FetchSeq}};
249 0 push (@{$Self->{FetchSeq}}, $tmp);
250 }
251 elsif($arg == 2)
252 {
253 1 my $tmp = shift @{$Self->{FetchSeq}};
254 1 push (@{$Self->{FetchSeq}}, $tmp);
255 }
256 1 return $Self;
257 }
258
259 #################################
260 ## generates the cards sequences and drops one jack
261 5 ▶sub _GenerateDeckSequence {
Subroutine _GenerateDeckSequence: called CC=6 SCAR=17.9
262 #################################
263 5 my $Self = shift;
264 5 my @DeckTypes = ('Heart', 'Diamond', 'Spade', 'Club');
265 5 my @DeckNumbers = ('A', 2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K');
266 5 my $i = 1;
267 5 my $DropOneJack = 0;
268 5 foreach(1..$Self->{no_of_Decks})
269 {
270 13 foreach(@DeckTypes) {
271 52 my $D = $_;
272 52 foreach(@DeckNumbers) {
273 676 my $N = $_;
274 676 ▶ if (($N eq "J") && (!$DropOneJack)) { $DropOneJack++; }
Branch: 1 branch
BranchTrueFalseTotal
if ($N eq 'J' and not $DropOneJack) { } 5 671 676
Condition100%3/3
$N eq "J" and not $DropOneJack
!ll&&!rl&&r
624475
MC/DC100%2/2
$N eq "J" and not $DropOneJack
$N eq "J" not $DropOneJack
Truth table100%3/3
A and not(B)
A= $N eq "J"B= not $DropOneJack
ABresult
0X0
100
111
275 else {
276 #$Self->{'AllCards'}->{$i}->{$D} = $N;
277 671 push(@{$Self->{AllCards}}, $N);
278 671 $i++;
279 }
280 }
281 }
282 }
283 5 $Self->_shuffle;
284 5 return $Self;
285 }
286
287 #################################
288 ## randomize the sequence of cards
289 5 ▶sub _shuffle {
Subroutine _shuffle: called CC=3 SCAR=11.0
290 #################################
291 5 my $Self = shift;
292 5 my $i;
293 5 ▶ for ($i = @{$Self->{AllCards}}; --$i; ) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$i) 666 5 671
294 666 my $j = int rand ($i+1);
295 666 ▶ next if $i == $j;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $j 23 643 666
296 643 @{$Self->{AllCards}}[$i,$j] = @{$Self->{AllCards}}[$j,$i];
297 }
298 }
299
300 #########################################################################################
301
302 =head1 AUTHOR
303
304 Kapil Rathore, C<< <kapil.rthr at gmail.com> >>
305
306 =head1 BUGS
307
308 Please report any bugs or feature requests to C<bug-games-jackthief at rt.cpan.org>, or through
309 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-JackThief>. I will be notified, and then you'll
310 automatically be notified of progress on your bug as I make changes.
311
312
313
314
315 =head1 SUPPORT
316
317 You can find documentation for this module with the perldoc command.
318
319 perldoc Games::JackThief
320
321
322 You can also look for information at:
323
324 =over 4
325
326 =item * RT: CPAN's request tracker (report bugs here)
327
328 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-JackThief>
329
330 =item * AnnoCPAN: Annotated CPAN documentation
331
332 L<http://annocpan.org/dist/Games-JackThief>
333
334 =item * CPAN Ratings
335
336 L<http://cpanratings.perl.org/d/Games-JackThief>
337
338 =item * Search CPAN
339
340 L<http://search.cpan.org/dist/Games-JackThief/>
341
342 =back
343
344
345 =head1 ACKNOWLEDGEMENTS
346
347
348 =head1 LICENSE AND COPYRIGHT
349
350 Copyright 2015 Kapil Rathore.
351
352 This program is free software; you can redistribute it and/or modify it
353 under the terms of the the Artistic License (1.0). You may obtain a
354 copy of the full license at:
355
356 L<http://www.perlfoundation.org/artistic_license_1_0>
357
358 Aggregation of this Package with a commercial distribution is always
359 permitted provided that the use of this Package is embedded; that is,
360 when no overt attempt is made to make this Package's interfaces visible
361 to the end user of the commercial distribution. Such use shall not be
362 construed as a distribution of this Package.
363
364 The name of the Copyright Holder may not be used to endorse or promote
365 products derived from this software without specific prior written
366 permission.
367
368 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
369 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
370 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
371
372
373 =cut
374
375 4 1; # End of Games::JackThief
↑ Summary