Pair.pm

filter: Statementstmt 70.5% 108 / 153 Branchbran 46.1% 12 / 26 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 71.8% 23 / 32 Podpod 100% 7 / 7 totaltotal 63.8% 150 / 235 SCAR 40.4
CC
25
Cov
63%
is_valid_card_count
30.0
_process
12.0
_seen
12.0
CRAP 57.1
↑ Summary Pair/Card.pm »
1 package Games::Cards::Pair;
2  
3 5 $Games::Cards::Pair::VERSION = '0.18';
4 5 $Games::Cards::Pair::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Games::Cards::Pair - Interface to the Pelmanism (Pair) Card Game.
9  
10 =head1 VERSION
11  
12 Version 0.18
13  
14 =cut
15  
16 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 5 ▶use Attribute::Memoize;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 5 ▶use List::Util qw(shuffle);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use Term::Screen::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use Types::Standard qw(Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Games::Cards::Pair::Params qw(ZeroOrOne Cards);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Games::Cards::Pair::Card;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 ▶use overload ('""' => \&as_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 has [ qw(bank seen) ] => (is => 'rw', isa => Cards);
33 5 ▶has 'cards' => (is => 'rw', default => sub { 12 });
Subroutine __ANON__: called CC=1 SCAR=0.0
34 5 has 'board' => (is => 'rw');
35 5 has 'available' => (is => 'ro', isa => Cards);
36 3 ▶has 'screen' => (is => 'ro', default => sub { Term::Screen::Lite->new; });
Subroutine __ANON__: called CC=1 SCAR=0.0
37 5 ▶has 'count' => (is => 'rw', isa => Int, default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
38 3 ▶has 'debug' => (is => 'rw', isa => ZeroOrOne, default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 =head1 DESCRIPTION
41  
42 A single-player game of Pelmanism, played with minimum of 12 cards and maximum up
43 to 54 cards. Depending on number of cards choosen the user, it prepares the game.
44  
45 Cards picked up from the collection comprises each of the thirteen values (2,3,4,
46 5,6,7,8,9,10,Queen,King,Ace and Jack) in each of the four suits (Clubs,Diamonds,
47 Hearts and Spades) plus two jokers. The Joker will not have any suit.
48  
49 The game script C<play-pelmanism> is supplied with the distribution and on install
50 is available to play with.
51  
52 USAGE: play-pelmanism [-h] [long options...]
53  
54 --cards=Int Cards count (min:12, max:54).
55 --verbose Play the game in verbose mode.
56  
57 --usage show a short help message
58 -h show a compact help message
59 --help show a long help message
60 --man show the manual
61  
62 =head1 METHODS
63  
64 =head2 play($index)
65  
66 Accepts comma separated card indices and play the game.
67  
68 =cut
69  
70 sub play {
71 2 ▶ my ($self, $index) = @_;
Subroutine play: called CC=2 SCAR=7.2
Pod documented
72  
73 2 my ($card, $new) = $self->_pick($index);
74  
75 1 ▶ if ($new->equal($card)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new->equal($card)) { } 0 1 1
76 0 $self->_process($new, $card);
77 }
78 else {
79 1 $self->{deck}->{$new->index} = $new;
80 1 $self->{deck}->{$card->index} = $card;
81 }
82 }
83  
84 =head2 is_over()
85  
86 Returns 1 or 0 depending if the deck is empty or not.
87  
88 =cut
89  
90 sub is_over {
91 1 ▶ my ($self) = @_;
Subroutine is_over: called CC=1 SCAR=0.0
Pod documented
92  
93 1 return (scalar(@{$self->{available}}) == 0);
94 }
95  
96 =head2 get_board()
97  
98 Return game board with hidden card, showing only the card index.
99  
100 =cut
101  
102 sub get_board {
103 0 ▶ return $_[0]->as_string(0);
Subroutine get_board: not called CC=1 SCAR=6.9
Pod documented
104 }
105  
106 =head2 is_valid_card_count($count)
107  
108 Valid card count is any number between 12 and 54 (both inclusive). Also it should
109 be a multiple of 4.
110  
111 =cut
112  
113 sub is_valid_card_count {
114 0 ▶ my ($self, $count) = @_;
Subroutine is_valid_card_count: not called CC=5 SCAR=34.0
Pod documented
115  
116 0 ▶ return (defined $count
Condition0%0/12
defined $count && $count =~ /^\d+$/ && ($count >= 12 || $count <= 54) && $count % 4 == 0
!ll&&!rl&&r
000
defined $count and $count =~ /^\d+$/ and $count >= 12 || $count <= 54
!ll&&!rl&&r
000
defined $count and $count =~ /^\d+$/
!ll&&!rl&&r
000
$count >= 12 or $count <= 54
l!l&&r!l&&!r
000
MC/DC0%0/5
defined $count && $count =~ /^\d+$/ && ($count >= 12 || $count <= 54) && $count % 4 == 0
defined $count $count =~ /^\d+$/ $count >= 12 $count <= 54 $count % 4 == 0
Truth table0%0/7
A and B and C or D && E
A= defined $countB= $count =~ /^\d+$/C= $count >= 12D= $count <= 54E= $count % 4 == 0
ABCDEresult
0XXXX0
10XXX0
1100X0
111X00
110100
111X11
110111
117 && ($count =~ /^\d+$/)
118 && ($count >=12 || $count <= 54)
119 && ($count % 4 == 0));
120 }
121  
122 =head2 init()
123  
124 Shuffles the pack and then pick required number of cards.
125  
126 =cut
127  
128 sub init {
129 2 ▶ my ($self) = @_;
Subroutine init: called CC=5 SCAR=16.1
Pod documented
130  
131 2 my $cards = [];
132 2 $self->{available} = [];
133 2 my $i = $self->cards / 4;
134 2 foreach my $suit (qw(C D H S)) {
135 8 my $j = 1;
136 8 foreach my $value (qw(A 2 3 4 5 6 7 8 9 10 J Q K)) {
137 104 ▶ next if ($j > $i);
Branch: 1 branch
BranchTrueFalseTotal
if $j > $i 80 24 104
138 24 push @$cards, Games::Cards::Pair::Card->new({ suit => $suit, value => $value });
139 24 $j++;
140 }
141 }
142  
143 # Adding two Jokers to the Suit.
144 2 push @$cards, Games::Cards::Pair::Card->new({ value => 'Joker' });
145 2 push @$cards, Games::Cards::Pair::Card->new({ value => 'Joker' });
146 2 push @{$self->{available}}, $_ for (0..$self->cards-1);
147  
148 # Index card after shuffle.
149 2 $self->_index($cards);
150 }
151  
152 =head2 get_matched_pairs()
153  
154 Returns all the matching pair, if any found, from the bank.
155  
156 =cut
157  
158 sub get_matched_pairs {
159 0 ▶ my ($self) = @_;
Subroutine get_matched_pairs: not called CC=2 SCAR=17.9
Pod documented
160  
161 0 my $string = '';
162 0 foreach (@{$self->{bank}}) {
163 0 $string .= sprintf("%s %s\n", $_->[0], $_->[1]);
164 }
165  
166 0 return $string;
167 }
168  
169 =head2 as_string()
170  
171 Returns deck arranged as 4 in a row blocks. This is overloaded as string context.
172  
173 =cut
174  
175 sub as_string {
176 1 ▶ my ($self, $hide) = @_;
Subroutine as_string: called CC=5 SCAR=16.3
Pod documented
177  
178 1 my $deck = '';
179 1 foreach my $i (1..$self->cards) {
180 12 my $card = $self->{deck}->{$i-1};
181 12 my $c = ' ';
182 12 ▶ if (defined $card) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $card) 12 0 12
183 12 ▶ if ($hide) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hide) { } 0 12 12
184 0 $c = $i;
185 }
186 else {
187 12 $c = $card->as_string;
188 }
189 }
190  
191 12 $deck .= sprintf("[ %5s ]", $c);
192 12 ▶ $deck .= "\n" if ($i % 4 == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $i % 4 == 0 3 9 12
193 }
194  
195 1 return $deck;
196 }
197  
198 #
199 #
200 # PRIVATE METHODS
201  
202 sub _pick {
203 2 ▶ my ($self, $index) = @_;
Subroutine _pick: called CC=3 SCAR=11.0
204  
205 2 $self->{count}++;
206  
207 2 my ($i, $j) = split /\,/, $index, 2;
208 2 --$i; --$j;
209 2 my $c1 = $self->{deck}->{$i};
210 2 ▶ die "ERROR: Invalid card received [$i].\n" unless defined $c1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c1 1 1 2
211  
212 1 my $c2 = $self->{deck}->{$j};
213 1 ▶ die "ERROR: Invalid card received [$j].\n" unless defined $c2;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c2 0 1 1
214  
215 1 push @{$self->{seen}}, $c1, $c2;
216  
217 1 return ($c1, $c2);
218 }
219  
220 sub _save {
221 0 ▶ my ($self, @cards) = @_;
Subroutine _save: not called CC=2 SCAR=17.9
222  
223 0 ▶ die("ERROR: Expecting atleast a pair of cards.\n") unless (scalar(@cards) == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @cards == 2 0 0 0
224  
225 0 push @{$self->{bank}}, [@cards];
226 }
227  
228 sub _process {
229 0 ▶ my ($self, $card, $new) = @_;
Subroutine _process: not called CC=3 SCAR=24.8
230  
231 0 $self->{deck}->{$new->index} = undef;
232 0 $self->{deck}->{$card->index} = undef;
233 0 $self->_save($card, $new);
234  
235 0 ▶ my $index = first_index { $_ == $new->index } @{$self->{available}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
236 0 ▶ splice(@{$self->{available}}, $index, 1) if ($index != -1);
Branch: 1 branch
BranchTrueFalseTotal
if $index != -1 0 0 0
237  
238 0 ▶ $index = first_index { $_ == $card->index } @{$self->{available}};
Subroutine __ANON__: not called CC=1 SCAR=6.9
239 0 ▶ splice(@{$self->{available}}, $index, 1) if ($index != -1);
Branch: 1 branch
BranchTrueFalseTotal
if $index != -1 0 0 0
240 }
241  
242 sub _index {
243 2 ▶ my ($self, $cards) = @_;
Subroutine _index: called CC=3 SCAR=11.0
244  
245 2 $cards = [shuffle @{$cards}];
246 2 my $index = 0;
247 2 foreach my $card (@{$cards}) {
248 24 $card->index($index);
249 24 $self->{deck}->{$index} = $card;
250 24 $index++;
251 24 ▶ last if ($self->cards == $index);
Branch: 1 branch
BranchTrueFalseTotal
if $self->cards == $index 2 22 24
252 }
253 }
254  
255 sub _draw {
256 0 ▶ my ($self) = @_;
Subroutine _draw: not called CC=2 SCAR=17.9
257  
258 0 my @random = shuffle(@{$self->{available}});
259 0 my $index = shift @random;
260  
261 0 ▶ return $self->{deck}->{$index} if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 0 0 0
262 0 return;
263 }
264  
265 sub _seen :Memoize {
266 0 ▶ my ($self, $card) = @_;
Subroutine _seen: not called CC=3 SCAR=24.8
267  
268 0 my $index = 0;
269 0 foreach (@{$self->{seen}}) {
270 0 ▶ if ($card->equal($_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($card->equal($_)) 0 0 0
271 0 splice(@{$self->{seen}}, $index, 1);
272 0 return $_;
273 }
274 0 $index++;
275 }
276  
277 0 return;
278 5 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
279  
280 =head1 AUTHOR
281  
282 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
283  
284 =head1 REPOSITORY
285  
286 L<https://github.com/manwar/Games-Cards-Pair>
287  
288 =head1 BUGS
289  
290 Please report any bugs / feature requests to C<bug-games-cards-pair at rt.cpan.org>
291 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Cards-Pair>.
292 I will be notified and then you'll automatically be notified of progress on your
293 bug as I make changes.
294  
295 =head1 SUPPORT
296  
297 You can find documentation for this module with the perldoc command.
298  
299 perldoc Games::Cards::Pair
300  
301 You can also look for information at:
302  
303 =over 4
304  
305 =item * RT: CPAN's request tracker (report bugs here)
306  
307 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Cards-Pair>
308  
309 =item * AnnoCPAN: Annotated CPAN documentation
310  
311 L<http://annocpan.org/dist/Games-Cards-Pair>
312  
313 =item * CPAN Ratings
314  
315 L<http://cpanratings.perl.org/d/Games-Cards-Pair>
316  
317 =item * Search CPAN
318  
319 L<http://search.cpan.org/dist/Games-Cards-Pair/>
320  
321 =back
322  
323 =head1 LICENSE AND COPYRIGHT
324  
325 Copyright (C) 2012 - 2016 Mohammad S Anwar.
326  
327 This program is free software; you can redistribute it and / or modify it under
328 the terms of the the Artistic License (2.0). You may obtain a copy of the full
329 license at:
330  
331 L<http://www.perlfoundation.org/artistic_license_2_0>
332  
333 Any use, modification, and distribution of the Standard or Modified Versions is
334 governed by this Artistic License.By using, modifying or distributing the Package,
335 you accept this license. Do not use, modify, or distribute the Package, if you do
336 not accept this license.
337  
338 If your Modified Version has been derived from a Modified Version made by someone
339 other than you,you are nevertheless required to ensure that your Modified Version
340 complies with the requirements of this license.
341  
342 This license does not grant you the right to use any trademark, service mark,
343 tradename, or logo of the Copyright Holder.
344  
345 This license includes the non-exclusive, worldwide, free-of-charge patent license
346 to make, have made, use, offer to sell, sell, import and otherwise transfer the
347 Package with respect to any patent claims licensable by the Copyright Holder that
348 are necessarily infringed by the Package. If you institute patent litigation
349 (including a cross-claim or counterclaim) against any party alleging that the
350 Package constitutes direct or contributory patent infringement,then this Artistic
351 License to you shall terminate on the date that such litigation is filed.
352  
353 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
354 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
355 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
356 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
357 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
358 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
359 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360  
361 =cut
362  
363 5 1; # End of Games::Cards::Pair
↑ Summary Pair/Card.pm »