Dominoes/Tile.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« Dominoes/Terminal.pm ↑ Summary Dominoes/Bot.pm »
1 package Game::Dominoes::Tile;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 0 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 our $VERSION = '0.04';
9  
10 sub _offset { my ($l) = @_; return 7 * $l - ($l * ($l - 1)) / 2 }
11  
12 has high => (
13 is => 'ro',
14 isa => Int
15 );
16  
17 has low => (
18 is => 'ro',
19 isa => Int
20 );
21  
22 sub BUILD {
23 my ($self) = @_;
24 for my $face (qw/high low/) {
25 my $v = $self->$face;
26 die "Game::Dominoes::Tile: $face must be a face from 0 to 6"
27 unless defined $v && $v =~ /\A[0-6]\z/;
28 }
29 die 'Game::Dominoes::Tile: high must not be less than low'
30 if $self->high < $self->low;
31 return $self;
32 }
33  
34 sub of {
35 my ($class, $a, $b) = @_;
36 ($a, $b) = ($b, $a) if defined $a && defined $b && $a < $b;
37 return $class->new(high => $a, low => $b);
38 }
39  
40 sub id {
41 my ($self) = @_;
42 return _offset($self->low) + ($self->high - $self->low) + 1;
43 }
44  
45 sub from_id {
46 my ($class, $id) = @_;
47 die "Game::Dominoes::Tile: no tile has id " . (defined $id ? $id : 'undef')
48 unless defined $id && $id =~ /\A\d+\z/ && $id >= 1 && $id <= 28;
49 for my $low (reverse 0 .. 6) {
50 my $offset = _offset($low);
51 next if $offset >= $id;
52 return $class->new(low => $low, high => $low + ($id - $offset - 1));
53 }
54 die 'unreachable';
55 }
56  
57 sub is_double {
58 return $_[0]->high == $_[0]->low ? 1 : 0;
59 }
60  
61 sub pips {
62 return $_[0]->high + $_[0]->low;
63 }
64  
65 sub has_face {
66 my ($self, $face) = @_;
67 return 0 unless defined $face;
68 return ($self->high == $face || $self->low == $face) ? 1 : 0;
69 }
70  
71 sub other {
72 my ($self, $face) = @_;
73 die 'Game::Dominoes::Tile: that tile does not carry face '
74 . (defined $face ? $face : 'undef')
75 unless $self->has_face($face);
76 return $self->high == $face ? $self->low : $self->high;
77 }
78  
79 sub equals {
80 my ($self, $other) = @_;
81 return 0 unless ref $other;
82 return $self->id == $other->id ? 1 : 0;
83 }
84  
85 sub stringify {
86 my ($self) = @_;
87 return $self->high . '-' . $self->low;
88 }
89  
90 1;
91  
92 __END__
« Dominoes/Terminal.pm ↑ Summary Dominoes/Bot.pm »