blib/lib/Game/Mahjong/Score.pm

filter: Statementstmt 100% 127 / 127 Branchbran 93.1% 41 / 44 Conditioncond 59.2% 16 / 27 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 89.0% 203 / 228 CC 46 SCAR 26.7
CC
46
Cov
93%
_combine
22.6
score
15.0
floor
9.0
CRAP 14.4
« blib/lib/Game/Mahjong/Terminal.pm ↑ Summary blib/lib/Game/Mahjong/Rules.pm »
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 || {}
l!l
247790
MC/DC0%0/1
$ctx || {}
$ctx
Truth table50%1/2
A
A= $ctx
Aresult
11
00
19 24779 ▶ $winning ||= 0;
Condition100%2/2
$winning ||= 0
l!l
23840939
MC/DC100%1/1
$winning ||= 0
$winning
Truth table100%2/2
A
A= $winning
Aresult
11
00
20 24779 ▶ die 'Game::Mahjong::Score: a hand scores at fourteen tiles, this one is ' . $hand->total
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->total == 14 1 24778 24779
21 unless $hand->total == 14;
22  
23 24778 ▶ if ($winning) {
Branch: 1 branch
BranchTrueFalseTotal
if ($winning) 23840 938 24778
24 23840 $ctx->{winning} = $winning;
25 23840 ▶ unless ($ctx->{waits}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->{"waits"}) 35 23805 23840
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
BranchTrueFalseTotal
unless @splits 23167 1611 24778
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
BranchTrueFalseTotal
unless ($basic) 19 1849 1868
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
BranchTrueFalseTotal
if $best and $basic <= $best->{"basic"} 252 1616 1868
Condition100%3/3
$best and $basic <= $best->{"basic"}
!ll&&!rl&&r
16115252
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"}
ABresult
0X0
100
111
47 1616 $best = { basic => $basic, fans => \@fans, split => $split };
48 }
49  
50 1611 ▶ my $flowers = $ctx->{flowers} || 0;
Condition100%2/2
$ctx->{'flowers'} || 0
l!l
1272339
MC/DC100%1/1
$ctx->{'flowers'} || 0
$ctx->{'flowers'}
Truth table100%2/2
A
A= $ctx->{'flowers'}
Aresult
11
00
51 1611 my @fans = @{ $best->{fans} };
52 1611 ▶ if ($flowers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flowers) 1272 339 1611
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
BranchTrueFalseTotal
$best->{'basic'} >= 8 ? : 449 1162 1611
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
BranchTrueFalseTotal
$tally && $tally->basic >= 8 ? : 0 1 1
Condition33%1/3
$tally and $tally->basic >= 8
!ll&&!rl&&r
100
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $key eq "flower_tiles" 1477 7047 8524
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
l!l&&r!l&&!r
449942160
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
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if $blocked{$key} 1030 6017 7047
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'} || []
l!l
62620
MC/DC0%0/1
$inst->{'sets'} || []
$inst->{'sets'}
Truth table50%1/2
A
A= $inst->{'sets'}
Aresult
11
00
93 6262 ▶ if ($fan->combining) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fan->combining) 1509 4753 6262
94 1509 ▶ next if grep { $fan_used{$_} } @sets;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$fan_used{$_};} @sets 79 1430 1509
95 1430 ▶ next unless grep { !$used{$_} } @sets;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {not $used{$_};} @sets 103 1327 1430
96 1327 $fan_used{$_} = 1 for @sets;
97 }
98 6080 $times++;
99 6080 ▶ $total += defined $inst->{points} ? $inst->{points} : $fan->points;
Branch: 1 branch
BranchTrueFalseTotal
defined $inst->{'points'} ? : 4 6076 6080
100 }
101 6017 ▶ next unless $times;
Branch: 1 branch
BranchTrueFalseTotal
unless $times 68 5949 6017
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
BranchTrueFalseTotal
if $kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points and $other ne $f->{"key"} 0 1364 1364
Condition33%2/6
$kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points and $other ne $f->{"key"}
!ll&&!rl&&r
136400
$kept{$other} and Game::Mahjong::Fans::by_key($other)->points >= $fan->points
!ll&&!rl&&r
136400
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"}
ABCresult
0XX0
10X0
1100
1111
115 }
116 5949 ▶ push @final, $f unless $clash;
Branch: 1 branch
BranchTrueFalseTotal
unless $clash 5949 0 5949
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
BranchTrueFalseTotal
unless @melds 1 328 329
125 my $split = {
126 form => 'standard',
127 328 ▶ sets => [ map { { kind => $_->kind, tiles => [ @{ $_->tiles } ], concealed => $_->concealed ? 1 : 0, melded => 1 } } @melds ],
Branch: 1 branch
BranchTrueFalseTotal
$_->concealed ? : 5 381 386
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 || {}
l!l
3280
MC/DC0%0/1
$ctx || {}
$ctx
Truth table50%1/2
A
A= $ctx
Aresult
11
00
133 328 for my $key (keys %$found) {
134 1977 ▶ if (Game::Mahjong::Fans::by_key($key)->whole) { delete $found->{$key}; next }
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Fans::by_key($key)->whole) 195 1782 1977
135 1782 ▶ my @with_sets = grep { @{ $_->{sets} || [] } } @{ $found->{$key} };
Condition50%1/2
$_->{'sets'} || []
l!l
19440
MC/DC0%0/1
$_->{'sets'} || []
$_->{'sets'}
Truth table50%1/2
A
A= $_->{'sets'}
Aresult
11
00
136 1782 ▶ if (@with_sets) { $found->{$key} = \@with_sets } else { delete $found->{$key} }
Branch: 1 branch
BranchTrueFalseTotal
if (@with_sets) { } 185 1597 1782
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__
« blib/lib/Game/Mahjong/Terminal.pm ↑ Summary blib/lib/Game/Mahjong/Rules.pm »