blib/lib/Game/Mahjong/Hand.pm

filter: Statementstmt 100% 176 / 176 Branchbran 97.6% 41 / 42 Conditioncond 90.4% 19 / 21 MC/DCmcdc 81.8% 9 / 11 Subroutinesub 100% 37 / 37 Podpod 100% 26 / 26 totaltotal 98.4% 308 / 313 CC 39 SCAR 11.3
CC
39
Cov
99%
chow_shapes
7.0
claim_chow
6.0
exposed_pung_of
5.0
CRAP 3.1
« blib/lib/Game/Mahjong/Decompose.pm ↑ Summary blib/lib/Game/Mahjong/Error.pm »
1 package Game::Mahjong::Hand;
2  
3 34 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 34 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 34 ▶use Game::Mahjong::Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Game::Mahjong::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Game::Mahjong::Meld;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 34 our $VERSION = '0.02';
14  
15 34 ▶use constant FULL => 13;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1820 ▶has counts => (is => 'ro', isa => ArrayRef, default => sub { [ (0) x (Game::Mahjong::Tiles::KINDS + 1) ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 34 has melds => (is => 'ro', isa => ArrayRef, default => []);
20  
21 34 has flowers => (is => 'ro', isa => ArrayRef, default => []);
22  
23 34 has waits => (is => 'rw');
24  
25 1 ▶sub from_tiles {
Subroutine from_tiles: called CC=2 SCAR=6.9
Pod documented
26 1 my ($class, $tiles) = @_;
27 1 my $self = $class->new;
28 1 $self->add($_) for @$tiles;
29 1 return $self;
30 }
31  
32 582 ▶sub from_notation {
Subroutine from_notation: called CC=4 SCAR=13.9
Pod documented
33 582 my ($class, $string) = @_;
34 582 my $parsed = Game::Mahjong::Notation::parse_hand($string);
35 582 my $self = $class->new;
36 582 $self->add($_) for @{ $parsed->{concealed} };
37 582 $self->add_flower($_) for @{ $parsed->{flowers} };
38 582 push @{ $self->melds }, Game::Mahjong::Meld->new(%$_) for @{ $parsed->{melds} };
39 582 return $self;
40 }
41  
42 2449364 ▶sub _kind {
Subroutine _kind: called CC=2 SCAR=6.9
43 2449364 my ($kind) = @_;
44 2449364 Game::Mahjong::Tiles::code_of($kind);
45 2449363 ▶ die 'Game::Mahjong::Hand: a bonus tile is never in the hand; add_flower takes it'
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_bonus($kind) 1 2449362 2449363
46 if Game::Mahjong::Tiles::is_bonus($kind);
47 2449362 return $kind;
48 }
49  
50 39599 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
51 39599 my ($self, $kind) = @_;
52 39599 _kind($kind);
53 39597 ▶ die 'Game::Mahjong::Hand: a fifth ' . Game::Mahjong::Tiles::code_of($kind)
Branch: 1 branch
BranchTrueFalseTotal
if $self->counts->[$kind] >= 4 1 39596 39597
54 if $self->counts->[$kind] >= Game::Mahjong::Tiles::PER_KIND;
55 39596 $self->counts->[$kind]++;
56 39596 $self->waits(undef);
57 39596 return $self;
58 }
59  
60 75529 ▶sub remove {
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
61 75529 my ($self, $kind) = @_;
62 75529 _kind($kind);
63 75529 ▶ die 'Game::Mahjong::Hand: no ' . Game::Mahjong::Tiles::code_of($kind) . ' to remove'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->counts->[$kind] > 0 1 75528 75529
64 unless $self->counts->[$kind] > 0;
65 75528 $self->counts->[$kind]--;
66 75528 $self->waits(undef);
67 75528 return $self;
68 }
69  
70 1837 ▶sub add_flower {
Subroutine add_flower: called CC=3 SCAR=11.0
Pod documented
71 1837 my ($self, $kind) = @_;
72 1837 Game::Mahjong::Tiles::code_of($kind);
73 1837 ▶ die 'Game::Mahjong::Hand: ' . Game::Mahjong::Tiles::code_of($kind) . ' is not a bonus tile'
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Mahjong::Tiles::is_bonus($kind) 1 1836 1837
74 unless Game::Mahjong::Tiles::is_bonus($kind);
75 die 'Game::Mahjong::Hand: ' . Game::Mahjong::Tiles::code_of($kind) . ' is exposed already'
76 1836 ▶ if grep { $_ == $kind } @{ $self->flowers };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $kind;} @{$self->flowers;} 1 1835 1836
77 1835 push @{ $self->flowers }, $kind;
78 1835 @{ $self->flowers } = sort { $a <=> $b } @{ $self->flowers };
79 1835 return $self;
80 }
81  
82 2074392 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
83 2074392 my ($self, $kind) = @_;
84 2074392 _kind($kind);
85 2074392 return $self->counts->[$kind];
86 }
87  
88 156980 ▶sub size {
Subroutine size: called CC=2 SCAR=6.9
Pod documented
89 156980 my ($self) = @_;
90 156980 my $n = 0;
91 156980 $n += $_ for @{ $self->counts };
92 156980 return $n;
93 }
94  
95 43975 ▶sub tiles {
Subroutine tiles: called CC=1 SCAR=0.0
Pod documented
96 43975 my ($self) = @_;
97 43975 my $counts = $self->counts;
98 43975 return map { ($_) x $counts->[$_] } grep { $counts->[$_] } 1 .. Game::Mahjong::Tiles::KINDS;
99 }
100  
101 65877 ▶sub kinds {
Subroutine kinds: called CC=1 SCAR=0.0
Pod documented
102 65877 my ($self) = @_;
103 65877 my $counts = $self->counts;
104 65877 return grep { $counts->[$_] } 1 .. Game::Mahjong::Tiles::KINDS;
105 }
106  
107 311866 ▶sub meld_count { return scalar @{ $_[0]->melds } }
Subroutine meld_count: called CC=1 SCAR=0.0
Pod documented
108  
109 153846 ▶sub total { return $_[0]->size + 3 * $_[0]->meld_count }
Subroutine total: called CC=1 SCAR=0.0
Pod documented
110  
111 6 ▶sub expects { return FULL - 3 * $_[0]->meld_count }
Subroutine expects: called CC=1 SCAR=0.0
Pod documented
112  
113 3 ▶sub is_concealed {
Subroutine is_concealed: called CC=2 SCAR=6.9
Pod documented
114 3 my ($self) = @_;
115 3 ▶ return (grep { !$_->concealed } @{ $self->melds }) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
grep({not $_->concealed;} @{$self->melds;}) ? : 1 2 3
116 }
117  
118 934 ▶sub concealed_kongs { return [ grep { $_->concealed } @{ $_[0]->melds } ] }
Subroutine concealed_kongs: called CC=1 SCAR=0.0
Pod documented
119  
120 45706 ▶sub holds_pair { return $_[0]->count($_[1]) >= 2 ? 1 : 0 }
Subroutine holds_pair: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->count($_[1]) >= 2 ? : 2720 42986 45706
121  
122 45006 ▶sub holds_pung_of { return $_[0]->count($_[1]) >= 3 ? 1 : 0 }
Subroutine holds_pung_of: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->count($_[1]) >= 3 ? : 198 44808 45006
123  
124 235634 ▶sub holds_kong_of { return $_[0]->count($_[1]) == 4 ? 1 : 0 }
Subroutine holds_kong_of: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->count($_[1]) == 4 ? : 447 235187 235634
125  
126 235552 ▶sub exposed_pung_of {
Subroutine exposed_pung_of: called CC=5 SCAR=16.1
Pod documented
127 235552 my ($self, $kind) = @_;
128 235552 _kind($kind);
129 235552 for my $meld (@{ $self->melds }) {
130 35041 ▶ return $meld if $meld->kind eq 'pung' && !$meld->concealed && $meld->tile == $kind;
Branch: 1 branch
BranchTrueFalseTotal
if $meld->kind eq "pung" and not $meld->concealed and $meld->tile == $kind 108 34933 35041
Condition83%5/6
$meld->kind eq "pung" and not $meld->concealed and $meld->tile == $kind
!ll&&!rl&&r
1627018663108
$meld->kind eq "pung" and not $meld->concealed
!ll&&!rl&&r
16270018771
MC/DC66%2/3
$meld->kind eq "pung" and not $meld->concealed and $meld->tile == $kind
$meld->kind eq "pung" not $meld->concealed $meld->tile == $kind
Truth table75%3/4
A and not(B) and C
A= $meld->kind eq "pung"B= not $meld->concealedC= $meld->tile == $kind
ABCresult
0XX0
10X0
1100
1111
131 }
132 235444 return undef;
133 }
134  
135 19945 ▶sub chow_shapes {
Subroutine chow_shapes: called CC=7 SCAR=19.5
Pod documented
136 19945 my ($self, $kind) = @_;
137 19945 _kind($kind);
138 19945 my $suit = Game::Mahjong::Tiles::suit_of($kind);
139 19945 ▶ return () unless defined $suit;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $suit 4636 15309 19945
140 15309 my $rank = Game::Mahjong::Tiles::rank_of($kind);
141 15309 my $counts = $self->counts;
142 15309 my @shapes;
143 15309 for my $low ($rank - 2 .. $rank) {
144 45927 ▶ next if $low < 1 || $low + 2 > 9;
Branch: 1 branch
BranchTrueFalseTotal
if $low < 1 or $low + 2 > 9 11079 34848 45927
Condition100%3/3
$low < 1 or $low + 2 > 9
l!l&&r!l&&!r
5486559334848
MC/DC100%2/2
$low < 1 or $low + 2 > 9
$low < 1 $low + 2 > 9
Truth table100%3/3
A or B
A= $low < 1B= $low + 2 > 9
ABresult
1X1
011
000
145 34848 my @others = grep { $_ != $kind } map { $kind + ($_ - $rank) } $low .. $low + 2;
146 34848 ▶ next unless $counts->[ $others[0] ] && $counts->[ $others[1] ];
Branch: 1 branch
BranchTrueFalseTotal
unless $$counts[$others[0]] and $$counts[$others[1]] 24748 10100 34848
Condition100%3/3
$$counts[$others[0]] and $$counts[$others[1]]
!ll&&!rl&&r
18483626510100
MC/DC100%2/2
$$counts[$others[0]] and $$counts[$others[1]]
$$counts[$others[0]] $$counts[$others[1]]
Truth table100%3/3
A and B
A= $$counts[$others[0]]B= $$counts[$others[1]]
ABresult
0X0
100
111
147 10100 push @shapes, [ @others ];
148 }
149 15309 return @shapes;
150 }
151  
152 2306 ▶sub _meld {
Subroutine _meld: called CC=1 SCAR=0.0
153 2306 my ($self, $meld) = @_;
154 2306 push @{ $self->melds }, $meld;
155 2306 $self->waits(undef);
156 2306 return $meld;
157 }
158  
159 1449 ▶sub claim_chow {
Subroutine claim_chow: called CC=6 SCAR=17.9
Pod documented
160 1449 my ($self, $kind, $p, $q, $from) = @_;
161 1449 _kind($_) for $kind, $p, $q;
162 1449 my ($x, $y) = sort { $a <=> $b } $p, $q;
163 1449 ▶ die 'Game::Mahjong::Hand: those two tiles are not both held'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count($x) and $self->count($y) and $x != $y || $self->count($x) >= 2 3 1446 1449
Condition88%8/9
$self->count($x) and $self->count($y) and $x != $y || $self->count($x) >= 2
!ll&&!rl&&r
211446
$self->count($x) and $self->count($y)
!ll&&!rl&&r
111447
$x != $y or $self->count($x) >= 2
l!l&&r!l&&!r
144601
MC/DC75%3/4
$self->count($x) and $self->count($y) and $x != $y || $self->count($x) >= 2
$self->count($x) $self->count($y) $x != $y $self->count($x) >= 2
Truth table80%4/5
A and B and C or D
A= $self->count($x)B= $self->count($y)C= $x != $yD= $self->count($x) >= 2
ABCDresult
0XXX0
10XX0
11000
111X1
11011
164 unless $self->count($x) && $self->count($y) && ($x != $y || $self->count($x) >= 2);
165 1446 my $meld = Game::Mahjong::Meld->new(
166 kind => 'chow', tiles => [ $kind, $x, $y ],
167 claimed_from => $from, claimed_tile => $kind,
168 );
169 1446 $self->remove($x)->remove($y);
170 1446 return $self->_meld($meld);
171 }
172  
173 752 ▶sub claim_pung {
Subroutine claim_pung: called CC=2 SCAR=6.9
Pod documented
174 752 my ($self, $kind, $from) = @_;
175 752 ▶ die 'Game::Mahjong::Hand: a pung needs a pair in hand' unless $self->holds_pair($kind);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->holds_pair($kind) 1 751 752
176 751 my $meld = Game::Mahjong::Meld->new(
177 kind => 'pung', tiles => [ ($kind) x 3 ],
178 claimed_from => $from, claimed_tile => $kind,
179 );
180 751 $self->remove($kind)->remove($kind);
181 751 return $self->_meld($meld);
182 }
183  
184 52 ▶sub claim_kong {
Subroutine claim_kong: called CC=3 SCAR=11.0
Pod documented
185 52 my ($self, $kind, $from) = @_;
186 52 ▶ die 'Game::Mahjong::Hand: a claimed kong needs three in hand' unless $self->holds_pung_of($kind);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->holds_pung_of($kind) 1 51 52
187 51 my $meld = Game::Mahjong::Meld->new(
188 kind => 'kong', tiles => [ ($kind) x 4 ],
189 claimed_from => $from, claimed_tile => $kind,
190 );
191 51 $self->remove($kind) for 1 .. 3;
192 51 return $self->_meld($meld);
193 }
194  
195 59 ▶sub concealed_kong {
Subroutine concealed_kong: called CC=3 SCAR=11.0
Pod documented
196 59 my ($self, $kind) = @_;
197 59 ▶ die 'Game::Mahjong::Hand: a concealed kong needs four in hand' unless $self->holds_kong_of($kind);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->holds_kong_of($kind) 1 58 59
198 58 my $meld = Game::Mahjong::Meld->new(kind => 'kong', tiles => [ ($kind) x 4 ], concealed => 1);
199 58 $self->remove($kind) for 1 .. 4;
200 58 return $self->_meld($meld);
201 }
202  
203 44 ▶sub promote_kong {
Subroutine promote_kong: called CC=4 SCAR=13.9
Pod documented
204 44 my ($self, $kind) = @_;
205 44 ▶ my $pung = $self->exposed_pung_of($kind)
Branch: 1 branch
BranchTrueFalseTotal
my $pung = $self->exposed_pung_of($kind) or die 'Game::Mahjong::Hand: no exposed pung of ' . Game::Mahjong::Tiles::code_of($kind) . ' to promote' 41 3 44
206 or die 'Game::Mahjong::Hand: no exposed pung of ' . Game::Mahjong::Tiles::code_of($kind) . ' to promote';
207 41 ▶ die 'Game::Mahjong::Hand: the fourth ' . Game::Mahjong::Tiles::code_of($kind) . ' is not in hand'
Branch: 1 branch
BranchTrueFalseTotal
unless $self->count($kind) 0 41 41
208 unless $self->count($kind);
209 41 my $kong = $pung->promote;
210 41 ▶ @{ $self->melds } = map { $_ == $pung ? $kong : $_ } @{ $self->melds };
Branch: 1 branch
BranchTrueFalseTotal
$_ == $pung ? : 41 34 75
211 41 $self->remove($kind);
212 41 $self->waits(undef);
213 41 return $kong;
214 }
215  
216 75912 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
217 75912 my ($self) = @_;
218 return ref($self)->new(
219 75912 counts => [ @{ $self->counts } ],
220 75912 melds => [ @{ $self->melds } ],
221 75912 ▶ flowers => [ @{ $self->flowers } ],
Branch: 1 branch
BranchTrueFalseTotal
$self->waits ? : 6573 69339 75912
222 75912 waits => $self->waits ? [ @{ $self->waits } ] : undef,
223 );
224 }
225  
226 36 ▶sub to_notation {
Subroutine to_notation: called CC=1 SCAR=0.0
Pod documented
227 36 my ($self) = @_;
228 return Game::Mahjong::Notation::print_hand({
229 concealed => [ $self->tiles ],
230 18 melds => [ map { $_->to_hash } @{ $self->melds } ],
231 36 flowers => [ @{ $self->flowers } ],
232 });
233 }
234  
235 34 1;
236  
237 __END__
« blib/lib/Game/Mahjong/Decompose.pm ↑ Summary blib/lib/Game/Mahjong/Error.pm »