| 1 | package Game::Mahjong::Score; | ||
| 2 | |||
| 3 | use 5.010; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | |||
| 7 | use Game::Mahjong::Tiles; | ||
| 8 | use Game::Mahjong::Decompose; | ||
| 9 | use Game::Mahjong::Fans; | ||
| 10 | use Game::Mahjong::Tally; | ||
| 11 | |||
| 12 | our $VERSION = '0.04'; | ||
| 13 | |||
| 14 | use constant MINIMUM => 8; | ||
| 15 | |||
| 16 | sub score { | ||
| 17 | my ($hand, $winning, $ctx) = @_; | ||
| 18 | $ctx = { %{ $ctx || {} } }; | ||
| 19 | $winning ||= 0; | ||
| 20 | die 'Game::Mahjong::Score: a hand scores at fourteen tiles, this one is ' . $hand->total | ||
| 21 | unless $hand->total == 14; | ||
| 22 | |||
| 23 | if ($winning) { | ||
| 24 | $ctx->{winning} = $winning; | ||
| 25 | unless ($ctx->{waits}) { | ||
| 26 | my $thirteen = $hand->clone; | ||
| 27 | $thirteen->remove($winning); | ||
| 28 | $ctx->{waits} = [ Game::Mahjong::Decompose::waits($thirteen) ]; | ||
| 29 | } | ||
| 30 | } | ||
| 31 | |||
| 32 | my @splits = Game::Mahjong::Decompose::decompose($hand, $winning); | ||
| 33 | return undef unless @splits; | ||
| 34 | |||
| 35 | my $best; | ||
| 36 | for my $split (@splits) { | ||
| 37 | my $found = Game::Mahjong::Fans::check_all($split, $ctx); | ||
| 38 | my @fans = _combine($found); | ||
| 39 | my $basic = 0; | ||
| 40 | $basic += $_->{total} for @fans; | ||
| 41 | if (!$basic) { | ||
| 42 | my $chicken = Game::Mahjong::Fans::by_key('chicken_hand'); | ||
| 43 | @fans = ({ key => $chicken->key, name => $chicken->name, points => $chicken->points, times => 1, total => $chicken->points }); | ||
| 44 | $basic = $chicken->points; | ||
| 45 | } | ||
| 46 | next if $best && $basic <= $best->{basic}; | ||
| 47 | $best = { basic => $basic, fans => \@fans, split => $split }; | ||
| 48 | } | ||
| 49 | |||
| 50 | my $flowers = $ctx->{flowers} || 0; | ||
| 51 | my @fans = @{ $best->{fans} }; | ||
| 52 | if ($flowers) { | ||
| 53 | my $f = Game::Mahjong::Fans::by_key('flower_tiles'); | ||
| 54 | push @fans, { key => $f->key, name => $f->name, points => $f->points, times => $flowers, total => $flowers * $f->points }; | ||
| 55 | } | ||
| 56 | |||
| 57 | return Game::Mahjong::Tally->new( | ||
| 58 | points => $best->{basic} + $flowers, | ||
| 59 | basic => $best->{basic}, | ||
| 60 | flowers => $flowers, | ||
| 61 | fans => \@fans, | ||
| 62 | split => $best->{split}, | ||
| 63 | minimum_met => $best->{basic} >= MINIMUM ? 1 : 0, | ||
| 64 | ); | ||
| 65 | } | ||
| 66 | |||
| 67 | sub minimum_met { | ||
| 68 | my ($tally) = @_; | ||
| 69 | return $tally && $tally->basic >= MINIMUM ? 1 : 0; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _combine { | ||
| 73 | my ($found) = @_; | ||
| 74 | my @candidates; | ||
| 75 | for my $key (keys %$found) { | ||
| 76 | next if $key eq 'flower_tiles'; | ||
| 77 | my $fan = Game::Mahjong::Fans::by_key($key); | ||
| 78 | push @candidates, { fan => $fan, instances => $found->{$key} }; | ||
| 79 | } | ||
| 80 | @candidates = sort { $b->{fan}->points <=> $a->{fan}->points || $a->{fan}->n <=> $b->{fan}->n } @candidates; | ||
| 81 | |||
| 82 | my (%kept, %blocked, %used); | ||
| 83 | my @out; | ||
| 84 | for my $c (@candidates) { | ||
| 85 | my $fan = $c->{fan}; | ||
| 86 | my $key = $fan->key; | ||
| 87 | next if $blocked{$key}; | ||
| 88 | |||
| 89 | my %fan_used; | ||
| 90 | my ($times, $total) = (0, 0); | ||
| 91 | for my $inst (@{ $c->{instances} }) { | ||
| 92 | my @sets = @{ $inst->{sets} || [] }; | ||
| 93 | if ($fan->combining) { | ||
| 94 | next if grep { $fan_used{$_} } @sets; | ||
| 95 | next unless grep { !$used{$_} } @sets; | ||
| 96 | $fan_used{$_} = 1 for @sets; | ||
| 97 | } | ||
| 98 | $times++; | ||
| 99 | $total += defined $inst->{points} ? $inst->{points} : $fan->points; | ||
| 100 | } | ||
| 101 | next unless $times; | ||
| 102 | |||
| 103 | $used{$_} = 1 for keys %fan_used; | ||
| 104 | $kept{$key} = 1; | ||
| 105 | $blocked{$_} = 1 for @{ $fan->excludes }, @{ $fan->implies }; | ||
| 106 | push @out, { key => $key, name => $fan->name, points => $fan->points, times => $times, total => $total }; | ||
| 107 | } | ||
| 108 | |||
| 109 | my @final; | ||
| 110 | for my $f (@out) { | ||
| 111 | my $fan = Game::Mahjong::Fans::by_key($f->{key}); | ||
| 112 | my $clash = 0; | ||
| 113 | for my $other (@{ $fan->excludes }, @{ $fan->implies }) { | ||
| 114 | $clash = 1 if $kept{$other} && Game::Mahjong::Fans::by_key($other)->points >= $fan->points && $other ne $f->{key}; | ||
| 115 | } | ||
| 116 | push @final, $f unless $clash; | ||
| 117 | } | ||
| 118 | return @final; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub floor { | ||
| 122 | my ($hand, $ctx) = @_; | ||
| 123 | my @melds = @{ $hand->melds }; | ||
| 124 | return 0 unless @melds; | ||
| 125 | my $split = { | ||
| 126 | form => 'standard', | ||
| 127 | sets => [ map { { kind => $_->kind, tiles => [ @{ $_->tiles } ], concealed => $_->concealed ? 1 : 0, melded => 1 } } @melds ], | ||
| 128 | pair => undef, | ||
| 129 | singles => [], | ||
| 130 | placement => { in => undef, index => 0, wait => undef }, | ||
| 131 | }; | ||
| 132 | my $found = Game::Mahjong::Fans::check_all($split, { %{ $ctx || {} }, by => undef, waits => [ 0, 0 ] }); | ||
| 133 | for my $key (keys %$found) { | ||
| 134 | if (Game::Mahjong::Fans::by_key($key)->whole) { delete $found->{$key}; next } | ||
| 135 | my @with_sets = grep { @{ $_->{sets} || [] } } @{ $found->{$key} }; | ||
| 136 | if (@with_sets) { $found->{$key} = \@with_sets } else { delete $found->{$key} } | ||
| 137 | } | ||
| 138 | my $total = 0; | ||
| 139 | $total += $_->{total} for _combine($found); | ||
| 140 | return $total; | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; | ||
| 144 | |||
| 145 | __END__ |