| 1 | package Game::Mahjong::Score; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 34 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 34 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 34 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 34 | ▶ | use Game::Mahjong::Tiles; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 34 | ▶ | use Game::Mahjong::Decompose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 34 | ▶ | use Game::Mahjong::Fans; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 34 | ▶ | use Game::Mahjong::Tally; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 34 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 34 | ▶ | use constant MINIMUM => 8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 24779 | ▶ | sub score { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
score: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 24779 | my ($hand, $winning, $ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 24779 | ▶ | $ctx = { %{ $ctx || {} } }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ctx || {}
MC/DC0%0/1
$ctx || {}
$ctx
Truth table50%1/2
A
A= $ctx
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 24779 | ▶ | $winning ||= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$winning ||= 0
MC/DC100%1/1
$winning ||= 0
$winning
Truth table100%2/2
A
A= $winning
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 24779 | ▶ | die 'Game::Mahjong::Score: a hand scores at fourteen tiles, this one is ' . $hand->total | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | unless $hand->total == 14; | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 24778 | ▶ | if ($winning) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 23840 | $ctx->{winning} = $winning; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 23840 | ▶ | unless ($ctx->{waits}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | 35 | my $thirteen = $hand->clone; | |||||||||||||||||||||||||||||||||||||||||
| 27 | 35 | $thirteen->remove($winning); | |||||||||||||||||||||||||||||||||||||||||
| 28 | 35 | $ctx->{waits} = [ Game::Mahjong::Decompose::waits($thirteen) ]; | |||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 24778 | my @splits = Game::Mahjong::Decompose::decompose($hand, $winning); | |||||||||||||||||||||||||||||||||||||||||
| 33 | 24778 | ▶ | return undef unless @splits; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 1611 | my $best; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 1611 | for my $split (@splits) { | |||||||||||||||||||||||||||||||||||||||||
| 37 | 1868 | my $found = Game::Mahjong::Fans::check_all($split, $ctx); | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1868 | my @fans = _combine($found); | |||||||||||||||||||||||||||||||||||||||||
| 39 | 1868 | my $basic = 0; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 1868 | $basic += $_->{total} for @fans; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 1868 | ▶ | if (!$basic) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 19 | my $chicken = Game::Mahjong::Fans::by_key('chicken_hand'); | |||||||||||||||||||||||||||||||||||||||||
| 43 | 19 | @fans = ({ key => $chicken->key, name => $chicken->name, points => $chicken->points, times => 1, total => $chicken->points }); | |||||||||||||||||||||||||||||||||||||||||
| 44 | 19 | $basic = $chicken->points; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 1868 | ▶ | next if $best && $basic <= $best->{basic}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$best and $basic <= $best->{"basic"}
MC/DC100%2/2
$best and $basic <= $best->{"basic"}
$best
$basic <= $best->{"basic"}
Truth table100%3/3
A and B
A= $bestB= $basic <= $best->{"basic"}
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 1616 | $best = { basic => $basic, fans => \@fans, split => $split }; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 1611 | ▶ | my $flowers = $ctx->{flowers} || 0; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$ctx->{'flowers'} || 0
MC/DC100%1/1
$ctx->{'flowers'} || 0
$ctx->{'flowers'}
Truth table100%2/2
A
A= $ctx->{'flowers'}
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 1611 | my @fans = @{ $best->{fans} }; | |||||||||||||||||||||||||||||||||||||||||
| 52 | 1611 | ▶ | if ($flowers) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 1272 | my $f = Game::Mahjong::Fans::by_key('flower_tiles'); | |||||||||||||||||||||||||||||||||||||||||
| 54 | 1272 | 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 | 1611 | ▶ | minimum_met => $best->{basic} >= MINIMUM ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | ▶ | sub minimum_met { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
minimum_met: called CC=3 SCAR=13.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | my ($tally) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | ▶ | return $tally && $tally->basic >= MINIMUM ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$tally and $tally->basic >= 8
MC/DC0%0/2
$tally and $tally->basic >= 8
$tally
$tally->basic >= 8
Truth table33%1/3
A and B
A= $tallyB= $tally->basic >= 8
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2196 | ▶ | sub _combine { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_combine: called CC=22 SCAR=31.2
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 2196 | my ($found) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 74 | 2196 | my @candidates; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 2196 | for my $key (keys %$found) { | |||||||||||||||||||||||||||||||||||||||||
| 76 | 8524 | ▶ | next if $key eq 'flower_tiles'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 7047 | my $fan = Game::Mahjong::Fans::by_key($key); | |||||||||||||||||||||||||||||||||||||||||
| 78 | 7047 | push @candidates, { fan => $fan, instances => $found->{$key} }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 2196 | ▶ | @candidates = sort { $b->{fan}->points <=> $a->{fan}->points || $a->{fan}->n <=> $b->{fan}->n } @candidates; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$Game::Mahjong::Score::b->{'fan'}->points <=> $Game::Mahjong::Score::a->{'fan'}->points || $Game::Mahjong::Score::a->{'fan'}->n <=> $Game::Mahjong::Score::b->{'fan'}->n
MC/DC0%0/2
$Game::Mahjong::Score::b->{'fan'}->points <=> $Game::Mahjong::Score::a->{'fan'}->points || $Game::Mahjong::Score::a->{'fan'}->n <=> $Game::Mahjong::Score::b->{'fan'}->n
$Game::Mahjong::Score::b->{'fan'}->points <=> $Game::Mahjong::Score::a->{'fan'}->points
$Game::Mahjong::Score::a->{'fan'}->n <=> $Game::Mahjong::Score::b->{'fan'}->n
Truth table66%2/3
A || B
A= $Game::Mahjong::Score::b->{'fan'}->points <=> $Game::Mahjong::Score::a->{'fan'}->pointsB= $Game::Mahjong::Score::a->{'fan'}->n <=> $Game::Mahjong::Score::b->{'fan'}->n
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | 2196 | my (%kept, %blocked, %used); | |||||||||||||||||||||||||||||||||||||||||
| 83 | 2196 | my @out; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 2196 | for my $c (@candidates) { | |||||||||||||||||||||||||||||||||||||||||
| 85 | 7047 | my $fan = $c->{fan}; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 7047 | my $key = $fan->key; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 7047 | ▶ | next if $blocked{$key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6017 | my %fan_used; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 6017 | my ($times, $total) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 91 | 6017 | for my $inst (@{ $c->{instances} }) { | |||||||||||||||||||||||||||||||||||||||||
| 92 | 6262 | ▶ | my @sets = @{ $inst->{sets} || [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$inst->{'sets'} || []
MC/DC0%0/1
$inst->{'sets'} || []
$inst->{'sets'}
Truth table50%1/2
A
A= $inst->{'sets'}
| |||||||||||||||||||||||||||||||||||||||||||
| 93 | 6262 | ▶ | if ($fan->combining) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 1509 | ▶ | next if grep { $fan_used{$_} } @sets; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | 1430 | ▶ | next unless grep { !$used{$_} } @sets; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | 1327 | $fan_used{$_} = 1 for @sets; | |||||||||||||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 6080 | $times++; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 6080 | ▶ | $total += defined $inst->{points} ? $inst->{points} : $fan->points; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 6017 | ▶ | next unless $times; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | 5949 | $used{$_} = 1 for keys %fan_used; | |||||||||||||||||||||||||||||||||||||||||
| 104 | 5949 | $kept{$key} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 105 | 5949 | $blocked{$_} = 1 for @{ $fan->excludes }, @{ $fan->implies }; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 5949 | push @out, { key => $key, name => $fan->name, points => $fan->points, times => $times, total => $total }; | |||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 2196 | my @final; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 2196 | for my $f (@out) { | |||||||||||||||||||||||||||||||||||||||||
| 111 | 5949 | my $fan = Game::Mahjong::Fans::by_key($f->{key}); | |||||||||||||||||||||||||||||||||||||||||
| 112 | 5949 | my $clash = 0; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 5949 | for my $other (@{ $fan->excludes }, @{ $fan->implies }) { | |||||||||||||||||||||||||||||||||||||||||
| 114 | 1364 | ▶ | $clash = 1 if $kept{$other} && Game::Mahjong::Fans::by_key($other)->points >= $fan->points && $other ne $f->{key}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points and $other ne $f->{"key"}
$kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points
MC/DC0%0/3
$kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points and $other ne $f->{"key"}
$kept{$other}
Game::Mahjong::Fans::by_key($other)->points >= $fan->points
$other ne $f->{"key"}
Truth table25%1/4
A and B and C
A= $kept{$other}B= Game::Mahjong::Fans::by_key($other)->points >= $fan->pointsC= $other ne $f->{"key"}
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 5949 | ▶ | push @final, $f unless $clash; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | 2196 | return @final; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 329 | ▶ | sub floor { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
floor: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 329 | my ($hand, $ctx) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 329 | my @melds = @{ $hand->melds }; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 329 | ▶ | return 0 unless @melds; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | my $split = { | ||||||||||||||||||||||||||||||||||||||||||
| 126 | form => 'standard', | ||||||||||||||||||||||||||||||||||||||||||
| 127 | 328 | ▶ | sets => [ map { { kind => $_->kind, tiles => [ @{ $_->tiles } ], concealed => $_->concealed ? 1 : 0, melded => 1 } } @melds ], | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | pair => undef, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | singles => [], | ||||||||||||||||||||||||||||||||||||||||||
| 130 | placement => { in => undef, index => 0, wait => undef }, | ||||||||||||||||||||||||||||||||||||||||||
| 131 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 328 | ▶ | my $found = Game::Mahjong::Fans::check_all($split, { %{ $ctx || {} }, by => undef, waits => [ 0, 0 ] }); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ctx || {}
MC/DC0%0/1
$ctx || {}
$ctx
Truth table50%1/2
A
A= $ctx
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 328 | for my $key (keys %$found) { | |||||||||||||||||||||||||||||||||||||||||
| 134 | 1977 | ▶ | if (Game::Mahjong::Fans::by_key($key)->whole) { delete $found->{$key}; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 1782 | ▶ | my @with_sets = grep { @{ $_->{sets} || [] } } @{ $found->{$key} }; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$_->{'sets'} || []
MC/DC0%0/1
$_->{'sets'} || []
$_->{'sets'}
Truth table50%1/2
A
A= $_->{'sets'}
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 1782 | ▶ | if (@with_sets) { $found->{$key} = \@with_sets } else { delete $found->{$key} } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | 328 | my $total = 0; | |||||||||||||||||||||||||||||||||||||||||
| 139 | 328 | $total += $_->{total} for _combine($found); | |||||||||||||||||||||||||||||||||||||||||
| 140 | 328 | return $total; | |||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||
| 143 | 34 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||||||||||||||||||||||||