File Coverage

blib/lib/Games/Domino/Tile.pm
Criterion Covered Total %
statement 21 21 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 2 2 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
1             package Games::Domino::Tile;
2              
3             $Games::Domino::Tile::VERSION = '0.32';
4             $Games::Domino::Tile::AUTHORITY = 'cpan:MANWAR';
5              
6             =head1 NAME
7              
8             Games::Domino::Tile - Represents the tile of the Domino game.
9              
10             =head1 VERSION
11              
12             Version 0.32
13              
14             =cut
15              
16 4     4   959 use 5.006;
  4         20  
17 4     4   1060 use Data::Dumper;
  4         15418  
  4         224  
18 4     4   1501 use Games::Domino::Params qw(ZeroOrOne ZeroToSix);
  4         13  
  4         35  
19              
20 4     4   2546 use Moo;
  4         31385  
  4         25  
21 4     4   4771 use namespace::autoclean;
  4         25230  
  4         15  
22              
23 4     4   226 use overload q{""} => 'as_string', fallback => 1;
  4         7  
  4         34  
24              
25             has 'left' => (is => 'rw', isa => ZeroToSix, required => 1);
26             has 'right' => (is => 'rw', isa => ZeroToSix, required => 1);
27             has 'double' => (is => 'ro', isa => ZeroOrOne, required => 1);
28             has 'top' => (is => 'rw', isa => ZeroToSix);
29             has 'bottom' => (is => 'rw', isa => ZeroToSix);
30             has 'color' => (is => 'rw', default => sub { 'blue' });
31              
32             =head1 DESCRIPTION
33              
34             It is used internally by L.
35              
36             =cut
37              
38             around BUILDARGS => sub {
39             my $orig = shift;
40             my $class = shift;
41              
42             unless (exists $_[0]->{double}) {
43             if (defined($_[0]->{left})
44             && defined($_[0]->{right}) && ($_[0]->{left} == $_[0]->{right})) {
45             $_[0]->{double} = 1;
46             $_[0]->{top} = $_[0]->{bottom} = $_[0]->{left};
47             }
48             else {
49             $_[0]->{double} = 0;
50             }
51             }
52              
53             die("ERROR: Invalid double attribute for the tile.\n")
54             if (defined($_[0]->{left})
55             && defined($_[0]->{right})
56             && ( (($_[0]->{left} == $_[0]->{right}) && ($_[0]->{double} != 1))
57             ||
58             (($_[0]->{left} != $_[0]->{right}) && ($_[0]->{double} != 0))
59             )
60             );
61              
62             if ($_[0]->{double} == 1) {
63             $_[0]->{top} = $_[0]->{bottom} = $_[0]->{left};
64             }
65              
66             return $class->$orig(@_);
67             };
68              
69             =head1 METHODS
70              
71             =head2 value()
72              
73             Returns the value of the tile i.e. sum of left and right bips.
74              
75             use strict; use warnings;
76             use Games::Domino::Tile;
77              
78             my $tile = Games::Domino::Tile->new({ left => 1, right => 4 });
79             print "Value of the tile is [" . $tile->value . "].\n";
80              
81             =cut
82              
83             sub value {
84 11     11 1 2005 my ($self) = @_;
85              
86 11         30 return ($self->{left} + $self->{right});
87             }
88              
89             =head2 as_string()
90              
91             Returns the tile object as string. This method is overloaded as string context.So
92             if we print the object then this method gets called. You can explictly call this
93             method as well. Suppose the tile has 3 left pips and 6 right pips then this would
94             return it as [3 | 6].
95              
96             use strict; use warnings;
97             use Games::Domino::Tile;
98              
99             my $tile = Games::Domino::Tile->new({ left => 1, right => 4 });
100             print "The tile is $tile\n";
101             # same as above
102             print "The tile is " . $tile->as_string() . "\n";
103              
104             =cut
105              
106             sub as_string {
107 18     18 1 197 my ($self) = @_;
108              
109 18         256 return sprintf("[%d | %d]", $self->left, $self->right);
110             }
111              
112             =head1 AUTHOR
113              
114             Mohammad S Anwar, C<< >>
115              
116             =head1 REPOSITORY
117              
118             L
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests to C,
123             or through the web interface at L.
124             I will be notified, and then you'll automatically be notified of progress on your
125             bug as I make changes.
126              
127             =head1 SUPPORT
128              
129             You can find documentation for this module with the perldoc command.
130              
131             perldoc Games::Domino::Tile
132              
133             You can also look for information at:
134              
135             =over 4
136              
137             =item * RT: CPAN's request tracker (report bugs here)
138              
139             L
140              
141             =item * AnnoCPAN: Annotated CPAN documentation
142              
143             L
144              
145             =item * CPAN Ratings
146              
147             L
148              
149             =item * Search CPAN
150              
151             L
152              
153             =back
154              
155             =head1 LICENSE AND COPYRIGHT
156              
157             Copyright 2012 - 2016 Mohammad S Anwar.
158              
159             This program is free software; you can redistribute it and / or modify it under
160             the terms of the the Artistic License (2.0). You may obtain a copy of the full
161             license at:
162              
163             L
164              
165             Any use, modification, and distribution of the Standard or Modified Versions is
166             governed by this Artistic License.By using, modifying or distributing the Package,
167             you accept this license. Do not use, modify, or distribute the Package, if you do
168             not accept this license.
169              
170             If your Modified Version has been derived from a Modified Version made by someone
171             other than you,you are nevertheless required to ensure that your Modified Version
172             complies with the requirements of this license.
173              
174             This license does not grant you the right to use any trademark, service mark,
175             tradename, or logo of the Copyright Holder.
176              
177             This license includes the non-exclusive, worldwide, free-of-charge patent license
178             to make, have made, use, offer to sell, sell, import and otherwise transfer the
179             Package with respect to any patent claims licensable by the Copyright Holder that
180             are necessarily infringed by the Package. If you institute patent litigation
181             (including a cross-claim or counterclaim) against any party alleging that the
182             Package constitutes direct or contributory patent infringement,then this Artistic
183             License to you shall terminate on the date that such litigation is filed.
184              
185             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND
186             CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
187             WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
188             NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW. UNLESS
189             REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT,
190             INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE
191             OF THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
192              
193             =cut
194              
195             1; # End of Games::Domino::Tile