blib/lib/Game/Mahjong/Terminal.pm

filter: Statementstmt 86.1% 547 / 635 Branchbran 66.4% 234 / 352 Conditioncond 61.0% 94 / 154 MC/DCmcdc 43.4% 40 / 92 Subroutinesub 96.5% 56 / 58 Podpod 96.6% 29 / 30 totaltotal 75.7% 1000 / 1321 CC 250 SCAR 34.3
CC
250
Cov
77%
pick_discard
157.3
_parse
116.3
run
20.5
CRAP 31.0
↑ Summary blib/lib/Game/Mahjong/Score.pm »
1 package Game::Mahjong::Terminal;
2  
3 2 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 2 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 2 ▶use Game::Mahjong::Tiles;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 2 ▶use Game::Mahjong::Notation;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 ▶use Game::Mahjong::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
12 2 ▶use Game::Mahjong::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 2 ▶use Game::Mahjong::Fans;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 2 ▶use Game::Mahjong::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
15  
16 2 our $VERSION = '0.02';
17  
18 2 has seed => (is => 'ro', isa => Str, required => 1);
19  
20 2 has level => (is => 'ro', isa => Int, default => 2);
21  
22 2 has seat => (is => 'ro', isa => Int, default => 0);
23  
24 2 has in => (is => 'ro', required => 1);
25  
26 2 has out => (is => 'ro', required => 1);
27  
28 2 has colour => (is => 'ro', default => 0);
29  
30 2 has glyphs => (is => 'ro', default => 0);
31  
32 2 has ascii => (is => 'ro', default => 0);
33  
34 2 has clear => (is => 'ro', default => 0);
35  
36 2 has auto => (is => 'ro', default => 0);
37  
38 35 ▶has names => (is => 'ro', isa => ArrayRef, default => sub { [ 'You', 'Ada', 'Bram', 'Cho' ] });
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 2 has rules => (is => 'rw');
41  
42 2 has bots => (is => 'rw', isa => ArrayRef, default => []);
43  
44 2 has hinter => (is => 'rw');
45  
46 2 has quit => (is => 'rw', default => 0);
47  
48 2 has finished => (is => 'rw', default => 0);
49  
50 2 has winner => (is => 'rw');
51  
52 2 has cursor => (is => 'rw');
53  
54 2 has raw => (is => 'rw', default => 0);
55  
56 2 has keysource => (is => 'rw');
57  
58 35 ▶has pending => (is => 'rw', isa => ArrayRef, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
59  
60 2 has _picking => (is => 'rw', init_arg => 'picking');
61  
62 2 our %SEQUENCE = (
63 'A' => 'up',
64 'B' => 'down',
65 'C' => 'right',
66 'D' => 'left',
67 'H' => 'home',
68 'F' => 'end',
69 '1~' => 'home',
70 '4~' => 'end',
71 '5~' => 'page_up',
72 '6~' => 'page_down',
73 '7~' => 'home',
74 '8~' => 'end',
75 );
76  
77 2 our %CONTROL = (
78 "\r" => 'enter',
79 "\n" => 'enter',
80 "\t" => 'tab',
81 "\x7f" => 'backspace',
82 "\x08" => 'backspace',
83 "\x03" => 'interrupt',
84 "\x04" => 'eof',
85 );
86  
87 2 our %WIND = (0 => 'East', 1 => 'South', 2 => 'West', 3 => 'North');
88 2 our $DOT = '|';
89  
90 1043 ▶sub _clear {
Subroutine _clear: called CC=3 SCAR=15.0
91 1043 my ($self) = @_;
92 1043 ▶ return unless $self->colour && $self->clear;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour and $self->clear 1043 0 1043
Condition66%2/3
$self->colour and $self->clear
!ll&&!rl&&r
104210
MC/DC0%0/2
$self->colour and $self->clear
$self->colour $self->clear
Truth table66%2/3
A and B
A= $self->colourB= $self->clear
ABresult
0X0
100
111
93 0 my $out = $self->out;
94 0 print {$out} "\e[H\e[2J";
95 0 return;
96 }
97  
98 2 my %GLYPH;
99 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
100 2 my @m = map { chr(0x1F007 + $_) } 0 .. 8;
101 2 my @p = map { chr(0x1F019 + $_) } 0 .. 8;
102 2 my @s = map { chr(0x1F010 + $_) } 0 .. 8;
103 2 my @w = (chr 0x1F000, chr 0x1F001, chr 0x1F002, chr 0x1F003);
104 2 my @d = (chr 0x1F004, chr 0x1F005, chr 0x1F006);
105 2 my @f = map { chr(0x1F022 + $_) } 0 .. 3;
106 2 my @t = map { chr(0x1F026 + $_) } 0 .. 3;
107 2 my $i = 1;
108 2 $GLYPH{ $i++ } = $_ for @m, @p, @s, @w, @d, @f, @t;
109 }
110  
111 35 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.2
Pod undocumented
112 35 my ($self) = @_;
113 35 ▶ die 'Game::Mahjong::Terminal: the seat is 0 to 3' unless $self->seat >= 0 && $self->seat <= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->seat >= 0 and $self->seat <= 3 1 34 35
Condition66%2/3
$self->seat >= 0 and $self->seat <= 3
!ll&&!rl&&r
0134
MC/DC50%1/2
$self->seat >= 0 and $self->seat <= 3
$self->seat >= 0 $self->seat <= 3
Truth table66%2/3
A and B
A= $self->seat >= 0B= $self->seat <= 3
ABresult
0X0
100
111
114 34 ▶ die 'Game::Mahjong::Terminal: the level is 1 to 3' unless $self->level >= 1 && $self->level <= 3;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->level >= 1 and $self->level <= 3 1 33 34
Condition66%2/3
$self->level >= 1 and $self->level <= 3
!ll&&!rl&&r
0133
MC/DC50%1/2
$self->level >= 1 and $self->level <= 3
$self->level >= 1 $self->level <= 3
Truth table66%2/3
A and B
A= $self->level >= 1B= $self->level <= 3
ABresult
0X0
100
111
115 33 $self->rules(Game::Mahjong::Rules->new(seed => $self->seed));
116 33 $self->bots([ map { Game::Mahjong::Bot->new(level => $self->level, seed => $self->seed) } 0 .. 3 ]);
117 33 $self->hinter(Game::Mahjong::Bot->new(level => 3, seed => $self->seed . ':hint'));
118 33 return;
119 }
120  
121 8917 ▶sub _c {
Subroutine _c: called CC=2 SCAR=6.9
122 8917 my ($self, $code, $text) = @_;
123 8917 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 8904 13 8917
124 13 my %code = (bold => 1, dim => 2, red => 31, green => 32, yellow => 33, blue => 34, magenta => 35, cyan => 36);
125 13 return "\e[$code{$code}m$text\e[0m";
126 }
127  
128 2 our %HONOUR_FACE = (
129 we => 'E', ws => 'S', ww => 'W', wn => 'N',
130 dr => 'R', dg => 'G', dw => 'B',
131 );
132  
133 2 our %INK = (m => 196, p => 25, s => 29, f => 170, t => 178);
134 2 our %HONOUR_INK = (we => 240, ws => 240, ww => 240, wn => 240,
135 dr => 196, dg => 29, dw => 245);
136 2 our $FACE_BG = 230;
137 2 our $EDGE_FG = 180;
138 2 our $SIDE_FG = 137;
139 2 our $BACK_BG = 66;
140  
141 2 our $FRESH_FG = 28;
142  
143 2 our %FRAME = (
144 plain => {
145 wide => [ "\x{250c}", "\x{2510}", "\x{2514}", "\x{2518}",
146 "\x{2502}", "\x{2500}" ],
147 ascii => [ '+', '+', '+', '+', '|', '-' ],
148 },
149 fresh => {
150 wide => [ "\x{2554}", "\x{2557}", "\x{255a}", "\x{255d}",
151 "\x{2551}", "\x{2550}" ],
152 ascii => [ '#', '#', '#', '#', '#', '=' ],
153 },
154 cursor => {
155 wide => [ "\x{250f}", "\x{2513}", "\x{2517}", "\x{251b}",
156 "\x{2503}", "\x{2501}" ],
157 ascii => [ '+', '+', '+', '+', '!', '=' ],
158 },
159 );
160  
161 54607 ▶sub face {
Subroutine face: called CC=6 SCAR=18.2
Pod documented
162 54607 my ($self, $kind) = @_;
163 54607 ▶ return ' ' unless defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kind 0 54607 54607
164 54607 ▶ return $GLYPH{$kind} if $self->glyphs;
Branch: 1 branch
BranchTrueFalseTotal
if $self->glyphs 18 54589 54607
165 54589 my $code = Game::Mahjong::Tiles::code_of($kind);
166 54589 ▶ return $HONOUR_FACE{$code} . ' ' if $HONOUR_FACE{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Mahjong::Terminal::HONOUR_FACE{$code} 15427 39162 54589
167 39162 my $text = Game::Mahjong::Notation::print($kind);
168 39162 ▶ $text = substr($text, 0, 2) if length $text > 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $text > 2 0 39162 39162
169 39162 ▶ return length $text == 1 ? "$text " : $text;
Branch: 1 branch
BranchTrueFalseTotal
length $text == 1 ? : 0 39162 39162
170 }
171  
172 18 ▶sub _ink {
Subroutine _ink: called CC=3 SCAR=11.0
173 18 my ($self, $kind) = @_;
174 18 my $code = Game::Mahjong::Tiles::code_of($kind);
175 18 ▶ return $HONOUR_INK{$code} if $HONOUR_INK{$code};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Mahjong::Terminal::HONOUR_INK{$code} 4 14 18
176 14 ▶ return $INK{ substr($code, 0, 1) } // 240;
Condition50%1/2
$Game::Mahjong::Terminal::INK{substr($code, 0, 1)} // 240
l!l
140
MC/DC0%0/1
$Game::Mahjong::Terminal::INK{substr($code, 0, 1)} // 240
$Game::Mahjong::Terminal::INK{substr($code, 0, 1)}
Truth table50%1/2
A
A= $Game::Mahjong::Terminal::INK{substr($code, 0, 1)}
Aresult
11
00
177 }
178  
179 14660 ▶sub tile_art {
Subroutine tile_art: called CC=13 SCAR=27.2
Pod documented
180 14660 my ($self, $kind, %o) = @_;
181 14660 my $ascii = $self->ascii;
182 14660 ▶ my $style = $o{cursor} ? 'cursor' : $o{mark} ? 'fresh' : 'plain';
Branch: 2 branches
BranchTrueFalseTotal
$o{'cursor'} ? : 25 14635 14660
$o{'mark'} ? : 989 13646 14635
183 my ($tl, $tr, $bl, $br, $v, $h)
184 14660 ▶ = @{ $FRAME{$style}{ $ascii ? 'ascii' : 'wide' } };
Branch: 1 branch
BranchTrueFalseTotal
$ascii ? : 14 14646 14660
185  
186 14660 ▶ my $face = $o{back} ? ($ascii ? '##' : "\x{2592}\x{2592}") : $self->face($kind);
Branch: 2 branches
BranchTrueFalseTotal
$o{'back'} ? : 0 14660 14660
$ascii ? : 0 0 0
187  
188 14660 ▶ unless ($self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->colour) 14646 14 14660
189 14646 return ($tl . $h x 2 . $tr, $v . $face . $v, $bl . $h x 2 . $br);
190 }
191  
192 14 ▶ my $bg = $o{back} ? $BACK_BG : $FACE_BG;
Branch: 1 branch
BranchTrueFalseTotal
$o{'back'} ? : 0 14 14
193 14 ▶ my $ink = $o{back} ? 250 : $self->_ink($kind);
Branch: 1 branch
BranchTrueFalseTotal
$o{'back'} ? : 0 14 14
194  
195 14 ▶ my $edge = $o{mark} ? $FRESH_FG : $EDGE_FG;
Branch: 1 branch
BranchTrueFalseTotal
$o{'mark'} ? : 0 14 14
196 14 ▶ my $side = $o{mark} ? $FRESH_FG : $SIDE_FG;
Branch: 1 branch
BranchTrueFalseTotal
$o{'mark'} ? : 0 14 14
197  
198 14 ▶ my $on = $o{cursor} ? "\e[1m\e[7m" : $o{mark} ? "\e[1m" : '';
Branch: 2 branches
BranchTrueFalseTotal
$o{'cursor'} ? : 0 14 14
$o{'mark'} ? : 0 14 14
199  
200 return (
201 14 "$on\e[38;5;${edge}m\e[48;5;${bg}m" . $tl . $h x 2 . $tr . "\e[0m",
202 "$on\e[38;5;${ink}m\e[48;5;${bg}m" . $v . $face . $v . "\e[0m",
203 "$on\e[38;5;${side}m\e[48;5;${bg}m" . $bl . $h x 2 . $br . "\e[0m",
204 );
205 }
206  
207 1189 ▶sub tile_row {
Subroutine tile_row: called CC=9 SCAR=22.0
Pod documented
208 1189 my ($self, $kinds, %o) = @_;
209 1189 my @line = ('', '', '');
210 1189 ▶ my $backs = $o{backs} || {};
Condition100%2/2
$o{'backs'} || {}
l!l
981091
MC/DC100%1/1
$o{'backs'} || {}
$o{'backs'}
Truth table100%2/2
A
A= $o{'backs'}
Aresult
11
00
211 1189 ▶ my $gaps = $o{gaps} || {};
Condition100%2/2
$o{'gaps'} || {}
l!l
114148
MC/DC100%1/1
$o{'gaps'} || {}
$o{'gaps'}
Truth table100%2/2
A
A= $o{'gaps'}
Aresult
11
00
212 1189 for my $i (0 .. $#$kinds) {
213 14660 ▶ my $pad = $gaps->{$i} ? ' ' : ($i ? ' ' : '');
Branch: 2 branches
BranchTrueFalseTotal
$gaps->{$i} ? : 1013 13647 14660
$i ? : 12458 1189 13647
214 my @art = $self->tile_art($kinds->[$i],
215 back => $backs->{$i},
216 mark => (defined $o{mark} && $o{mark} == $i),
217 14660 ▶ cursor => (defined $o{cursor} && $o{cursor} == $i));
Condition100%6/6
defined $o{'mark'} && $o{'mark'} == $i
!ll&&!rl&&r
905127531002
defined $o{'cursor'} && $o{'cursor'} == $i
!ll&&!rl&&r
1431032525
MC/DC100%2/2
defined $o{'mark'} && $o{'mark'} == $i
defined $o{'mark'} $o{'mark'} == $i
MC/DC100%2/2
defined $o{'cursor'} && $o{'cursor'} == $i
defined $o{'cursor'} $o{'cursor'} == $i
Truth table100%3/3
A && B
A= defined $o{'mark'}B= $o{'mark'} == $i
ABresult
0X0
100
111
Truth table100%3/3
A && B
A= defined $o{'cursor'}B= $o{'cursor'} == $i
ABresult
0X0
100
111
218 14660 $line[$_] .= $pad . $art[$_] for 0 .. 2;
219 }
220 1189 return @line;
221 }
222  
223 39947 ▶sub tile_chip {
Subroutine tile_chip: called CC=6 SCAR=18.3
Pod documented
224 39947 my ($self, $kind, %o) = @_;
225 39947 my $face = $self->face($kind);
226 39947 ▶ return ($o{last} ? "<$face>" : "[$face]") unless $self->colour;
Branch: 2 branches
BranchTrueFalseTotal
unless $self->colour 39943 4 39947
$o{'last'} ? : 1 39942 39943
227 4 my $ink = $self->_ink($kind);
228 4 ▶ my $bg = $o{last} ? 223 : $FACE_BG;
Branch: 1 branch
BranchTrueFalseTotal
$o{'last'} ? : 0 4 4
229 4 ▶ return "\e[38;5;${ink}m\e[48;5;${bg}m" . ($o{last} ? '<' : '[') . $face . ($o{last} ? '>' : ']') . "\e[0m";
Branch: 2 branches
BranchTrueFalseTotal
$o{'last'} ? : 0 4 4
$o{'last'} ? : 0 4 4
230 }
231  
232 3243 ▶sub tile_chips {
Subroutine tile_chips: called CC=1 SCAR=0.0
Pod documented
233 3243 my ($self, @kinds) = @_;
234 3243 return join '', map { $self->tile_chip($_) } @kinds;
235 }
236  
237 4593 ▶sub tile {
Subroutine tile: called CC=2 SCAR=6.9
Pod documented
238 4593 my ($self, $kind) = @_;
239 4593 ▶ return $self->glyphs ? $GLYPH{$kind} : Game::Mahjong::Notation::print($kind);
Branch: 1 branch
BranchTrueFalseTotal
$self->glyphs ? : 4 4589 4593
240 }
241  
242 2 ▶sub tiles {
Subroutine tiles: called CC=3 SCAR=12.5
Pod documented
243 2 my ($self, @kinds) = @_;
244 2 ▶ return '' unless @kinds;
Branch: 1 branch
BranchTrueFalseTotal
unless @kinds 0 2 2
245 2 ▶ return $self->glyphs ? join('', map { $GLYPH{$_} } @kinds) : Game::Mahjong::Notation::print(@kinds);
Branch: 1 branch
BranchTrueFalseTotal
$self->glyphs ? : 0 2 2
246 }
247  
248 12558 ▶sub name_of {
Subroutine name_of: called CC=1 SCAR=0.0
Pod documented
249 12558 my ($self, $seat) = @_;
250 12558 return $self->names->[$seat];
251 }
252  
253 603 ▶sub _does {
Subroutine _does: called CC=2 SCAR=6.9
254 603 my ($self, $seat, $third, $second) = @_;
255 603 ▶ return $self->name_of($seat) . ' ' . ($seat == $self->seat ? $second : $third);
Branch: 1 branch
BranchTrueFalseTotal
$seat == $self->seat ? : 145 458 603
256 }
257  
258 27126 ▶sub say {
Subroutine say: called CC=1 SCAR=0.0
Pod documented
259 27126 my ($self, @text) = @_;
260 27126 my $out = $self->out;
261 27126 print {$out} @text, "\n";
262 27126 return;
263 }
264  
265 6412 ▶sub _on_turn {
Subroutine _on_turn: called CC=4 SCAR=13.9
266 6412 my ($self, $seat) = @_;
267 6412 my $turn = $self->rules->turn;
268 6412 ▶ return defined $turn && $self->rules->phase eq 'discard' && $turn == $seat ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $turn && $self->rules->phase eq 'discard' && $turn == $seat ? : 3251 3161 6412
Condition83%5/6
defined $turn and $self->rules->phase eq "discard" and $turn == $seat
!ll&&!rl&&r
531563251
defined $turn and $self->rules->phase eq "discard"
!ll&&!rl&&r
506407
MC/DC66%2/3
defined $turn and $self->rules->phase eq "discard" and $turn == $seat
defined $turn $self->rules->phase eq "discard" $turn == $seat
Truth table75%3/4
A and B and C
A= defined $turnB= $self->rules->phase eq "discard"C= $turn == $seat
ABCresult
0XX0
10X0
1100
1111
269 }
270  
271 5215 ▶sub _wind_of {
Subroutine _wind_of: called CC=1 SCAR=0.0
272 5215 my ($self, $seat) = @_;
273 5215 return $WIND{ $self->rules->seat_wind($seat) };
274 }
275  
276 1043 ▶sub show_table {
Subroutine show_table: called CC=16 SCAR=27.7
Pod documented
277 1043 my ($self) = @_;
278 1043 my $g = $self->rules;
279 1043 my $me = $self->seat;
280 1043 my $w = $g->window;
281  
282 1043 $self->_clear;
283 1043 $self->say('');
284 $self->say($self->_c('bold', sprintf 'Hand %d of %d %s %s round %s you are %s %s wall %d',
285 1043 $g->hand_no, Game::Mahjong::Result::HANDS, $DOT, $WIND{ $g->prevailing }, $DOT,
286 $self->_wind_of($me), $DOT, $g->wall->remaining));
287 $self->say('Totals ' . join(' ', map {
288 1043 ▶ sprintf '%s %s', $self->name_of($_),
Branch: 1 branch
BranchTrueFalseTotal
$g->totals->[$_] < 0 ? : 1918 2254 4172
289 $self->_c($g->totals->[$_] < 0 ? 'red' : 'green', sprintf '%+d', $g->totals->[$_])
290 } 0 .. 3));
291 1043 $self->say('');
292  
293 1043 for my $i (1 .. 3) {
294 3129 my $seat = ($me + $i) % 4;
295 3129 my $hand = $g->hand_of($seat);
296 3129 ▶ my $mark = $self->_on_turn($seat) ? $self->_c('yellow', '>') : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$self->_on_turn($seat) ? : 0 3129 3129
297 3129 ▶ my $dealer = $seat == $g->dealer ? ' (deals)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$seat == $g->dealer ? : 779 2350 3129
298 $self->say(sprintf '%s %-5s %-6s %2d tiles%s%s',
299 $mark, $self->name_of($seat), $self->_wind_of($seat), $hand->size, $dealer,
300 3129 ▶ (@{ $hand->flowers } ? ' flowers ' . $self->tile_chips(@{ $hand->flowers }) : ''));
Branch: 1 branch
BranchTrueFalseTotal
@{$hand->flowers;} ? : 2363 766 3129
301 3129 ▶ $self->say(' sets ' . $self->_melds_line($hand)) if @{ $hand->melds };
Branch: 1 branch
BranchTrueFalseTotal
if @{$hand->melds;} 246 2883 3129
302 3129 $self->say(' pool ' . $self->_pool($seat));
303 }
304  
305 1043 ▶ if ($w && $g->is_active) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w and $g->is_active) 1 1042 1043
Condition66%2/3
$w and $g->is_active
!ll&&!rl&&r
104201
MC/DC50%1/2
$w and $g->is_active
$w $g->is_active
Truth table66%2/3
A and B
A= $wB= $g->is_active
ABresult
0X0
100
111
306 1 $self->say('');
307 1 my @art = $self->tile_row([ $w->{tile} ]);
308 1 my $from = $self->name_of($w->{from});
309 1 ▶ my $verb = $g->phase eq 'rob' ? 'adds to a pung' : 'discards';
Branch: 1 branch
BranchTrueFalseTotal
$g->phase eq 'rob' ? : 0 1 1
310 1 $self->say(' ' . $art[0]);
311 1 $self->say(' ' . $art[1] . ' ' . $self->_c('yellow', "$from $verb this"));
312 1 $self->say(' ' . $art[2]);
313 }
314  
315 1043 my $mine = $g->hand_of($me);
316 1043 $self->say('');
317 $self->say($self->_c('bold', sprintf '%s %-5s %-6s%s%s',
318 ($self->_on_turn($me) ? $self->_c('yellow', '>') : ' '),
319 $self->name_of($me), $self->_wind_of($me),
320 ($me == $g->dealer ? ' (you deal)' : ''),
321 1043 ▶ (@{ $mine->flowers } ? ' flowers ' . $self->tile_chips(@{ $mine->flowers }) : '')));
Branch: 3 branches
BranchTrueFalseTotal
$self->_on_turn($me) ? : 1042 1 1043
$me == $g->dealer ? : 264 779 1043
@{$mine->flowers;} ? : 626 417 1043
322 1043 $self->say(' pool ' . $self->_pool($me));
323 1043 ▶ if (@{ $mine->melds }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$mine->melds;}) 98 945 1043
324 98 $self->say('');
325 98 $self->say(' Your sets:');
326 98 $self->say(' ' . $_) for $self->_melds_art($mine);
327 }
328 1043 $self->say('');
329 1043 $self->say(' Your hand:');
330 1043 $self->say(' ' . $_) for $self->_rack_art;
331 1043 return;
332 }
333  
334 98 ▶sub _melds_art {
Subroutine _melds_art: called CC=8 SCAR=20.8
335 98 my ($self, $hand) = @_;
336 98 my (@kinds, %backs, %gaps);
337 98 my $n = 0;
338 98 for my $meld (@{ $hand->melds }) {
339 109 ▶ $gaps{$n} = 1 if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 11 98 109
340 109 for my $t (@{ $meld->tiles }) { push @kinds, $t; $n++ }
341 }
342 98 ▶ return ('-') unless @kinds;
Branch: 1 branch
BranchTrueFalseTotal
unless @kinds 0 98 98
343 98 my @line = $self->tile_row(\@kinds, backs => \%backs, gaps => \%gaps);
344 98 my $label = '';
345 98 my $at = 0;
346 98 for my $meld (@{ $hand->melds }) {
347 109 my $width = 5 * scalar(@{ $meld->tiles }) - 1;
348 109 ▶ $label .= ' ' if $at;
Branch: 1 branch
BranchTrueFalseTotal
if $at 11 98 109
349 109 ▶ $label .= sprintf '%-*s', $width, $meld->kind . ($meld->concealed ? ' (concealed)' : '');
Branch: 1 branch
BranchTrueFalseTotal
$meld->concealed ? : 0 109 109
350 109 $at++;
351 }
352 98 return (@line, $label);
353 }
354  
355 246 ▶sub _melds_line {
Subroutine _melds_line: called CC=5 SCAR=16.2
356 246 my ($self, $hand) = @_;
357 246 my @out;
358 246 for my $meld (@{ $hand->melds }) {
359 push @out, $meld->concealed && $hand != $self->rules->hand_of($self->seat)
360 ? ($self->colour ? "\e[48;5;${BACK_BG}m\e[38;5;250m[##][##][##][##]\e[0m" : '[##][##][##][##]')
361 270 ▶ : $self->tile_chips(@{ $meld->tiles });
Branch: 2 branches
BranchTrueFalseTotal
$meld->concealed && $hand != $self->rules->hand_of($self->seat) ? : 16 254 270
$self->colour ? : 0 16 16
Condition66%2/3
$meld->concealed and $hand != $self->rules->hand_of($self->seat)
!ll&&!rl&&r
254016
MC/DC50%1/2
$meld->concealed and $hand != $self->rules->hand_of($self->seat)
$meld->concealed $hand != $self->rules->hand_of($self->seat)
Truth table66%2/3
A and B
A= $meld->concealedB= $hand != $self->rules->hand_of($self->seat)
ABresult
0X0
100
111
362 }
363 246 return join ' ', @out;
364 }
365  
366 1158 ▶sub rack_order {
Subroutine rack_order: called CC=4 SCAR=13.9
Pod documented
367 1158 my ($self) = @_;
368 1158 my $g = $self->rules;
369 1158 my @kinds = $g->hand_of($self->seat)->tiles;
370  
371 1158 ▶ my $drawn = $self->_on_turn($self->seat) ? $g->drawn : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_on_turn($self->seat) ? : 1157 1 1158
372 1158 ▶ return (\@kinds, undef) unless defined $drawn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $drawn 42 1116 1158
373  
374 1116 my ($at) = grep { $kinds[$_] == $drawn } 0 .. $#kinds;
375 1116 ▶ return (\@kinds, undef) unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 0 1116 1116
376  
377 1116 splice @kinds, $at, 1;
378 1116 push @kinds, $drawn;
379 1116 return (\@kinds, $#kinds);
380 }
381  
382 1043 ▶sub _rack_art {
Subroutine _rack_art: called CC=7 SCAR=19.5
383 1043 my ($self) = @_;
384 1043 my ($kinds, $last) = $self->rack_order;
385 1043 my @kinds = @$kinds;
386 1043 my %gaps;
387 1043 ▶ $gaps{$last} = 1 if defined $last;
Branch: 1 branch
BranchTrueFalseTotal
if defined $last 1002 41 1043
388 1043 my @line = $self->tile_row(\@kinds, gaps => \%gaps,
389 mark => $last, cursor => $self->cursor);
390 1043 my $numbers = '';
391 1043 for my $i (0 .. $#kinds) {
392 14274 ▶ $numbers .= $gaps{$i} ? ' ' : ($i ? ' ' : '');
Branch: 2 branches
BranchTrueFalseTotal
$gaps{$i} ? : 1002 13272 14274
$i ? : 12229 1043 13272
393 14274 $numbers .= sprintf ' %-3s', $i + 1;
394 }
395 1043 ▶ $numbers = substr($numbers, 0, length($numbers) - 1) if length $numbers;
Branch: 1 branch
BranchTrueFalseTotal
if length $numbers 1043 0 1043
396 1043 push @line, $numbers;
397 1043 ▶ push @line, $self->_c('dim', sprintf ' %s the tile you just drew',
Branch: 1 branch
BranchTrueFalseTotal
if defined $last 1002 41 1043
398 $self->_caret_under($last, \%gaps))
399 if defined $last;
400 1043 return @line;
401 }
402  
403 1002 ▶sub _caret_under {
Subroutine _caret_under: called CC=4 SCAR=13.9
404 1002 my ($self, $i, $gaps) = @_;
405 1002 my $col = 0;
406 1002 ▶ for my $n (0 .. $i) { $col += ($gaps->{$n} ? 3 : ($n ? 1 : 0)) + 4 }
Branch: 2 branches
BranchTrueFalseTotal
$gaps->{$n} ? : 1002 12753 13755
$n ? : 11751 1002 12753
407 1002 return (' ' x ($col - 3)) . '^';
408 }
409  
410 4172 ▶sub _pool {
Subroutine _pool: called CC=5 SCAR=16.1
411 4172 my ($self, $seat) = @_;
412 4172 my @p = @{ $self->rules->pool_of($seat) };
413 4172 ▶ return $self->_c('dim', 'nothing thrown yet') unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 200 3972 4172
414 3972 my $w = $self->rules->window;
415 3972 ▶ my $lit = ($w && $w->{from} == $seat) ? $#p : -1;
Branch: 1 branch
BranchTrueFalseTotal
$w && $w->{'from'} == $seat ? : 1 3971 3972
Condition100%3/3
$w and $w->{"from"} == $seat
!ll&&!rl&&r
396831
MC/DC100%2/2
$w and $w->{"from"} == $seat
$w $w->{"from"} == $seat
Truth table100%3/3
A and B
A= $wB= $w->{"from"} == $seat
ABresult
0X0
100
111
416 3972 my @rows;
417 3972 my $i = 0;
418 3972 my @chips = map { $self->tile_chip($_, last => ($i++ == $lit)) } @p;
419 3972 ▶ while (@chips) { push @rows, join '', splice @chips, 0, 12 }
Branch: 1 branch
BranchTrueFalseTotal
if (@chips) 4991 3972 8963
420 3972 return join "\n ", @rows;
421 }
422  
423 64 ▶sub show_hand_end {
Subroutine show_hand_end: called CC=12 SCAR=25.0
Pod documented
424 64 my ($self, $e) = @_;
425 64 $self->say('');
426 64 ▶ if (!defined $e->{winner}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $e->{'winner'}) { } 17 47 64
427 17 $self->say($self->_c('bold', 'The wall is exhausted: nobody wins this hand.'));
428 }
429 else {
430 47 my $who = $self->name_of($e->{winner});
431 47 ▶ my $how = $e->{by} eq 'self' ? 'self-drawn' : $e->{by} eq 'rob' ? 'robbing the kong of ' . $self->name_of($e->{from}) : 'off ' . $self->name_of($e->{from}) . "'s discard";
Branch: 2 branches
BranchTrueFalseTotal
$e->{'by'} eq 'self' ? : 10 37 47
$e->{'by'} eq 'rob' ? : 0 37 37
432 47 ▶ if (defined $e->{tile}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $e->{'tile'}) { } 47 0 47
433 47 my @art = $self->tile_row([ $e->{tile} ]);
434 47 $self->say(' ' . $art[0]);
435 47 $self->say(' ' . $art[1] . ' ' . $self->_c('bold', sprintf '%s, %s.', $who, $how));
436 47 $self->say(' ' . $art[2]);
437 }
438 else {
439 0 $self->say($self->_c('bold', sprintf '%s with the dealt tiles, a heavenly hand.', $who));
440 }
441 47 for my $f (@{ $e->{fans} }) {
442 242 my $fan = Game::Mahjong::Fans::by_key($f->{key});
443 242 ▶ $self->say(sprintf ' %-32s %3d%s', $fan->name, $f->{points} * $f->{times}, $f->{times} > 1 ? " ($f->{times} x $f->{points})" : '');
Branch: 1 branch
BranchTrueFalseTotal
$f->{'times'} > 1 ? : 24 218 242
444 }
445 47 $self->say(sprintf ' %-32s %3d', $self->_c('bold', 'Total'), $e->{points});
446 47 ▶ $self->say('Settlement: ' . join(', ', map { sprintf '%s %s', $self->name_of($_), $self->_c($e->{deltas}[$_] < 0 ? 'red' : 'green', ($e->{deltas}[$_] >= 0 ? '+' : '') . $e->{deltas}[$_]) } 0 .. 3));
Branch: 2 branches
BranchTrueFalseTotal
$e->{'deltas'}[$_] < 0 ? : 141 47 188
$e->{'deltas'}[$_] >= 0 ? : 47 141 188
447 }
448 64 for my $seat (0 .. 3) {
449 256 ▶ my @k = @{ $e->{concealed_kongs}[$seat] || [] };
Condition50%1/2
$e->{'concealed_kongs'}[$seat] || []
l!l
2560
MC/DC0%0/1
$e->{'concealed_kongs'}[$seat] || []
$e->{'concealed_kongs'}[$seat]
Truth table50%1/2
A
A= $e->{'concealed_kongs'}[$seat]
Aresult
11
00
450 256 $self->say(sprintf '%s a concealed kong of %s.', $self->_does($seat, 'shows', 'show'), $self->tiles(($_) x 4)) for @k;
451 }
452 64 $self->say('Totals now: ' . join(' ', map { sprintf '%s %d', $self->name_of($_), $e->{totals}[$_] } 0 .. 3));
453 64 return;
454 }
455  
456 4 ▶sub show_game_end {
Subroutine show_game_end: called CC=3 SCAR=11.0
Pod documented
457 4 my ($self, $e) = @_;
458 4 $self->finished(1);
459 4 $self->winner($e->{winner});
460 4 $self->say('');
461 4 $self->say($self->_c('bold', 'The game is over after sixteen hands.'));
462 4 my @order = sort { $e->{places}[$a] <=> $e->{places}[$b] } 0 .. 3;
463 4 $self->say(sprintf ' %d. %-5s %5d', $e->{places}[$_], $self->name_of($_), $e->{totals}[$_]) for @order;
464 4 ▶ $self->say(defined $e->{winner} ? $self->_does($e->{winner}, 'wins', 'win') . ' the game.' : 'A shared first place: the game is drawn.');
Branch: 1 branch
BranchTrueFalseTotal
defined $e->{'winner'} ? : 4 0 4
465 4 return;
466 }
467  
468 5432 ▶sub narrate {
Subroutine narrate: called CC=18 SCAR=28.9
Pod documented
469 5432 my ($self, @events) = @_;
470 5432 for my $e (@events) {
471 10508 my $k = $e->{kind};
472 10508 my $actor = $e->{actor};
473 10508 ▶ if ($k eq 'deal') { $self->say($self->_c('dim', sprintf 'Hand %d. %s.', $e->{hand}, $self->_does($e->{dealer}, 'deals', 'deal'))) }
Branch: 8 branches
BranchTrueFalseTotal
if ($k eq 'deal') { } 73 10435 10508
elsif ($k eq 'flower') { } 473 9962 10435
elsif ($k eq 'drew' and $e->{'seat'} == $self->seat and $self->_on_turn($self->seat) and defined $self->rules->drawn) { } 1028 8934 9962
elsif ($k eq 'discard' and $actor ne $self->seat) { } 3011 5923 8934
elsif ($k eq 'claimed') { } 88 5835 5923
elsif ($k eq 'kong' and $actor ne $self->seat) { } 3 5832 5835
elsif ($k eq 'hand_end') { } 64 5768 5832
elsif ($k eq 'game_end') { } 4 5764 5768
474 473 ▶ elsif ($k eq 'flower') { $self->say(sprintf '%s a flower, %s, and %s again.', $self->_does($e->{seat}, 'exposes', 'expose'), $self->tile($e->{tile}), $e->{seat} == $self->seat ? 'draw' : 'draws') }
Branch: 1 branch
BranchTrueFalseTotal
$e->{'seat'} == $self->seat ? : 105 368 473
475 ▶ elsif ($k eq 'drew' && $e->{seat} == $self->seat && $self->_on_turn($self->seat) && defined $self->rules->drawn) {
Condition100%9/9
$k eq "drew" and $e->{"seat"} == $self->seat and $self->_on_turn($self->seat) and defined $self->rules->drawn
!ll&&!rl&&r
8910241028
$k eq "drew" and $e->{"seat"} == $self->seat and $self->_on_turn($self->seat)
!ll&&!rl&&r
8880301052
$k eq "drew" and $e->{"seat"} == $self->seat
!ll&&!rl&&r
557533051082
MC/DC100%4/4
$k eq "drew" and $e->{"seat"} == $self->seat and $self->_on_turn($self->seat) and defined $self->rules->drawn
$k eq "drew" $e->{"seat"} == $self->seat $self->_on_turn($self->seat) defined $self->rules->drawn
Truth table100%5/5
A and B and C and D
A= $k eq "drew"B= $e->{"seat"} == $self->seatC= $self->_on_turn($self->seat)D= defined $self->rules->drawn
ABCDresult
0XXX0
10XX0
110X0
11100
11111
476 1028 ▶ $self->say(sprintf 'You draw %s from the %s.', $self->tile($self->rules->drawn), $e->{from} eq 'back' ? 'back of the wall' : 'wall');
Branch: 1 branch
BranchTrueFalseTotal
$e->{'from'} eq 'back' ? : 52 976 1028
477 }
478 3011 ▶ elsif ($k eq 'discard' && $actor ne $self->seat) { $self->say(sprintf '%s discards %s.', $self->name_of($actor), $self->tile($e->{tile})) }
Condition100%3/3
$k eq "discard" and $actor ne $self->seat
!ll&&!rl&&r
491810053011
MC/DC100%2/2
$k eq "discard" and $actor ne $self->seat
$k eq "discard" $actor ne $self->seat
Truth table100%3/3
A and B
A= $k eq "discard"B= $actor ne $self->seat
ABresult
0X0
100
111
479 elsif ($k eq 'claimed') {
480 88 ▶ next if $e->{meld} eq 'win';
Branch: 1 branch
BranchTrueFalseTotal
if $e->{"meld"} eq "win" 37 51 88
481 51 $self->say(sprintf '%s %s from %s for a %s.', $self->_does($e->{seat}, 'claims', 'claim'), $self->tile($e->{tile}), $self->name_of($e->{from}), $e->{meld});
482 }
483 3 ▶ elsif ($k eq 'kong' && $actor ne $self->seat) { $self->say(sprintf '%s declares a %s kong of %s.', $self->name_of($actor), $e->{how}, $self->tile($e->{tile})) }
Condition100%3/3
$k eq "kong" and $actor ne $self->seat
!ll&&!rl&&r
583113
MC/DC100%2/2
$k eq "kong" and $actor ne $self->seat
$k eq "kong" $actor ne $self->seat
Truth table100%3/3
A and B
A= $k eq "kong"B= $actor ne $self->seat
ABresult
0X0
100
111
484 64 elsif ($k eq 'hand_end') { $self->show_hand_end($e) }
485 4 elsif ($k eq 'game_end') { $self->show_game_end($e) }
486 }
487 5432 return;
488 }
489  
490 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod documented
491 1 my ($self) = @_;
492 1 $self->say('Commands:');
493 1 $self->say(' <n> discard the tile numbered n in your hand');
494 1 $self->say(' d <tile> discard a tile by name, e.g. d 5m, d E, d B (white dragon)');
495 1 $self->say(' kong <tile> declare a kong (four in hand, or the fourth for your exposed pung)');
496 1 $self->say(' win declare your hand complete');
497 1 $self->say(' pass | pung | kong | chow <n> answer a discard (chow <n> picks the shape offered)');
498 1 $self->say(' hint what the strongest bot would do');
499 1 $self->say(' table show the table again');
500 1 $self->say(' fans list the eighty-one scoring elements and their points');
501 1 $self->say(' help this list');
502 1 $self->say(' quit leave the game');
503 1 return;
504 }
505  
506 1 ▶sub show_fans {
Subroutine show_fans: called CC=2 SCAR=6.9
Pod documented
507 1 my ($self) = @_;
508 1 for my $fan (Game::Mahjong::Fans::all()) {
509 81 $self->say(sprintf '%2d %-36s %2d', $fan->n, $fan->name, $fan->points);
510 }
511 1 return;
512 }
513  
514 36 ▶sub _describe_legal {
Subroutine _describe_legal: called CC=7 SCAR=20.6
515 36 my ($self, @legal) = @_;
516 36 my $g = $self->rules;
517 36 ▶ if ($g->phase ne 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->phase ne "discard") 1 35 36
518 1 my $w = $g->window;
519 1 my @lines;
520 1 my $n = 0;
521 1 for my $m (@legal) {
522 2 ▶ my $text = $m->{kind} eq 'chow' ? sprintf('chow %d (%s)', ++$n, $self->tiles(@{ $m->{tiles} })) : $m->{kind};
Branch: 1 branch
BranchTrueFalseTotal
$m->{'kind'} eq 'chow' ? : 0 2 2
523 2 push @lines, $text;
524 }
525 1 ▶ $self->say(sprintf '%s %s %s. You may: %s', $self->name_of($w->{from}), ($g->phase eq 'rob' ? 'adds to a pung' : 'discards'), $self->tile($w->{tile}), join(', ', @lines));
Branch: 1 branch
BranchTrueFalseTotal
$g->phase eq 'rob' ? : 0 1 1
526 1 return;
527 }
528 35 my @extra = grep { $_->{kind} ne 'discard' } @legal;
529 35 ▶ $self->say('You may also: ' . join(', ', map { $_->{kind} eq 'kong' ? 'kong ' . $self->tile($_->{tile}) : $_->{kind} } @extra)) if @extra;
Branch: 2 branches
BranchTrueFalseTotal
if @extra 0 35 35
$_->{'kind'} eq 'kong' ? : 0 0 0
530 35 return;
531 }
532  
533 9 ▶sub picking {
Subroutine picking: called CC=6 SCAR=22.7
Pod documented
534 9 my ($self, @set) = @_;
535 9 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 9 9
$set[0] ? : 0 0 0
536 9 ▶ return 0 if $self->auto;
Branch: 1 branch
BranchTrueFalseTotal
if $self->auto 0 9 9
537 9 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 0 9 9
$self->_picking ? : 0 0 0
538 9 return $self->keys_available;
539 }
540  
541 9 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
542 9 my ($self) = @_;
543 9 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 9 9
544 9 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 9 0 9
545 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
546 }
547  
548 13 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=24.2
Pod documented
549 13 my ($self) = @_;
550 13 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 13 13
551  
552 13 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 13 0 13
553 13 $self->raw(1);
554 13 return $self;
555 }
556  
557 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
558 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
559  
560 0 $self->raw(1);
561 0 return $self;
562 }
563  
564 0 ▶sub leave_raw {
Subroutine leave_raw: not called CC=3 SCAR=24.8
Pod documented
565 0 my ($self) = @_;
566 0 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 0 0
567 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
568 0 $self->raw(0);
569 0 return $self;
570 }
571  
572 86 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
573 86 my ($self, $wait) = @_;
574  
575 86 my $pending = $self->pending;
576 86 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 84 86
577 84 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 84 0 84
578  
579 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
580 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
581  
582 0 select undef, undef, undef, 0.05;
583 0 return Term::ReadKey::ReadKey(-1, $self->in);
584 }
585  
586 44 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
587 44 my ($self) = @_;
588 44 my $char = $self->read_char(1);
589 44 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 1 43 44
590 43 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Mahjong::Terminal::CONTROL{$char} 17 26 43
591 26 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 21 5 26
592 5 return $char;
593 }
594  
595 21 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
596 21 my ($self) = @_;
597  
598 21 my $opener = $self->read_char(0);
599 21 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 21 21
600  
601 21 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 19 21
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
1812
MC/DC100%2/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table100%3/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
602 2 unshift @{ $self->pending }, $opener;
603 2 return 'escape';
604 }
605  
606 19 my $tail = '';
607 19 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 21 0 21
608 21 my $char = $self->read_char(0);
609 21 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 21 21
610 21 $tail .= $char;
611 21 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 19 2 21
612 }
613  
614 19 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Mahjong::Terminal::SEQUENCE{$tail} || 'escape'
l!l
190
MC/DC0%0/1
$Game::Mahjong::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Mahjong::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Mahjong::Terminal::SEQUENCE{$tail}
Aresult
11
00
615 }
616  
617 25 ▶sub throw_line {
Subroutine throw_line: called CC=6 SCAR=18.4
Pod documented
618 25 my ($self, $kind) = @_;
619  
620 25 my $hand = $self->rules->hand_of($self->seat);
621 25 my $left = Game::Mahjong::Shanten::after_discard($hand, $kind);
622  
623 25 ▶ my $line = sprintf 'throw %s and you are %s',
Branch: 2 branches
BranchTrueFalseTotal
$left < 0 ? : 0 25 25
$left == 0 ? : 0 25 25
624 $self->tile($kind),
625 $left < 0 ? 'out' : $left == 0 ? 'waiting' : "$left away";
626  
627 25 my $rest = $hand->clone;
628 25 $rest->remove($kind);
629 25 my @help = Game::Mahjong::Shanten::ukeire($rest);
630 25 ▶ $line .= sprintf ', %d %s help', scalar @help,
Branch: 2 branches
BranchTrueFalseTotal
if @help 25 0 25
@help == 1 ? : 0 25 25
631 @help == 1 ? 'kind would' : 'kinds would' if @help;
632  
633 25 ▶ $line .= ', a step backwards'
Branch: 1 branch
BranchTrueFalseTotal
if $left > Game::Mahjong::Shanten::shanten($hand) 11 14 25
634 if $left > Game::Mahjong::Shanten::shanten($hand);
635  
636 25 return $line;
637 }
638  
639 13 ▶sub pick_discard {
Subroutine pick_discard: called CC=28 SCAR=50.6
Pod documented
640 13 my ($self, @legal) = @_;
641  
642 13 my ($kinds) = $self->rack_order;
643 153 my %throwable = map { $_->{tile} => 1 }
644 13 grep { $_->{kind} eq 'discard' } @legal;
645 13 my @slot = grep { $throwable{ $kinds->[$_] } } 0 .. $#$kinds;
646 13 ▶ return undef unless @slot;
Branch: 1 branch
BranchTrueFalseTotal
unless @slot 0 13 13
647  
648 13 my $at = $#slot;
649 13 my @notice;
650  
651 13 while (1) {
652 25 $self->show_pick(\@slot, $at, \@notice, \@legal);
653 25 @notice = ();
654  
655 25 my $key = $self->read_key;
656 25 ▶ if (!defined $key || $key eq 'eof' || $key eq 'interrupt' || $key eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q") 1 24 25
Condition44%4/9
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
l!l&&r!l&&!r
0124
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
0025
not defined $key or $key eq "eof"
l!l&&r!l&&!r
0025
MC/DC25%1/4
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
not defined $key $key eq "eof" $key eq "interrupt" $key eq "q"
Truth table40%2/5
not(A) or B or C or D
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"D= $key eq "q"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
657 1 $self->quit(1);
658 1 return undef;
659 }
660  
661 24 ▶ if ($key eq 'left' || $key eq 'down') { $at = ($at - 1) % @slot; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "left" or $key eq "down") 9 15 24
Condition66%2/3
$key eq "left" or $key eq "down"
l!l&&r!l&&!r
9015
MC/DC50%1/2
$key eq "left" or $key eq "down"
$key eq "left" $key eq "down"
Truth table66%2/3
A or B
A= $key eq "left"B= $key eq "down"
ABresult
1X1
011
000
662 15 ▶ if ($key eq 'right' || $key eq 'up' || $key eq 'tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "right" or $key eq "up" or $key eq "tab") 3 12 15
Condition66%4/6
$key eq "right" or $key eq "up" or $key eq "tab"
l!l&&r!l&&!r
3012
$key eq "right" or $key eq "up"
l!l&&r!l&&!r
3012
MC/DC33%1/3
$key eq "right" or $key eq "up" or $key eq "tab"
$key eq "right" $key eq "up" $key eq "tab"
Truth table50%2/4
A or B or C
A= $key eq "right"B= $key eq "up"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
663 3 $at = ($at + 1) % @slot;
664 3 next;
665 }
666 12 ▶ if ($key eq 'home' || $key eq 'page_up') { $at = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home" or $key eq "page_up") 0 12 12
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
0012
MC/DC0%0/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table33%1/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
667 12 ▶ if ($key eq 'end' || $key eq 'page_down') { $at = $#slot; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end" or $key eq "page_down") 0 12 12
Condition33%1/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
0012
MC/DC0%0/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table33%1/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
668  
669 12 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 12 0 12
Condition33%1/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
1200
MC/DC0%0/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table33%1/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
670 12 my $t = $kinds->[ $slot[$at] ];
671 12 ▶ my ($m) = grep { $_->{kind} eq 'discard' && $_->{tile} == $t } @legal;
Condition66%2/3
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
!ll&&!rl&&r
012812
MC/DC50%1/2
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
$_->{'kind'} eq 'discard' $_->{'tile'} == $t
Truth table66%2/3
A && B
A= $_->{'kind'} eq 'discard'B= $_->{'tile'} == $t
ABresult
0X0
100
111
672 12 ▶ return $m if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 12 0 12
673 0 @notice = ('you cannot throw that one');
674 0 next;
675 }
676  
677 0 ▶ if ($key =~ /\A[1-9]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-9]\z/) 0 0 0
678 0 my ($found) = grep { $slot[$_] == $key - 1 } 0 .. $#slot;
679 0 ▶ if (defined $found) { $at = $found; next }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $found) 0 0 0
680 0 @notice = ('no tile is numbered ' . $key . ' here');
681 0 next;
682 }
683  
684 0 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 0 0 0
685 0 my $m = $self->hinter->hint($self->rules, $self->seat);
686 0 ▶ if ($m && $m->{kind} eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m and $m->{"kind"} eq "discard") 0 0 0
Condition0%0/3
$m and $m->{"kind"} eq "discard"
!ll&&!rl&&r
000
MC/DC0%0/2
$m and $m->{"kind"} eq "discard"
$m $m->{"kind"} eq "discard"
Truth table0%0/3
A and B
A= $mB= $m->{"kind"} eq "discard"
ABresult
0X0
100
111
687 0 my ($found) = grep { $kinds->[ $slot[$_] ] == $m->{tile} }
688 0 .. $#slot;
689 0 ▶ $at = $found if defined $found;
Branch: 1 branch
BranchTrueFalseTotal
if defined $found 0 0 0
690 }
691 0 ▶ @notice = ('Hint: ' . $self->_move_text($m)) if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 0 0 0
692 0 next;
693 }
694  
695 0 ▶ if ($key eq 't') { $self->picking(0); return $self->read_move(@legal) }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t") 0 0 0
696  
697 0 ▶ if ($key eq '?') { @notice = $self->pick_help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
698  
699 0 @notice = ('that key does nothing here. ? for the ones that do');
700 }
701 }
702  
703 0 ▶sub pick_help {
Subroutine pick_help: not called CC=1 SCAR=6.9
Pod documented
704 return (
705 0 'left and right walk your rack. the tile under the cursor is the one',
706 'you would throw, and the line under it says what that leaves you.',
707 'enter throws it, a number jumps to that tile, h is the hint,',
708 't goes back to typing, q stops.',
709 );
710 }
711  
712 25 ▶sub show_pick {
Subroutine show_pick: called CC=2 SCAR=6.9
Pod documented
713 25 my ($self, $slot, $at, $notice, $legal) = @_;
714  
715 25 my ($kinds) = $self->rack_order;
716 25 $self->cursor($slot->[$at]);
717 25 $self->show_table;
718 25 $self->cursor(undef);
719  
720 25 $self->say(' ' . $self->throw_line($kinds->[ $slot->[$at] ]));
721 25 $self->_describe_legal(@$legal);
722 25 $self->say(' ' . $self->_c('dim', $_)) for @$notice;
723 25 $self->say(' ' . $self->_c('dim', 'arrows to choose, enter to throw, ? for the keys'));
724 25 return;
725 }
726  
727 10 ▶sub read_move {
Subroutine read_move: called CC=17 SCAR=29.4
Pod documented
728 10 my ($self, @legal) = @_;
729 10 my $g = $self->rules;
730 10 my $in = $self->in;
731 10 my $hand = $g->hand_of($self->seat);
732  
733 10 ▶ if ($g->phase eq 'discard' && $self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->phase eq "discard" and $self->picking) 0 10 10
Condition66%2/3
$g->phase eq "discard" and $self->picking
!ll&&!rl&&r
190
MC/DC0%0/2
$g->phase eq "discard" and $self->picking
$g->phase eq "discard" $self->picking
Truth table66%2/3
A and B
A= $g->phase eq "discard"B= $self->picking
ABresult
0X0
100
111
734 0 ▶ if ($self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->enter_raw) 0 0 0
735 0 my $move = $self->pick_discard(@legal);
736 0 $self->leave_raw;
737 0 return $move;
738 }
739 0 $self->picking(0);
740 }
741  
742 10 while (1) {
743 17 my $out = $self->out;
744 17 print {$out} '> ';
745 17 my $line = <$in>;
746 17 ▶ unless (defined $line) { $self->quit(1); return undef }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 1 16 17
747 16 $line =~ s/\A\s+|\s+\z//g;
748 16 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 16 16
749 16 my ($cmd, @args) = split ' ', lc $line;
750 16 ▶ if ($cmd eq 'quit' || $cmd eq 'q') { $self->quit(1); return undef }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "quit" or $cmd eq "q") 8 8 16
Condition66%2/3
$cmd eq "quit" or $cmd eq "q"
l!l&&r!l&&!r
808
MC/DC50%1/2
$cmd eq "quit" or $cmd eq "q"
$cmd eq "quit" $cmd eq "q"
Truth table66%2/3
A or B
A= $cmd eq "quit"B= $cmd eq "q"
ABresult
1X1
011
000
751 8 ▶ if ($cmd eq 'help' || $cmd eq '?') { $self->help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "help" or $cmd eq "?") 1 7 8
Condition66%2/3
$cmd eq "help" or $cmd eq "?"
l!l&&r!l&&!r
107
MC/DC50%1/2
$cmd eq "help" or $cmd eq "?"
$cmd eq "help" $cmd eq "?"
Truth table66%2/3
A or B
A= $cmd eq "help"B= $cmd eq "?"
ABresult
1X1
011
000
752 7 ▶ if ($cmd eq 'table' || $cmd eq 't') { $self->show_table; $self->_describe_legal(@legal); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "table" or $cmd eq "t") 1 6 7
Condition66%2/3
$cmd eq "table" or $cmd eq "t"
l!l&&r!l&&!r
106
MC/DC50%1/2
$cmd eq "table" or $cmd eq "t"
$cmd eq "table" $cmd eq "t"
Truth table66%2/3
A or B
A= $cmd eq "table"B= $cmd eq "t"
ABresult
1X1
011
000
753 6 ▶ if ($cmd eq 'fans') { $self->show_fans; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "fans") 1 5 6
754 5 ▶ if ($cmd eq 'hint' || $cmd eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "hint" or $cmd eq "h") 1 4 5
Condition66%2/3
$cmd eq "hint" or $cmd eq "h"
l!l&&r!l&&!r
104
MC/DC50%1/2
$cmd eq "hint" or $cmd eq "h"
$cmd eq "hint" $cmd eq "h"
Truth table66%2/3
A or B
A= $cmd eq "hint"B= $cmd eq "h"
ABresult
1X1
011
000
755 1 my $m = $self->hinter->hint($g, $self->seat);
756 1 ▶ $self->say('Hint: ' . $self->_move_text($m)) if $m;
Branch: 1 branch
BranchTrueFalseTotal
if $m 1 0 1
757 1 next;
758 }
759 4 my $move = $self->_parse($cmd, \@args, \@legal, $hand);
760 4 ▶ unless ($move) { $self->say('Not a move here. Type help for the commands.'); next }
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 3 1 4
761 1 return $move;
762 }
763 }
764  
765 1 ▶sub _move_text {
Subroutine _move_text: called CC=5 SCAR=28.8
766 1 my ($self, $m) = @_;
767 1 ▶ return 'discard ' . $self->tile($m->{tile}) if $m->{kind} eq 'discard';
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "discard" 1 0 1
768 0 ▶ return 'kong ' . $self->tile($m->{tile}) if $m->{kind} eq 'kong' && $m->{tile};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "kong" and $m->{"tile"} 0 0 0
Condition0%0/3
$m->{"kind"} eq "kong" and $m->{"tile"}
!ll&&!rl&&r
000
MC/DC0%0/2
$m->{"kind"} eq "kong" and $m->{"tile"}
$m->{"kind"} eq "kong" $m->{"tile"}
Truth table0%0/3
A and B
A= $m->{"kind"} eq "kong"B= $m->{"tile"}
ABresult
0X0
100
111
769 0 ▶ return 'chow with ' . $self->tiles(@{ $m->{tiles} }) if $m->{kind} eq 'chow';
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "chow" 0 0 0
770 0 return $m->{kind};
771 }
772  
773 15 ▶sub _parse_tile {
Subroutine _parse_tile: called CC=4 SCAR=13.9
774 15 my ($self, $text) = @_;
775 15 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 15 15
776 15 my $t = $text;
777 15 $t =~ s/\A([1-9])([mps])\z/$1$2/;
778 15 ▶ $t = uc $t if $t =~ /\A[eswnrgb]\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $t =~ /\A[eswnrgb]\z/ 1 14 15
779 15 my @ids = eval { Game::Mahjong::Notation::parse($t) };
780 15 ▶ return @ids == 1 ? $ids[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@ids == 1 ? : 14 1 15
781 }
782  
783 74 ▶sub _parse {
Subroutine _parse: called CC=21 SCAR=47.6
784 74 my ($self, $cmd, $args, $legal, $hand) = @_;
785 74 my $g = $self->rules;
786 74 ▶ if ($g->phase eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->phase eq "discard") 74 0 74
787 74 ▶ if ($cmd =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\A\d+\z/) 71 3 74
788 71 my ($kinds) = $self->rack_order;
789 71 ▶ my $t = $cmd >= 1 ? $kinds->[ $cmd - 1 ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cmd >= 1 ? : 71 0 71
790 71 ▶ return undef unless $t;
Branch: 1 branch
BranchTrueFalseTotal
unless $t 0 71 71
791 71 ▶ my ($m) = grep { $_->{kind} eq 'discard' && $_->{tile} == $t } @$legal;
Condition66%2/3
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
!ll&&!rl&&r
074071
MC/DC50%1/2
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
$_->{'kind'} eq 'discard' $_->{'tile'} == $t
Truth table66%2/3
A && B
A= $_->{'kind'} eq 'discard'B= $_->{'tile'} == $t
ABresult
0X0
100
111
792 71 return $m;
793 }
794 3 ▶ if ($cmd eq 'd' || $cmd eq 'discard') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "d" or $cmd eq "discard") 2 1 3
Condition66%2/3
$cmd eq "d" or $cmd eq "discard"
l!l&&r!l&&!r
201
MC/DC50%1/2
$cmd eq "d" or $cmd eq "discard"
$cmd eq "d" $cmd eq "discard"
Truth table66%2/3
A or B
A= $cmd eq "d"B= $cmd eq "discard"
ABresult
1X1
011
000
795 2 ▶ my $t = $self->_parse_tile($args->[0]) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->_parse_tile($args->[0]) or return undef 1 1 2
796 1 ▶ my ($m) = grep { $_->{kind} eq 'discard' && $_->{tile} == $t } @$legal;
Condition33%1/3
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
!ll&&!rl&&r
0110
MC/DC0%0/2
$_->{'kind'} eq 'discard' && $_->{'tile'} == $t
$_->{'kind'} eq 'discard' $_->{'tile'} == $t
Truth table33%1/3
A && B
A= $_->{'kind'} eq 'discard'B= $_->{'tile'} == $t
ABresult
0X0
100
111
797 1 return $m;
798 }
799 1 ▶ if ($cmd eq 'kong') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "kong") 0 1 1
800 0 my @k = grep { $_->{kind} eq 'kong' } @$legal;
801 0 ▶ return $k[0] if @k == 1 && !@$args;
Branch: 1 branch
BranchTrueFalseTotal
if @k == 1 and not @$args 0 0 0
Condition0%0/3
@k == 1 and not @$args
!ll&&!rl&&r
000
MC/DC0%0/2
@k == 1 and not @$args
@k == 1 not @$args
Truth table0%0/3
A and not(B)
A= @k == 1B= not @$args
ABresult
0X0
100
111
802 0 ▶ my $t = $self->_parse_tile($args->[0]) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $self->_parse_tile($args->[0]) or return undef 0 0 0
803 0 my ($m) = grep { $_->{tile} == $t } @k;
804 0 return $m;
805 }
806 1 ▶ if ($cmd eq 'win') { my ($m) = grep { $_->{kind} eq 'win' } @$legal; return $m }
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "win") 0 1 1
807 1 return undef;
808 }
809 0 ▶ if ($cmd eq 'chow') {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "chow") 0 0 0
810 0 my @c = grep { $_->{kind} eq 'chow' } @$legal;
811 0 ▶ return $c[0] if @c == 1 && !@$args;
Branch: 1 branch
BranchTrueFalseTotal
if @c == 1 and not @$args 0 0 0
Condition0%0/3
@c == 1 and not @$args
!ll&&!rl&&r
000
MC/DC0%0/2
@c == 1 and not @$args
@c == 1 not @$args
Truth table0%0/3
A and not(B)
A= @c == 1B= not @$args
ABresult
0X0
100
111
812 0 my $n = $args->[0];
813 0 ▶ return undef unless defined $n && $n =~ /\A\d+\z/ && $c[ $n - 1 ];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n and $n =~ /\A\d+\z/ and $c[$n - 1] 0 0 0
Condition0%0/6
defined $n and $n =~ /\A\d+\z/ and $c[$n - 1]
!ll&&!rl&&r
000
defined $n and $n =~ /\A\d+\z/
!ll&&!rl&&r
000
MC/DC0%0/3
defined $n and $n =~ /\A\d+\z/ and $c[$n - 1]
defined $n $n =~ /\A\d+\z/ $c[$n - 1]
Truth table0%0/4
A and B and C
A= defined $nB= $n =~ /\A\d+\z/C= $c[$n - 1]
ABCresult
0XX0
10X0
1100
1111
814 0 return $c[ $n - 1 ];
815 }
816 0 my ($m) = grep { $_->{kind} eq $cmd } @$legal;
817 0 return $m;
818 }
819  
820 13 ▶sub run {
Subroutine run: called CC=19 SCAR=30.2
Pod documented
821 13 my ($self) = @_;
822 13 my $g = $self->rules;
823 13 $self->say($self->_c('bold', 'Mahjong, the competition rules: sixteen hands, eight points to win.'));
824 13 ▶ $self->say('Seed ' . $self->seed . ', bots at level ' . $self->level . '. Type help for the commands.') unless $self->auto;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto 9 4 13
825 13 $self->narrate($g->take_outcomes);
826 13 my $shown = -1;
827  
828 13 ▶ while ($g->is_active && !$self->quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->is_active and not $self->quit) 5428 4 5432
Condition66%2/3
$g->is_active and not $self->quit
!ll&&!rl&&r
405428
MC/DC50%1/2
$g->is_active and not $self->quit
$g->is_active not $self->quit
Truth table66%2/3
A and not(B)
A= $g->is_activeB= not $self->quit
ABresult
0X0
100
111
829 5428 my @waiting = $g->waiting_on;
830 5428 ▶ last unless @waiting;
Branch: 1 branch
BranchTrueFalseTotal
unless @waiting 0 5428 5428
831 5428 my ($seat) = grep { $_ == $self->seat } @waiting;
832 5428 ▶ if (defined $seat && !$self->auto) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seat and not $self->auto) 10 5418 5428
Condition100%3/3
defined $seat and not $self->auto
!ll&&!rl&&r
4018140010
MC/DC100%2/2
defined $seat and not $self->auto
defined $seat not $self->auto
Truth table100%3/3
A and not(B)
A= defined $seatB= not $self->auto
ABresult
0X0
100
111
833 10 my @legal = $g->legal($seat);
834 10 ▶ if ($shown != $g->moves) { $self->show_table; $shown = $g->moves }
Branch: 1 branch
BranchTrueFalseTotal
if ($shown != $g->moves) 10 0 10
835 10 $self->_describe_legal(@legal);
836 10 my $move = $self->read_move(@legal);
837 10 ▶ last unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 9 1 10
838 1 my $r = $g->apply($seat, $move);
839 1 ▶ if (ref $r && $r->can('error')) { $self->say('Refused: ' . $r->message); next }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r and $r->can("error")) 0 1 1
Condition33%1/3
ref $r and $r->can("error")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $r and $r->can("error")
ref $r $r->can("error")
Truth table33%1/3
A and B
A= ref $rB= $r->can("error")
ABresult
0X0
100
111
840 1 my @out = $g->take_outcomes;
841 1 $self->narrate(@out);
842 1 next;
843 }
844 5418 my $bot_seat = $waiting[0];
845 5418 ▶ if ($self->auto && $bot_seat == $self->seat && $g->phase eq 'discard' && $shown != $g->moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->auto and $bot_seat == $self->seat and $g->phase eq "discard" and $shown != $g->moves) 1007 4411 5418
Condition88%8/9
$self->auto and $bot_seat == $self->seat and $g->phase eq "discard" and $shown != $g->moves
!ll&&!rl&&r
441101007
$self->auto and $bot_seat == $self->seat and $g->phase eq "discard"
!ll&&!rl&&r
40443671007
$self->auto and $bot_seat == $self->seat
!ll&&!rl&&r
440401374
MC/DC75%3/4
$self->auto and $bot_seat == $self->seat and $g->phase eq "discard" and $shown != $g->moves
$self->auto $bot_seat == $self->seat $g->phase eq "discard" $shown != $g->moves
Truth table80%4/5
A and B and C and D
A= $self->autoB= $bot_seat == $self->seatC= $g->phase eq "discard"D= $shown != $g->moves
ABCDresult
0XXX0
10XX0
110X0
11100
11111
846 1007 $self->show_table;
847 1007 $shown = $g->moves;
848 }
849 5418 ▶ my $move = $self->bots->[$bot_seat]->choose($g, $bot_seat) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $self->bots->[$bot_seat]->choose($g, $bot_seat) or last 5418 0 5418
850 5418 my $r = $g->apply($bot_seat, $move);
851 5418 ▶ die 'Game::Mahjong::Terminal: the bot was refused: ' . $r->code if ref $r && $r->can('error');
Branch: 1 branch
BranchTrueFalseTotal
if ref $r and $r->can("error") 0 5418 5418
Condition33%1/3
ref $r and $r->can("error")
!ll&&!rl&&r
541800
MC/DC0%0/2
ref $r and $r->can("error")
ref $r $r->can("error")
Truth table33%1/3
A and B
A= ref $rB= $r->can("error")
ABresult
0X0
100
111
852 5418 $self->narrate($g->take_outcomes);
853 }
854 13 ▶ if ($self->quit) { $self->say('Goodbye.') }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->quit) 9 4 13
855 13 return $g;
856 }
857  
858 2 1;
859  
860 __END__
↑ Summary blib/lib/Game/Mahjong/Score.pm »