Domino/Player.pm

filter: Statementstmt 54.5% 60 / 110 Branchbran 19.2% 5 / 26 Conditioncond 9.5% 2 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 64.7% 11 / 17 Podpod 100% 5 / 5 totaltotal 43.0% 83 / 193 CC 26 SCAR 31.7
CC
26
Cov
43%
_pick
56.0
_validate_tile
56.0
_validate_index
30.0
CRAP 23.8
↑ Summary Domino.pm »
1 package Games::Domino::Player;
2  
3 3 $Games::Domino::Player::VERSION = '0.22';
4 3 $Games::Domino::Player::AUTHORITY = 'cpan:MANWAR';
5  
6 =head1 NAME
7  
8 Games::Domino::Player - Represents the player of the Domino game.
9  
10 =head1 VERSION
11  
12 Version 0.22
13  
14 =cut
15  
16 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Games::Domino::Params qw($HorC);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use overload ('""' => \&as_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 3 has 'name' => (is => 'ro', isa => $HorC, required => 1);
26 3 has 'bank' => (is => 'rw');
27 3 has 'score' => (is => 'rw');
28 1 ▶has 'show' => (is => 'rw', default => sub { return 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 =head1 DESCRIPTION
31  
32 It is used internally by L<Games::Domino>.
33  
34 =head1 METHODS
35  
36 =head2 save()
37  
38 Saves the given tile to the bank of the player.
39  
40 use strict; use warnings;
41 use Games::Domino::Tile;
42 use Games::Domino::Player;
43  
44 my $player = Games::Domino::Player->new({ name => 'H' });
45 $player->save(Games::Domino::Tile->new({ left => 1, right => 4 }));
46  
47 =cut
48  
49 sub save {
50 18 ▶ my ($self, $tile) = @_;
Subroutine save: called CC=2 SCAR=6.9
Pod documented
51  
52 18 ▶ die("ERROR: Undefined tile found.\n") unless defined $tile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tile 1 17 18
53  
54 17 push @{$self->{bank}}, $tile;
55 }
56  
57 =head2 reset()
58  
59 =cut
60  
61 sub reset {
62 0 ▶ my ($self) = @_;
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
63  
64 0 $self->{bank} = [];
65 0 $self->{score} = 0;
66 }
67  
68 =head2 value()
69  
70 Returns the total value of all the tiles of the current player.
71  
72 use strict; use warnings;
73 use Games::Domino::Tile;
74 use Games::Domino::Player;
75  
76 my $player = Games::Domino::Player->new({ name => 'H' });
77 $player->save(Games::Domino::Tile->new({ left => 1, right => 4 }));
78 $player->save(Games::Domino::Tile->new({ left => 5, right => 3 }));
79 print "The total value of the player is [" . $player->value . "]\n";
80  
81 =cut
82  
83 sub value {
84 1 ▶ my ($self) = @_;
Subroutine value: called CC=2 SCAR=6.9
Pod documented
85  
86 1 $self->{score} = 0;
87 1 foreach (@{$self->{bank}}) {
88 2 $self->{score} += $_->value;
89 }
90 1 return $self->{score};
91 }
92  
93 =head2 pick()
94  
95 Returns a matching tile for the given open ends. If no open ends found it then
96 returns highest value tile from the bank of the player.
97  
98 use strict; use warnings;
99 use Games::Domino::Tile;
100 use Games::Domino::Player;
101  
102 my $player = Games::Domino::Player->new({ name => 'H' });
103 $player->save(Games::Domino::Tile->new({ left => 1, right => 4 }));
104 $player->save(Games::Domino::Tile->new({ left => 5, right => 3 }));
105 my $tile = $player->pick();
106 print "Tile: $tile\n";
107  
108 =cut
109  
110 sub pick {
111 2 ▶ my ($self, $left, $right) = @_;
Subroutine pick: called CC=5 SCAR=16.2
Pod documented
112  
113 2 ▶ return $self->_pick($left, $right)
Branch: 1 branch
BranchTrueFalseTotal
if defined $left and defined $right 0 2 2
Condition66%2/3
defined $left and defined $right
!ll&&!rl&&r
110
MC/DC0%0/2
defined $left and defined $right
defined $left defined $right
Truth table66%2/3
A and B
A= defined $leftB= defined $right
ABresult
0X0
100
111
114 if (defined($left) && defined($right));
115  
116 2 my $i = 0;
117 2 my $pos = 0;
118 2 my $max = 0;
119 2 my $tile = undef;
120  
121 2 foreach (@{$self->{bank}}) {
122 4 ▶ if ($_->value > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->value > $max) 4 0 4
123 4 $max = $_->value;
124 4 $tile = $_;
125 4 $pos = $i;
126 }
127 4 $i++;
128 }
129  
130 2 splice(@{$self->{bank}}, $pos, 1);
131 2 return $tile;
132 }
133  
134 =head2 as_string()
135  
136 Returns the player object as string.This method is overloaded as string context.
137 So if we print the object then this method gets called. You can explictly call
138 this method as well. Suppose the player has 2 tiles then this return something
139 like [1 | 4] == [5 | 3].
140  
141 use strict; use warnings;
142 use Games::Domino::Tile;
143 use Games::Domino::Player;
144  
145 my $player = Games::Domino::Player->new({ name => 'H' });
146 $player->save(Games::Domino::Tile->new({ left => 1, right => 4 }));
147 $player->save(Games::Domino::Tile->new({ left => 5, right => 3 }));
148 print "Player: $player\n";
149  
150 =cut
151  
152 sub as_string {
153 1 ▶ my ($self) = @_;
Subroutine as_string: called CC=3 SCAR=11.1
Pod documented
154  
155 1 my $bank = '';
156 1 foreach (@{$self->{bank}}) {
157 2 ▶ if ($self->show) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->show) { } 2 0 2
158 2 $bank .= sprintf("[%d | %d]==", $_->left, $_->right);
159 } else {
160 0 $bank .= sprintf("[x | x]==");
161 }
162 }
163 1 $bank =~ s/[\=]+\s?$//;
164 1 $bank =~ s/\s+$//;
165 1 return $bank;
166 }
167  
168 #
169 #
170 # PRIVATE METHODS
171  
172 sub _pick {
173 0 ▶ my ($self, $left, $right) = @_;
Subroutine _pick: not called CC=7 SCAR=40.3
174  
175 0 my $i = 0;
176 0 my $pos = 0;
177 0 my $tile = undef;
178  
179 # Find all matching tiles.
180 0 my $matched = {};
181 0 foreach (@{$self->{bank}}) {
182 0 my $L = $_->left;
183 0 my $R = $_->right;
184 0 ▶ if (($left =~ /$L|$R/) || ($right =~ /$L|$R/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left =~ /$L|$R/ or $right =~ /$L|$R/) 0 0 0
Condition0%0/3
$left =~ /$L|$R/ or $right =~ /$L|$R/
l!l&&r!l&&!r
000
MC/DC0%0/2
$left =~ /$L|$R/ or $right =~ /$L|$R/
$left =~ /$L|$R/ $right =~ /$L|$R/
Truth table0%0/3
A or B
A= $left =~ /$L|$R/B= $right =~ /$L|$R/
ABresult
1X1
011
000
185 0 $pos = $i;
186 0 $tile = $_;
187 0 $matched->{$i} = $tile;
188 }
189 0 $i++;
190 }
191  
192 # Pick the maximum value tile among all the matched ones.
193 0 my $pick = undef;
194 0 my $max = 0;
195 0 foreach (keys %{$matched}) {
196 0 ▶ if ($matched->{$_}->value > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matched->{$_}->value > $max) 0 0 0
197 0 $max = $matched->{$_}->value;
198 0 $pick = { i => $_, t => $matched->{$_} };
199 }
200 }
201  
202 0 ▶ if (defined($pick)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pick) 0 0 0
203 0 splice(@{$self->{bank}}, $pick->{i}, 1);
204 0 return $pick->{t};
205 }
206 0 return;
207 }
208  
209 sub _available_indexes {
210 0 ▶ my ($self) = @_;
Subroutine _available_indexes: not called CC=2 SCAR=17.9
211  
212 0 ▶ return 1 if (scalar(@{$self->{bank}}) == 1);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"bank"};} == 1 0 0 0
213 0 return "1..".scalar(@{$self->{bank}});
214 }
215  
216 sub _validate_index {
217 0 ▶ my ($self, $index) = @_;
Subroutine _validate_index: not called CC=5 SCAR=34.0
218  
219 0 ▶ return 0 unless (defined($index) && ($index =~ /^\d+$/));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index and $index =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $index and $index =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $index and $index =~ /^\d+$/
defined $index $index =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $indexB= $index =~ /^\d+$/
ABresult
0X0
100
111
220 0 ▶ return 1 if ((scalar(@{$self->{bank}}) >= $index) && ($index >= 1));
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"bank"};} >= $index and $index >= 1 0 0 0
Condition0%0/3
scalar @{$self->{"bank"};} >= $index and $index >= 1
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @{$self->{"bank"};} >= $index and $index >= 1
scalar @{$self->{"bank"};} >= $index $index >= 1
Truth table0%0/3
A and B
A= scalar @{$self->{"bank"};} >= $indexB= $index >= 1
ABresult
0X0
100
111
221 0 return 0;
222 }
223  
224 sub _validate_tile {
225 0 ▶ my ($self, $index, $left, $right) = @_;
Subroutine _validate_tile: not called CC=7 SCAR=40.3
226  
227 0 ▶ return 0 unless (defined($index) && ($index =~ /^\d+$/));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index and $index =~ /^\d+$/ 0 0 0
Condition0%0/3
defined $index and $index =~ /^\d+$/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $index and $index =~ /^\d+$/
defined $index $index =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $indexB= $index =~ /^\d+$/
ABresult
0X0
100
111
228 0 ▶ return 1 unless (defined $left && defined $right);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $left and defined $right 0 0 0
Condition0%0/3
defined $left and defined $right
!ll&&!rl&&r
000
MC/DC0%0/2
defined $left and defined $right
defined $left defined $right
Truth table0%0/3
A and B
A= defined $leftB= defined $right
ABresult
0X0
100
111
229  
230 0 my $tile = $self->{bank}->[$index-1];
231 0 my $L = $tile->left;
232 0 my $R = $tile->right;
233  
234 0 ▶ return 1 if (($left =~ /$L|$R/) || ($right =~ /$L|$R/));
Branch: 1 branch
BranchTrueFalseTotal
if $left =~ /$L|$R/ or $right =~ /$L|$R/ 0 0 0
Condition0%0/3
$left =~ /$L|$R/ or $right =~ /$L|$R/
l!l&&r!l&&!r
000
MC/DC0%0/2
$left =~ /$L|$R/ or $right =~ /$L|$R/
$left =~ /$L|$R/ $right =~ /$L|$R/
Truth table0%0/3
A or B
A= $left =~ /$L|$R/B= $right =~ /$L|$R/
ABresult
1X1
011
000
235 0 return 0;
236 }
237  
238 sub _tile {
239 0 ▶ my ($self, $index) = @_;
Subroutine _tile: not called CC=1 SCAR=6.9
240  
241 0 return $self->{bank}->[$index-1];
242 }
243  
244 =head1 AUTHOR
245  
246 Mohammad S Anwar, C<< <mohammad.anwar at yahoo.com> >>
247  
248 =head1 REPOSITORY
249  
250 L<https://github.com/manwar/Games-Domino>
251  
252 =head1 BUGS
253  
254 Please report any bugs or feature requests to C<bug-games-domino at rt.cpan.org>,
255 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Games-Domino>.
256 I will be notified, and then you'll automatically be notified of progress on your
257 bug as I make changes.
258  
259 =head1 SUPPORT
260  
261 You can find documentation for this module with the perldoc command.
262  
263 perldoc Games::Domino::Player
264  
265 You can also look for information at:
266  
267 =over 4
268  
269 =item * RT: CPAN's request tracker (report bugs here)
270  
271 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Games-Domino>
272  
273 =item * AnnoCPAN: Annotated CPAN documentation
274  
275 L<http://annocpan.org/dist/Games-Domino>
276  
277 =item * CPAN Ratings
278  
279 L<http://cpanratings.perl.org/d/Games-Domino>
280  
281 =item * Search CPAN
282  
283 L<http://search.cpan.org/dist/Games-Domino/>
284  
285 =back
286  
287 =head1 LICENSE AND COPYRIGHT
288  
289 Copyright 2012 - 2015 Mohammad S Anwar.
290  
291 This program is free software; you can redistribute it and / or modify it under
292 the terms of the the Artistic License (2.0). You may obtain a copy of the full
293 license at:
294  
295 L<http://www.perlfoundation.org/artistic_license_2_0>
296  
297 Any use, modification, and distribution of the Standard or Modified Versions is
298 governed by this Artistic License.By using, modifying or distributing the Package,
299 you accept this license. Do not use, modify, or distribute the Package, if you do
300 not accept this license.
301  
302 If your Modified Version has been derived from a Modified Version made by someone
303 other than you,you are nevertheless required to ensure that your Modified Version
304 complies with the requirements of this license.
305  
306 This license does not grant you the right to use any trademark, service mark,
307 tradename, or logo of the Copyright Holder.
308  
309 This license includes the non-exclusive, worldwide, free-of-charge patent license
310 to make, have made, use, offer to sell, sell, import and otherwise transfer the
311 Package with respect to any patent claims licensable by the Copyright Holder that
312 are necessarily infringed by the Package. If you institute patent litigation
313 (including a cross-claim or counterclaim) against any party alleging that the
314 Package constitutes direct or contributory patent infringement,then this Artistic
315 License to you shall terminate on the date that such litigation is filed.
316  
317 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
318 CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
319 WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
320 NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
321 REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
322 INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
323 OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
324  
325 =cut
326  
327 3 1; # End of Games::Domino::Player
↑ Summary Domino.pm »