blib/lib/Game/Mahjong/Hand.pm untested

filter: Statementstmt 0.0% 0 / 192 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 36 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 332 CC 21 SCAR 18.3
CC
21
Cov
0%
chow_shapes
20.0
add_flower
12.0
promote_kong
12.0
CRAP 6.2
1 package Game::Mahjong::Hand;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6  
7 use Object::Proto::Sugar -types;
8  
9 use Game::Mahjong::Tiles;
10 use Game::Mahjong::Notation;
11 use Game::Mahjong::Meld;
12  
13 our $VERSION = '0.04';
14  
15 use constant FULL => 13;
16  
17 has counts => (is => 'ro', isa => ArrayRef, default => sub { [ (0) x (Game::Mahjong::Tiles::KINDS + 1) ] });
18  
19 has melds => (is => 'ro', isa => ArrayRef, default => []);
20  
21 has flowers => (is => 'ro', isa => ArrayRef, default => []);
22  
23 has waits => (is => 'rw');
24  
25 sub from_tiles {
26 my ($class, $tiles) = @_;
27 my $self = $class->new;
28 $self->add($_) for @$tiles;
29 return $self;
30 }
31  
32 sub from_notation {
33 my ($class, $string) = @_;
34 my $parsed = Game::Mahjong::Notation::parse_hand($string);
35 my $self = $class->new;
36 $self->add($_) for @{ $parsed->{concealed} };
37 $self->add_flower($_) for @{ $parsed->{flowers} };
38 push @{ $self->melds }, Game::Mahjong::Meld->new(%$_) for @{ $parsed->{melds} };
39 return $self;
40 }
41  
42 sub _kind {
43 my ($kind) = @_;
44 Game::Mahjong::Tiles::code_of($kind);
45 die 'Game::Mahjong::Hand: a bonus tile is never in the hand; add_flower takes it'
46 if Game::Mahjong::Tiles::is_bonus($kind);
47 return $kind;
48 }
49  
50 sub add {
51 my ($self, $kind) = @_;
52 _kind($kind);
53 die 'Game::Mahjong::Hand: a fifth ' . Game::Mahjong::Tiles::code_of($kind)
54 if $self->counts->[$kind] >= Game::Mahjong::Tiles::PER_KIND;
55 $self->counts->[$kind]++;
56 $self->waits(undef);
57 return $self;
58 }
59  
60 sub remove {
61 my ($self, $kind) = @_;
62 _kind($kind);
63 die 'Game::Mahjong::Hand: no ' . Game::Mahjong::Tiles::code_of($kind) . ' to remove'
64 unless $self->counts->[$kind] > 0;
65 $self->counts->[$kind]--;
66 $self->waits(undef);
67 return $self;
68 }
69  
70 sub add_flower {
71 my ($self, $kind) = @_;
72 Game::Mahjong::Tiles::code_of($kind);
73 die 'Game::Mahjong::Hand: ' . Game::Mahjong::Tiles::code_of($kind) . ' is not a bonus tile'
74 unless Game::Mahjong::Tiles::is_bonus($kind);
75 die 'Game::Mahjong::Hand: ' . Game::Mahjong::Tiles::code_of($kind) . ' is exposed already'
76 if grep { $_ == $kind } @{ $self->flowers };
77 push @{ $self->flowers }, $kind;
78 @{ $self->flowers } = sort { $a <=> $b } @{ $self->flowers };
79 return $self;
80 }
81  
82 sub count {
83 my ($self, $kind) = @_;
84 _kind($kind);
85 return $self->counts->[$kind];
86 }
87  
88 sub size {
89 my ($self) = @_;
90 my $n = 0;
91 $n += $_ for @{ $self->counts };
92 return $n;
93 }
94  
95 sub tiles {
96 my ($self) = @_;
97 my $counts = $self->counts;
98 return map { ($_) x $counts->[$_] } grep { $counts->[$_] } 1 .. Game::Mahjong::Tiles::KINDS;
99 }
100  
101 sub kinds {
102 my ($self) = @_;
103 my $counts = $self->counts;
104 return grep { $counts->[$_] } 1 .. Game::Mahjong::Tiles::KINDS;
105 }
106  
107 sub meld_count { return scalar @{ $_[0]->melds } }
108  
109 sub total { return $_[0]->size + 3 * $_[0]->meld_count }
110  
111 sub expects { return FULL - 3 * $_[0]->meld_count }
112  
113 sub is_concealed {
114 my ($self) = @_;
115 return (grep { !$_->concealed } @{ $self->melds }) ? 0 : 1;
116 }
117  
118 sub concealed_kongs { return [ grep { $_->concealed } @{ $_[0]->melds } ] }
119  
120 sub holds_pair { return $_[0]->count($_[1]) >= 2 ? 1 : 0 }
121  
122 sub holds_pung_of { return $_[0]->count($_[1]) >= 3 ? 1 : 0 }
123  
124 sub holds_kong_of { return $_[0]->count($_[1]) == 4 ? 1 : 0 }
125  
126 sub exposed_pung_of {
127 my ($self, $kind) = @_;
128 _kind($kind);
129 for my $meld (@{ $self->melds }) {
130 return $meld if $meld->kind eq 'pung' && !$meld->concealed && $meld->tile == $kind;
131 }
132 return undef;
133 }
134  
135 sub chow_shapes {
136 my ($self, $kind) = @_;
137 _kind($kind);
138 my $suit = Game::Mahjong::Tiles::suit_of($kind);
139 return () unless defined $suit;
140 my $rank = Game::Mahjong::Tiles::rank_of($kind);
141 my $counts = $self->counts;
142 my @shapes;
143 for my $low ($rank - 2 .. $rank) {
144 next if $low < 1 || $low + 2 > 9;
145 my @others = grep { $_ != $kind } map { $kind + ($_ - $rank) } $low .. $low + 2;
146 next unless $counts->[ $others[0] ] && $counts->[ $others[1] ];
147 push @shapes, [ @others ];
148 }
149 return @shapes;
150 }
151  
152 sub _meld {
153 my ($self, $meld) = @_;
154 push @{ $self->melds }, $meld;
155 $self->waits(undef);
156 return $meld;
157 }
158  
159 sub claim_chow {
160 my ($self, $kind, $p, $q, $from) = @_;
161 _kind($_) for $kind, $p, $q;
162 my ($x, $y) = sort { $a <=> $b } $p, $q;
163 die 'Game::Mahjong::Hand: those two tiles are not both held'
164 unless $self->count($x) && $self->count($y) && ($x != $y || $self->count($x) >= 2);
165 my $meld = Game::Mahjong::Meld->new(
166 kind => 'chow', tiles => [ $kind, $x, $y ],
167 claimed_from => $from, claimed_tile => $kind,
168 );
169 $self->remove($x)->remove($y);
170 return $self->_meld($meld);
171 }
172  
173 sub claim_pung {
174 my ($self, $kind, $from) = @_;
175 die 'Game::Mahjong::Hand: a pung needs a pair in hand' unless $self->holds_pair($kind);
176 my $meld = Game::Mahjong::Meld->new(
177 kind => 'pung', tiles => [ ($kind) x 3 ],
178 claimed_from => $from, claimed_tile => $kind,
179 );
180 $self->remove($kind)->remove($kind);
181 return $self->_meld($meld);
182 }
183  
184 sub claim_kong {
185 my ($self, $kind, $from) = @_;
186 die 'Game::Mahjong::Hand: a claimed kong needs three in hand' unless $self->holds_pung_of($kind);
187 my $meld = Game::Mahjong::Meld->new(
188 kind => 'kong', tiles => [ ($kind) x 4 ],
189 claimed_from => $from, claimed_tile => $kind,
190 );
191 $self->remove($kind) for 1 .. 3;
192 return $self->_meld($meld);
193 }
194  
195 sub concealed_kong {
196 my ($self, $kind) = @_;
197 die 'Game::Mahjong::Hand: a concealed kong needs four in hand' unless $self->holds_kong_of($kind);
198 my $meld = Game::Mahjong::Meld->new(kind => 'kong', tiles => [ ($kind) x 4 ], concealed => 1);
199 $self->remove($kind) for 1 .. 4;
200 return $self->_meld($meld);
201 }
202  
203 sub promote_kong {
204 my ($self, $kind) = @_;
205 my $pung = $self->exposed_pung_of($kind)
206 or die 'Game::Mahjong::Hand: no exposed pung of ' . Game::Mahjong::Tiles::code_of($kind) . ' to promote';
207 die 'Game::Mahjong::Hand: the fourth ' . Game::Mahjong::Tiles::code_of($kind) . ' is not in hand'
208 unless $self->count($kind);
209 my $kong = $pung->promote;
210 @{ $self->melds } = map { $_ == $pung ? $kong : $_ } @{ $self->melds };
211 $self->remove($kind);
212 $self->waits(undef);
213 return $kong;
214 }
215  
216 sub clone {
217 my ($self) = @_;
218 return ref($self)->new(
219 counts => [ @{ $self->counts } ],
220 melds => [ @{ $self->melds } ],
221 flowers => [ @{ $self->flowers } ],
222 waits => $self->waits ? [ @{ $self->waits } ] : undef,
223 );
224 }
225  
226 sub to_notation {
227 my ($self) = @_;
228 return Game::Mahjong::Notation::print_hand({
229 concealed => [ $self->tiles ],
230 melds => [ map { $_->to_hash } @{ $self->melds } ],
231 flowers => [ @{ $self->flowers } ],
232 });
233 }
234  
235 1;
236  
237 __END__