blib/lib/Game/Mahjong/Search.pm

filter: Statementstmt 97.9% 237 / 242 Branchbran 95.0% 95 / 100 Conditioncond 86.7% 98 / 113 MC/DCmcdc 74.1% 46 / 62 Subroutinesub 92.3% 24 / 26 Podpod 100% 2 / 2 totaltotal 92.1% 502 / 545 CC 107 SCAR 26.2
CC
107
Cov
95%
_answer
30.0
_potential_after
17.0
_discard
13.2
CRAP 13.8
« blib/lib/Game/Mahjong/Rules.pm ↑ Summary blib/lib/Game/Mahjong/Result.pm »
1 package Game::Mahjong::Search;
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 Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 34 ▶use Game::Mahjong::Tiles;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 34 ▶use Game::Mahjong::Hand;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 34 ▶use Game::Mahjong::Meld;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 34 ▶use Game::Mahjong::Decompose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 34 ▶use Game::Mahjong::Shanten;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 34 ▶use Game::Mahjong::Score;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 34 our $VERSION = '0.03';
16  
17 34 our %OFF;
18  
19 17014 ▶sub view_of {
Subroutine view_of: called CC=13 SCAR=25.6
Pod documented
20 17014 my ($rules, $seat) = @_;
21 17014 my $hand = $rules->hand_of($seat)->clone;
22 17014 my %visible;
23 17014 for my $s (0 .. 3) {
24 68056 $visible{$_}++ for @{ $rules->pool_of($s) };
25 68056 for my $m (@{ $rules->hand_of($s)->melds }) {
26 7739 ▶ next if $m->concealed && $s != $seat;
Branch: 1 branch
BranchTrueFalseTotal
if $m->concealed and $s != $seat 265 7474 7739
Condition100%3/3
$m->concealed and $s != $seat
!ll&&!rl&&r
738094265
MC/DC100%2/2
$m->concealed and $s != $seat
$m->concealed $s != $seat
Truth table100%3/3
A and B
A= $m->concealedB= $s != $seat
ABresult
0X0
100
111
27 7474 $visible{$_}++ for @{ $m->tiles };
28 }
29 }
30 17014 $visible{$_}++ for $hand->tiles;
31 17014 my @melds;
32 17014 for my $s (0 .. 3) {
33 push @melds, [ map {
34 7739 ▶ { kind => $_->kind, tiles => ($_->concealed && $s != $seat) ? [] : [ @{ $_->tiles } ], concealed => $_->concealed ? 1 : 0 }
Branch: 2 branches
BranchTrueFalseTotal
$_->concealed && $s != $seat ? : 265 7474 7739
$_->concealed ? : 359 7380 7739
Condition100%3/3
$_->concealed and $s != $seat
!ll&&!rl&&r
738094265
MC/DC100%2/2
$_->concealed and $s != $seat
$_->concealed $s != $seat
Truth table100%3/3
A and B
A= $_->concealedB= $s != $seat
ABresult
0X0
100
111
35 68056 } @{ $rules->hand_of($s)->melds } ];
36 }
37 17014 my $w = $rules->window;
38 return {
39 seat => $seat,
40 hand => $hand,
41 phase => $rules->phase,
42 turn => $rules->turn,
43 window => $w ? { tile => $w->{tile}, from => $w->{from}, kind => $w->{kind} } : undef,
44 prevailing => $rules->prevailing,
45 seat_wind => $rules->seat_wind($seat),
46 wall => $rules->wall->remaining,
47 68056 pools => [ map { [ @{ $rules->pool_of($_) } ] } 0 .. 3 ],
48 melds => \@melds,
49 visible => \%visible,
50 17014 ▶ totals => [ @{ $rules->totals } ],
Branch: 1 branch
BranchTrueFalseTotal
$w ? : 4353 12661 17014
51 hand_no => $rules->hand_no,
52 moves => $rules->moves,
53 drawn => $rules->drawn,
54 legal => [ $rules->legal($seat) ],
55 };
56 }
57  
58 0 ▶sub _word {
Subroutine _word: not called CC=2 SCAR=17.9
59 0 my ($view, $tag, $n) = @_;
60 0 ▶ my $seed = $view->{seed} // 'search';
Condition0%0/2
$view->{"seed"} // "search"
l!l
00
MC/DC0%0/1
$view->{"seed"} // "search"
$view->{"seed"}
Truth table0%0/2
A
A= $view->{"seed"}
Aresult
11
00
61 0 my ($w) = unpack 'N', Digest::SHA::sha256("$seed:$view->{hand_no}:$view->{moves}:$tag");
62 0 return $w % $n;
63 }
64  
65 17028 ▶sub best {
Subroutine best: called CC=7 SCAR=19.7
Pod documented
66 17028 my ($view, $level, $word) = @_;
67 17028 ▶ $word ||= sub { _word($view, @_) };
Subroutine __ANON__: not called CC=1 SCAR=2.0
Condition33%1/3
$word ||= sub { _word($view, @_); }
l!l&&r!l&&!r
0170280
MC/DC0%0/2
$word ||= sub { _word($view, @_); }
$word sub { _word($view, @_); }
Truth table33%1/3
A ||= B
A= $wordB= sub { _word($view, @_); }
ABresult
1X1
011
000
68 17028 my @legal = @{ $view->{legal} };
69 17028 ▶ return undef unless @legal;
Branch: 1 branch
BranchTrueFalseTotal
unless @legal 0 17028 17028
70 17028 ▶ return $legal[0] if @legal == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @legal == 1 3 17025 17028
71  
72 17025 my ($win) = grep { $_->{kind} eq 'win' } @legal;
73 17025 ▶ return $win if $win;
Branch: 1 branch
BranchTrueFalseTotal
if $win 137 16888 17025
74  
75 16888 ▶ if ($view->{phase} eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($view->{"phase"} eq "discard") 12623 4265 16888
76 12623 my $kong = _self_kong($view, $level, \@legal);
77 12623 ▶ return $kong if $kong;
Branch: 1 branch
BranchTrueFalseTotal
if $kong 20 12603 12623
78 12603 return _discard($view, $level, \@legal, $word);
79 }
80 4265 return _answer($view, $level, \@legal, $word);
81 }
82  
83 370275 ▶sub _isolation {
Subroutine _isolation: called CC=8 SCAR=20.8
84 370275 my ($hand, $kind) = @_;
85 370275 my $c = $hand->count($kind);
86 370275 my $n = ($c - 1) * 3;
87 370275 ▶ if (my $rank = Game::Mahjong::Tiles::rank_of($kind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rank = Game::Mahjong::Tiles::rank_of($kind)) 346484 23791 370275
88 346484 ▶ $n += 2 * $hand->count($kind - 1) if $rank > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rank > 1 313409 33075 346484
89 346484 ▶ $n += 2 * $hand->count($kind + 1) if $rank < 9;
Branch: 1 branch
BranchTrueFalseTotal
if $rank < 9 314453 32031 346484
90 346484 ▶ $n += $hand->count($kind - 2) if $rank > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $rank > 2 274646 71838 346484
91 346484 ▶ $n += $hand->count($kind + 2) if $rank < 8;
Branch: 1 branch
BranchTrueFalseTotal
if $rank < 8 278529 67955 346484
92 }
93 370275 ▶ my $tie = Game::Mahjong::Tiles::is_honour($kind) ? 0 : Game::Mahjong::Tiles::is_terminal($kind) ? 1 : 2;
Branch: 2 branches
BranchTrueFalseTotal
Game::Mahjong::Tiles::is_honour($kind) ? : 23791 346484 370275
Game::Mahjong::Tiles::is_terminal($kind) ? : 65106 281378 346484
94 370275 return $n * 10 + $tie;
95 }
96  
97 169226 ▶sub _remaining {
Subroutine _remaining: called CC=3 SCAR=11.1
98 169226 my ($view, $kind) = @_;
99 169226 ▶ my $r = Game::Mahjong::Tiles::PER_KIND - ($view->{visible}{$kind} || 0);
Condition100%2/2
$view->{'visible'}{$kind} || 0
l!l
10149567731
MC/DC100%1/1
$view->{'visible'}{$kind} || 0
$view->{'visible'}{$kind}
Truth table100%2/2
A
A= $view->{'visible'}{$kind}
Aresult
11
00
100 169226 ▶ return $r < 0 ? 0 : $r;
Branch: 1 branch
BranchTrueFalseTotal
$r < 0 ? : 0 169226 169226
101 }
102  
103 22551 ▶sub _acceptance {
Subroutine _acceptance: called CC=2 SCAR=6.9
104 22551 my ($view, $kind) = @_;
105 22551 my $after = $view->{hand}->clone;
106 22551 $after->remove($kind);
107 22551 my $n = 0;
108 22551 $n += _remaining($view, $_) for Game::Mahjong::Shanten::ukeire($after);
109 22551 return $n;
110 }
111  
112 12603 ▶sub _discard {
Subroutine _discard: called CC=13 SCAR=25.8
113 12603 my ($view, $level, $legal, $word) = @_;
114 12603 my @discards = grep { $_->{kind} eq 'discard' } @$legal;
115 12603 my $hand = $view->{hand};
116  
117 12603 ▶ if ($level <= 1 || $OFF{shanten}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level <= 1 or $Game::Mahjong::Search::OFF{"shanten"}) 7627 4976 12603
Condition100%3/3
$level <= 1 or $Game::Mahjong::Search::OFF{"shanten"}
l!l&&r!l&&!r
762614976
MC/DC100%2/2
$level <= 1 or $Game::Mahjong::Search::OFF{"shanten"}
$level <= 1 $Game::Mahjong::Search::OFF{"shanten"}
Truth table100%3/3
A or B
A= $level <= 1B= $Game::Mahjong::Search::OFF{"shanten"}
ABresult
1X1
011
000
118 7627 ▶ my @ranked = sort { _isolation($hand, $a->{tile}) <=> _isolation($hand, $b->{tile}) || $a->{tile} <=> $b->{tile} } @discards;
Condition66%2/3
_isolation($hand, $Game::Mahjong::Search::a->{'tile'}) <=> _isolation($hand, $Game::Mahjong::Search::b->{'tile'}) || $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
l!l&&r!l&&!r
145314285490
MC/DC0%0/2
_isolation($hand, $Game::Mahjong::Search::a->{'tile'}) <=> _isolation($hand, $Game::Mahjong::Search::b->{'tile'}) || $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
_isolation($hand, $Game::Mahjong::Search::a->{'tile'}) <=> _isolation($hand, $Game::Mahjong::Search::b->{'tile'}) $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
Truth table66%2/3
A || B
A= _isolation($hand, $Game::Mahjong::Search::a->{'tile'}) <=> _isolation($hand, $Game::Mahjong::Search::b->{'tile'})B= $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
ABresult
1X1
011
000
119 7627 return $ranked[0];
120 }
121  
122 4976 my %after = map { $_->{tile} => Game::Mahjong::Shanten::after_discard($hand, $_->{tile}) } @discards;
123 4976 my $min = (sort { $a <=> $b } values %after)[0];
124 4976 my @keep = grep { $after{ $_->{tile} } == $min } @discards;
125  
126 4976 ▶ my @safe = $level >= 3 && !$OFF{defence} ? _safe_kinds($view) : ();
Branch: 1 branch
BranchTrueFalseTotal
$level >= 3 && !$Game::Mahjong::Search::OFF{'defence'} ? : 2018 2958 4976
Condition66%2/3
$level >= 3 and not $Game::Mahjong::Search::OFF{"defence"}
!ll&&!rl&&r
295802018
MC/DC50%1/2
$level >= 3 and not $Game::Mahjong::Search::OFF{"defence"}
$level >= 3 not $Game::Mahjong::Search::OFF{"defence"}
Truth table66%2/3
A and not(B)
A= $level >= 3B= not $Game::Mahjong::Search::OFF{"defence"}
ABresult
0X0
100
111
127 4976 my %safe = map { $_ => 1 } @safe;
128 4976 ▶ if (@safe && Game::Mahjong::Shanten::shanten($hand) >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@safe and Game::Mahjong::Shanten::shanten($hand) >= 2) 1 4975 4976
Condition66%2/3
@safe and Game::Mahjong::Shanten::shanten($hand) >= 2
!ll&&!rl&&r
497501
MC/DC50%1/2
@safe and Game::Mahjong::Shanten::shanten($hand) >= 2
@safe Game::Mahjong::Shanten::shanten($hand) >= 2
Truth table66%2/3
A and B
A= @safeB= Game::Mahjong::Shanten::shanten($hand) >= 2
ABresult
0X0
100
111
129 1 my @s = grep { $safe{ $_->{tile} } } @keep;
130 1 ▶ @keep = @s if @s;
Branch: 1 branch
BranchTrueFalseTotal
if @s 1 0 1
131 }
132  
133 4976 my %score;
134 4976 for my $d (@keep) {
135 22549 my $s = _acceptance($view, $d->{tile}) * 10;
136 22549 ▶ $s += _potential_after($view, $d->{tile}) if $level >= 3 && !$OFF{planning};
Branch: 1 branch
BranchTrueFalseTotal
if $level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} 8735 13814 22549
Condition66%2/3
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"}
!ll&&!rl&&r
1381408735
MC/DC50%1/2
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"}
$level >= 3 not $Game::Mahjong::Search::OFF{"planning"}
Truth table66%2/3
A and not(B)
A= $level >= 3B= not $Game::Mahjong::Search::OFF{"planning"}
ABresult
0X0
100
111
137 22549 $s -= _isolation($hand, $d->{tile}) / 100;
138 22549 $score{ $d->{tile} } = $s;
139 }
140 4976 ▶ my @ranked = sort { $score{ $b->{tile} } <=> $score{ $a->{tile} } || $a->{tile} <=> $b->{tile} } @keep;
Condition66%2/3
$score{$Game::Mahjong::Search::b->{'tile'}} <=> $score{$Game::Mahjong::Search::a->{'tile'}} || $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
l!l&&r!l&&!r
3251531120
MC/DC0%0/2
$score{$Game::Mahjong::Search::b->{'tile'}} <=> $score{$Game::Mahjong::Search::a->{'tile'}} || $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
$score{$Game::Mahjong::Search::b->{'tile'}} <=> $score{$Game::Mahjong::Search::a->{'tile'}} $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
Truth table66%2/3
A || B
A= $score{$Game::Mahjong::Search::b->{'tile'}} <=> $score{$Game::Mahjong::Search::a->{'tile'}}B= $Game::Mahjong::Search::a->{'tile'} <=> $Game::Mahjong::Search::b->{'tile'}
ABresult
1X1
011
000
141 4976 return $ranked[0];
142 }
143  
144 2018 ▶sub _safe_kinds {
Subroutine _safe_kinds: called CC=7 SCAR=19.5
145 2018 my ($view) = @_;
146 2018 ▶ my @near = grep { $_ != $view->{seat} && scalar(@{ $view->{melds}[$_] }) >= 3 } 0 .. 3;
Condition100%3/3
$_ != $view->{'seat'} && scalar @{$view->{'melds'}[$_];} >= 3
!ll&&!rl&&r
201860531
MC/DC100%2/2
$_ != $view->{'seat'} && scalar @{$view->{'melds'}[$_];} >= 3
$_ != $view->{'seat'} scalar @{$view->{'melds'}[$_];} >= 3
Truth table100%3/3
A && B
A= $_ != $view->{'seat'}B= scalar @{$view->{'melds'}[$_];} >= 3
ABresult
0X0
100
111
147 2018 ▶ return () unless @near;
Branch: 1 branch
BranchTrueFalseTotal
unless @near 2017 1 2018
148 1 my %safe;
149 1 for my $kind ($view->{hand}->kinds) {
150 12 ▶ $safe{$kind} = 1 if _remaining($view, $kind) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if _remaining($view, $kind) == 0 1 11 12
151 12 for my $s (@near) {
152 12 ▶ $safe{$kind} = 1 if grep { $_ == $kind } @{ $view->{pools}[$s] };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $kind;} @{$$view{"pools"}[$s];} 2 10 12
153 }
154 }
155 1 return sort { $a <=> $b } keys %safe;
156 }
157  
158 8877 ▶sub _potential_after {
Subroutine _potential_after: called CC=17 SCAR=28.3
159 8877 my ($view, $discard) = @_;
160 8877 my $hand = $view->{hand}->clone;
161 8877 $hand->remove($discard);
162 8877 my (%suit, $honours);
163 8877 my @tiles = ($hand->tiles, map { @{ $_->tiles } } @{ $hand->melds });
164 8877 for my $t (@tiles) {
165 115563 ▶ if (my $s = Game::Mahjong::Tiles::suit_of($t)) { $suit{$s}++ } else { $honours++ }
Branch: 1 branch
BranchTrueFalseTotal
if (my $s = Game::Mahjong::Tiles::suit_of($t)) { } 101883 13680 115563
166 }
167 8877 my ($big) = sort { $b <=> $a } values %suit;
168 8877 ▶ $big ||= 0;
Condition50%1/2
$big ||= 0
l!l
88770
MC/DC0%0/1
$big ||= 0
$big
Truth table50%1/2
A
A= $big
Aresult
11
00
169 8877 my $p = 0;
170 8877 ▶ $p += 24 * ($big / 14) if $big >= 9 && !$honours;
Branch: 1 branch
BranchTrueFalseTotal
if $big >= 9 and not $honours 317 8560 8877
Condition100%3/3
$big >= 9 and not $honours
!ll&&!rl&&r
848278317
MC/DC100%2/2
$big >= 9 and not $honours
$big >= 9 not $honours
Truth table100%3/3
A and not(B)
A= $big >= 9B= not $honours
ABresult
0X0
100
111
171 8877 ▶ $p += 6 * (($big + ($honours || 0)) / 14) if $big >= 8 && $honours;
Branch: 1 branch
BranchTrueFalseTotal
if $big >= 8 and $honours 312 8565 8877
Condition80%4/5
$big >= 8 and $honours
!ll&&!rl&&r
7849716312
$honours || 0
l!l
3120
MC/DC100%2/2
$big >= 8 and $honours
$big >= 8 $honours
MC/DC0%0/1
$honours || 0
$honours
Truth table100%3/3
A and B
A= $big >= 8B= $honours
ABresult
0X0
100
111
Truth table50%1/2
A
A= $honours
Aresult
11
00
172 8877 my $pungs = grep { $hand->count($_) >= 3 } $hand->kinds;
173 8877 $pungs += grep { $_->is_pung } @{ $hand->melds };
174 8877 ▶ $p += 6 * ($pungs / 4) if $pungs >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $pungs >= 2 689 8188 8877
175 8877 for my $k ($hand->kinds) {
176 89366 ▶ next unless $hand->count($k) >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->count($k) >= 2 71436 17930 89366
177 17930 ▶ $p += 2 if Game::Mahjong::Tiles::is_dragon($k);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_dragon($k) 1179 16751 17930
178 17930 ▶ $p += 2 if Game::Mahjong::Tiles::is_wind($k) && (Game::Mahjong::Tiles::wind_index($k) == $view->{prevailing} || Game::Mahjong::Tiles::wind_index($k) == $view->{seat_wind});
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_wind($k) and Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"} || Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"} 450 17480 17930
Condition100%6/6
Game::Mahjong::Tiles::is_wind($k) and Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"} || Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"}
!ll&&!rl&&r
164001080450
Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"} or Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"}
l!l&&r!l&&!r
2402101080
MC/DC100%3/3
Game::Mahjong::Tiles::is_wind($k) and Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"} || Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"}
Game::Mahjong::Tiles::is_wind($k) Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"} Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"}
Truth table100%4/4
A and B or C
A= Game::Mahjong::Tiles::is_wind($k)B= Game::Mahjong::Tiles::wind_index($k) == $view->{"prevailing"}C= Game::Mahjong::Tiles::wind_index($k) == $view->{"seat_wind"}
ABCresult
0XX0
1000
11X1
1011
179 17930 ▶ $p += 1 if Game::Mahjong::Tiles::is_terminal($k);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Tiles::is_terminal($k) 3070 14860 17930
180 }
181 8877 return $p;
182 }
183  
184 12623 ▶sub _self_kong {
Subroutine _self_kong: called CC=6 SCAR=17.9
185 12623 my ($view, $level, $legal) = @_;
186 12623 ▶ return undef if $level <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $level <= 1 7626 4997 12623
187 4997 my @kongs = grep { $_->{kind} eq 'kong' } @$legal;
188 4997 ▶ return undef unless @kongs;
Branch: 1 branch
BranchTrueFalseTotal
unless @kongs 4946 51 4997
189 51 my $hand = $view->{hand};
190 51 my $now = Game::Mahjong::Shanten::shanten($hand);
191 51 for my $k (@kongs) {
192 51 my $after = $hand->clone;
193 51 ▶ if ($after->holds_kong_of($k->{tile})) { $after->concealed_kong($k->{tile}) }
Branch: 1 branch
BranchTrueFalseTotal
if ($after->holds_kong_of($k->{'tile'})) { } 41 10 51
194 10 else { $after->promote_kong($k->{tile}) }
195 51 ▶ return $k if Game::Mahjong::Shanten::shanten($after) <= $now;
Branch: 1 branch
BranchTrueFalseTotal
if Game::Mahjong::Shanten::shanten($after) <= $now 20 31 51
196 }
197 31 return undef;
198 }
199  
200 1226 ▶sub _ctx {
Subroutine _ctx: called CC=1 SCAR=0.0
201 1226 my ($view, $by, $from) = @_;
202 return { by => $by, prevailing => $view->{prevailing}, seat => $view->{seat_wind}, from => $from,
203 1226 flowers => scalar @{ $view->{hand}->flowers } };
204 }
205  
206 560 ▶sub _reaches_eight {
Subroutine _reaches_eight: called CC=5 SCAR=16.2
207 560 my ($view, $hand) = @_;
208 560 ▶ return 0 unless $hand->total == 13;
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->total == 13 0 560 560
209 560 my @waits = Game::Mahjong::Decompose::waits($hand);
210 560 for my $w (@waits) {
211 901 my $with = $hand->clone;
212 901 $with->add($w);
213 901 my $ctx = _ctx($view, 'discard', undef);
214 901 $ctx->{waits} = \@waits;
215 901 my $t = Game::Mahjong::Score::score($with, $w, $ctx);
216 901 ▶ return 1 if $t && $t->minimum_met;
Branch: 1 branch
BranchTrueFalseTotal
if $t and $t->minimum_met 65 836 901
Condition66%2/3
$t and $t->minimum_met
!ll&&!rl&&r
083665
MC/DC50%1/2
$t and $t->minimum_met
$t $t->minimum_met
Truth table66%2/3
A and B
A= $tB= $t->minimum_met
ABresult
0X0
100
111
217 }
218 495 return 0;
219 }
220  
221 4265 ▶sub _answer {
Subroutine _answer: called CC=30 SCAR=34.0
222 4265 my ($view, $level, $legal, $word) = @_;
223 4265 my ($pass) = grep { $_->{kind} eq 'pass' } @$legal;
224 4265 ▶ return $pass if $level <= 1 || $OFF{claims};
Branch: 1 branch
BranchTrueFalseTotal
if $level <= 1 or $Game::Mahjong::Search::OFF{"claims"} 2781 1484 4265
Condition66%2/3
$level <= 1 or $Game::Mahjong::Search::OFF{"claims"}
l!l&&r!l&&!r
278101484
MC/DC50%1/2
$level <= 1 or $Game::Mahjong::Search::OFF{"claims"}
$level <= 1 $Game::Mahjong::Search::OFF{"claims"}
Truth table66%2/3
A or B
A= $level <= 1B= $Game::Mahjong::Search::OFF{"claims"}
ABresult
1X1
011
000
225 1484 my $hand = $view->{hand};
226 1484 my $tile = $view->{window}{tile};
227 1484 my $from = $view->{window}{from};
228 1484 my $now = Game::Mahjong::Shanten::shanten($hand);
229  
230 1484 ▶ for my $claim (grep { $_->{kind} eq 'kong' || $_->{kind} eq 'pung' } @$legal) {
Condition100%3/3
$_->{'kind'} eq 'kong' || $_->{'kind'} eq 'pung'
l!l&&r!l&&!r
465362711
MC/DC100%2/2
$_->{'kind'} eq 'kong' || $_->{'kind'} eq 'pung'
$_->{'kind'} eq 'kong' $_->{'kind'} eq 'pung'
Truth table100%3/3
A || B
A= $_->{'kind'} eq 'kong'B= $_->{'kind'} eq 'pung'
ABresult
1X1
011
000
231 582 my $after = $hand->clone;
232 582 ▶ if ($claim->{kind} eq 'kong') { $after->claim_kong($tile, $from) } else { $after->claim_pung($tile, $from) }
Branch: 1 branch
BranchTrueFalseTotal
if ($claim->{'kind'} eq 'kong') { } 46 536 582
233 582 my $kept = _best_thirteen($after);
234 582 my $s = Game::Mahjong::Shanten::shanten($kept);
235 582 ▶ next unless $s < $now;
Branch: 1 branch
BranchTrueFalseTotal
unless $s < $now 367 215 582
236 my $scores = Game::Mahjong::Tiles::is_dragon($tile)
237 215 ▶ || (Game::Mahjong::Tiles::is_wind($tile) && (Game::Mahjong::Tiles::wind_index($tile) == $view->{prevailing} || Game::Mahjong::Tiles::wind_index($tile) == $view->{seat_wind}))
Condition100%12/12
Game::Mahjong::Tiles::is_dragon($tile) || Game::Mahjong::Tiles::is_wind($tile) && (Game::Mahjong::Tiles::wind_index($tile) == $view->{'prevailing'} || Game::Mahjong::Tiles::wind_index($tile) == $view->{'seat_wind'}) || Game::Mahjong::Tiles::is_terminal($tile)
l!l&&r!l&&!r
4252121
Game::Mahjong::Tiles::is_dragon($tile) or Game::Mahjong::Tiles::is_wind($tile) and Game::Mahjong::Tiles::wind_index($tile) == $view->{"prevailing"} || Game::Mahjong::Tiles::wind_index($tile) == $view->{"seat_wind"}
l!l&&r!l&&!r
2616173
Game::Mahjong::Tiles::is_wind($tile) and Game::Mahjong::Tiles::wind_index($tile) == $view->{"prevailing"} || Game::Mahjong::Tiles::wind_index($tile) == $view->{"seat_wind"}
!ll&&!rl&&r
1512216
Game::Mahjong::Tiles::wind_index($tile) == $view->{"prevailing"} or Game::Mahjong::Tiles::wind_index($tile) == $view->{"seat_wind"}
l!l&&r!l&&!r
8822
MC/DC100%5/5
Game::Mahjong::Tiles::is_dragon($tile) || Game::Mahjong::Tiles::is_wind($tile) && (Game::Mahjong::Tiles::wind_index($tile) == $view->{'prevailing'} || Game::Mahjong::Tiles::wind_index($tile) == $view->{'seat_wind'}) || Game::Mahjong::Tiles::is_terminal($tile)
Game::Mahjong::Tiles::is_dragon($tile) Game::Mahjong::Tiles::is_wind($tile) Game::Mahjong::Tiles::wind_index($tile) == $view->{"prevailing"} Game::Mahjong::Tiles::wind_index($tile) == $view->{"seat_wind"} Game::Mahjong::Tiles::is_terminal($tile)
Truth table85%6/7
A or B and C or D || E
A= Game::Mahjong::Tiles::is_dragon($tile)B= Game::Mahjong::Tiles::is_wind($tile)C= Game::Mahjong::Tiles::wind_index($tile) == $view->{"prevailing"}D= Game::Mahjong::Tiles::wind_index($tile) == $view->{"seat_wind"}E= Game::Mahjong::Tiles::is_terminal($tile)
ABCDEresult
1XXXX1
011XX1
0101X1
00XX11
010011
00XX00
010000
238 || Game::Mahjong::Tiles::is_terminal($tile);
239 215 my $floor = Game::Mahjong::Score::floor($kept, _ctx($view, 'discard', $from));
240 215 ▶ return $claim if $scores || $floor >= Game::Mahjong::Score::MINIMUM || ($s == 0 && _reaches_eight($view, $kept));
Branch: 1 branch
BranchTrueFalseTotal
if $scores or $floor >= 8 or $s == 0 and _reaches_eight($view, $kept) 96 119 215
Condition100%9/9
$scores or $floor >= 8 or $s == 0 and _reaches_eight($view, $kept)
l!l&&r!l&&!r
951119
$scores or $floor >= 8
l!l&&r!l&&!r
941120
$s == 0 and _reaches_eight($view, $kept)
!ll&&!rl&&r
74451
MC/DC100%4/4
$scores or $floor >= 8 or $s == 0 and _reaches_eight($view, $kept)
$scores $floor >= 8 $s == 0 _reaches_eight($view, $kept)
Truth table100%5/5
A or B or C and D
A= $scoresB= $floor >= 8C= $s == 0D= _reaches_eight($view, $kept)
ABCDresult
1XXX1
01XX1
00111
000X0
00100
241 119 ▶ return $claim if $level >= 3 && !$OFF{planning} && $s <= 1 && _potential_after_hand($view, $kept) >= 6;
Branch: 1 branch
BranchTrueFalseTotal
if $level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and _potential_after_hand($view, $kept) >= 6 3 116 119
Condition88%8/9
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and _potential_after_hand($view, $kept) >= 6
!ll&&!rl&&r
87293
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1
!ll&&!rl&&r
761132
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"}
!ll&&!rl&&r
76043
MC/DC75%3/4
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and _potential_after_hand($view, $kept) >= 6
$level >= 3 not $Game::Mahjong::Search::OFF{"planning"} $s <= 1 _potential_after_hand($view, $kept) >= 6
Truth table80%4/5
A and not(B) and C and D
A= $level >= 3B= not $Game::Mahjong::Search::OFF{"planning"}C= $s <= 1D= _potential_after_hand($view, $kept) >= 6
ABCDresult
0XXX0
10XX0
110X0
11100
11111
242 }
243  
244 1385 my @chows = grep { $_->{kind} eq 'chow' } @$legal;
245 1385 for my $chow (@chows) {
246 1218 my $after = $hand->clone;
247 1218 $after->claim_chow($tile, @{ $chow->{tiles} }, $from);
248 1218 my $kept = _best_thirteen($after);
249 1218 my $s = Game::Mahjong::Shanten::shanten($kept);
250 1218 ▶ next unless $s < $now || ($now == 0 && $s == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $s < $now or $now == 0 and $s == 0 478 740 1218
Condition100%6/6
$s < $now or $now == 0 and $s == 0
l!l&&r!l&&!r
371369478
$now == 0 and $s == 0
!ll&&!rl&&r
43840369
MC/DC100%3/3
$s < $now or $now == 0 and $s == 0
$s < $now $now == 0 $s == 0
Truth table100%4/4
A or B and C
A= $s < $nowB= $now == 0C= $s == 0
ABCresult
1XX1
0111
00X0
0100
251 740 ▶ return $chow if $s == 0 && _reaches_eight($view, $kept);
Branch: 1 branch
BranchTrueFalseTotal
if $s == 0 and _reaches_eight($view, $kept) 64 676 740
Condition100%3/3
$s == 0 and _reaches_eight($view, $kept)
!ll&&!rl&&r
22645064
MC/DC100%2/2
$s == 0 and _reaches_eight($view, $kept)
$s == 0 _reaches_eight($view, $kept)
Truth table100%3/3
A and B
A= $s == 0B= _reaches_eight($view, $kept)
ABresult
0X0
100
111
252 676 ▶ return $chow if $level >= 3 && !$OFF{planning} && $s <= 1 && $s < $now && Game::Mahjong::Score::floor($kept, _ctx($view, 'discard', $from)) + _potential_after_hand($view, $kept) >= Game::Mahjong::Score::MINIMUM;
Branch: 1 branch
BranchTrueFalseTotal
if $level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and $s < $now and Game::Mahjong::Score::floor($kept, _ctx($view, "discard", $from)) + _potential_after_hand($view, $kept) >= 8 4 672 676
Condition91%11/12
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and $s < $now and Game::Mahjong::Score::floor($kept, _ctx($view, "discard", $from)) + _potential_after_hand($view, $kept) >= 8
!ll&&!rl&&r
5661064
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and $s < $now
!ll&&!rl&&r
406160110
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1
!ll&&!rl&&r
36442270
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"}
!ll&&!rl&&r
3640312
MC/DC80%4/5
$level >= 3 and not $Game::Mahjong::Search::OFF{"planning"} and $s <= 1 and $s < $now and Game::Mahjong::Score::floor($kept, _ctx($view, "discard", $from)) + _potential_after_hand($view, $kept) >= 8
$level >= 3 not $Game::Mahjong::Search::OFF{"planning"} $s <= 1 $s < $now Game::Mahjong::Score::floor($kept, _ctx($view, "discard", $from)) + _potential_after_hand($view, $kept) >= 8
Truth table83%5/6
A and not(B) and C and D and E
A= $level >= 3B= not $Game::Mahjong::Search::OFF{"planning"}C= $s <= 1D= $s < $nowE= Game::Mahjong::Score::floor($kept, _ctx($view, "discard", $from)) + _potential_after_hand($view, $kept) >= 8
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
253 }
254 1317 return $pass;
255 }
256  
257 1800 ▶sub _best_thirteen {
Subroutine _best_thirteen: called CC=5 SCAR=16.1
258 1800 my ($after) = @_;
259 1800 ▶ return $after if $after->total == 13;
Branch: 1 branch
BranchTrueFalseTotal
if $after->total == 13 46 1754 1800
260 1754 my ($best, $best_s);
261 1754 for my $k ($after->kinds) {
262 14559 my $s = Game::Mahjong::Shanten::after_discard($after, $k);
263 14559 ▶ next if defined $best_s && $s >= $best_s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $best_s and $s >= $best_s 12029 2530 14559
Condition100%3/3
defined $best_s and $s >= $best_s
!ll&&!rl&&r
175477612029
MC/DC100%2/2
defined $best_s and $s >= $best_s
defined $best_s $s >= $best_s
Truth table100%3/3
A and B
A= defined $best_sB= $s >= $best_s
ABresult
0X0
100
111
264 2530 ($best, $best_s) = ($k, $s);
265 }
266 1754 my $h = $after->clone;
267 1754 $h->remove($best);
268 1754 return $h;
269 }
270  
271 142 ▶sub _potential_after_hand {
Subroutine _potential_after_hand: called CC=2 SCAR=7.0
272 142 my ($view, $hand) = @_;
273 142 my $v = { %$view, hand => $hand };
274 142 my ($k) = $hand->kinds;
275 142 ▶ return $k ? _potential_after($v, $k) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$k ? : 142 0 142
276 }
277  
278 34 1;
279  
280 __END__
« blib/lib/Game/Mahjong/Rules.pm ↑ Summary blib/lib/Game/Mahjong/Result.pm »