| 1 | package Game::Mahjong::Decompose; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Game::Mahjong::Tiles; | ||
| 8 | |||
| 9 | our $VERSION = '0.04'; | ||
| 10 | |||
| 11 | our %FORM = ( | ||
| 12 | 1 => 'standard', | ||
| 13 | 2 => 'seven_pairs', | ||
| 14 | 3 => 'thirteen_orphans', | ||
| 15 | 4 => 'honours_knitted', | ||
| 16 | 5 => 'knitted_straight', | ||
| 17 | ); | ||
| 18 | |||
| 19 | sub decompose { | ||
| 20 | my ($hand, $winning) = @_; | ||
| 21 | my $splits = _decompose($hand->counts, $hand->meld_count, $winning // 0); | ||
| 22 | die 'Game::Mahjong::Decompose: more splits than the table holds' unless defined $splits; | ||
| 23 | |||
| 24 | my @melded = map { | ||
| 25 | { | ||
| 26 | kind => $_->kind, | ||
| 27 | tiles => [ @{ $_->tiles } ], | ||
| 28 | concealed => $_->concealed ? 1 : 0, | ||
| 29 | melded => 1, | ||
| 30 | } | ||
| 31 | } @{ $hand->melds }; | ||
| 32 | |||
| 33 | for my $split (@$splits) { | ||
| 34 | $_->{concealed} = 1, $_->{melded} = 0 for @{ $split->{sets} }; | ||
| 35 | push @{ $split->{sets} }, @melded; | ||
| 36 | } | ||
| 37 | return @$splits; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub is_complete { | ||
| 41 | my ($hand) = @_; | ||
| 42 | return _is_complete($hand->counts, $hand->meld_count); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub waits { | ||
| 46 | my ($hand) = @_; | ||
| 47 | die 'Game::Mahjong::Decompose: waits are asked of a hand between turns, not of one holding a fourteenth' | ||
| 48 | unless $hand->total == 13; | ||
| 49 | return @{ _waits($hand->counts, $hand->meld_count) }; | ||
| 50 | } | ||
| 51 | |||
| 52 | 1; | ||
| 53 | |||
| 54 | __END__ |