blib/lib/Game/Mahjong/Rules.pm

filter: Statementstmt 99.4% 539 / 542 Branchbran 89.1% 230 / 258 Conditioncond 70.6% 82 / 116 MC/DCmcdc 50.7% 34 / 67 Subroutinesub 100% 61 / 61 Podpod 92.8% 13 / 14 totaltotal 90.6% 959 / 1058 CC 195 SCAR 26.4
CC
195
Cov
93%
check_invariants
47.3
_answer
18.7
apply
15.1
CRAP 14.0
« blib/lib/Game/Mahjong/Score.pm ↑ Summary blib/lib/Game/Mahjong/Search.pm »
1 package Game::Mahjong::Rules;
2  
3 34 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 34 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 34 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 34 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 34 ▶use Game::Mahjong::Tiles;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 34 ▶use Game::Mahjong::Notation;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 34 ▶use Game::Mahjong::Wall;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 34 ▶use Game::Mahjong::Hand;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 34 ▶use Game::Mahjong::Decompose;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 34 ▶use Game::Mahjong::Score;
Subroutine __ANON__: called CC=1 SCAR=0.0
15 34 ▶use Game::Mahjong::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
16 34 ▶use Game::Mahjong::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
17  
18 34 our $VERSION = '0.03';
19  
20 34 our %PRIORITY;
21 34 ▶BEGIN { %PRIORITY = (pass => 0, chow => 1, pung => 2, kong => 2, win => 3) }
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 34 has seed => (is => 'ro', isa => Str, required => 1);
24  
25 34 has hand_no => (is => 'rw', isa => Int, default => 1);
26  
27 34 has dealer => (is => 'rw', isa => Int, default => 0);
28  
29 134 ▶has totals => (is => 'rw', isa => ArrayRef, default => sub { [ 0, 0, 0, 0 ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
30  
31 34 has wall => (is => 'rw');
32  
33 34 has hands => (is => 'rw', isa => ArrayRef, default => []);
34  
35 135 ▶has pools => (is => 'rw', isa => ArrayRef, default => sub { [ [], [], [], [] ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 34 has phase => (is => 'rw', isa => Str, default => 'discard');
38  
39 34 has turn => (is => 'rw');
40  
41 34 has window => (is => 'rw');
42  
43 34 has drawn => (is => 'rw');
44  
45 34 has drawn_from => (is => 'rw');
46  
47 34 has replacement => (is => 'rw');
48  
49 34 has claimed_this_turn => (is => 'rw', default => 0);
50  
51 34 has last_draw_emptied => (is => 'rw', default => 0);
52  
53 34 has last_discard_is_last => (is => 'rw', default => 0);
54  
55 34 has status => (is => 'rw', isa => Str, default => 'active');
56  
57 34 has history => (is => 'rw', isa => ArrayRef, default => []);
58  
59 34 has outcomes => (is => 'rw', isa => ArrayRef, default => []);
60  
61 34 has last => (is => 'rw');
62  
63 34 has winner => (is => 'rw');
64  
65 34 has result => (is => 'rw');
66  
67 34 has position => (is => 'ro');
68  
69 34 has moves => (is => 'rw', isa => Int, default => 0);
70  
71 135 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
72 135 my ($self) = @_;
73 135 ▶ die 'Game::Mahjong::Rules: a seed is a non-empty string' unless length $self->seed;
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->seed 1 134 135
74 134 ▶ die 'Game::Mahjong::Rules: hand_no is 1 to ' . Game::Mahjong::Result::HANDS
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hand_no >= 1 and $self->hand_no <= 16 2 132 134
Condition100%3/3
$self->hand_no >= 1 and $self->hand_no <= 16
!ll&&!rl&&r
11132
MC/DC100%2/2
$self->hand_no >= 1 and $self->hand_no <= 16
$self->hand_no >= 1 $self->hand_no <= 16
Truth table100%3/3
A and B
A= $self->hand_no >= 1B= $self->hand_no <= 16
ABresult
0X0
100
111
75 unless $self->hand_no >= 1 && $self->hand_no <= Game::Mahjong::Result::HANDS;
76 132 ▶ die 'Game::Mahjong::Rules: totals are four numbers' unless @{ $self->totals } == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->totals;} == 4 1 131 132
77 131 ▶ if ($self->position) { $self->_take_written_position($self->position) }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->position) { } 41 90 131
78 90 else { $self->_deal }
79 129 return;
80 }
81  
82 42527 ▶sub prevailing { my ($self) = @_; return int(($self->hand_no - 1) / 4) }
Subroutine prevailing: called CC=1 SCAR=0.0
Pod documented
83  
84 310 ▶sub round { my ($self) = @_; return $self->prevailing + 1 }
Subroutine round: called CC=1 SCAR=0.0
Pod documented
85  
86 47315 ▶sub seat_wind { my ($self, $seat) = @_; return ($seat - $self->dealer) % 4 }
Subroutine seat_wind: called CC=1 SCAR=0.0
Pod documented
87  
88 59379 ▶sub next_seat { my ($self, $seat) = @_; return ($seat + 1) % 4 }
Subroutine next_seat: called CC=1 SCAR=0.0
Pod documented
89  
90 38935 ▶sub _emit {
Subroutine _emit: called CC=1 SCAR=0.0
91 38935 my ($self, $actor, $kind, %payload) = @_;
92 38935 push @{ $self->outcomes }, { actor => $actor, kind => $kind, %payload };
93 38935 return;
94 }
95  
96 19660 ▶sub take_outcomes {
Subroutine take_outcomes: called CC=1 SCAR=0.0
Pod documented
97 19660 my ($self) = @_;
98 19660 my @out = @{ $self->outcomes };
99 19660 @{ $self->outcomes } = ();
100 19660 return @out;
101 }
102  
103 309 ▶sub _deal {
Subroutine _deal: called CC=9 SCAR=22.0
104 309 my ($self) = @_;
105 309 $self->dealer(($self->hand_no - 1) % 4);
106 309 my $wall = Game::Mahjong::Wall->new(seed => $self->seed, hand => $self->hand_no);
107 309 my $deal = $wall->deal($self->dealer);
108 309 $self->wall($wall);
109 309 $self->hands([ map { Game::Mahjong::Hand->new } 0 .. 3 ]);
110 309 $self->pools([ [], [], [], [] ]);
111 309 $self->_reset_turn_state;
112 309 $self->window(undef);
113 $self->_emit('sys', 'deal',
114 hand => $self->hand_no, round => $self->round, prevailing => $self->prevailing,
115 309 dealer => $self->dealer, winds => [ map { $self->seat_wind($_) } 0 .. 3 ]);
116  
117 309 for my $i (0 .. 3) {
118 1236 my $seat = ($self->dealer + $i) % 4;
119 1236 for my $kind (@{ $deal->{hands}{$seat} }) {
120 16377 ▶ if (Game::Mahjong::Tiles::is_bonus($kind)) { $self->hands->[$seat]->add_flower($kind); $self->_emit('sys', 'flower', seat => $seat, tile => $kind) }
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Tiles::is_bonus($kind)) { } 937 15440 16377
121 15440 else { $self->hands->[$seat]->add($kind) }
122 }
123 }
124 309 for my $i (0 .. 3) {
125 1236 my $seat = ($self->dealer + $i) % 4;
126 1236 ▶ while ($self->hands->[$seat]->total < ($seat == $self->dealer ? 14 : 13)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->hands->[$seat]->total < ($seat == $self->dealer ? 14 : 13)) 973 1236 2209
$seat == $self->dealer ? : 595 1614 2209
127 973 ▶ return $self->_exhausted if $self->wall->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wall->is_empty 0 973 973
128 973 my $kind = $self->wall->replace;
129 973 $self->_emit('sys', 'drew', seat => $seat, from => 'back');
130 973 ▶ if (Game::Mahjong::Tiles::is_bonus($kind)) { $self->hands->[$seat]->add_flower($kind); $self->_emit('sys', 'flower', seat => $seat, tile => $kind) }
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Tiles::is_bonus($kind)) { } 36 937 973
131 937 else { $self->hands->[$seat]->add($kind) }
132 }
133 }
134 309 $self->phase('discard');
135 309 $self->turn($self->dealer);
136 309 return;
137 }
138  
139 349 ▶sub _reset_turn_state {
Subroutine _reset_turn_state: called CC=1 SCAR=0.0
140 349 my ($self) = @_;
141 349 $self->drawn(undef);
142 349 $self->drawn_from(undef);
143 349 $self->replacement(undef);
144 349 $self->claimed_this_turn(0);
145 349 $self->last_draw_emptied(0);
146 349 $self->last_discard_is_last(0);
147 349 $self->last(undef);
148 349 return;
149 }
150  
151 41 ▶sub _take_written_position {
Subroutine _take_written_position: called CC=13 SCAR=26.1
152 41 my ($self, $p) = @_;
153 41 ▶ die 'Game::Mahjong::Rules: a position names four hands' unless ref $p->{hands} eq 'ARRAY' && @{ $p->{hands} } == 4;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p->{"hands"} eq "ARRAY" and @{$p->{"hands"};} == 4 1 40 41
Condition66%2/3
ref $p->{"hands"} eq "ARRAY" and @{$p->{"hands"};} == 4
!ll&&!rl&&r
0140
MC/DC50%1/2
ref $p->{"hands"} eq "ARRAY" and @{$p->{"hands"};} == 4
ref $p->{"hands"} eq "ARRAY" @{$p->{"hands"};} == 4
Truth table66%2/3
A and B
A= ref $p->{"hands"} eq "ARRAY"B= @{$p->{"hands"};} == 4
ABresult
0X0
100
111
154 40 ▶ $self->dealer($p->{dealer} // (($self->hand_no - 1) % 4));
Condition66%2/3
$p->{"dealer"} // ($self->hand_no - 1) % 4
l!l&&r!l&&!r
0436
MC/DC50%1/2
$p->{"dealer"} // ($self->hand_no - 1) % 4
$p->{"dealer"} ($self->hand_no - 1) % 4
Truth table66%2/3
A // B
A= $p->{"dealer"}B= ($self->hand_no - 1) % 4
ABresult
1X1
011
000
155 40 $self->hands([ map { Game::Mahjong::Hand->from_notation($_) } @{ $p->{hands} } ]);
156 40 ▶ $self->pools([ map { [ map { Game::Mahjong::Tiles::id_of($_) } @{ $_ } ] } @{ $p->{pools} || [ [], [], [], [] ] } ]);
Condition50%1/2
$p->{'pools'} || [[], [], [], []]
l!l
040
MC/DC0%0/1
$p->{'pools'} || [[], [], [], []]
$p->{'pools'}
Truth table50%1/2
A
A= $p->{'pools'}
Aresult
11
00
157 $self->wall(Game::Mahjong::Wall->new(seed => $self->seed, hand => $self->hand_no,
158 40 ▶ tiles => [ map { /\A\d+\z/ ? $_ : Game::Mahjong::Tiles::id_of($_) } @{ $p->{wall} || [] } ]));
Branch: 1 branch
BranchTrueFalseTotal
/\A\d+\z/ ? : 0 118 118
Condition50%1/2
$p->{'wall'} || []
l!l
400
MC/DC0%0/1
$p->{'wall'} || []
$p->{'wall'}
Truth table50%1/2
A
A= $p->{'wall'}
Aresult
11
00
159 40 $self->wall->dealt(1);
160 40 $self->_reset_turn_state;
161 40 $self->window(undef);
162 40 ▶ my $turn = $p->{turn} // $self->dealer;
Condition66%2/3
$p->{"turn"} // $self->dealer
l!l&&r!l&&!r
3901
MC/DC50%1/2
$p->{"turn"} // $self->dealer
$p->{"turn"} $self->dealer
Truth table66%2/3
A // B
A= $p->{"turn"}B= $self->dealer
ABresult
1X1
011
000
163 40 for my $seat (0 .. 3) {
164 157 ▶ my $want = $seat == $turn ? 14 : 13;
Branch: 1 branch
BranchTrueFalseTotal
$seat == $turn ? : 40 117 157
165 157 ▶ die "Game::Mahjong::Rules: seat $seat holds " . $self->hands->[$seat]->total . " tiles' worth, not $want"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->hands->[$seat]->total == $want 1 156 157
166 unless $self->hands->[$seat]->total == $want;
167 }
168 39 $self->phase('discard');
169 39 $self->turn($turn);
170 39 ▶ $self->claimed_this_turn($p->{claimed_this_turn} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$p->{'claimed_this_turn'} ? : 0 39 39
171 39 ▶ $self->drawn($p->{drawn} ? Game::Mahjong::Tiles::id_of($p->{drawn}) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$p->{'drawn'} ? : 5 34 39
172 39 return;
173 }
174  
175 158621 ▶sub hand_of { my ($self, $seat) = @_; return $self->hands->[$seat] }
Subroutine hand_of: called CC=1 SCAR=0.0
Pod documented
176  
177 140286 ▶sub pool_of { my ($self, $seat) = @_; return $self->pools->[$seat] }
Subroutine pool_of: called CC=1 SCAR=0.0
Pod documented
178  
179 113858 ▶sub is_active { return $_[0]->status eq 'active' ? 1 : 0 }
Subroutine is_active: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->status eq 'active' ? : 113840 18 113858
180  
181 39643 ▶sub waiting_on {
Subroutine waiting_on: called CC=5 SCAR=16.6
Pod documented
182 39643 my ($self) = @_;
183 39643 ▶ return () unless $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active 1 39642 39643
184 39642 ▶ return ($self->turn) if $self->phase eq 'discard';
Branch: 1 branch
BranchTrueFalseTotal
if $self->phase eq "discard" 30684 8958 39642
185 8958 ▶ return $self->_window_waiting if $self->phase eq 'claim' || $self->phase eq 'rob';
Branch: 1 branch
BranchTrueFalseTotal
if $self->phase eq "claim" or $self->phase eq "rob" 8958 0 8958
Condition66%2/3
$self->phase eq "claim" or $self->phase eq "rob"
l!l&&r!l&&!r
895710
MC/DC0%0/2
$self->phase eq "claim" or $self->phase eq "rob"
$self->phase eq "claim" $self->phase eq "rob"
Truth table66%2/3
A or B
A= $self->phase eq "claim"B= $self->phase eq "rob"
ABresult
1X1
011
000
186 0 return ();
187 }
188  
189 22903 ▶sub _visible_count {
Subroutine _visible_count: called CC=4 SCAR=13.9
190 22903 my ($self, $kind) = @_;
191 22903 my $n = 0;
192 22903 for my $seat (0 .. 3) {
193 91612 $n += grep { $_ == $kind } @{ $self->pools->[$seat] };
194 91612 for my $meld (@{ $self->hands->[$seat]->melds }) {
195 21553 ▶ next if $meld->concealed;
Branch: 1 branch
BranchTrueFalseTotal
if $meld->concealed 636 20917 21553
196 20917 $n += grep { $_ == $kind } @{ $meld->tiles };
197 }
198 }
199 22903 return $n;
200 }
201  
202 45149 ▶sub _waits_of {
Subroutine _waits_of: called CC=3 SCAR=11.0
203 45149 my ($self, $seat) = @_;
204 45149 my $hand = $self->hands->[$seat];
205 45149 ▶ return [] unless $hand->total == 13;
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->total == 13 0 45149 45149
206 45149 ▶ $hand->waits([ Game::Mahjong::Decompose::waits($hand) ]) unless $hand->waits;
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->waits 15532 29617 45149
207 45149 return $hand->waits;
208 }
209  
210 23840 ▶sub _win_ctx {
Subroutine _win_ctx: called CC=6 SCAR=17.9
211 23840 my ($self, $seat, $by, $from, $tile) = @_;
212 return {
213 by => $by,
214 prevailing => $self->prevailing,
215 seat => $self->seat_wind($seat),
216 winning => $tile,
217 last_of_wall => ($by eq 'self' ? $self->last_draw_emptied : $self->last_discard_is_last) ? 1 : 0,
218 replacement => $by eq 'self' ? $self->replacement : undef,
219 last_tile => ($tile && $self->_visible_count($tile) == 3) ? 1 : 0,
220 23840 ▶ flowers => scalar @{ $self->hands->[$seat]->flowers },
Branch: 4 branches
BranchTrueFalseTotal
($by eq 'self' ? $self->last_draw_emptied : $self->last_discard_is_last) ? : 116 23724 23840
$by eq 'self' ? : 23417 423 23840
$by eq 'self' ? : 23417 423 23840
$tile && $self->_visible_count($tile) == 3 ? : 2228 21612 23840
Condition100%3/3
$tile and $self->_visible_count($tile) == 3
!ll&&!rl&&r
937206752228
MC/DC100%2/2
$tile and $self->_visible_count($tile) == 3
$tile $self->_visible_count($tile) == 3
Truth table100%3/3
A and B
A= $tileB= $self->_visible_count($tile) == 3
ABresult
0X0
100
111
221 from => $from,
222 };
223 }
224  
225 45149 ▶sub _tally_on {
Subroutine _tally_on: called CC=4 SCAR=13.9
226 45149 my ($self, $seat, $tile, $by, $from) = @_;
227 45149 my $waits = $self->_waits_of($seat);
228 45149 ▶ return undef unless grep { $_ == $tile } @$waits;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $tile;} @$waits 44726 423 45149
229 423 my $with = $self->hands->[$seat]->clone;
230 423 $with->add($tile);
231 423 my $ctx = $self->_win_ctx($seat, $by, $from, $tile);
232 423 $ctx->{waits} = [ @$waits ];
233 423 my $tally = Game::Mahjong::Score::score($with, $tile, $ctx);
234 423 ▶ return $tally && $tally->minimum_met ? $tally : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tally && $tally->minimum_met ? : 218 205 423
Condition66%2/3
$tally and $tally->minimum_met
!ll&&!rl&&r
0205218
MC/DC50%1/2
$tally and $tally->minimum_met
$tally $tally->minimum_met
Truth table66%2/3
A and B
A= $tallyB= $tally->minimum_met
ABresult
0X0
100
111
235 }
236  
237 23417 ▶sub _tally_self {
Subroutine _tally_self: called CC=6 SCAR=17.9
238 23417 my ($self, $seat) = @_;
239 23417 my $hand = $self->hands->[$seat];
240 23417 ▶ return undef unless $hand->total == 14;
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->total == 14 0 23417 23417
241 23417 my $tile = $self->drawn;
242 23417 my $ctx = $self->_win_ctx($seat, 'self', undef, $tile);
243 23417 ▶ if ($tile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tile) 22480 937 23417
244 22480 my $thirteen = $hand->clone;
245 22480 $thirteen->remove($tile);
246 22480 $ctx->{waits} = [ Game::Mahjong::Decompose::waits($thirteen) ];
247 }
248 23417 ▶ my $tally = Game::Mahjong::Score::score($hand, $tile || 0, $ctx);
Condition100%2/2
$tile || 0
l!l
22480937
MC/DC100%1/1
$tile || 0
$tile
Truth table100%2/2
A
A= $tile
Aresult
11
00
249 23417 ▶ return $tally && $tally->minimum_met ? $tally : undef;
Branch: 1 branch
BranchTrueFalseTotal
$tally && $tally->minimum_met ? : 133 23284 23417
Condition100%3/3
$tally and $tally->minimum_met
!ll&&!rl&&r
23166118133
MC/DC100%2/2
$tally and $tally->minimum_met
$tally $tally->minimum_met
Truth table100%3/3
A and B
A= $tallyB= $tally->minimum_met
ABresult
0X0
100
111
250 }
251  
252 30978 ▶sub legal {
Subroutine legal: called CC=15 SCAR=27.1
Pod documented
253 30978 my ($self, $seat) = @_;
254 30978 ▶ return () unless $self->is_active && defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active and defined $seat 2 30976 30978
Condition66%2/3
$self->is_active and defined $seat
!ll&&!rl&&r
2030976
MC/DC50%1/2
$self->is_active and defined $seat
$self->is_active defined $seat
Truth table66%2/3
A and B
A= $self->is_activeB= defined $seat
ABresult
0X0
100
111
255 30976 my $hand = $self->hands->[$seat];
256  
257 30976 ▶ if ($self->phase eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->phase eq "discard") 23368 7608 30976
258 23368 ▶ return () unless $seat == $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $seat == $self->turn 4 23364 23368
259 23364 my @legal = map { { kind => 'discard', tile => $_ } } $hand->kinds;
260 23364 ▶ unless ($self->claimed_this_turn) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->claimed_this_turn) 22849 515 23364
261 22849 for my $kind ($hand->kinds) {
262 235473 ▶ push @legal, { kind => 'kong', tile => $kind } if $hand->holds_kong_of($kind);
Branch: 1 branch
BranchTrueFalseTotal
if $hand->holds_kong_of($kind) 331 235142 235473
263 235473 ▶ push @legal, { kind => 'kong', tile => $kind } if $hand->exposed_pung_of($kind);
Branch: 1 branch
BranchTrueFalseTotal
if $hand->exposed_pung_of($kind) 34 235439 235473
264 }
265 }
266 23364 ▶ push @legal, { kind => 'win' } if $self->_tally_self($seat);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_tally_self($seat) 82 23282 23364
267 23364 return @legal;
268 }
269  
270 7608 ▶ my $w = $self->window or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $w = $self->window or return () 7608 0 7608
271 7608 ▶ return () unless $w->{may}{$seat} && !exists $w->{answers}{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless $w->{"may"}{$seat} and not exists $w->{"answers"}{$seat} 13 7595 7608
Condition100%3/3
$w->{"may"}{$seat} and not exists $w->{"answers"}{$seat}
!ll&&!rl&&r
1217595
MC/DC100%2/2
$w->{"may"}{$seat} and not exists $w->{"answers"}{$seat}
$w->{"may"}{$seat} not exists $w->{"answers"}{$seat}
Truth table100%3/3
A and not(B)
A= $w->{"may"}{$seat}B= not exists $w->{"answers"}{$seat}
ABresult
0X0
100
111
272 7595 my @legal = ({ kind => 'pass' });
273 7595 for my $claim (@{ $w->{may}{$seat} }) {
274 8285 ▶ if ($claim eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($claim eq 'chow') { } 4715 3570 8285
275 4715 push @legal, map { { kind => 'chow', tiles => $_ } } $hand->chow_shapes($w->{tile});
276 }
277 3570 else { push @legal, { kind => $claim } }
278 }
279 7595 return @legal;
280 }
281  
282 24 ▶sub _refuse {
Subroutine _refuse: called CC=1 SCAR=0.0
283 24 my ($self, $code, $seat) = @_;
284 24 return Game::Mahjong::Error->throw($code, legal => [ $self->legal($seat) ]);
285 }
286  
287 19793 ▶sub apply {
Subroutine apply: called CC=15 SCAR=27.2
Pod documented
288 19793 my ($self, $seat, $move) = @_;
289 19793 ▶ return $self->_refuse('game_over', $seat) unless $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active 1 19792 19793
290 19792 ▶ return $self->_refuse('bad_move', $seat) unless ref $move eq 'HASH' && defined $move->{kind};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "HASH" and defined $move->{"kind"} 1 19791 19792
Condition66%2/3
ref $move eq "HASH" and defined $move->{"kind"}
!ll&&!rl&&r
1019791
MC/DC50%1/2
ref $move eq "HASH" and defined $move->{"kind"}
ref $move eq "HASH" defined $move->{"kind"}
Truth table66%2/3
A and B
A= ref $move eq "HASH"B= defined $move->{"kind"}
ABresult
0X0
100
111
291 19791 ▶ return $self->_refuse('bad_move', $seat) unless defined $seat && $seat =~ /\A[0-3]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat =~ /\A[0-3]\z/ 1 19790 19791
Condition66%2/3
defined $seat and $seat =~ /\A[0-3]\z/
!ll&&!rl&&r
0119790
MC/DC50%1/2
defined $seat and $seat =~ /\A[0-3]\z/
defined $seat $seat =~ /\A[0-3]\z/
Truth table66%2/3
A and B
A= defined $seatB= $seat =~ /\A[0-3]\z/
ABresult
0X0
100
111
292 19790 my $kind = $move->{kind};
293  
294 19790 ▶ if ($self->phase eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->phase eq "discard") 15097 4693 19790
295 15097 ▶ return $self->_refuse('wrong_phase', $seat) if $kind eq 'pass' || $kind eq 'pung' || $kind eq 'chow';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "pass" or $kind eq "pung" or $kind eq "chow" 3 15094 15097
Condition83%5/6
$kind eq "pass" or $kind eq "pung" or $kind eq "chow"
l!l&&r!l&&!r
3015094
$kind eq "pass" or $kind eq "pung"
l!l&&r!l&&!r
2115094
MC/DC66%2/3
$kind eq "pass" or $kind eq "pung" or $kind eq "chow"
$kind eq "pass" $kind eq "pung" $kind eq "chow"
Truth table75%3/4
A or B or C
A= $kind eq "pass"B= $kind eq "pung"C= $kind eq "chow"
ABCresult
1XX1
01X1
0011
0000
296 15094 ▶ return $self->_refuse('not_your_turn', $seat) unless $seat == $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $seat == $self->turn 2 15092 15094
297 15092 ▶ return $self->_discard($seat, $move) if $kind eq 'discard';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "discard" 14988 104 15092
298 104 ▶ return $self->_kong($seat, $move) if $kind eq 'kong';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "kong" 50 54 104
299 54 ▶ return $self->_self_win($seat) if $kind eq 'win';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "win" 53 1 54
300 1 return $self->_refuse('bad_move', $seat);
301 }
302  
303 4693 ▶ return $self->_refuse('wrong_phase', $seat) if $kind eq 'discard';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "discard" 0 4693 4693
304 4693 return $self->_answer($seat, $move);
305 }
306  
307 15038 ▶sub _tile_arg {
Subroutine _tile_arg: called CC=5 SCAR=16.2
308 15038 my ($self, $move, $seat) = @_;
309 15038 my $tile = $move->{tile};
310 15038 ▶ return (undef, $self->_refuse('bad_move', $seat)) unless defined $tile && $tile =~ /\A\d+\z/ && $tile >= 1 && $tile <= Game::Mahjong::Tiles::KINDS;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tile and $tile =~ /\A\d+\z/ and $tile >= 1 and $tile <= 34 2 15036 15038
Condition77%7/9
defined $tile and $tile =~ /\A\d+\z/ and $tile >= 1 and $tile <= 34
!ll&&!rl&&r
1115036
defined $tile and $tile =~ /\A\d+\z/ and $tile >= 1
!ll&&!rl&&r
1015037
defined $tile and $tile =~ /\A\d+\z/
!ll&&!rl&&r
1015037
MC/DC50%2/4
defined $tile and $tile =~ /\A\d+\z/ and $tile >= 1 and $tile <= 34
defined $tile $tile =~ /\A\d+\z/ $tile >= 1 $tile <= 34
Truth table60%3/5
A and B and C and D
A= defined $tileB= $tile =~ /\A\d+\z/C= $tile >= 1D= $tile <= 34
ABCDresult
0XXX0
10XX0
110X0
11100
11111
311 15036 return ($tile + 0, undef);
312 }
313  
314 14988 ▶sub _discard {
Subroutine _discard: called CC=4 SCAR=13.9
315 14988 my ($self, $seat, $move) = @_;
316 14988 my ($tile, $err) = $self->_tile_arg($move, $seat);
317 14988 ▶ return $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 14986 14988
318 14986 my $hand = $self->hands->[$seat];
319 14986 ▶ return $self->_refuse('tile_not_held', $seat) unless $hand->count($tile);
Branch: 1 branch
BranchTrueFalseTotal
unless $hand->count($tile) 2 14984 14986
320  
321 14984 $hand->remove($tile);
322 14984 push @{ $self->pools->[$seat] }, $tile;
323 14984 $self->moves($self->moves + 1);
324 14984 $self->_emit($seat, 'discard', tile => $tile);
325 14984 $self->last({ seat => $seat, kind => 'discard', tile => $tile });
326 14984 ▶ $self->last_discard_is_last($self->last_draw_emptied ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->last_draw_emptied ? : 73 14911 14984
327 14984 $self->last_draw_emptied(0);
328 14984 $self->drawn(undef);
329 14984 $self->replacement(undef);
330 14984 $self->claimed_this_turn(0);
331  
332 14984 return $self->_open_window('claim', $tile, $seat);
333 }
334  
335 50 ▶sub _kong {
Subroutine _kong: called CC=7 SCAR=19.5
336 50 my ($self, $seat, $move) = @_;
337 50 my ($tile, $err) = $self->_tile_arg($move, $seat);
338 50 ▶ return $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 50 50
339 50 ▶ return $self->_refuse('no_kong', $seat) if $self->claimed_this_turn;
Branch: 1 branch
BranchTrueFalseTotal
if $self->claimed_this_turn 2 48 50
340 48 my $hand = $self->hands->[$seat];
341  
342 48 ▶ if ($hand->holds_kong_of($tile)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand->holds_kong_of($tile)) 16 32 48
343 16 $hand->concealed_kong($tile);
344 16 $self->moves($self->moves + 1);
345 16 $self->_emit($seat, 'kong', tile => $tile, how => 'concealed');
346 16 $self->last({ seat => $seat, kind => 'kong', tile => $tile });
347 16 return $self->_replacement_draw($seat, 'kong');
348 }
349 32 ▶ if ($hand->exposed_pung_of($tile) && $hand->count($tile)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hand->exposed_pung_of($tile) and $hand->count($tile)) 31 1 32
Condition66%2/3
$hand->exposed_pung_of($tile) and $hand->count($tile)
!ll&&!rl&&r
1031
MC/DC50%1/2
$hand->exposed_pung_of($tile) and $hand->count($tile)
$hand->exposed_pung_of($tile) $hand->count($tile)
Truth table66%2/3
A and B
A= $hand->exposed_pung_of($tile)B= $hand->count($tile)
ABresult
0X0
100
111
350 31 $self->moves($self->moves + 1);
351 31 $self->_emit($seat, 'kong', tile => $tile, how => 'promoted');
352 31 $self->last({ seat => $seat, kind => 'kong', tile => $tile });
353 31 return $self->_open_window('rob', $tile, $seat);
354 }
355 1 ▶ return $self->_refuse($hand->count($tile) ? 'no_kong' : 'tile_not_held', $seat);
Branch: 1 branch
BranchTrueFalseTotal
$hand->count($tile) ? : 0 1 1
356 }
357  
358 53 ▶sub _self_win {
Subroutine _self_win: called CC=3 SCAR=11.0
359 53 my ($self, $seat) = @_;
360 53 my $hand = $self->hands->[$seat];
361 53 my $tally = $self->_tally_self($seat);
362 53 ▶ unless ($tally) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($tally) 2 51 53
363 2 ▶ return $self->_refuse(Game::Mahjong::Decompose::is_complete($hand) ? 'too_few_points' : 'not_a_win', $seat);
Branch: 1 branch
BranchTrueFalseTotal
Game::Mahjong::Decompose::is_complete($hand) ? : 1 1 2
364 }
365 51 $self->moves($self->moves + 1);
366 51 $self->_emit($seat, 'win');
367 51 return $self->_hand_won($seat, $tally, 'self', undef, $self->drawn);
368 }
369  
370 45045 ▶sub _may_claim {
Subroutine _may_claim: called CC=9 SCAR=22.0
371 45045 my ($self, $seat, $tile, $from, $rob) = @_;
372 45045 my $hand = $self->hands->[$seat];
373 45045 my @may;
374 45045 ▶ push @may, 'win' if $self->_tally_on($seat, $tile, $rob ? 'rob' : 'discard', $from);
Branch: 2 branches
BranchTrueFalseTotal
if $self->_tally_on($seat, $tile, $rob ? "rob" : "discard", $from) 114 44931 45045
$rob ? : 93 44952 45045
375 45045 ▶ unless ($rob) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rob) 44952 93 45045
376 44952 ▶ push @may, 'kong' if $hand->holds_pung_of($tile) && $self->wall->remaining;
Branch: 1 branch
BranchTrueFalseTotal
if $hand->holds_pung_of($tile) and $self->wall->remaining 142 44810 44952
Condition100%3/3
$hand->holds_pung_of($tile) and $self->wall->remaining
!ll&&!rl&&r
448064142
MC/DC100%2/2
$hand->holds_pung_of($tile) and $self->wall->remaining
$hand->holds_pung_of($tile) $self->wall->remaining
Truth table100%3/3
A and B
A= $hand->holds_pung_of($tile)B= $self->wall->remaining
ABresult
0X0
100
111
377 44952 ▶ push @may, 'pung' if $hand->holds_pair($tile);
Branch: 1 branch
BranchTrueFalseTotal
if $hand->holds_pair($tile) 1967 42985 44952
378 44952 ▶ push @may, 'chow' if $seat == $self->next_seat($from) && $hand->chow_shapes($tile);
Branch: 1 branch
BranchTrueFalseTotal
if $seat == $self->next_seat($from) and $hand->chow_shapes($tile) 2908 42044 44952
Condition100%3/3
$seat == $self->next_seat($from) and $hand->chow_shapes($tile)
!ll&&!rl&&r
29968120762908
MC/DC100%2/2
$seat == $self->next_seat($from) and $hand->chow_shapes($tile)
$seat == $self->next_seat($from) $hand->chow_shapes($tile)
Truth table100%3/3
A and B
A= $seat == $self->next_seat($from)B= $hand->chow_shapes($tile)
ABresult
0X0
100
111
379 }
380 45045 return @may;
381 }
382  
383 15015 ▶sub _open_window {
Subroutine _open_window: called CC=4 SCAR=13.9
384 15015 my ($self, $kind, $tile, $from) = @_;
385 15015 my %may;
386 15015 for my $seat (grep { $_ != $from } 0 .. 3) {
387 45045 my @claims = $self->_may_claim($seat, $tile, $from, $kind eq 'rob');
388 45045 ▶ $may{$seat} = \@claims if @claims;
Branch: 1 branch
BranchTrueFalseTotal
if @claims 4708 40337 45045
389 }
390 15015 ▶ if (!%may) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%may) 10662 4353 15015
391 10662 return $self->_close_window_with($kind, $tile, $from, undef);
392 }
393 4353 $self->window({ kind => $kind, tile => $tile, from => $from, may => \%may, answers => {} });
394 4353 $self->phase($kind);
395 4353 $self->turn(undef);
396 4353 return 1;
397 }
398  
399 15899 ▶sub _best_possible {
Subroutine _best_possible: called CC=3 SCAR=11.0
400 15899 my ($self, $seat) = @_;
401 15899 my $w = $self->window;
402 15899 my $best = 0;
403 15899 ▶ for my $c (@{ $w->{may}{$seat} }) { $best = $PRIORITY{$c} if $PRIORITY{$c} > $best }
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Mahjong::Rules::PRIORITY{$c} > $best 15899 1432 17331
404 15899 return $best;
405 }
406  
407 19144 ▶sub _window_waiting {
Subroutine _window_waiting: called CC=9 SCAR=22.0
408 19144 my ($self) = @_;
409 19144 ▶ my $w = $self->window or return ();
Branch: 1 branch
BranchTrueFalseTotal
my $w = $self->window or return () 19144 0 19144
410 19144 my ($lodged, $lodged_seat) = $self->_lodged_best;
411 19144 my @waiting;
412 19144 for my $seat ($self->_seats_after($w->{from})) {
413 57432 ▶ next unless $w->{may}{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless $w->{"may"}{$seat} 35435 21997 57432
414 21997 ▶ next if exists $w->{answers}{$seat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $w->{"answers"}{$seat} 6098 15899 21997
415 15899 my $best = $self->_best_possible($seat);
416 15899 ▶ next if $best < $lodged;
Branch: 1 branch
BranchTrueFalseTotal
if $best < $lodged 14 15885 15899
417 15885 ▶ next if $best == $lodged && !($best == $PRIORITY{win} && $self->_nearer($seat, $lodged_seat, $w->{from}));
Branch: 1 branch
BranchTrueFalseTotal
if $best == $lodged and not $best == $Game::Mahjong::Rules::PRIORITY{"win"} && $self->_nearer($seat, $lodged_seat, $w->{"from"}) 1 15884 15885
Condition83%5/6
$best == $lodged and not $best == $Game::Mahjong::Rules::PRIORITY{"win"} && $self->_nearer($seat, $lodged_seat, $w->{"from"})
!ll&&!rl&&r
1588131
$best == $Game::Mahjong::Rules::PRIORITY{'win'} && $self->_nearer($seat, $lodged_seat, $w->{'from'})
!ll&&!rl&&r
013
MC/DC66%2/3
$best == $lodged and not $best == $Game::Mahjong::Rules::PRIORITY{"win"} && $self->_nearer($seat, $lodged_seat, $w->{"from"})
$best == $lodged $best == $Game::Mahjong::Rules::PRIORITY{'win'} $self->_nearer($seat, $lodged_seat, $w->{'from'})
Truth table75%3/4
A and not(B && C)
A= $best == $lodgedB= $best == $Game::Mahjong::Rules::PRIORITY{'win'}C= $self->_nearer($seat, $lodged_seat, $w->{'from'})
ABCresult
0XX0
1110
10X1
1101
418 15884 push @waiting, $seat;
419 }
420 19144 return @waiting;
421 }
422  
423 42632 ▶sub _seats_after {
Subroutine _seats_after: called CC=1 SCAR=0.0
424 42632 my ($self, $from) = @_;
425 42632 return map { ($from + $_) % 4 } 1 .. 3;
426 }
427  
428 12 ▶sub _distance { my ($self, $seat, $from) = @_; return ($seat - $from) % 4 }
Subroutine _distance: called CC=1 SCAR=0.0
429  
430 6 ▶sub _nearer {
Subroutine _nearer: called CC=3 SCAR=11.1
431 6 my ($self, $seat, $than, $from) = @_;
432 6 ▶ return 1 unless defined $than;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $than 0 6 6
433 6 ▶ return $self->_distance($seat, $from) < $self->_distance($than, $from) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->_distance($seat, $from) < $self->_distance($than, $from) ? : 3 3 6
434 }
435  
436 23488 ▶sub _lodged_best {
Subroutine _lodged_best: called CC=8 SCAR=21.4
437 23488 my ($self) = @_;
438 23488 my $w = $self->window;
439 23488 my ($best, $seat) = (0, undef);
440 23488 for my $s ($self->_seats_after($w->{from})) {
441 70464 ▶ my $a = $w->{answers}{$s} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $a = $w->{"answers"}{$s} or next 10785 59679 70464
442 10785 ▶ next if !ref $a;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $a 9585 1200 10785
443 1200 my $p = $PRIORITY{ $a->{kind} };
444 1200 ▶ if ($p > $best || ($p == $best && $p == $PRIORITY{win} && $self->_nearer($s, $seat, $w->{from}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p > $best or $p == $best and $p == $Game::Mahjong::Rules::PRIORITY{"win"} and $self->_nearer($s, $seat, $w->{"from"})) 1198 2 1200
Condition44%4/9
$p > $best or $p == $best and $p == $Game::Mahjong::Rules::PRIORITY{"win"} and $self->_nearer($s, $seat, $w->{"from"})
l!l&&r!l&&!r
119802
$p == $best and $p == $Game::Mahjong::Rules::PRIORITY{"win"} and $self->_nearer($s, $seat, $w->{"from"})
!ll&&!rl&&r
020
$p == $best and $p == $Game::Mahjong::Rules::PRIORITY{"win"}
!ll&&!rl&&r
002
MC/DC25%1/4
$p > $best or $p == $best and $p == $Game::Mahjong::Rules::PRIORITY{"win"} and $self->_nearer($s, $seat, $w->{"from"})
$p > $best $p == $best $p == $Game::Mahjong::Rules::PRIORITY{"win"} $self->_nearer($s, $seat, $w->{"from"})
Truth table40%2/5
A or B and C and D
A= $p > $bestB= $p == $bestC= $p == $Game::Mahjong::Rules::PRIORITY{"win"}D= $self->_nearer($s, $seat, $w->{"from"})
ABCDresult
1XXX1
01111
00XX0
010X0
01100
445 1198 ($best, $seat) = ($p, $s);
446 }
447 }
448 23488 return ($best, $seat);
449 }
450  
451 4693 ▶sub _answer {
Subroutine _answer: called CC=18 SCAR=29.3
452 4693 my ($self, $seat, $move) = @_;
453 4693 ▶ my $w = $self->window or return $self->_refuse('no_window', $seat);
Branch: 1 branch
BranchTrueFalseTotal
my $w = $self->window or return $self->_refuse("no_window", $seat) 4693 0 4693
454 4693 my $kind = $move->{kind};
455 4693 ▶ return $self->_refuse('cannot_claim', $seat) unless $w->{may}{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless $w->{"may"}{$seat} 1 4692 4693
456 4692 ▶ return $self->_refuse('already_answered', $seat) if exists $w->{answers}{$seat};
Branch: 1 branch
BranchTrueFalseTotal
if exists $w->{"answers"}{$seat} 1 4691 4692
457  
458 4691 my %waiting = map { $_ => 1 } $self->_window_waiting;
459 4691 ▶ if ($kind eq 'pass') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'pass') { } 4127 564 4691
460 4127 $w->{answers}{$seat} = 'pass';
461 }
462 else {
463 564 ▶ return $self->_refuse('cannot_claim', $seat) unless $waiting{$seat};
Branch: 1 branch
BranchTrueFalseTotal
unless $waiting{$seat} 1 563 564
464 563 ▶ return $self->_refuse('cannot_claim', $seat) unless grep { $_ eq $kind } @{ $w->{may}{$seat} };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $kind;} @{$$w{"may"}{$seat};} 1 562 563
465 562 my $answer = { kind => $kind };
466 562 ▶ if ($kind eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "chow") 238 324 562
467 238 my $tiles = $move->{tiles};
468 return $self->_refuse('bad_move', $seat) unless ref $tiles eq 'ARRAY' && @$tiles == 2
469 238 ▶ && !grep { !defined $_ || !/\A\d+\z/ || $_ < 1 || $_ > Game::Mahjong::Tiles::KINDS } @$tiles;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tiles eq "ARRAY" and @$tiles == 2 and not grep({not defined $_ && /\A\d+\z/ or $_ < 1 or $_ > 34;} @$tiles) 1 237 238
Condition46%7/15
ref $tiles eq "ARRAY" and @$tiles == 2 and not grep({not defined $_ && /\A\d+\z/ or $_ < 1 or $_ > 34;} @$tiles)
!ll&&!rl&&r
10237
ref $tiles eq "ARRAY" and @$tiles == 2
!ll&&!rl&&r
01237
!(defined $_ && /\A\d+\z/) || $_ < 1 || $_ > 34
l!l&&r!l&&!r
00474
not defined $_ && /\A\d+\z/ or $_ < 1
l!l&&r!l&&!r
00474
defined $_ && /\A\d+\z/
!ll&&!rl&&r
00474
MC/DC33%1/3
ref $tiles eq "ARRAY" and @$tiles == 2 and not grep({not defined $_ && /\A\d+\z/ or $_ < 1 or $_ > 34;} @$tiles)
ref $tiles eq "ARRAY" @$tiles == 2 not grep({not defined $_ && /\A\d+\z/ or $_ < 1 or $_ > 34;} @$tiles)
MC/DC0%0/4
!(defined $_ && /\A\d+\z/) || $_ < 1 || $_ > 34
defined $_ /\A\d+\z/ $_ < 1 $_ > 34
Truth table50%2/4
A and B and not(C)
A= ref $tiles eq "ARRAY"B= @$tiles == 2C= not grep({not defined $_ && /\A\d+\z/ or $_ < 1 or $_ > 34;} @$tiles)
ABCresult
0XX0
10X0
1100
1111
Truth table20%1/5
not(A && B) or C || D
A= defined $_B= /\A\d+\z/C= $_ < 1D= $_ > 34
ABCDresult
0XXX1
10XX1
111X1
11011
11000
470 237 my ($a, $b) = sort { $a <=> $b } map { $_ + 0 } @$tiles;
471 237 ▶ my $ok = grep { $_->[0] == $a && $_->[1] == $b } $self->hands->[$seat]->chow_shapes($w->{tile});
Condition100%3/3
$_->[0] == $a && $_->[1] == $b
!ll&&!rl&&r
361236
MC/DC100%2/2
$_->[0] == $a && $_->[1] == $b
$_->[0] == $a $_->[1] == $b
Truth table100%3/3
A && B
A= $_->[0] == $aB= $_->[1] == $b
ABresult
0X0
100
111
472 237 ▶ return $self->_refuse('not_a_meld', $seat) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 1 236 237
473 236 $answer->{tiles} = [ $a, $b ];
474 }
475 560 $w->{answers}{$seat} = $answer;
476 }
477 4687 $self->moves($self->moves + 1);
478 4687 ▶ $self->_emit($seat, $kind, ($kind eq 'chow' ? (tiles => $w->{answers}{$seat}{tiles}) : ()));
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'chow' ? : 236 4451 4687
479  
480 4687 ▶ return 1 if $self->_window_waiting;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_window_waiting 343 4344 4687
481 4344 return $self->_resolve_window;
482 }
483  
484 4344 ▶sub _resolve_window {
Subroutine _resolve_window: called CC=2 SCAR=6.9
485 4344 my ($self) = @_;
486 4344 my $w = $self->window;
487 4344 my ($best, $seat) = $self->_lodged_best;
488 4344 ▶ my $answer = defined $seat ? $w->{answers}{$seat} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $seat ? : 549 3795 4344
489 4344 return $self->_close_window_with($w->{kind}, $w->{tile}, $w->{from}, $seat, $answer);
490 }
491  
492 15006 ▶sub _close_window_with {
Subroutine _close_window_with: called CC=12 SCAR=24.9
493 15006 my ($self, $kind, $tile, $from, $seat, $answer) = @_;
494 15006 $self->window(undef);
495  
496 15006 ▶ if (!defined $seat) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $seat) 14457 549 15006
497 14457 ▶ if ($kind eq 'rob') { return $self->_perform_promotion($from, $tile) }
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq "rob") 30 14427 14457
498 14427 return $self->_draw_for($self->next_seat($from));
499 }
500  
501 549 my $claim = $answer->{kind};
502 549 ▶ if ($claim eq 'win') {
Branch: 1 branch
BranchTrueFalseTotal
if ($claim eq "win") 104 445 549
503 104 ▶ my $tally = $self->_tally_on($seat, $tile, $kind eq 'rob' ? 'rob' : 'discard', $from);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'rob' ? : 1 103 104
504 104 ▶ die 'Game::Mahjong::Rules: a lodged win that no longer scores' unless $tally;
Branch: 1 branch
BranchTrueFalseTotal
unless $tally 0 104 104
505 104 ▶ if ($kind eq 'rob') { $self->hands->[$from]->remove($tile) }
Branch: 1 branch
BranchTrueFalseTotal
if ($kind eq 'rob') { } 1 103 104
506 103 else { pop @{ $self->pools->[$from] } }
507 104 $self->hands->[$seat]->add($tile);
508 104 $self->_emit('sys', 'claimed', seat => $seat, meld => 'win', tile => $tile, from => $from);
509 104 ▶ return $self->_hand_won($seat, $tally, $kind eq 'rob' ? 'rob' : 'discard', $from, $tile);
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'rob' ? : 1 103 104
510 }
511  
512 445 pop @{ $self->pools->[$from] };
513 445 my $hand = $self->hands->[$seat];
514 445 ▶ if ($claim eq 'chow') { $hand->claim_chow($tile, @{ $answer->{tiles} }, $from) }
Branch: 3 branches
BranchTrueFalseTotal
if ($claim eq 'chow') { } 227 218 445
elsif ($claim eq 'pung') { } 214 4 218
elsif ($claim eq 'kong') { } 4 0 4
515 214 elsif ($claim eq 'pung') { $hand->claim_pung($tile, $from) }
516 4 elsif ($claim eq 'kong') { $hand->claim_kong($tile, $from) }
517 445 $self->_emit('sys', 'claimed', seat => $seat, meld => $claim, tile => $tile, from => $from);
518 445 $self->last({ seat => $seat, kind => $claim, tile => $tile, from => $from });
519  
520 445 ▶ if ($claim eq 'kong') { return $self->_replacement_draw($seat, 'kong') }
Branch: 1 branch
BranchTrueFalseTotal
if ($claim eq "kong") 4 441 445
521 441 $self->phase('discard');
522 441 $self->turn($seat);
523 441 $self->claimed_this_turn(1);
524 441 $self->drawn(undef);
525 441 $self->replacement(undef);
526 441 return 1;
527 }
528  
529 30 ▶sub _perform_promotion {
Subroutine _perform_promotion: called CC=1 SCAR=0.0
530 30 my ($self, $seat, $tile) = @_;
531 30 $self->hands->[$seat]->promote_kong($tile);
532 30 return $self->_replacement_draw($seat, 'kong');
533 }
534  
535 14427 ▶sub _draw_for {
Subroutine _draw_for: called CC=3 SCAR=11.0
536 14427 my ($self, $seat) = @_;
537 14427 ▶ return $self->_exhausted if $self->wall->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wall->is_empty 73 14354 14427
538 14354 my $kind = $self->wall->draw;
539 14354 $self->_emit('sys', 'drew', seat => $seat, from => 'wall');
540 14354 ▶ $self->last_draw_emptied($self->wall->is_empty ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->wall->is_empty ? : 74 14280 14354
541 14354 return $self->_took($seat, $kind, 'wall', undef);
542 }
543  
544 908 ▶sub _replacement_draw {
Subroutine _replacement_draw: called CC=3 SCAR=11.0
545 908 my ($self, $seat, $why) = @_;
546 908 ▶ return $self->_exhausted if $self->wall->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->wall->is_empty 5 903 908
547 903 my $kind = $self->wall->replace;
548 903 $self->_emit('sys', 'drew', seat => $seat, from => 'back');
549 903 ▶ $self->last_draw_emptied($self->wall->is_empty ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->wall->is_empty ? : 6 897 903
550 903 return $self->_took($seat, $kind, 'back', $why);
551 }
552  
553 15257 ▶sub _took {
Subroutine _took: called CC=2 SCAR=6.9
554 15257 my ($self, $seat, $kind, $from, $why) = @_;
555 15257 ▶ if (Game::Mahjong::Tiles::is_bonus($kind)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Tiles::is_bonus($kind)) 858 14399 15257
556 858 $self->hands->[$seat]->add_flower($kind);
557 858 $self->_emit('sys', 'flower', seat => $seat, tile => $kind);
558 858 return $self->_replacement_draw($seat, 'flower');
559 }
560 14399 $self->hands->[$seat]->add($kind);
561 14399 $self->drawn($kind);
562 14399 $self->drawn_from($from);
563 14399 $self->replacement($why);
564 14399 $self->claimed_this_turn(0);
565 14399 $self->phase('discard');
566 14399 $self->turn($seat);
567 14399 $self->last({ seat => $seat, kind => 'drew', from => $from });
568 14399 return 1;
569 }
570  
571 155 ▶sub _hand_won {
Subroutine _hand_won: called CC=1 SCAR=0.0
572 155 my ($self, $seat, $tally, $by, $from, $tile) = @_;
573 155 my $deltas = Game::Mahjong::Result::settle(winner => $seat, by => $by, from => $from, points => $tally->points);
574 $self->_end_hand({
575 winner => $seat,
576 by => $by,
577 from => $from,
578 tile => $tile,
579 155 fans => [ map { { key => $_->{key}, points => $_->{points}, times => $_->{times} } } @{ $tally->fans } ],
580 points => $tally->points,
581 flowers => $tally->flowers,
582 deltas => $deltas,
583 });
584 155 return 1;
585 }
586  
587 78 ▶sub _exhausted {
Subroutine _exhausted: called CC=1 SCAR=0.0
588 78 my ($self) = @_;
589 78 $self->_end_hand({ winner => undef, by => 'exhausted', from => undef, tile => undef, fans => [], points => 0, flowers => 0, deltas => [ 0, 0, 0, 0 ] });
590 78 return 1;
591 }
592  
593 233 ▶sub _end_hand {
Subroutine _end_hand: called CC=4 SCAR=13.9
594 233 my ($self, $record) = @_;
595 233 my $totals = $self->totals;
596 233 $totals->[$_] += $record->{deltas}[$_] for 0 .. 3;
597 233 $record->{hand} = $self->hand_no;
598 233 $record->{totals} = [ @$totals ];
599 233 $record->{concealed_kongs} = [ map { [ map { $_->tile } @{ $self->hands->[$_]->concealed_kongs } ] } 0 .. 3 ];
600 233 push @{ $self->history }, $record;
601 233 $self->_emit('sys', 'hand_end', %$record);
602 233 $self->window(undef);
603  
604 233 ▶ if (Game::Mahjong::Result::finished($self->hand_no)) {
Branch: 1 branch
BranchTrueFalseTotal
if (Game::Mahjong::Result::finished($self->hand_no)) 14 219 233
605 14 $self->status('finished');
606 14 $self->phase('finished');
607 14 $self->turn(undef);
608 14 my $winner = Game::Mahjong::Result::winner($totals);
609 14 $self->winner($winner);
610 14 ▶ $self->result(defined $winner ? 'score' : 'draw');
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 13 1 14
611 14 $self->_emit('sys', 'game_end', winner => $winner, result => $self->result,
612 totals => [ @$totals ], places => Game::Mahjong::Result::places($totals));
613 14 return;
614 }
615 219 $self->hand_no($self->hand_no + 1);
616 219 $self->_deal;
617 219 return;
618 }
619  
620 4219 ▶sub check_invariants {
Subroutine check_invariants: called CC=37 SCAR=38.6
Pod documented
621 4219 my ($self) = @_;
622 4219 my @bad;
623 4219 ▶ return @bad unless $self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active 2 4217 4219
624  
625 4217 my %count;
626 4217 for my $seat (0 .. 3) {
627 16868 my $h = $self->hands->[$seat];
628 16868 $count{$_}++ for $h->tiles;
629 16868 $count{$_}++ for @{ $h->flowers };
630 16868 for my $m (@{ $h->melds }) { $count{$_}++ for @{ $m->tiles } }
631 16868 $count{$_}++ for @{ $self->pools->[$seat] };
632 }
633 4217 $count{$_}++ for @{ $self->wall->peek };
634 4217 my $tiles = 0;
635 4217 $tiles += $_ for values %count;
636 4217 my $whole = $tiles == Game::Mahjong::Tiles::TILES;
637 4217 for my $kind (1 .. Game::Mahjong::Tiles::PATTERNS) {
638 177114 ▶ my $cap = Game::Mahjong::Tiles::is_bonus($kind) ? 1 : Game::Mahjong::Tiles::PER_KIND;
Branch: 1 branch
BranchTrueFalseTotal
Game::Mahjong::Tiles::is_bonus($kind) ? : 33736 143378 177114
639 177114 ▶ my $seen = $count{$kind} || 0;
Condition100%2/2
$count{$kind} || 0
l!l
176846268
MC/DC100%1/1
$count{$kind} || 0
$count{$kind}
Truth table100%2/2
A
A= $count{$kind}
Aresult
11
00
640 177114 ▶ push @bad, "kind $kind is seen $seen times, not $cap" if $whole && $seen != $cap;
Branch: 1 branch
BranchTrueFalseTotal
if $whole and $seen != $cap 0 177114 177114
Condition66%2/3
$whole and $seen != $cap
!ll&&!rl&&r
6721764420
MC/DC0%0/2
$whole and $seen != $cap
$whole $seen != $cap
Truth table66%2/3
A and B
A= $wholeB= $seen != $cap
ABresult
0X0
100
111
641 177114 ▶ push @bad, "kind $kind is seen $seen times, more than the set holds" if !$whole && $seen > $cap;
Branch: 1 branch
BranchTrueFalseTotal
if not $whole and $seen > $cap 0 177114 177114
Condition66%2/3
not $whole and $seen > $cap
!ll&&!rl&&r
1764426720
MC/DC0%0/2
not $whole and $seen > $cap
not $whole $seen > $cap
Truth table66%2/3
not(A) and B
A= not $wholeB= $seen > $cap
ABresult
0X0
100
111
642 }
643  
644 my $on = $self->phase eq 'discard' ? $self->turn
645 : $self->phase eq 'rob' && $self->window ? $self->window->{from}
646 4217 ▶ : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->phase eq 'discard' ? : 3409 808 4217
$self->phase eq 'rob' && $self->window ? : 0 808 808
Condition33%1/3
$self->phase eq "rob" and $self->window
!ll&&!rl&&r
80800
MC/DC0%0/2
$self->phase eq "rob" and $self->window
$self->phase eq "rob" $self->window
Truth table33%1/3
A and B
A= $self->phase eq "rob"B= $self->window
ABresult
0X0
100
111
647 4217 for my $seat (0 .. 3) {
648 16868 my $h = $self->hands->[$seat];
649 16868 ▶ my $want = (defined $on && $seat == $on) ? 14 : 13;
Branch: 1 branch
BranchTrueFalseTotal
defined $on && $seat == $on ? : 3409 13459 16868
Condition100%3/3
defined $on and $seat == $on
!ll&&!rl&&r
3232102273409
MC/DC100%2/2
defined $on and $seat == $on
defined $on $seat == $on
Truth table100%3/3
A and B
A= defined $onB= $seat == $on
ABresult
0X0
100
111
650 16868 ▶ push @bad, "seat $seat holds " . $h->total . " tiles' worth, not $want" unless $h->total == $want;
Branch: 1 branch
BranchTrueFalseTotal
unless $h->total == $want 0 16868 16868
651 }
652  
653 4217 my $sum = 0;
654 4217 $sum += $_ for @{ $self->totals };
655 4217 ▶ push @bad, "totals sum to $sum" if $sum != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sum != 0 0 4217 4217
656  
657 4217 ▶ if ($self->phase eq 'discard') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->phase eq 'discard') { } 3409 808 4217
elsif ($self->phase eq 'claim' or $self->phase eq 'rob') { } 808 0 808
658 3409 my @w = $self->waiting_on;
659 3409 ▶ push @bad, 'discard phase waits on ' . scalar(@w) . ' seats' unless @w == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @w == 1 0 3409 3409
660 3409 ▶ push @bad, 'a window is open in the discard phase' if $self->window;
Branch: 1 branch
BranchTrueFalseTotal
if $self->window 0 3409 3409
661 }
662 ▶ elsif ($self->phase eq 'claim' || $self->phase eq 'rob') {
Condition33%1/3
$self->phase eq "claim" or $self->phase eq "rob"
l!l&&r!l&&!r
80800
MC/DC0%0/2
$self->phase eq "claim" or $self->phase eq "rob"
$self->phase eq "claim" $self->phase eq "rob"
Truth table33%1/3
A or B
A= $self->phase eq "claim"B= $self->phase eq "rob"
ABresult
1X1
011
000
663 808 my $w = $self->window;
664 808 ▶ push @bad, 'no window in the ' . $self->phase . ' phase' unless $w;
Branch: 1 branch
BranchTrueFalseTotal
unless $w 0 808 808
665 808 ▶ if ($w) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w) 808 0 808
666 808 ▶ push @bad, 'the discarder is asked' if $w->{may}{ $w->{from} };
Branch: 1 branch
BranchTrueFalseTotal
if $w->{"may"}{$w->{"from"}} 0 808 808
667 808 for my $seat (keys %{ $w->{may} }) {
668 910 ▶ push @bad, "seat $seat is asked with no claim" unless @{ $w->{may}{$seat} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$w->{"may"}{$seat};} 0 910 910
669 }
670 808 ▶ push @bad, 'a window with nobody left to answer' unless $self->_window_waiting;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_window_waiting 0 808 808
671 }
672 }
673 0 else { push @bad, 'phase ' . $self->phase . ' while active' }
674 4217 return @bad;
675 }
676  
677 8 ▶sub to_string {
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
678 8 my ($self) = @_;
679 8 return join "\n", map { "seat $_: " . $self->hands->[$_]->to_notation . ' | pool ' . Game::Mahjong::Notation::print(@{ $self->pools->[$_] }) } 0 .. 3;
680 }
681  
682 34 1;
683  
684 __END__
« blib/lib/Game/Mahjong/Score.pm ↑ Summary blib/lib/Game/Mahjong/Search.pm »