| 1 | package Game::Mahjong::Rules; | ||
| 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::Wall; | ||
| 12 | use Game::Mahjong::Hand; | ||
| 13 | use Game::Mahjong::Decompose; | ||
| 14 | use Game::Mahjong::Score; | ||
| 15 | use Game::Mahjong::Result; | ||
| 16 | use Game::Mahjong::Error; | ||
| 17 | |||
| 18 | our $VERSION = '0.04'; | ||
| 19 | |||
| 20 | our %PRIORITY; | ||
| 21 | BEGIN { %PRIORITY = (pass => 0, chow => 1, pung => 2, kong => 2, win => 3) } | ||
| 22 | |||
| 23 | has seed => (is => 'ro', isa => Str, required => 1); | ||
| 24 | |||
| 25 | has hand_no => (is => 'rw', isa => Int, default => 1); | ||
| 26 | |||
| 27 | has dealer => (is => 'rw', isa => Int, default => 0); | ||
| 28 | |||
| 29 | has totals => (is => 'rw', isa => ArrayRef, default => sub { [ 0, 0, 0, 0 ] }); | ||
| 30 | |||
| 31 | has wall => (is => 'rw'); | ||
| 32 | |||
| 33 | has hands => (is => 'rw', isa => ArrayRef, default => []); | ||
| 34 | |||
| 35 | has pools => (is => 'rw', isa => ArrayRef, default => sub { [ [], [], [], [] ] }); | ||
| 36 | |||
| 37 | has phase => (is => 'rw', isa => Str, default => 'discard'); | ||
| 38 | |||
| 39 | has turn => (is => 'rw'); | ||
| 40 | |||
| 41 | has window => (is => 'rw'); | ||
| 42 | |||
| 43 | has drawn => (is => 'rw'); | ||
| 44 | |||
| 45 | has drawn_from => (is => 'rw'); | ||
| 46 | |||
| 47 | has replacement => (is => 'rw'); | ||
| 48 | |||
| 49 | has claimed_this_turn => (is => 'rw', default => 0); | ||
| 50 | |||
| 51 | has last_draw_emptied => (is => 'rw', default => 0); | ||
| 52 | |||
| 53 | has last_discard_is_last => (is => 'rw', default => 0); | ||
| 54 | |||
| 55 | has status => (is => 'rw', isa => Str, default => 'active'); | ||
| 56 | |||
| 57 | has history => (is => 'rw', isa => ArrayRef, default => []); | ||
| 58 | |||
| 59 | has outcomes => (is => 'rw', isa => ArrayRef, default => []); | ||
| 60 | |||
| 61 | has last => (is => 'rw'); | ||
| 62 | |||
| 63 | has winner => (is => 'rw'); | ||
| 64 | |||
| 65 | has result => (is => 'rw'); | ||
| 66 | |||
| 67 | has position => (is => 'ro'); | ||
| 68 | |||
| 69 | has moves => (is => 'rw', isa => Int, default => 0); | ||
| 70 | |||
| 71 | sub BUILD { | ||
| 72 | my ($self) = @_; | ||
| 73 | die 'Game::Mahjong::Rules: a seed is a non-empty string' unless length $self->seed; | ||
| 74 | die 'Game::Mahjong::Rules: hand_no is 1 to ' . Game::Mahjong::Result::HANDS | ||
| 75 | unless $self->hand_no >= 1 && $self->hand_no <= Game::Mahjong::Result::HANDS; | ||
| 76 | die 'Game::Mahjong::Rules: totals are four numbers' unless @{ $self->totals } == 4; | ||
| 77 | if ($self->position) { $self->_take_written_position($self->position) } | ||
| 78 | else { $self->_deal } | ||
| 79 | return; | ||
| 80 | } | ||
| 81 | |||
| 82 | sub prevailing { my ($self) = @_; return int(($self->hand_no - 1) / 4) } | ||
| 83 | |||
| 84 | sub round { my ($self) = @_; return $self->prevailing + 1 } | ||
| 85 | |||
| 86 | sub seat_wind { my ($self, $seat) = @_; return ($seat - $self->dealer) % 4 } | ||
| 87 | |||
| 88 | sub next_seat { my ($self, $seat) = @_; return ($seat + 1) % 4 } | ||
| 89 | |||
| 90 | sub _emit { | ||
| 91 | my ($self, $actor, $kind, %payload) = @_; | ||
| 92 | push @{ $self->outcomes }, { actor => $actor, kind => $kind, %payload }; | ||
| 93 | return; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub take_outcomes { | ||
| 97 | my ($self) = @_; | ||
| 98 | my @out = @{ $self->outcomes }; | ||
| 99 | @{ $self->outcomes } = (); | ||
| 100 | return @out; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub _deal { | ||
| 104 | my ($self) = @_; | ||
| 105 | $self->dealer(($self->hand_no - 1) % 4); | ||
| 106 | my $wall = Game::Mahjong::Wall->new(seed => $self->seed, hand => $self->hand_no); | ||
| 107 | my $deal = $wall->deal($self->dealer); | ||
| 108 | $self->wall($wall); | ||
| 109 | $self->hands([ map { Game::Mahjong::Hand->new } 0 .. 3 ]); | ||
| 110 | $self->pools([ [], [], [], [] ]); | ||
| 111 | $self->_reset_turn_state; | ||
| 112 | $self->window(undef); | ||
| 113 | $self->_emit('sys', 'deal', | ||
| 114 | hand => $self->hand_no, round => $self->round, prevailing => $self->prevailing, | ||
| 115 | dealer => $self->dealer, winds => [ map { $self->seat_wind($_) } 0 .. 3 ]); | ||
| 116 | |||
| 117 | for my $i (0 .. 3) { | ||
| 118 | my $seat = ($self->dealer + $i) % 4; | ||
| 119 | for my $kind (@{ $deal->{hands}{$seat} }) { | ||
| 120 | if (Game::Mahjong::Tiles::is_bonus($kind)) { $self->hands->[$seat]->add_flower($kind); $self->_emit('sys', 'flower', seat => $seat, tile => $kind) } | ||
| 121 | else { $self->hands->[$seat]->add($kind) } | ||
| 122 | } | ||
| 123 | } | ||
| 124 | for my $i (0 .. 3) { | ||
| 125 | my $seat = ($self->dealer + $i) % 4; | ||
| 126 | while ($self->hands->[$seat]->total < ($seat == $self->dealer ? 14 : 13)) { | ||
| 127 | return $self->_exhausted if $self->wall->is_empty; | ||
| 128 | my $kind = $self->wall->replace; | ||
| 129 | $self->_emit('sys', 'drew', seat => $seat, from => 'back'); | ||
| 130 | if (Game::Mahjong::Tiles::is_bonus($kind)) { $self->hands->[$seat]->add_flower($kind); $self->_emit('sys', 'flower', seat => $seat, tile => $kind) } | ||
| 131 | else { $self->hands->[$seat]->add($kind) } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | $self->phase('discard'); | ||
| 135 | $self->turn($self->dealer); | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub _reset_turn_state { | ||
| 140 | my ($self) = @_; | ||
| 141 | $self->drawn(undef); | ||
| 142 | $self->drawn_from(undef); | ||
| 143 | $self->replacement(undef); | ||
| 144 | $self->claimed_this_turn(0); | ||
| 145 | $self->last_draw_emptied(0); | ||
| 146 | $self->last_discard_is_last(0); | ||
| 147 | $self->last(undef); | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _take_written_position { | ||
| 152 | my ($self, $p) = @_; | ||
| 153 | die 'Game::Mahjong::Rules: a position names four hands' unless ref $p->{hands} eq 'ARRAY' && @{ $p->{hands} } == 4; | ||
| 154 | $self->dealer($p->{dealer} // (($self->hand_no - 1) % 4)); | ||
| 155 | $self->hands([ map { Game::Mahjong::Hand->from_notation($_) } @{ $p->{hands} } ]); | ||
| 156 | $self->pools([ map { [ map { Game::Mahjong::Tiles::id_of($_) } @{ $_ } ] } @{ $p->{pools} || [ [], [], [], [] ] } ]); | ||
| 157 | $self->wall(Game::Mahjong::Wall->new(seed => $self->seed, hand => $self->hand_no, | ||
| 158 | tiles => [ map { /\A\d+\z/ ? $_ : Game::Mahjong::Tiles::id_of($_) } @{ $p->{wall} || [] } ])); | ||
| 159 | $self->wall->dealt(1); | ||
| 160 | $self->_reset_turn_state; | ||
| 161 | $self->window(undef); | ||
| 162 | my $turn = $p->{turn} // $self->dealer; | ||
| 163 | for my $seat (0 .. 3) { | ||
| 164 | my $want = $seat == $turn ? 14 : 13; | ||
| 165 | die "Game::Mahjong::Rules: seat $seat holds " . $self->hands->[$seat]->total . " tiles' worth, not $want" | ||
| 166 | unless $self->hands->[$seat]->total == $want; | ||
| 167 | } | ||
| 168 | $self->phase('discard'); | ||
| 169 | $self->turn($turn); | ||
| 170 | $self->claimed_this_turn($p->{claimed_this_turn} ? 1 : 0); | ||
| 171 | $self->drawn($p->{drawn} ? Game::Mahjong::Tiles::id_of($p->{drawn}) : undef); | ||
| 172 | return; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub hand_of { my ($self, $seat) = @_; return $self->hands->[$seat] } | ||
| 176 | |||
| 177 | sub pool_of { my ($self, $seat) = @_; return $self->pools->[$seat] } | ||
| 178 | |||
| 179 | sub is_active { return $_[0]->status eq 'active' ? 1 : 0 } | ||
| 180 | |||
| 181 | sub waiting_on { | ||
| 182 | my ($self) = @_; | ||
| 183 | return () unless $self->is_active; | ||
| 184 | return ($self->turn) if $self->phase eq 'discard'; | ||
| 185 | return $self->_window_waiting if $self->phase eq 'claim' || $self->phase eq 'rob'; | ||
| 186 | return (); | ||
| 187 | } | ||
| 188 | |||
| 189 | sub _visible_count { | ||
| 190 | my ($self, $kind) = @_; | ||
| 191 | my $n = 0; | ||
| 192 | for my $seat (0 .. 3) { | ||
| 193 | $n += grep { $_ == $kind } @{ $self->pools->[$seat] }; | ||
| 194 | for my $meld (@{ $self->hands->[$seat]->melds }) { | ||
| 195 | next if $meld->concealed; | ||
| 196 | $n += grep { $_ == $kind } @{ $meld->tiles }; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | return $n; | ||
| 200 | } | ||
| 201 | |||
| 202 | sub _waits_of { | ||
| 203 | my ($self, $seat) = @_; | ||
| 204 | my $hand = $self->hands->[$seat]; | ||
| 205 | return [] unless $hand->total == 13; | ||
| 206 | $hand->waits([ Game::Mahjong::Decompose::waits($hand) ]) unless $hand->waits; | ||
| 207 | return $hand->waits; | ||
| 208 | } | ||
| 209 | |||
| 210 | sub _win_ctx { | ||
| 211 | my ($self, $seat, $by, $from, $tile) = @_; | ||
| 212 | return { | ||
| 213 | by => $by, | ||
| 214 | prevailing => $self->prevailing, | ||
| 215 | seat => $self->seat_wind($seat), | ||
| 216 | winning => $tile, | ||
| 217 | last_of_wall => ($by eq 'self' ? $self->last_draw_emptied : $self->last_discard_is_last) ? 1 : 0, | ||
| 218 | replacement => $by eq 'self' ? $self->replacement : undef, | ||
| 219 | last_tile => ($tile && $self->_visible_count($tile) == 3) ? 1 : 0, | ||
| 220 | flowers => scalar @{ $self->hands->[$seat]->flowers }, | ||
| 221 | from => $from, | ||
| 222 | }; | ||
| 223 | } | ||
| 224 | |||
| 225 | sub _tally_on { | ||
| 226 | my ($self, $seat, $tile, $by, $from) = @_; | ||
| 227 | my $waits = $self->_waits_of($seat); | ||
| 228 | return undef unless grep { $_ == $tile } @$waits; | ||
| 229 | my $with = $self->hands->[$seat]->clone; | ||
| 230 | $with->add($tile); | ||
| 231 | my $ctx = $self->_win_ctx($seat, $by, $from, $tile); | ||
| 232 | $ctx->{waits} = [ @$waits ]; | ||
| 233 | my $tally = Game::Mahjong::Score::score($with, $tile, $ctx); | ||
| 234 | return $tally && $tally->minimum_met ? $tally : undef; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub _tally_self { | ||
| 238 | my ($self, $seat) = @_; | ||
| 239 | my $hand = $self->hands->[$seat]; | ||
| 240 | return undef unless $hand->total == 14; | ||
| 241 | my $tile = $self->drawn; | ||
| 242 | my $ctx = $self->_win_ctx($seat, 'self', undef, $tile); | ||
| 243 | if ($tile) { | ||
| 244 | my $thirteen = $hand->clone; | ||
| 245 | $thirteen->remove($tile); | ||
| 246 | $ctx->{waits} = [ Game::Mahjong::Decompose::waits($thirteen) ]; | ||
| 247 | } | ||
| 248 | my $tally = Game::Mahjong::Score::score($hand, $tile || 0, $ctx); | ||
| 249 | return $tally && $tally->minimum_met ? $tally : undef; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub legal { | ||
| 253 | my ($self, $seat) = @_; | ||
| 254 | return () unless $self->is_active && defined $seat; | ||
| 255 | my $hand = $self->hands->[$seat]; | ||
| 256 | |||
| 257 | if ($self->phase eq 'discard') { | ||
| 258 | return () unless $seat == $self->turn; | ||
| 259 | my @legal = map { { kind => 'discard', tile => $_ } } $hand->kinds; | ||
| 260 | unless ($self->claimed_this_turn) { | ||
| 261 | for my $kind ($hand->kinds) { | ||
| 262 | push @legal, { kind => 'kong', tile => $kind } if $hand->holds_kong_of($kind); | ||
| 263 | push @legal, { kind => 'kong', tile => $kind } if $hand->exposed_pung_of($kind); | ||
| 264 | } | ||
| 265 | } | ||
| 266 | push @legal, { kind => 'win' } if $self->_tally_self($seat); | ||
| 267 | return @legal; | ||
| 268 | } | ||
| 269 | |||
| 270 | my $w = $self->window or return (); | ||
| 271 | return () unless $w->{may}{$seat} && !exists $w->{answers}{$seat}; | ||
| 272 | my @legal = ({ kind => 'pass' }); | ||
| 273 | for my $claim (@{ $w->{may}{$seat} }) { | ||
| 274 | if ($claim eq 'chow') { | ||
| 275 | push @legal, map { { kind => 'chow', tiles => $_ } } $hand->chow_shapes($w->{tile}); | ||
| 276 | } | ||
| 277 | else { push @legal, { kind => $claim } } | ||
| 278 | } | ||
| 279 | return @legal; | ||
| 280 | } | ||
| 281 | |||
| 282 | sub _refuse { | ||
| 283 | my ($self, $code, $seat) = @_; | ||
| 284 | return Game::Mahjong::Error->throw($code, legal => [ $self->legal($seat) ]); | ||
| 285 | } | ||
| 286 | |||
| 287 | sub apply { | ||
| 288 | my ($self, $seat, $move) = @_; | ||
| 289 | return $self->_refuse('game_over', $seat) unless $self->is_active; | ||
| 290 | return $self->_refuse('bad_move', $seat) unless ref $move eq 'HASH' && defined $move->{kind}; | ||
| 291 | return $self->_refuse('bad_move', $seat) unless defined $seat && $seat =~ /\A[0-3]\z/; | ||
| 292 | my $kind = $move->{kind}; | ||
| 293 | |||
| 294 | if ($self->phase eq 'discard') { | ||
| 295 | return $self->_refuse('wrong_phase', $seat) if $kind eq 'pass' || $kind eq 'pung' || $kind eq 'chow'; | ||
| 296 | return $self->_refuse('not_your_turn', $seat) unless $seat == $self->turn; | ||
| 297 | return $self->_discard($seat, $move) if $kind eq 'discard'; | ||
| 298 | return $self->_kong($seat, $move) if $kind eq 'kong'; | ||
| 299 | return $self->_self_win($seat) if $kind eq 'win'; | ||
| 300 | return $self->_refuse('bad_move', $seat); | ||
| 301 | } | ||
| 302 | |||
| 303 | return $self->_refuse('wrong_phase', $seat) if $kind eq 'discard'; | ||
| 304 | return $self->_answer($seat, $move); | ||
| 305 | } | ||
| 306 | |||
| 307 | sub _tile_arg { | ||
| 308 | my ($self, $move, $seat) = @_; | ||
| 309 | my $tile = $move->{tile}; | ||
| 310 | return (undef, $self->_refuse('bad_move', $seat)) unless defined $tile && $tile =~ /\A\d+\z/ && $tile >= 1 && $tile <= Game::Mahjong::Tiles::KINDS; | ||
| 311 | return ($tile + 0, undef); | ||
| 312 | } | ||
| 313 | |||
| 314 | sub _discard { | ||
| 315 | my ($self, $seat, $move) = @_; | ||
| 316 | my ($tile, $err) = $self->_tile_arg($move, $seat); | ||
| 317 | return $err if $err; | ||
| 318 | my $hand = $self->hands->[$seat]; | ||
| 319 | return $self->_refuse('tile_not_held', $seat) unless $hand->count($tile); | ||
| 320 | |||
| 321 | $hand->remove($tile); | ||
| 322 | push @{ $self->pools->[$seat] }, $tile; | ||
| 323 | $self->moves($self->moves + 1); | ||
| 324 | $self->_emit($seat, 'discard', tile => $tile); | ||
| 325 | $self->last({ seat => $seat, kind => 'discard', tile => $tile }); | ||
| 326 | $self->last_discard_is_last($self->last_draw_emptied ? 1 : 0); | ||
| 327 | $self->last_draw_emptied(0); | ||
| 328 | $self->drawn(undef); | ||
| 329 | $self->replacement(undef); | ||
| 330 | $self->claimed_this_turn(0); | ||
| 331 | |||
| 332 | return $self->_open_window('claim', $tile, $seat); | ||
| 333 | } | ||
| 334 | |||
| 335 | sub _kong { | ||
| 336 | my ($self, $seat, $move) = @_; | ||
| 337 | my ($tile, $err) = $self->_tile_arg($move, $seat); | ||
| 338 | return $err if $err; | ||
| 339 | return $self->_refuse('no_kong', $seat) if $self->claimed_this_turn; | ||
| 340 | my $hand = $self->hands->[$seat]; | ||
| 341 | |||
| 342 | if ($hand->holds_kong_of($tile)) { | ||
| 343 | $hand->concealed_kong($tile); | ||
| 344 | $self->moves($self->moves + 1); | ||
| 345 | $self->_emit($seat, 'kong', tile => $tile, how => 'concealed'); | ||
| 346 | $self->last({ seat => $seat, kind => 'kong', tile => $tile }); | ||
| 347 | return $self->_replacement_draw($seat, 'kong'); | ||
| 348 | } | ||
| 349 | if ($hand->exposed_pung_of($tile) && $hand->count($tile)) { | ||
| 350 | $self->moves($self->moves + 1); | ||
| 351 | $self->_emit($seat, 'kong', tile => $tile, how => 'promoted'); | ||
| 352 | $self->last({ seat => $seat, kind => 'kong', tile => $tile }); | ||
| 353 | return $self->_open_window('rob', $tile, $seat); | ||
| 354 | } | ||
| 355 | return $self->_refuse($hand->count($tile) ? 'no_kong' : 'tile_not_held', $seat); | ||
| 356 | } | ||
| 357 | |||
| 358 | sub _self_win { | ||
| 359 | my ($self, $seat) = @_; | ||
| 360 | my $hand = $self->hands->[$seat]; | ||
| 361 | my $tally = $self->_tally_self($seat); | ||
| 362 | unless ($tally) { | ||
| 363 | return $self->_refuse(Game::Mahjong::Decompose::is_complete($hand) ? 'too_few_points' : 'not_a_win', $seat); | ||
| 364 | } | ||
| 365 | $self->moves($self->moves + 1); | ||
| 366 | $self->_emit($seat, 'win'); | ||
| 367 | return $self->_hand_won($seat, $tally, 'self', undef, $self->drawn); | ||
| 368 | } | ||
| 369 | |||
| 370 | sub _may_claim { | ||
| 371 | my ($self, $seat, $tile, $from, $rob) = @_; | ||
| 372 | my $hand = $self->hands->[$seat]; | ||
| 373 | my @may; | ||
| 374 | push @may, 'win' if $self->_tally_on($seat, $tile, $rob ? 'rob' : 'discard', $from); | ||
| 375 | unless ($rob) { | ||
| 376 | push @may, 'kong' if $hand->holds_pung_of($tile) && $self->wall->remaining; | ||
| 377 | push @may, 'pung' if $hand->holds_pair($tile); | ||
| 378 | push @may, 'chow' if $seat == $self->next_seat($from) && $hand->chow_shapes($tile); | ||
| 379 | } | ||
| 380 | return @may; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub _open_window { | ||
| 384 | my ($self, $kind, $tile, $from) = @_; | ||
| 385 | my %may; | ||
| 386 | for my $seat (grep { $_ != $from } 0 .. 3) { | ||
| 387 | my @claims = $self->_may_claim($seat, $tile, $from, $kind eq 'rob'); | ||
| 388 | $may{$seat} = \@claims if @claims; | ||
| 389 | } | ||
| 390 | if (!%may) { | ||
| 391 | return $self->_close_window_with($kind, $tile, $from, undef); | ||
| 392 | } | ||
| 393 | $self->window({ kind => $kind, tile => $tile, from => $from, may => \%may, answers => {} }); | ||
| 394 | $self->phase($kind); | ||
| 395 | $self->turn(undef); | ||
| 396 | return 1; | ||
| 397 | } | ||
| 398 | |||
| 399 | sub _best_possible { | ||
| 400 | my ($self, $seat) = @_; | ||
| 401 | my $w = $self->window; | ||
| 402 | my $best = 0; | ||
| 403 | for my $c (@{ $w->{may}{$seat} }) { $best = $PRIORITY{$c} if $PRIORITY{$c} > $best } | ||
| 404 | return $best; | ||
| 405 | } | ||
| 406 | |||
| 407 | sub _window_waiting { | ||
| 408 | my ($self) = @_; | ||
| 409 | my $w = $self->window or return (); | ||
| 410 | my ($lodged, $lodged_seat) = $self->_lodged_best; | ||
| 411 | my @waiting; | ||
| 412 | for my $seat ($self->_seats_after($w->{from})) { | ||
| 413 | next unless $w->{may}{$seat}; | ||
| 414 | next if exists $w->{answers}{$seat}; | ||
| 415 | my $best = $self->_best_possible($seat); | ||
| 416 | next if $best < $lodged; | ||
| 417 | next if $best == $lodged && !($best == $PRIORITY{win} && $self->_nearer($seat, $lodged_seat, $w->{from})); | ||
| 418 | push @waiting, $seat; | ||
| 419 | } | ||
| 420 | return @waiting; | ||
| 421 | } | ||
| 422 | |||
| 423 | sub _seats_after { | ||
| 424 | my ($self, $from) = @_; | ||
| 425 | return map { ($from + $_) % 4 } 1 .. 3; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub _distance { my ($self, $seat, $from) = @_; return ($seat - $from) % 4 } | ||
| 429 | |||
| 430 | sub _nearer { | ||
| 431 | my ($self, $seat, $than, $from) = @_; | ||
| 432 | return 1 unless defined $than; | ||
| 433 | return $self->_distance($seat, $from) < $self->_distance($than, $from) ? 1 : 0; | ||
| 434 | } | ||
| 435 | |||
| 436 | sub _lodged_best { | ||
| 437 | my ($self) = @_; | ||
| 438 | my $w = $self->window; | ||
| 439 | my ($best, $seat) = (0, undef); | ||
| 440 | for my $s ($self->_seats_after($w->{from})) { | ||
| 441 | my $a = $w->{answers}{$s} or next; | ||
| 442 | next if !ref $a; | ||
| 443 | my $p = $PRIORITY{ $a->{kind} }; | ||
| 444 | if ($p > $best || ($p == $best && $p == $PRIORITY{win} && $self->_nearer($s, $seat, $w->{from}))) { | ||
| 445 | ($best, $seat) = ($p, $s); | ||
| 446 | } | ||
| 447 | } | ||
| 448 | return ($best, $seat); | ||
| 449 | } | ||
| 450 | |||
| 451 | sub _answer { | ||
| 452 | my ($self, $seat, $move) = @_; | ||
| 453 | my $w = $self->window or return $self->_refuse('no_window', $seat); | ||
| 454 | my $kind = $move->{kind}; | ||
| 455 | return $self->_refuse('cannot_claim', $seat) unless $w->{may}{$seat}; | ||
| 456 | return $self->_refuse('already_answered', $seat) if exists $w->{answers}{$seat}; | ||
| 457 | |||
| 458 | my %waiting = map { $_ => 1 } $self->_window_waiting; | ||
| 459 | if ($kind eq 'pass') { | ||
| 460 | $w->{answers}{$seat} = 'pass'; | ||
| 461 | } | ||
| 462 | else { | ||
| 463 | return $self->_refuse('cannot_claim', $seat) unless $waiting{$seat}; | ||
| 464 | return $self->_refuse('cannot_claim', $seat) unless grep { $_ eq $kind } @{ $w->{may}{$seat} }; | ||
| 465 | my $answer = { kind => $kind }; | ||
| 466 | if ($kind eq 'chow') { | ||
| 467 | my $tiles = $move->{tiles}; | ||
| 468 | return $self->_refuse('bad_move', $seat) unless ref $tiles eq 'ARRAY' && @$tiles == 2 | ||
| 469 | && !grep { !defined $_ || !/\A\d+\z/ || $_ < 1 || $_ > Game::Mahjong::Tiles::KINDS } @$tiles; | ||
| 470 | my ($a, $b) = sort { $a <=> $b } map { $_ + 0 } @$tiles; | ||
| 471 | my $ok = grep { $_->[0] == $a && $_->[1] == $b } $self->hands->[$seat]->chow_shapes($w->{tile}); | ||
| 472 | return $self->_refuse('not_a_meld', $seat) unless $ok; | ||
| 473 | $answer->{tiles} = [ $a, $b ]; | ||
| 474 | } | ||
| 475 | $w->{answers}{$seat} = $answer; | ||
| 476 | } | ||
| 477 | $self->moves($self->moves + 1); | ||
| 478 | $self->_emit($seat, $kind, ($kind eq 'chow' ? (tiles => $w->{answers}{$seat}{tiles}) : ())); | ||
| 479 | |||
| 480 | return 1 if $self->_window_waiting; | ||
| 481 | return $self->_resolve_window; | ||
| 482 | } | ||
| 483 | |||
| 484 | sub _resolve_window { | ||
| 485 | my ($self) = @_; | ||
| 486 | my $w = $self->window; | ||
| 487 | my ($best, $seat) = $self->_lodged_best; | ||
| 488 | my $answer = defined $seat ? $w->{answers}{$seat} : undef; | ||
| 489 | return $self->_close_window_with($w->{kind}, $w->{tile}, $w->{from}, $seat, $answer); | ||
| 490 | } | ||
| 491 | |||
| 492 | sub _close_window_with { | ||
| 493 | my ($self, $kind, $tile, $from, $seat, $answer) = @_; | ||
| 494 | $self->window(undef); | ||
| 495 | |||
| 496 | if (!defined $seat) { | ||
| 497 | if ($kind eq 'rob') { return $self->_perform_promotion($from, $tile) } | ||
| 498 | return $self->_draw_for($self->next_seat($from)); | ||
| 499 | } | ||
| 500 | |||
| 501 | my $claim = $answer->{kind}; | ||
| 502 | if ($claim eq 'win') { | ||
| 503 | my $tally = $self->_tally_on($seat, $tile, $kind eq 'rob' ? 'rob' : 'discard', $from); | ||
| 504 | die 'Game::Mahjong::Rules: a lodged win that no longer scores' unless $tally; | ||
| 505 | if ($kind eq 'rob') { $self->hands->[$from]->remove($tile) } | ||
| 506 | else { pop @{ $self->pools->[$from] } } | ||
| 507 | $self->hands->[$seat]->add($tile); | ||
| 508 | $self->_emit('sys', 'claimed', seat => $seat, meld => 'win', tile => $tile, from => $from); | ||
| 509 | return $self->_hand_won($seat, $tally, $kind eq 'rob' ? 'rob' : 'discard', $from, $tile); | ||
| 510 | } | ||
| 511 | |||
| 512 | pop @{ $self->pools->[$from] }; | ||
| 513 | my $hand = $self->hands->[$seat]; | ||
| 514 | if ($claim eq 'chow') { $hand->claim_chow($tile, @{ $answer->{tiles} }, $from) } | ||
| 515 | elsif ($claim eq 'pung') { $hand->claim_pung($tile, $from) } | ||
| 516 | elsif ($claim eq 'kong') { $hand->claim_kong($tile, $from) } | ||
| 517 | $self->_emit('sys', 'claimed', seat => $seat, meld => $claim, tile => $tile, from => $from); | ||
| 518 | $self->last({ seat => $seat, kind => $claim, tile => $tile, from => $from }); | ||
| 519 | |||
| 520 | if ($claim eq 'kong') { return $self->_replacement_draw($seat, 'kong') } | ||
| 521 | $self->phase('discard'); | ||
| 522 | $self->turn($seat); | ||
| 523 | $self->claimed_this_turn(1); | ||
| 524 | $self->drawn(undef); | ||
| 525 | $self->replacement(undef); | ||
| 526 | return 1; | ||
| 527 | } | ||
| 528 | |||
| 529 | sub _perform_promotion { | ||
| 530 | my ($self, $seat, $tile) = @_; | ||
| 531 | $self->hands->[$seat]->promote_kong($tile); | ||
| 532 | return $self->_replacement_draw($seat, 'kong'); | ||
| 533 | } | ||
| 534 | |||
| 535 | sub _draw_for { | ||
| 536 | my ($self, $seat) = @_; | ||
| 537 | return $self->_exhausted if $self->wall->is_empty; | ||
| 538 | my $kind = $self->wall->draw; | ||
| 539 | $self->_emit('sys', 'drew', seat => $seat, from => 'wall'); | ||
| 540 | $self->last_draw_emptied($self->wall->is_empty ? 1 : 0); | ||
| 541 | return $self->_took($seat, $kind, 'wall', undef); | ||
| 542 | } | ||
| 543 | |||
| 544 | sub _replacement_draw { | ||
| 545 | my ($self, $seat, $why) = @_; | ||
| 546 | return $self->_exhausted if $self->wall->is_empty; | ||
| 547 | my $kind = $self->wall->replace; | ||
| 548 | $self->_emit('sys', 'drew', seat => $seat, from => 'back'); | ||
| 549 | $self->last_draw_emptied($self->wall->is_empty ? 1 : 0); | ||
| 550 | return $self->_took($seat, $kind, 'back', $why); | ||
| 551 | } | ||
| 552 | |||
| 553 | sub _took { | ||
| 554 | my ($self, $seat, $kind, $from, $why) = @_; | ||
| 555 | if (Game::Mahjong::Tiles::is_bonus($kind)) { | ||
| 556 | $self->hands->[$seat]->add_flower($kind); | ||
| 557 | $self->_emit('sys', 'flower', seat => $seat, tile => $kind); | ||
| 558 | return $self->_replacement_draw($seat, 'flower'); | ||
| 559 | } | ||
| 560 | $self->hands->[$seat]->add($kind); | ||
| 561 | $self->drawn($kind); | ||
| 562 | $self->drawn_from($from); | ||
| 563 | $self->replacement($why); | ||
| 564 | $self->claimed_this_turn(0); | ||
| 565 | $self->phase('discard'); | ||
| 566 | $self->turn($seat); | ||
| 567 | $self->last({ seat => $seat, kind => 'drew', from => $from }); | ||
| 568 | return 1; | ||
| 569 | } | ||
| 570 | |||
| 571 | sub _hand_won { | ||
| 572 | my ($self, $seat, $tally, $by, $from, $tile) = @_; | ||
| 573 | my $deltas = Game::Mahjong::Result::settle(winner => $seat, by => $by, from => $from, points => $tally->points); | ||
| 574 | $self->_end_hand({ | ||
| 575 | winner => $seat, | ||
| 576 | by => $by, | ||
| 577 | from => $from, | ||
| 578 | tile => $tile, | ||
| 579 | fans => [ map { { key => $_->{key}, points => $_->{points}, times => $_->{times} } } @{ $tally->fans } ], | ||
| 580 | points => $tally->points, | ||
| 581 | flowers => $tally->flowers, | ||
| 582 | deltas => $deltas, | ||
| 583 | }); | ||
| 584 | return 1; | ||
| 585 | } | ||
| 586 | |||
| 587 | sub _exhausted { | ||
| 588 | my ($self) = @_; | ||
| 589 | $self->_end_hand({ winner => undef, by => 'exhausted', from => undef, tile => undef, fans => [], points => 0, flowers => 0, deltas => [ 0, 0, 0, 0 ] }); | ||
| 590 | return 1; | ||
| 591 | } | ||
| 592 | |||
| 593 | sub _end_hand { | ||
| 594 | my ($self, $record) = @_; | ||
| 595 | my $totals = $self->totals; | ||
| 596 | $totals->[$_] += $record->{deltas}[$_] for 0 .. 3; | ||
| 597 | $record->{hand} = $self->hand_no; | ||
| 598 | $record->{totals} = [ @$totals ]; | ||
| 599 | $record->{concealed_kongs} = [ map { [ map { $_->tile } @{ $self->hands->[$_]->concealed_kongs } ] } 0 .. 3 ]; | ||
| 600 | push @{ $self->history }, $record; | ||
| 601 | $self->_emit('sys', 'hand_end', %$record); | ||
| 602 | $self->window(undef); | ||
| 603 | |||
| 604 | if (Game::Mahjong::Result::finished($self->hand_no)) { | ||
| 605 | $self->status('finished'); | ||
| 606 | $self->phase('finished'); | ||
| 607 | $self->turn(undef); | ||
| 608 | my $winner = Game::Mahjong::Result::winner($totals); | ||
| 609 | $self->winner($winner); | ||
| 610 | $self->result(defined $winner ? 'score' : 'draw'); | ||
| 611 | $self->_emit('sys', 'game_end', winner => $winner, result => $self->result, | ||
| 612 | totals => [ @$totals ], places => Game::Mahjong::Result::places($totals)); | ||
| 613 | return; | ||
| 614 | } | ||
| 615 | $self->hand_no($self->hand_no + 1); | ||
| 616 | $self->_deal; | ||
| 617 | return; | ||
| 618 | } | ||
| 619 | |||
| 620 | sub check_invariants { | ||
| 621 | my ($self) = @_; | ||
| 622 | my @bad; | ||
| 623 | return @bad unless $self->is_active; | ||
| 624 | |||
| 625 | my %count; | ||
| 626 | for my $seat (0 .. 3) { | ||
| 627 | my $h = $self->hands->[$seat]; | ||
| 628 | $count{$_}++ for $h->tiles; | ||
| 629 | $count{$_}++ for @{ $h->flowers }; | ||
| 630 | for my $m (@{ $h->melds }) { $count{$_}++ for @{ $m->tiles } } | ||
| 631 | $count{$_}++ for @{ $self->pools->[$seat] }; | ||
| 632 | } | ||
| 633 | $count{$_}++ for @{ $self->wall->peek }; | ||
| 634 | my $tiles = 0; | ||
| 635 | $tiles += $_ for values %count; | ||
| 636 | my $whole = $tiles == Game::Mahjong::Tiles::TILES; | ||
| 637 | for my $kind (1 .. Game::Mahjong::Tiles::PATTERNS) { | ||
| 638 | my $cap = Game::Mahjong::Tiles::is_bonus($kind) ? 1 : Game::Mahjong::Tiles::PER_KIND; | ||
| 639 | my $seen = $count{$kind} || 0; | ||
| 640 | push @bad, "kind $kind is seen $seen times, not $cap" if $whole && $seen != $cap; | ||
| 641 | push @bad, "kind $kind is seen $seen times, more than the set holds" if !$whole && $seen > $cap; | ||
| 642 | } | ||
| 643 | |||
| 644 | my $on = $self->phase eq 'discard' ? $self->turn | ||
| 645 | : $self->phase eq 'rob' && $self->window ? $self->window->{from} | ||
| 646 | : undef; | ||
| 647 | for my $seat (0 .. 3) { | ||
| 648 | my $h = $self->hands->[$seat]; | ||
| 649 | my $want = (defined $on && $seat == $on) ? 14 : 13; | ||
| 650 | push @bad, "seat $seat holds " . $h->total . " tiles' worth, not $want" unless $h->total == $want; | ||
| 651 | } | ||
| 652 | |||
| 653 | my $sum = 0; | ||
| 654 | $sum += $_ for @{ $self->totals }; | ||
| 655 | push @bad, "totals sum to $sum" if $sum != 0; | ||
| 656 | |||
| 657 | if ($self->phase eq 'discard') { | ||
| 658 | my @w = $self->waiting_on; | ||
| 659 | push @bad, 'discard phase waits on ' . scalar(@w) . ' seats' unless @w == 1; | ||
| 660 | push @bad, 'a window is open in the discard phase' if $self->window; | ||
| 661 | } | ||
| 662 | elsif ($self->phase eq 'claim' || $self->phase eq 'rob') { | ||
| 663 | my $w = $self->window; | ||
| 664 | push @bad, 'no window in the ' . $self->phase . ' phase' unless $w; | ||
| 665 | if ($w) { | ||
| 666 | push @bad, 'the discarder is asked' if $w->{may}{ $w->{from} }; | ||
| 667 | for my $seat (keys %{ $w->{may} }) { | ||
| 668 | push @bad, "seat $seat is asked with no claim" unless @{ $w->{may}{$seat} }; | ||
| 669 | } | ||
| 670 | push @bad, 'a window with nobody left to answer' unless $self->_window_waiting; | ||
| 671 | } | ||
| 672 | } | ||
| 673 | else { push @bad, 'phase ' . $self->phase . ' while active' } | ||
| 674 | return @bad; | ||
| 675 | } | ||
| 676 | |||
| 677 | sub to_string { | ||
| 678 | my ($self) = @_; | ||
| 679 | return join "\n", map { "seat $_: " . $self->hands->[$_]->to_notation . ' | pool ' . Game::Mahjong::Notation::print(@{ $self->pools->[$_] }) } 0 .. 3; | ||
| 680 | } | ||
| 681 | |||
| 682 | 1; | ||
| 683 | |||
| 684 | __END__ |