blib/lib/Game/Mahjong/Terminal.pm

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