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

filter: Statementstmt 0.0% 0 / 670 Branchbran 0.0% 0 / 220 Conditioncond 0.0% 0 / 287 MC/DCmcdc 0.0% 0 / 163 Subroutinesub 0.0% 0 / 107 Podpod 0.0% 0 / 101 totaltotal 0.0% 0 / 1548 CC 108 SCAR 27.3
CC
108
Cov
0%
view
110.0
_f_three_suited_terminal_chows
42.0
_f_all_fives
42.0
CRAP 15.3
1 package Game::Mahjong::Fans;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6  
7 use Game::Mahjong::Tiles;
8 use Game::Mahjong::Fan;
9  
10 our $VERSION = '0.04';
11  
12 our @GRADES = (88, 64, 48, 32, 24, 16, 12, 8, 6, 4, 2, 1);
13 our %PER_GRADE = (88 => 7, 64 => 6, 48 => 2, 32 => 3, 24 => 9, 16 => 6, 12 => 5, 8 => 10, 6 => 6, 4 => 4, 2 => 10, 1 => 13);
14 use constant COUNT => 81;
15  
16 sub view {
17 my ($split, $ctx) = @_;
18 $ctx ||= {};
19 my @sets;
20 for my $i (0 .. $#{ $split->{sets} }) {
21 my $s = $split->{sets}[$i];
22 my $t0 = $s->{tiles}[0];
23 push @sets, {
24 %$s,
25 i => $i,
26 suit => Game::Mahjong::Tiles::suit_of($t0),
27 base => Game::Mahjong::Tiles::rank_of($t0),
28 tile => $t0,
29 honour => Game::Mahjong::Tiles::is_honour($t0),
30 exposed => ($s->{melded} && !$s->{concealed}) ? 1 : 0,
31 };
32 }
33 my @chows = grep { $_->{kind} eq 'chow' } @sets;
34 my @pungs = grep { $_->{kind} eq 'pung' || $_->{kind} eq 'kong' } @sets;
35 my @kongs = grep { $_->{kind} eq 'kong' } @sets;
36 my @knits = grep { $_->{kind} eq 'knit' } @sets;
37  
38 my @tiles;
39 for my $s (@sets) {
40 push @tiles, @{ $s->{tiles} };
41 }
42 push @tiles, ($split->{pair}) x 2 if $split->{pair} && $split->{form} ne 'seven_pairs' && $split->{form} ne 'thirteen_orphans';
43 if ($split->{form} eq 'seven_pairs') {
44 push @tiles, map { ($_) x 2 } @{ $split->{singles} };
45 }
46 elsif ($split->{form} eq 'thirteen_orphans') {
47 push @tiles, @{ $split->{singles} }, $split->{pair};
48 }
49 elsif ($split->{form} eq 'honours_knitted') {
50 push @tiles, @{ $split->{singles} };
51 }
52 my %count;
53 $count{$_}++ for @tiles;
54 my %suits;
55 my $honours = 0;
56 for my $t (@tiles) {
57 if (my $s = Game::Mahjong::Tiles::suit_of($t)) { $suits{$s}++ }
58 else { $honours++ }
59 }
60  
61 my @elements = (@sets);
62 if ($split->{form} eq 'seven_pairs') {
63 @elements = map { { kind => 'pair', tiles => [ ($_) x 2 ], tile => $_, suit => Game::Mahjong::Tiles::suit_of($_), base => Game::Mahjong::Tiles::rank_of($_), honour => Game::Mahjong::Tiles::is_honour($_) } } @{ $split->{singles} };
64 }
65 elsif ($split->{pair}) {
66 my $p = $split->{pair};
67 push @elements, { kind => 'pair', tiles => [ $p, $p ], tile => $p, suit => Game::Mahjong::Tiles::suit_of($p), base => Game::Mahjong::Tiles::rank_of($p), honour => Game::Mahjong::Tiles::is_honour($p) };
68 }
69  
70 return {
71 split => $split,
72 ctx => $ctx,
73 form => $split->{form},
74 sets => \@sets,
75 chows => \@chows,
76 pungs => \@pungs,
77 kongs => \@kongs,
78 knits => \@knits,
79 pair => $split->{pair},
80 elements => \@elements,
81 tiles => \@tiles,
82 count => \%count,
83 suits => [ sort keys %suits ],
84 nsuits => scalar keys %suits,
85 honours => $honours,
86 exposed => scalar(grep { $_->{exposed} } @sets),
87 concealed_pungs => [ grep { $_->{concealed} } @pungs ],
88 wait => $split->{placement}{wait},
89 place_in => $split->{placement}{in},
90 nwaits => ($ctx->{waits} ? scalar @{ $ctx->{waits} } : 1),
91 };
92 }
93  
94 sub _every { my ($v, $pred) = @_; return 0 unless @{ $v->{tiles} }; for (@{ $v->{tiles} }) { return 0 unless $pred->($_) } return 1 }
95 sub _one { return ({ sets => [ map { $_->{i} } @_ ] }) }
96 sub _standard { return $_[0]->{form} eq 'standard' ? 1 : 0 }
97 sub _pairs_of { my ($list) = @_; my @p; for my $i (0 .. $#$list) { for my $j ($i + 1 .. $#$list) { push @p, [ $list->[$i], $list->[$j] ] } } return @p }
98 sub _triples_of { my ($list) = @_; my @t; for my $i (0 .. $#$list) { for my $j ($i + 1 .. $#$list) { for my $k ($j + 1 .. $#$list) { push @t, [ $list->[$i], $list->[$j], $list->[$k] ] } } } return @t }
99 sub _wind_of { my ($v, $which) = @_; my $i = $v->{ctx}{$which}; return defined $i ? 28 + $i : -1 }
100 sub _same_suit { my @s = @_; my $suit = $s[0]{suit}; return 0 unless defined $suit; for (@s) { return 0 unless defined $_->{suit} && $_->{suit} eq $suit } return 1 }
101 sub _three_suits { my @s = @_; my %seen; for (@s) { return 0 unless defined $_->{suit}; $seen{ $_->{suit} }++ } return scalar(keys %seen) == 3 ? 1 : 0 }
102 sub _bases_shifted { my ($by, @s) = @_; my @b = sort { $a <=> $b } map { $_->{base} } @s; for my $i (1 .. $#b) { return 0 unless $b[$i] == $b[$i - 1] + $by } return 1 }
103 sub _bases_are { my ($want, @s) = @_; return join(',', sort { $a <=> $b } map { $_->{base} } @s) eq $want ? 1 : 0 }
104 sub _wind_pungs { return grep { Game::Mahjong::Tiles::is_wind($_->{tile}) } @{ $_[0]->{pungs} } }
105 sub _dragon_pungs { return grep { Game::Mahjong::Tiles::is_dragon($_->{tile}) } @{ $_[0]->{pungs} } }
106 sub _is_terminal_or_honour { my ($t) = @_; return Game::Mahjong::Tiles::is_terminal($t) || Game::Mahjong::Tiles::is_honour($t) ? 1 : 0 }
107 sub _rank_in { my ($lo, $hi) = @_; return sub { my $r = Game::Mahjong::Tiles::rank_of($_[0]); defined $r && $r >= $lo && $r <= $hi } }
108  
109 sub _f_big_four_winds { my ($v) = @_; my @w = _wind_pungs($v); return @w == 4 ? _one(@w) : () }
110 sub _f_big_three_dragons { my ($v) = @_; my @d = _dragon_pungs($v); return @d == 3 ? _one(@d) : () }
111 sub _f_all_green { my ($v) = @_; return _every($v, \&Game::Mahjong::Tiles::is_green) ? _one() : () }
112 sub _f_nine_gates {
113 my ($v) = @_;
114 return () unless _standard($v) && $v->{exposed} == 0 && !@{ $v->{kongs} } && $v->{nsuits} == 1 && !$v->{honours};
115 my $win = $v->{ctx}{winning} or return ();
116 my %c = %{ $v->{count} };
117 return () unless $c{$win};
118 $c{$win}--;
119 my $suit = $v->{suits}[0];
120 for my $r (1 .. 9) {
121 my $k = Game::Mahjong::Tiles::id_of($suit . $r);
122 my $want = ($r == 1 || $r == 9) ? 3 : 1;
123 return () unless ($c{$k} || 0) == $want;
124 }
125 return _one();
126 }
127 sub _f_four_kongs { my ($v) = @_; return @{ $v->{kongs} } == 4 ? _one(@{ $v->{kongs} }) : () }
128 sub _f_seven_shifted_pairs {
129 my ($v) = @_;
130 return () unless $v->{form} eq 'seven_pairs';
131 my @k = sort { $a <=> $b } @{ $v->{split}{singles} };
132 my $suit = Game::Mahjong::Tiles::suit_of($k[0]);
133 return () unless defined $suit;
134 for my $i (0 .. 6) {
135 return () unless (Game::Mahjong::Tiles::suit_of($k[$i]) // '') eq $suit;
136 return () if $i && $k[$i] != $k[$i - 1] + 1;
137 }
138 return _one();
139 }
140 sub _f_thirteen_orphans { my ($v) = @_; return $v->{form} eq 'thirteen_orphans' ? _one() : () }
141 sub _f_all_terminals { my ($v) = @_; return _every($v, \&Game::Mahjong::Tiles::is_terminal) ? _one() : () }
142 sub _f_little_four_winds {
143 my ($v) = @_;
144 my @w = _wind_pungs($v);
145 return () unless @w == 3 && $v->{pair} && Game::Mahjong::Tiles::is_wind($v->{pair});
146 return (grep { $_->{tile} == $v->{pair} } @w) ? () : _one(@w);
147 }
148 sub _f_little_three_dragons {
149 my ($v) = @_;
150 my @d = _dragon_pungs($v);
151 return () unless @d == 2 && $v->{pair} && Game::Mahjong::Tiles::is_dragon($v->{pair});
152 return (grep { $_->{tile} == $v->{pair} } @d) ? () : _one(@d);
153 }
154 sub _f_all_honours { my ($v) = @_; return _every($v, \&Game::Mahjong::Tiles::is_honour) ? _one() : () }
155 sub _f_four_concealed_pungs { my ($v) = @_; my @c = @{ $v->{concealed_pungs} }; return @c == 4 ? _one(@c) : () }
156 sub _f_pure_terminal_chows {
157 my ($v) = @_;
158 my @c = @{ $v->{chows} };
159 return () unless _standard($v) && @c == 4 && _same_suit(@c) && _bases_are('1,1,7,7', @c);
160 return () unless $v->{pair} && (Game::Mahjong::Tiles::suit_of($v->{pair}) // '') eq $c[0]{suit} && Game::Mahjong::Tiles::rank_of($v->{pair}) == 5;
161 return _one(@c);
162 }
163 sub _f_quadruple_chow { my ($v) = @_; my @c = @{ $v->{chows} }; return @c == 4 && _same_suit(@c) && _bases_shifted(0, @c) ? _one(@c) : () }
164 sub _f_four_pure_shifted_pungs { my ($v) = @_; my @p = grep { defined $_->{suit} } @{ $v->{pungs} }; return @p == 4 && _same_suit(@p) && _bases_shifted(1, @p) ? _one(@p) : () }
165 sub _f_four_pure_shifted_chows { my ($v) = @_; my @c = @{ $v->{chows} }; return @c == 4 && _same_suit(@c) && (_bases_shifted(1, @c) || _bases_shifted(2, @c)) ? _one(@c) : () }
166 sub _f_three_kongs { my ($v) = @_; return @{ $v->{kongs} } == 3 ? _one(@{ $v->{kongs} }) : () }
167 sub _f_all_terminals_and_honours { my ($v) = @_; return _every($v, \&_is_terminal_or_honour) && $v->{form} ne 'thirteen_orphans' ? _one() : () }
168 sub _f_seven_pairs { my ($v) = @_; return $v->{form} eq 'seven_pairs' ? _one() : () }
169 sub _f_greater_honours_and_knitted_tiles { my ($v) = @_; return $v->{form} eq 'honours_knitted' && $v->{honours} == 7 ? _one() : () }
170 sub _f_all_even_pungs {
171 my ($v) = @_;
172 return () unless _standard($v) && @{ $v->{pungs} } == 4;
173 my $even = sub { my $r = Game::Mahjong::Tiles::rank_of($_[0]); defined $r && $r % 2 == 0 };
174 return _every($v, $even) ? _one(@{ $v->{pungs} }) : ();
175 }
176 sub _f_full_flush { my ($v) = @_; return $v->{nsuits} == 1 && !$v->{honours} ? _one() : () }
177 sub _f_pure_triple_chow { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && _bases_shifted(0, @$_) } _triples_of($v->{chows}) }
178 sub _f_pure_shifted_pungs { my ($v) = @_; my @p = grep { defined $_->{suit} } @{ $v->{pungs} }; return map { _one(@$_) } grep { _same_suit(@$_) && _bases_shifted(1, @$_) } _triples_of(\@p) }
179 sub _f_upper_tiles { my ($v) = @_; return _every($v, _rank_in(7, 9)) ? _one() : () }
180 sub _f_middle_tiles { my ($v) = @_; return _every($v, _rank_in(4, 6)) ? _one() : () }
181 sub _f_lower_tiles { my ($v) = @_; return _every($v, _rank_in(1, 3)) ? _one() : () }
182 sub _f_pure_straight { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && _bases_are('1,4,7', @$_) } _triples_of($v->{chows}) }
183 sub _f_three_suited_terminal_chows {
184 my ($v) = @_;
185 my @c = @{ $v->{chows} };
186 return () unless _standard($v) && @c == 4 && $v->{pair};
187 my %by;
188 push @{ $by{ $_->{suit} } }, $_->{base} for @c;
189 my @s = keys %by;
190 return () unless @s == 2;
191 for my $suit (@s) { return () unless join(',', sort { $a <=> $b } @{ $by{$suit} }) eq '1,7' }
192 my $ps = Game::Mahjong::Tiles::suit_of($v->{pair}) // return ();
193 return () if $by{$ps};
194 return Game::Mahjong::Tiles::rank_of($v->{pair}) == 5 ? _one(@c) : ();
195 }
196 sub _f_pure_shifted_chows { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && (_bases_shifted(1, @$_) || _bases_shifted(2, @$_)) } _triples_of($v->{chows}) }
197 sub _f_all_fives {
198 my ($v) = @_;
199 return () unless _standard($v);
200 for my $e (@{ $v->{elements} }) {
201 return () if $e->{kind} eq 'knit';
202 return () unless defined $e->{base};
203 my $has5 = $e->{kind} eq 'chow' ? ($e->{base} >= 3 && $e->{base} <= 5) : $e->{base} == 5;
204 return () unless $has5;
205 }
206 return _one(@{ $v->{sets} });
207 }
208 sub _f_triple_pung { my ($v) = @_; my @p = grep { defined $_->{suit} } @{ $v->{pungs} }; return map { _one(@$_) } grep { _three_suits(@$_) && _bases_shifted(0, @$_) } _triples_of(\@p) }
209 sub _f_three_concealed_pungs { my ($v) = @_; my @c = @{ $v->{concealed_pungs} }; return @c == 3 ? _one(@c) : () }
210 sub _f_lesser_honours_and_knitted_tiles { my ($v) = @_; return $v->{form} eq 'honours_knitted' ? _one() : () }
211 sub _f_knitted_straight { my ($v) = @_; return $v->{form} eq 'knitted_straight' ? _one(@{ $v->{knits} }) : () }
212 sub _f_upper_four { my ($v) = @_; return _every($v, _rank_in(6, 9)) ? _one() : () }
213 sub _f_lower_four { my ($v) = @_; return _every($v, _rank_in(1, 4)) ? _one() : () }
214 sub _f_big_three_winds { my ($v) = @_; my @w = _wind_pungs($v); return @w == 3 ? _one(@w) : () }
215 sub _f_mixed_straight { my ($v) = @_; return map { _one(@$_) } grep { _three_suits(@$_) && _bases_are('1,4,7', @$_) } _triples_of($v->{chows}) }
216 sub _f_reversible_tiles { my ($v) = @_; return _every($v, \&Game::Mahjong::Tiles::is_reversible) ? _one() : () }
217 sub _f_mixed_triple_chow { my ($v) = @_; return map { _one(@$_) } grep { _three_suits(@$_) && _bases_shifted(0, @$_) } _triples_of($v->{chows}) }
218 sub _f_mixed_shifted_pungs { my ($v) = @_; my @p = grep { defined $_->{suit} } @{ $v->{pungs} }; return map { _one(@$_) } grep { _three_suits(@$_) && _bases_shifted(1, @$_) } _triples_of(\@p) }
219 sub _f_chicken_hand { return () }
220 sub _f_last_tile_draw { my ($v) = @_; return ($v->{ctx}{by} // '') eq 'self' && $v->{ctx}{last_of_wall} ? _one() : () }
221 sub _f_last_tile_claim { my ($v) = @_; return ($v->{ctx}{by} // '') eq 'discard' && $v->{ctx}{last_of_wall} ? _one() : () }
222 sub _f_out_with_replacement_tile { my ($v) = @_; return ($v->{ctx}{replacement} // '') eq 'kong' ? _one() : () }
223 sub _f_robbing_the_kong { my ($v) = @_; return ($v->{ctx}{by} // '') eq 'rob' ? _one() : () }
224 sub _f_two_concealed_kongs { my ($v) = @_; my @c = grep { $_->{concealed} } @{ $v->{kongs} }; return @c >= 2 ? _one(@c[0, 1]) : () }
225 sub _f_all_pungs { my ($v) = @_; return _standard($v) && @{ $v->{pungs} } == 4 ? _one(@{ $v->{pungs} }) : () }
226 sub _f_half_flush { my ($v) = @_; return $v->{nsuits} == 1 && $v->{honours} ? _one() : () }
227 sub _f_mixed_shifted_chows { my ($v) = @_; return map { _one(@$_) } grep { _three_suits(@$_) && _bases_shifted(1, @$_) } _triples_of($v->{chows}) }
228 sub _f_all_types {
229 my ($v) = @_;
230 return () unless $v->{form} eq 'standard' || $v->{form} eq 'seven_pairs';
231 my %type;
232 for my $e (@{ $v->{elements} }) {
233 return () if $e->{kind} eq 'knit';
234 my $t = $e->{suit} // (Game::Mahjong::Tiles::is_wind($e->{tile}) ? 'wind' : 'dragon');
235 $type{$t}++;
236 }
237 return () unless keys %type == 5;
238 return () if $v->{form} eq 'standard' && grep { $_ != 1 } values %type;
239 return _one(@{ $v->{sets} });
240 }
241 sub _f_melded_hand {
242 my ($v) = @_;
243 return () unless _standard($v) && $v->{exposed} == 4 && ($v->{ctx}{by} // '') ne 'self' && ($v->{place_in} // '') eq 'pair';
244 return _one(@{ $v->{sets} });
245 }
246 sub _f_two_dragon_pungs { my ($v) = @_; my @d = _dragon_pungs($v); return @d == 2 ? _one(@d) : () }
247 sub _f_outside_hand {
248 my ($v) = @_;
249 return () unless $v->{form} eq 'standard' || $v->{form} eq 'seven_pairs';
250 for my $e (@{ $v->{elements} }) {
251 return () if $e->{kind} eq 'knit';
252 return () unless grep { _is_terminal_or_honour($_) } @{ $e->{tiles} };
253 }
254 return _one(@{ $v->{sets} });
255 }
256 sub _f_fully_concealed_hand { my ($v) = @_; return $v->{exposed} == 0 && ($v->{ctx}{by} // '') eq 'self' ? _one() : () }
257 sub _f_two_melded_kongs {
258 my ($v) = @_;
259 my @m = grep { !$_->{concealed} } @{ $v->{kongs} };
260 my @c = grep { $_->{concealed} } @{ $v->{kongs} };
261 return _one(@m[0, 1]) if @m >= 2;
262 return ({ sets => [ $m[0]{i}, $c[0]{i} ], points => 6 }) if @m == 1 && @c == 1 && @{ $v->{kongs} } == 2;
263 return ();
264 }
265 sub _f_last_tile { my ($v) = @_; return $v->{ctx}{last_tile} ? _one() : () }
266 sub _f_dragon_pung { my ($v) = @_; return map { _one($_) } _dragon_pungs($v) }
267 sub _f_prevalent_wind { my ($v) = @_; my $w = _wind_of($v, 'prevailing'); return map { _one($_) } grep { $_->{tile} == $w } @{ $v->{pungs} } }
268 sub _f_seat_wind { my ($v) = @_; my $w = _wind_of($v, 'seat'); return map { _one($_) } grep { $_->{tile} == $w } @{ $v->{pungs} } }
269 sub _f_concealed_hand { my ($v) = @_; return $v->{exposed} == 0 && ($v->{ctx}{by} // '') ne 'self' && defined $v->{ctx}{by} ? _one() : () }
270 sub _f_all_chows { my ($v) = @_; return _standard($v) && @{ $v->{chows} } == 4 && !$v->{honours} ? _one(@{ $v->{chows} }) : () }
271 sub _f_tile_hog {
272 my ($v) = @_;
273 my %konged = map { $_->{tile} => 1 } @{ $v->{kongs} };
274 my @hogs = grep { $v->{count}{$_} == 4 && !$konged{$_} && Game::Mahjong::Tiles::is_suit($_) } sort { $a <=> $b } keys %{ $v->{count} };
275 return map { ({ sets => [], kind => $_ }) } @hogs;
276 }
277 sub _f_double_pung { my ($v) = @_; my @p = grep { defined $_->{suit} } @{ $v->{pungs} }; return map { _one(@$_) } grep { $_->[0]{suit} ne $_->[1]{suit} && $_->[0]{base} == $_->[1]{base} } _pairs_of(\@p) }
278 sub _f_two_concealed_pungs { my ($v) = @_; my @c = @{ $v->{concealed_pungs} }; return @c == 2 ? _one(@c) : () }
279 sub _f_concealed_kong { my ($v) = @_; return map { _one($_) } grep { $_->{concealed} } @{ $v->{kongs} } }
280 sub _f_all_simples { my ($v) = @_; return _every($v, \&Game::Mahjong::Tiles::is_simple) ? _one() : () }
281 sub _f_pure_double_chow { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && $_->[0]{base} == $_->[1]{base} } _pairs_of($v->{chows}) }
282 sub _f_mixed_double_chow { my ($v) = @_; return map { _one(@$_) } grep { $_->[0]{suit} ne $_->[1]{suit} && $_->[0]{base} == $_->[1]{base} } _pairs_of($v->{chows}) }
283 sub _f_short_straight { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && abs($_->[0]{base} - $_->[1]{base}) == 3 } _pairs_of($v->{chows}) }
284 sub _f_two_terminal_chows { my ($v) = @_; return map { _one(@$_) } grep { _same_suit(@$_) && _bases_are('1,7', @$_) } _pairs_of($v->{chows}) }
285 sub _f_pung_of_terminals_or_honours {
286 my ($v) = @_;
287 my ($prev, $seat) = (_wind_of($v, 'prevailing'), _wind_of($v, 'seat'));
288 return map { _one($_) } grep {
289 my $t = $_->{tile};
290 (Game::Mahjong::Tiles::is_terminal($t) || Game::Mahjong::Tiles::is_wind($t)) && $t != $prev && $t != $seat
291 } @{ $v->{pungs} };
292 }
293 sub _f_melded_kong { my ($v) = @_; return map { _one($_) } grep { !$_->{concealed} } @{ $v->{kongs} } }
294 sub _f_one_voided_suit { my ($v) = @_; return $v->{nsuits} == 2 ? _one() : () }
295 sub _f_no_honours { my ($v) = @_; return !$v->{honours} && @{ $v->{tiles} } ? _one() : () }
296 sub _f_edge_wait { my ($v) = @_; return ($v->{wait} // '') eq 'edge' && $v->{nwaits} == 1 ? _one() : () }
297 sub _f_closed_wait { my ($v) = @_; return ($v->{wait} // '') eq 'closed' && $v->{nwaits} == 1 ? _one() : () }
298 sub _f_single_wait { my ($v) = @_; my $w = $v->{wait} // ''; return ($w eq 'pair' || $w eq 'single') && $v->{nwaits} == 1 ? _one() : () }
299 sub _f_self_drawn { my ($v) = @_; return ($v->{ctx}{by} // '') eq 'self' ? _one() : () }
300 sub _f_flower_tiles { my ($v) = @_; my $n = $v->{ctx}{flowers} || 0; return map { _one() } 1 .. $n }
301  
302 my @ROWS = (
303 [ 1, 'big_four_winds', 'Big Four Winds', 88,
304 'The hand includes Pungs (or Kongs) of all four Wind Tiles. Does not combine with Big Three Winds, All Pungs, Prevalent Wind, Seat Wind, or Pung of Terminals or Honors.',
305 [qw(big_three_winds all_pungs prevalent_wind seat_wind pung_of_terminals_or_honours)], [qw(little_four_winds)] ],
306 [ 2, 'big_three_dragons', 'Big Three Dragons', 88,
307 'The hand includes Pungs (or Kong) of all three Dragon tiles. Does not combine with Two Dragons or Dragon Pung.',
308 [qw(two_dragon_pungs dragon_pung)], [qw(little_three_dragons)] ],
309 [ 3, 'all_green', 'All Green', 88,
310 'Hand is composed entirely of any of the 2, 3, 4, 6, 8 of Bamboo and Green Dragon. Combines with Full Flush and Half Flush.',
311 [], [] ],
312 [ 4, 'nine_gates', 'Nine Gates', 88,
313 'Holding the 1,1,1,2,3,4,5,6,7,8,9,9,9 tiles in one suit, creating the nine-sided wait of 1, 2, 3, 4, 5, 6, 7, 8, 9. Does not combine with Full Flush, Concealed hand, and Pung of Terminals or Honors.',
314 [qw(full_flush concealed_hand pung_of_terminals_or_honours)], [qw(no_honours one_voided_suit)] ],
315 [ 5, 'four_kongs', 'Four Kongs', 88,
316 'A hand that includes four Kongs. Points for concealed pungs may be added. Does not combine with Single Wait.',
317 [qw(single_wait)], [qw(all_pungs three_kongs two_concealed_kongs two_melded_kongs concealed_kong melded_kong)] ],
318 [ 6, 'seven_shifted_pairs', 'Seven Shifted Pairs', 88,
319 'Hand is composed of seven pairs in the same suit, each shifted one up from the last. Does not combine with Full Flush, Concealed hand, or Single Wait.',
320 [qw(full_flush concealed_hand single_wait)], [qw(seven_pairs no_honours one_voided_suit)] ],
321 [ 7, 'thirteen_orphans', 'Thirteen Orphans', 88,
322 'Hand is composed of singles of any 12 of the 1, 9, and Honor tiles, along with a pair of the 13th. Does not combine with All Types, Concealed Hand, or Single Wait.',
323 [qw(all_types concealed_hand single_wait)], [qw(all_terminals_and_honours outside_hand)] ],
324 [ 8, 'all_terminals', 'All Terminals', 64,
325 'The pair(s), Pungs or Kongs are all made up of 1 or 9 Number Tiles, without Honor Tiles. Does not combine with All Pungs, Outside Hand, Pung of Terminals or Honors or No Honors. Can combine with Double Pung or Triple Pung.',
326 [qw(all_pungs outside_hand pung_of_terminals_or_honours no_honours)], [qw(all_terminals_and_honours)] ],
327 [ 9, 'little_four_winds', 'Little Four Winds', 64,
328 'Hand includes three Pungs of Winds, and a pair of the fourth Wind. Combines with Prevalent Wind and Seat Wind, but does not combine with Big Three Winds, or Pung of Terminals or Honors.',
329 [qw(big_three_winds pung_of_terminals_or_honours)], [] ],
330 [ 10, 'little_three_dragons', 'Little Three Dragons', 64,
331 'Hand includes Pungs of two Dragons and a pair of the third Dragon. Does not combine with Dragon Pung, or Two Dragons.',
332 [qw(dragon_pung two_dragon_pungs)], [] ],
333 [ 11, 'all_honours', 'All Honors', 64,
334 'The pair(s), Pungs or Kongs are all made up of Honor Tiles. Can be formed with Pungs or Kongs, any of which may be concealed or melded. Does not combine with All Pungs, Outside Hand, and Pung of Terminals or Honors.',
335 [qw(all_pungs outside_hand pung_of_terminals_or_honours)], [qw(all_terminals_and_honours)] ],
336 [ 12, 'four_concealed_pungs', 'Four Concealed Pungs', 64,
337 'Hand includes four Pungs achieved without melding. Does not combine with All Pungs or Concealed. Does combine with Fully Concealed if Self-Drawn.',
338 [qw(all_pungs concealed_hand)], [qw(three_concealed_pungs two_concealed_pungs)] ],
339 [ 13, 'pure_terminal_chows', 'Pure Terminal Chows', 64,
340 'Hand consists of two each of the lower and upper terminal Chows in one suit, with a pair of fives in the same suit. Does not combine with Seven Pairs, Full Flush, All Chows, Pure Double Chow, or Two Terminal Chows.',
341 [qw(seven_pairs full_flush all_chows pure_double_chow two_terminal_chows)], [qw(no_honours one_voided_suit)] ],
342 [ 14, 'quadruple_chow', 'Quadruple Chow', 48,
343 'Four chows of the same numerical sequences in the same suit. Does not combine with Pure Shifted Pungs, Tile Hog, or Pure Double Chow.',
344 [qw(pure_shifted_pungs tile_hog pure_double_chow)], [qw(pure_triple_chow)] ],
345 [ 15, 'four_pure_shifted_pungs', 'Four Pure Shifted Pungs', 48,
346 'Four Pungs or Kongs in the same suit, each shifted up one from the last. Does not combine with Pure Triple Chow or All Pungs.',
347 [qw(pure_triple_chow all_pungs)], [qw(pure_shifted_pungs)] ],
348 [ 16, 'four_pure_shifted_chows', 'Four Pure Shifted Chows', 32,
349 'Four chows in one suit, each shifted up 1 or 2 numbers from the last, but not a combination of both. Does not combine with Short Straight.',
350 [qw(short_straight)], [qw(pure_shifted_chows)] ],
351 [ 17, 'three_kongs', 'Three Kongs', 32,
352 'Hand contains three Kongs. May combine with Three Concealed Pengs if the Kongs are all concealed.',
353 [], [qw(two_melded_kongs two_concealed_kongs concealed_kong melded_kong)] ],
354 [ 18, 'all_terminals_and_honours', 'All Terminals and Honors', 32,
355 'The pair(s), Pungs or Kongs are all made up of 1 or 9 Number Tiles and Honor Tiles. Does not combine with All Pungs or Pung of Terminals or Honors.',
356 [qw(all_pungs pung_of_terminals_or_honours)], [qw(outside_hand)] ],
357 [ 19, 'seven_pairs', 'Seven Pairs', 24,
358 'Hand consisting of seven pairs. Does not combine with Concealed Hand or Single Wait. May combine with Fully Concealed if Self-Drawn.',
359 [qw(concealed_hand single_wait)], [] ],
360 [ 20, 'greater_honours_and_knitted_tiles', 'Greater Honors and Knitted Tiles', 24,
361 'Formed by 7 single Honors (one of every Wind and Dragon), and singles of suit tiles belonging to separate Knitted sequences (for example, 1-4-7 of Bamboos, 2-5-8 of Characters, and 3-6-9 of Dots). Does not combine with All Types or Concealed Hand. May be combined with Fully Concealed if Self-Drawn.',
362 [qw(all_types concealed_hand)], [qw(lesser_honours_and_knitted_tiles)] ],
363 [ 21, 'all_even_pungs', 'All Even Pungs', 24,
364 'A hand formed with Pungs of even-numbered suit tiles, and a pair of the same. Does not combine with All Pungs or All Simples.',
365 [qw(all_pungs all_simples)], [qw(no_honours)] ],
366 [ 22, 'full_flush', 'Full Flush', 24,
367 'All the tiles are in the same suit. Does not combine with No Honors.',
368 [qw(no_honours)], [qw(one_voided_suit)] ],
369 [ 23, 'pure_triple_chow', 'Pure Triple Chow', 24,
370 'Three chows of the same numerical sequence and in the same suit. Does not combine with Pure Shifted Pungs or Pure Double Chow.',
371 [qw(pure_shifted_pungs pure_double_chow)], [] ],
372 [ 24, 'pure_shifted_pungs', 'Pure Shifted Pungs', 24,
373 'Three Pungs or Kongs of the same suit, each shifted one up from the last. Does not combine with Pure Triple Chow.',
374 [qw(pure_triple_chow)], [] ],
375 [ 25, 'upper_tiles', 'Upper Tiles', 24,
376 'Hand consisting entirely of 7, 8, and 9 tiles. Does not combine with No Honors.',
377 [qw(no_honours)], [qw(upper_four)] ],
378 [ 26, 'middle_tiles', 'Middle Tiles', 24,
379 'A hand consisting entirely of 4, 5, and 6 tiles. Does not combine with No Honors or All Simples.',
380 [qw(no_honours all_simples)], [] ],
381 [ 27, 'lower_tiles', 'Lower Tiles', 24,
382 'A hand consisting entirely of 1, 2, and 3 tiles. Does not combine with No Honors.',
383 [qw(no_honours)], [qw(lower_four)] ],
384 [ 28, 'pure_straight', 'Pure Straight', 16,
385 'Hand using one of every number, 1-9, in three consecutive chows, in the same suit.',
386 [], [] ],
387 [ 29, 'three_suited_terminal_chows', 'Three-Suited Terminal Chows', 16,
388 "Hand consisting of 1-2-3 + 7-8-9 in one suit (Two Terminal Chows), 1-2-3 + 7-8-9 in another suit, a pair of fives in the third suit. Doesn't combine with Pure Double Chow, Two Terminal Chows, No Honors, or All Chows.",
389 [qw(pure_double_chow two_terminal_chows no_honours all_chows)], [qw(mixed_double_chow)] ],
390 [ 30, 'pure_shifted_chows', 'Pure Shifted Chows', 16,
391 'Three chows in one suit, each shifted up either one or two numbers from the last, but not a combination of both.',
392 [], [] ],
393 [ 31, 'all_fives', 'All Fives', 16,
394 'A hand in which every element includes a 5 tile. Does not combine with All Simples.',
395 [qw(all_simples)], [qw(no_honours)] ],
396 [ 32, 'triple_pung', 'Triple Pung', 16,
397 'Three Pungs of the same number, in each suit.',
398 [], [qw(double_pung)] ],
399 [ 33, 'three_concealed_pungs', 'Three Concealed Pungs', 16,
400 'Three Pungs achieved without melding.',
401 [], [qw(two_concealed_pungs)] ],
402 [ 34, 'lesser_honours_and_knitted_tiles', 'Lesser Honors and Knitted Tiles', 12,
403 'A hand made of singles of the following tiles: Any Honors, along with Suit tiles that belong to different Knitted sequences (for example, 1-4-7 of Characters, 2-5-8 of Bamboos, and 3-6-9 of Dots - each of the 3 suits must belong to a different Knitted sequence, but not necessarily in the order listed here). Does not Combines with All Types and Concealed Hand. (Combines with Fully Concealed if Self-Drawn.)',
404 [qw(all_types concealed_hand)], [] ],
405 [ 35, 'knitted_straight', 'Knitted Straight', 12,
406 'A special Straight which is formed not with standard Chows but with 3 different Knitted sequences. For example, 1-4-7 of Dots, 2-5-8 of Characters, and 3-6-9 of Bamboos - but not necessarily in this order.',
407 [], [] ],
408 [ 36, 'upper_four', 'Upper Four', 12,
409 'A hand created solely with suit tiles 6 through 9. Does not combine with No Honors.',
410 [qw(no_honours)], [] ],
411 [ 37, 'lower_four', 'Lower Four', 12,
412 'A hand created with suit tiles 1 through 4 only. Does not combine with No Honors.',
413 [qw(no_honours)], [] ],
414 [ 38, 'big_three_winds', 'Big Three Winds', 12,
415 'Hand includes Pungs or Kongs of three of the Winds.',
416 [], [] ],
417 [ 39, 'mixed_straight', 'Mixed Straight', 8,
418 'Three chows in three suits making 9 continuous numbers (1-9).',
419 [], [] ],
420 [ 40, 'reversible_tiles', 'Reversible Tiles', 8,
421 'A hand created entirely with those tiles which are vertically symmetrical (1,2,3,4,5,8,9 Dots - 2,4,5,6,8,9 Bams , and White Dragon). Does not combine with One Voided Suit.',
422 [qw(one_voided_suit)], [] ],
423 [ 41, 'mixed_triple_chow', 'Mixed Triple Chow', 8,
424 'Three chows of the same numerical sequence, one in each suit.',
425 [], [qw(mixed_double_chow)] ],
426 [ 42, 'mixed_shifted_pungs', 'Mixed Shifted Pungs', 8,
427 'Three Pungs or Kongs, one in each suit, each shifted up one number from the last.',
428 [], [] ],
429 [ 43, 'chicken_hand', 'Chicken Hand', 8,
430 'A hand that would otherwise earn 0 points (excluding Flowers).',
431 [], [], 'chicken' ],
432 [ 44, 'last_tile_draw', 'Last Tile Draw', 8,
433 'Going out (making mahjong) on a pick of the very last tile of the wall. (Points for Self-Drawn may not be combined.)',
434 [qw(self_drawn)], [] ],
435 [ 45, 'last_tile_claim', 'Last Tile Claim', 8,
436 'The last tile (of the game) discarded by another player.',
437 [], [] ],
438 [ 46, 'out_with_replacement_tile', 'Out with Replacement Tile', 8,
439 'Going out (making mahjong) on the replacement tile drawn after achieving a kong (not on a Flower replacement).',
440 [], [] ],
441 [ 47, 'robbing_the_kong', 'Robbing The Kong', 8,
442 'Winning off the tile that somebody adds to a melded pung (to create a Kong). (The points for Last Tile may not be combined.)',
443 [qw(last_tile)], [] ],
444 [ 48, 'two_concealed_kongs', 'Two Concealed Kongs', 8,
445 'Hand includes two Concealed Kongs.',
446 [], [qw(concealed_kong two_concealed_pungs)] ],
447 [ 49, 'all_pungs', 'All Pungs', 6,
448 'Hand includes four Pungs or Kongs and a pair.',
449 [], [] ],
450 [ 50, 'half_flush', 'Half Flush', 6,
451 'Formed by tiles from any one of the three suits, in combination with Honors.',
452 [], [] ],
453 [ 51, 'mixed_shifted_chows', 'Mixed Shifted Chows', 6,
454 'Three chows one in each suit, each shifted up one number from the last.',
455 [], [] ],
456 [ 52, 'all_types', 'All Types', 6,
457 'A hand in which each of the five sets is composed of a different type of tile (Characters, Bamboos, Dots, Winds, and Dragons).',
458 [], [] ],
459 [ 53, 'melded_hand', 'Melded Hand', 6,
460 'Every element or set in the hand, including the pair, must be completed with tiles discarded by other players. Does not combine with Single Wait.',
461 [qw(single_wait)], [] ],
462 [ 54, 'two_dragon_pungs', 'Two Dragons Pungs', 6,
463 'Two Pungs (or Kongs) of Dragon tiles.',
464 [], [qw(dragon_pung)] ],
465 [ 55, 'outside_hand', 'Outside Hand', 4,
466 'Hand includes Terminals and Honors in each element or set, including the Pair.',
467 [], [] ],
468 [ 56, 'fully_concealed_hand', 'Fully Concealed Hand', 4,
469 'A hand that a player completes without any melds and Self-Draws to win.',
470 [], [qw(self_drawn)] ],
471 [ 57, 'two_melded_kongs', 'Two Melded Kongs', 4,
472 'Hand includes two Melded Kongs. (A Melded Kong and a Concealed Kong make 6 points.)',
473 [], [qw(melded_kong concealed_kong)] ],
474 [ 58, 'last_tile', 'Last Tile', 4,
475 'Winning on a tile that is the last of its kind. (It must be clear to all players based on the discards and exposures.)',
476 [], [] ],
477 [ 59, 'dragon_pung', 'Dragon Pung', 2,
478 'A Pung or Kong of Dragon Tiles.',
479 [], [] ],
480 [ 60, 'prevalent_wind', 'Prevalent Wind', 2,
481 'A Pung or Kong of the Wind Tile corresponding to the current Prevalent Wind.',
482 [], [] ],
483 [ 61, 'seat_wind', 'Seat Wind', 2,
484 "A Pung or Kong of the Wind Tile corresponding to the player's Seat position at the table.",
485 [], [] ],
486 [ 62, 'concealed_hand', 'Concealed Hand', 2,
487 'All the tiles are Concealed; winning on a discard.',
488 [], [] ],
489 [ 63, 'all_chows', 'All Chows', 2,
490 'Hand consists of all Chows and no Honors. No Honors is implied.',
491 [], [qw(no_honours)] ],
492 [ 64, 'tile_hog', 'Tile Hog', 2,
493 'Using all four of a single suit tile, without using them as any kind of Kong.',
494 [], [] ],
495 [ 65, 'double_pung', 'Double Pung', 2,
496 'Two Pungs of the same number in two different suits.',
497 [], [] ],
498 [ 66, 'two_concealed_pungs', 'Two Concealed Pungs', 2,
499 'Two Pungs which are achieved without melding.',
500 [], [] ],
501 [ 67, 'concealed_kong', 'Concealed Kong', 2,
502 'Created when four identical tiles, all self-drawn, are declared as a Kong.',
503 [], [] ],
504 [ 68, 'all_simples', 'All Simples', 2,
505 'Hand formed without any Terminal or Honor Tiles.',
506 [], [qw(no_honours)] ],
507 [ 69, 'pure_double_chow', 'Pure Double Chow', 1,
508 'Two identical chows in the same suit.',
509 [], [] ],
510 [ 70, 'mixed_double_chow', 'Mixed Double Chow', 1,
511 'Two chows of the same numbers but in different suits.',
512 [], [] ],
513 [ 71, 'short_straight', 'Short Straight', 1,
514 'Two chows in the same suit that runs consecutively after one another to make a six-tile straight.',
515 [], [] ],
516 [ 72, 'two_terminal_chows', 'Two Terminal Chows', 1,
517 'Chows of 1-2-3 and 7-8-9 in the same suit.',
518 [], [] ],
519 [ 73, 'pung_of_terminals_or_honours', 'Pung of Terminals or Honors', 1,
520 'A Pung or Kong of Ones, Nines, or Winds. (A dragon pung scores 2 points.)',
521 [], [] ],
522 [ 74, 'melded_kong', 'Melded Kong', 1,
523 'A kong that was claimed from another player or promoted from a melded pung.',
524 [], [] ],
525 [ 75, 'one_voided_suit', 'One Voided Suit', 1,
526 'A hand that uses tiles from only two of the three suits (it lacks any tiles from one of the three suits).',
527 [], [] ],
528 [ 76, 'no_honours', 'No Honors', 1,
529 'A hand formed entirely of suit tiles, without Winds or Dragons.',
530 [], [] ],
531 [ 77, 'edge_wait', 'Edge Wait', 1,
532 'Waiting solely for a 3 to form a 1-2-3 chow, or solely for a 7 to form a 7-8-9 chow. Not valid if waiting for more than one tile. Not valid if the edge wait is combined with any other waits.',
533 [], [] ],
534 [ 78, 'closed_wait', 'Closed Wait', 1,
535 'Waiting solely for a tile whose number is "inside" (in the middle) to form a chow. Not valid if waiting for more than one tile. Not valid if the closed wait is combined with other waits',
536 [], [] ],
537 [ 79, 'single_wait', 'Single Waiting', 1,
538 'Waiting solely for a tile to form a pair. Not valid if waiting for more than one tile (for example, holding 1-2-3-4 and waiting on the 1 and 4).',
539 [], [] ],
540 [ 80, 'self_drawn', 'Self-Drawn', 1,
541 'Going out (making mahjong) with a fresh tile picked from the wall.',
542 [], [] ],
543 [ 81, 'flower_tiles', 'Flower Tiles', 1,
544 'Each tile carved with Chinese word for Spring, Summer, Autumn, Winter, Plum, Orchid, Bamboo or Chrysanthemum, will award you one point when you succeed in Hu.',
545 [], [] ],
546 );
547  
548 my %COMBINING = map { $_ => 1 } qw(
549 pure_double_chow mixed_double_chow short_straight two_terminal_chows double_pung
550 pure_triple_chow mixed_triple_chow pure_straight mixed_straight
551 pure_shifted_chows mixed_shifted_chows pure_shifted_pungs mixed_shifted_pungs triple_pung
552 quadruple_chow four_pure_shifted_chows four_pure_shifted_pungs
553 pure_terminal_chows three_suited_terminal_chows
554 );
555  
556 my %WHOLE = map { $_ => 1 } qw(
557 all_pungs all_chows all_fives all_even_pungs all_types outside_hand
558 melded_hand knitted_straight
559 );
560  
561 my (@FANS, %BY_KEY, %BY_N);
562 for my $row (@ROWS) {
563 my ($n, $key, $name, $points, $says, $excludes, $implies, $special) = @$row;
564 my $check = __PACKAGE__->can("_f_$key") or die "Game::Mahjong::Fans: no checker for $key";
565 my $fan = Game::Mahjong::Fan->new(
566 n => $n, key => $key, name => $name, points => $points, says => $says,
567 excludes => $excludes, implies => $implies, check => $check,
568 combining => $COMBINING{$key} ? 1 : 0,
569 whole => $WHOLE{$key} ? 1 : 0,
570 (defined $special ? (special => $special) : ()),
571 );
572 push @FANS, $fan;
573 $BY_KEY{$key} = $fan;
574 $BY_N{$n} = $fan;
575 }
576  
577 sub all { return @FANS }
578  
579 sub by_key {
580 my ($key) = @_;
581 return $BY_KEY{$key} || die "Game::Mahjong::Fans: no fan '" . (defined $key ? $key : 'undef') . "'";
582 }
583  
584 sub by_n {
585 my ($n) = @_;
586 return $BY_N{$n} || die "Game::Mahjong::Fans: no fan number " . (defined $n ? $n : 'undef');
587 }
588  
589 sub keys_of { return map { $_->key } @FANS }
590  
591 sub check_all {
592 my ($split, $ctx) = @_;
593 my $v = view($split, $ctx);
594 my %found;
595 for my $fan (@FANS) {
596 next if $fan->special;
597 my @i = $fan->instances($v);
598 $found{ $fan->key } = \@i if @i;
599 }
600 return \%found;
601 }
602  
603 1;
604  
605 __END__