Dominoes/Terminal.pm

filter: Statementstmt 81.2% 527 / 649 Branchbran 66.4% 226 / 340 Conditioncond 53.3% 80 / 150 MC/DCmcdc 32.9% 29 / 88 Subroutinesub 91.2% 52 / 57 Podpod 95.4% 21 / 22 totaltotal 71.5% 935 / 1306 CC 247 SCAR 50.2
CC
247
Cov
73%
_run
1190.0
command
91.6
pick
27.6
CRAP 151.9
↑ Summary Dominoes/Bot.pm »
1 package Game::Dominoes::Terminal;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Game::Dominoes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Game::Dominoes::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Game::Dominoes::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Game::Dominoes::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Game::Dominoes::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 our $VERSION = '0.03';
16  
17 3 our @PIPS = (
18 [],
19 [[1, 1]],
20 [[0, 0], [2, 2]],
21 [[0, 0], [1, 1], [2, 2]],
22 [[0, 0], [0, 2], [2, 0], [2, 2]],
23 [[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]],
24 [[0, 0], [0, 2], [1, 0], [1, 2], [2, 0], [2, 2]],
25 );
26  
27 3 our %CHARS = (
28 wide => {
29 pip => "\x{25CF}",
30 h => "\x{2500}", v => "\x{2502}",
31 tl => "\x{250C}", tr => "\x{2510}", bl => "\x{2514}", br => "\x{2518}",
32 t => "\x{252C}", b => "\x{2534}", l => "\x{251C}", r => "\x{2524}",
33 },
34 ascii => {
35 pip => '*',
36 h => '-', v => '|',
37 tl => '+', tr => '+', bl => '+', br => '+',
38 t => '+', b => '+', l => '+', r => '+',
39 },
40 );
41  
42 3 our %MARKED = (
43 recent => {
44 wide => {
45 h => "\x{2550}", v => "\x{2551}",
46 tl => "\x{2554}", tr => "\x{2557}", bl => "\x{255A}", br => "\x{255D}",
47 t => "\x{2566}", b => "\x{2569}", l => "\x{2560}", r => "\x{2563}",
48 },
49 ascii => {
50 h => '=', v => '#',
51 tl => '#', tr => '#', bl => '#', br => '#',
52 t => '#', b => '#', l => '#', r => '#',
53 },
54 },
55 preview => {
56 wide => {
57 h => "\x{2501}", v => "\x{2503}",
58 tl => "\x{250F}", tr => "\x{2513}", bl => "\x{2517}", br => "\x{251B}",
59 t => "\x{2533}", b => "\x{253B}", l => "\x{2523}", r => "\x{252B}",
60 },
61 ascii => {
62 h => '=', v => '!',
63 tl => '+', tr => '+', bl => '+', br => '+',
64 t => '+', b => '+', l => '+', r => '+',
65 },
66 },
67 );
68  
69 3 our %MARK_INK = (
70 new => '1;32',
71 recent => '32',
72 preview => '1;36',
73 );
74  
75 3 our %MARK_FRAME = (
76 new => 'recent',
77 recent => 'recent',
78 preview => 'preview',
79 );
80  
81 3 our %SEQUENCE = (
82 'A' => 'up',
83 'B' => 'down',
84 'C' => 'right',
85 'D' => 'left',
86 'H' => 'home',
87 'F' => 'end',
88 '1~' => 'home',
89 '4~' => 'end',
90 '5~' => 'page_up',
91 '6~' => 'page_down',
92 '7~' => 'home',
93 '8~' => 'end',
94 );
95  
96 3 our %CONTROL = (
97 "\r" => 'enter',
98 "\n" => 'enter',
99 "\t" => 'tab',
100 "\x7f" => 'backspace',
101 "\x08" => 'backspace',
102 "\x03" => 'interrupt',
103 "\x04" => 'eof',
104 );
105  
106 3 our $WINDOW = 8;
107  
108 3 has game => (is => 'rw', isa => Object);
109  
110 3 has bots => (is => 'rw', isa => HashRef, default => {});
111  
112 3 has in => (is => 'rw');
113 3 has out => (is => 'rw');
114  
115 3 has colour => (is => 'rw', isa => Bool);
116 3 has ascii => (is => 'rw', isa => Bool, default => 0);
117  
118 3 has compact => (is => 'rw', isa => Bool, default => 0);
119  
120 3 has width => (is => 'rw', isa => Int, default => 0);
121  
122 3 has handover => (is => 'rw', isa => Bool);
123  
124 3 has quit => (is => 'rw', isa => Bool, default => 0);
125  
126 3 has recent => (is => 'rw', isa => ArrayRef, default => []);
127  
128 3 has raw => (is => 'rw', isa => Bool, default => 0);
129  
130 3 has keysource => (is => 'rw');
131  
132 3 has pending => (is => 'rw', isa => ArrayRef, default => []);
133  
134 3 has _encoded => (is => 'rw', isa => Bool, default => 0, private => 1);
135  
136 3 has _picking => (is => 'rw', init_arg => 'picking', private => 1);
137  
138 31 ▶sub BUILD {
Subroutine BUILD: called CC=10 SCAR=23.4
Pod undocumented
139 31 my ($self) = @_;
140 31 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 4 27 31
141 31 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 4 27 31
142 31 ▶ $self->game(Game::Dominoes->new(seed => "\0" x 32)) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 0 31 31
143  
144 31 ▶ unless (defined $self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->colour) 2 29 31
145 2 ▶ my $tty = eval { -t $self->out } || 0;
Condition50%1/2
eval { do { -t $self->out } } || 0
l!l
02
MC/DC0%0/1
eval { do { -t $self->out } } || 0
eval { do { -t $self->out } }
Truth table50%1/2
A
A= eval { do { -t $self->out } }
Aresult
11
00
146 2 ▶ $self->colour(($tty && !$ENV{NO_COLOR}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tty && !$ENV{'NO_COLOR'} ? : 0 2 2
Condition33%1/3
$tty and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
200
MC/DC0%0/2
$tty and not $ENV{"NO_COLOR"}
$tty not $ENV{"NO_COLOR"}
Truth table33%1/3
A and not(B)
A= $ttyB= not $ENV{"NO_COLOR"}
ABresult
0X0
100
111
147 }
148 31 ▶ unless (defined $self->handover) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->handover) 8 23 31
149 8 ▶ $self->handover($self->_humans > 1 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_humans > 1 ? : 3 5 8
150 }
151 31 $self->_encode;
152 31 return $self;
153 }
154  
155 31 ▶sub _encode {
Subroutine _encode: called CC=3 SCAR=11.0
156 31 my ($self) = @_;
157 31 ▶ return $self if $self->ascii || $self->_encoded;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii or $self->_encoded 19 12 31
Condition66%2/3
$self->ascii or $self->_encoded
l!l&&r!l&&!r
19012
MC/DC50%1/2
$self->ascii or $self->_encoded
$self->ascii $self->_encoded
Truth table66%2/3
A or B
A= $self->asciiB= $self->_encoded
ABresult
1X1
011
000
158 12 $self->_encoded(1);
159 12 eval { binmode $self->out, ':encoding(UTF-8)'; 1 };
160 12 return $self;
161 }
162  
163 8 ▶sub _humans {
Subroutine _humans: called CC=1 SCAR=0.0
164 8 my ($self) = @_;
165 8 return scalar grep { !$self->bots->{$_} } $self->game->seats;
166 }
167  
168 90 ▶sub remember {
Subroutine remember: called CC=5 SCAR=17.5
Pod documented
169 90 my ($self, $seat, $play) = @_;
170 90 ▶ return $self unless ref $play && $play->can('tile') && $play->tile;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $play and $play->can("tile") and $play->tile 0 90 90
Condition33%2/6
ref $play and $play->can("tile") and $play->tile
!ll&&!rl&&r
0090
ref $play and $play->can("tile")
!ll&&!rl&&r
0090
MC/DC0%0/3
ref $play and $play->can("tile") and $play->tile
ref $play $play->can("tile") $play->tile
Truth table25%1/4
A and B and C
A= ref $playB= $play->can("tile")C= $play->tile
ABCresult
0XX0
10X0
1100
1111
171  
172 90 my $recent = $self->recent;
173 90 push @$recent, { seat => $seat, play => $play };
174  
175 90 ▶ shift @$recent while @$recent > $self->game->players;
Branch: 1 branch
BranchTrueFalseTotal
@$recent > $self->game->players and do { shift @$recent } 58 90 148
176  
177 90 return $self;
178 }
179  
180 84 ▶sub table_marks {
Subroutine table_marks: called CC=3 SCAR=11.0
Pod documented
181 84 my ($self) = @_;
182 84 my $recent = $self->recent;
183 84 my %mark;
184 84 for my $i (0 .. $#$recent) {
185 $mark{ $recent->[$i]{play}->tile->id }
186 31 ▶ = $i == $#$recent ? 'new' : 'recent';
Branch: 1 branch
BranchTrueFalseTotal
$i == $recent->$#* ? : 14 17 31
187 }
188 84 return \%mark;
189 }
190  
191 49 ▶sub recent_lines {
Subroutine recent_lines: called CC=4 SCAR=13.9
Pod documented
192 49 my ($self) = @_;
193 49 my $recent = $self->recent;
194 49 ▶ return () unless @$recent;
Branch: 1 branch
BranchTrueFalseTotal
unless @$recent 37 12 49
195 return map {
196 12 "seat $_->{seat} played " . $_->{play}->tile->stringify
197 . ' on ' . $_->{play}->arm
198 . ($_->{play}->points ? ' for ' . $_->{play}->points : '')
199 27 ▶ . ($_->{play}->spinner ? ', the spinner' : '')
Branch: 2 branches
BranchTrueFalseTotal
$_->{'play'}->points ? : 2 25 27
$_->{'play'}->spinner ? : 9 18 27
200 } @$recent;
201 }
202  
203 2439 ▶sub _say {
Subroutine _say: called CC=2 SCAR=7.0
204 2439 my ($self, @text) = @_;
205 2439 my $out = $self->out;
206 2439 ▶ print {$out} @text ? join('', @text) : '', "\n";
Branch: 1 branch
BranchTrueFalseTotal
@text ? : 2439 0 2439
207 2439 return;
208 }
209  
210 28 ▶sub _ask {
Subroutine _ask: called CC=2 SCAR=6.9
211 28 my ($self, $prompt) = @_;
212 28 my $out = $self->out;
213 28 print {$out} $prompt;
214 28 my $in = $self->in;
215 28 my $line = <$in>;
216 28 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 27 28
217 27 chomp $line;
218 27 return $line;
219 }
220  
221 83 ▶sub table {
Subroutine table: called CC=7 SCAR=19.6
Pod documented
222 83 my ($self, $view, $marks) = @_;
223 83 my $layout = $view->{layout};
224 83 ▶ $marks ||= $self->table_marks;
Condition66%2/3
$marks ||= $self->table_marks
l!l&&r!l&&!r
41420
MC/DC0%0/2
$marks ||= $self->table_marks
$marks $self->table_marks
Truth table66%2/3
A ||= B
A= $marksB= $self->table_marks
ABresult
1X1
011
000
225 83 ▶ return (' (nothing on the table yet)') unless $layout->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $layout->count 37 46 83
226 46 ▶ return $self->_compact_table($layout, $marks) if $self->compact;
Branch: 1 branch
BranchTrueFalseTotal
if $self->compact 1 45 46
227  
228 45 my $spinner = $layout->spinner_index;
229 45 my $line = $layout->line;
230 my @cell = map {
231 45 $self->_tile_art($line->[$_]{left}, $line->[$_]{right},
232 $line->[$_]{tile}->is_double,
233 defined $spinner && $_ == $spinner,
234 $marks->{ $line->[$_]{tile}->id })
235 78 ▶ } 0 .. $#$line;
Condition100%3/3
defined $spinner && $_ == $spinner
!ll&&!rl&&r
153231
MC/DC100%2/2
defined $spinner && $_ == $spinner
defined $spinner $_ == $spinner
Truth table100%3/3
A && B
A= defined $spinnerB= $_ == $spinner
ABresult
0X0
100
111
236 45 my @band = $self->_wrap(\@cell);
237 45 my ($first, $last) = ($band[0], $band[-1]);
238  
239 2 ▶ my $at = sub {
Subroutine __ANON__: called CC=5 SCAR=20.9
240 2 my ($run) = @_;
241 return 2 unless defined $spinner && $run && $spinner >= $run->{from}
242 2 ▶ && $spinner <= $run->{to};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spinner and $run and $spinner >= $run->{"from"} and $spinner <= $run->{"to"} 0 2 2
Condition33%3/9
defined $spinner and $run and $spinner >= $run->{"from"} and $spinner <= $run->{"to"}
!ll&&!rl&&r
002
defined $spinner and $run and $spinner >= $run->{"from"}
!ll&&!rl&&r
002
defined $spinner and $run
!ll&&!rl&&r
002
MC/DC0%0/4
defined $spinner and $run and $spinner >= $run->{"from"} and $spinner <= $run->{"to"}
defined $spinner $run $spinner >= $run->{"from"} $spinner <= $run->{"to"}
Truth table20%1/5
A and B and C and D
A= defined $spinnerB= $runC= $spinner >= $run->{"from"}D= $spinner <= $run->{"to"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
243 2 return $run->{x}[ $spinner - $run->{from} ];
244 45 };
245  
246 45 my @rows;
247 45 ▶ if (@{ $layout->u }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$layout->u;}) 1 44 45
248 my @arm = map {
249 $self->_tile_art($_->{inner}, $_->{outer}, $_->{tile}->is_double, 0,
250 $marks->{ $_->{tile}->id })
251 1 } @{ $layout->u };
252 1 my $indent = $at->($first);
253 1 push @rows, map { $self->_band($_, $indent) } $self->_wrap(\@arm, $indent);
254 1 push @rows, $self->_stem($indent);
255 }
256  
257 45 push @rows, map { $self->_band($_, 2) } @band;
258  
259 45 ▶ if (@{ $layout->d }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$layout->d;}) 1 44 45
260 my @arm = map {
261 $self->_tile_art($_->{inner}, $_->{outer}, $_->{tile}->is_double, 0,
262 $marks->{ $_->{tile}->id })
263 1 } @{ $layout->d };
264 1 my $indent = $at->($last);
265 1 push @rows, $self->_stem($indent);
266 1 push @rows, map { $self->_band($_, $indent) } $self->_wrap(\@arm, $indent);
267 }
268 45 return @rows;
269 }
270  
271 1 ▶sub _compact_table {
Subroutine _compact_table: called CC=4 SCAR=14.0
272 1 my ($self, $layout, $marks) = @_;
273 1 ▶ $marks ||= {};
Condition50%1/2
$marks ||= {}
l!l
10
MC/DC0%0/1
$marks ||= {}
$marks
Truth table50%1/2
A
A= $marks
Aresult
11
00
274 1 my @rows;
275 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->u })
276 1 ▶ if @{ $layout->u };
Branch: 1 branch
BranchTrueFalseTotal
if @{$layout->u;} 1 0 1
277 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->line });
278 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->d })
279 1 ▶ if @{ $layout->d };
Branch: 1 branch
BranchTrueFalseTotal
if @{$layout->d;} 1 0 1
280 1 return @rows;
281 }
282  
283 54 ▶sub _tile {
Subroutine _tile: called CC=7 SCAR=25.7
284 54 my ($self, $tile, $marks) = @_;
285  
286 54 ▶ my $mark = $marks ? $marks->{ $tile->id } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$marks ? : 5 49 54
287 54 ▶ my $text = ($mark ? '{' : '[') . $tile->high . '|' . $tile->low
Branch: 2 branches
BranchTrueFalseTotal
$mark ? : 0 54 54
$mark ? : 0 54 54
288 . ($mark ? '}' : ']');
289  
290 54 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 54 0 54
291 0 ▶ return $self->_paint($text, $MARK_INK{$mark}) if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 0 0 0
292 0 ▶ return $tile->is_double ? "\e[1;33m$text\e[0m" : $text;
Branch: 1 branch
BranchTrueFalseTotal
$tile->is_double ? : 0 0 0
293 }
294  
295 609 ▶sub _tile_art {
Subroutine _tile_art: called CC=9 SCAR=22.0
296 609 my ($self, $one, $two, $across, $bright, $mark) = @_;
297 609 ▶ my $kind = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 470 139 609
298 my $c = $mark && $MARK_FRAME{$mark}
299 ? $MARKED{ $MARK_FRAME{$mark} }{$kind}
300 609 ▶ : $CHARS{$kind};
Branch: 1 branch
BranchTrueFalseTotal
$mark && $Game::Dominoes::Terminal::MARK_FRAME{$mark} ? : 70 539 609
Condition66%2/3
$mark and $Game::Dominoes::Terminal::MARK_FRAME{$mark}
!ll&&!rl&&r
539070
MC/DC50%1/2
$mark and $Game::Dominoes::Terminal::MARK_FRAME{$mark}
$mark $Game::Dominoes::Terminal::MARK_FRAME{$mark}
Truth table66%2/3
A and B
A= $markB= $Game::Dominoes::Terminal::MARK_FRAME{$mark}
ABresult
0X0
100
111
301 609 my @face = ($self->_face($one, $across), $self->_face($two, $across));
302  
303 609 my @lines;
304 609 ▶ if ($across) {
Branch: 1 branch
BranchTrueFalseTotal
if ($across) { } 31 578 609
305 31 push @lines, $c->{tl} . ($c->{h} x 3) . $c->{tr};
306 31 push @lines, map { $c->{v} . $_ . $c->{v} } @{ $face[0] };
307 31 push @lines, $c->{l} . ($c->{h} x 3) . $c->{r};
308 31 push @lines, map { $c->{v} . $_ . $c->{v} } @{ $face[1] };
309 31 push @lines, $c->{bl} . ($c->{h} x 3) . $c->{br};
310 }
311 else {
312 578 push @lines, $c->{tl} . ($c->{h} x 3) . $c->{t} . ($c->{h} x 3) . $c->{tr};
313 push @lines, map {
314 578 $c->{v} . $face[0][$_] . $c->{v} . $face[1][$_] . $c->{v}
315 1734 } 0 .. 2;
316 578 push @lines, $c->{bl} . ($c->{h} x 3) . $c->{b} . ($c->{h} x 3) . $c->{br};
317 }
318  
319 609 ▶ my $ink = $mark ? $MARK_INK{$mark} : ($bright ? '1;33' : undef);
Branch: 2 branches
BranchTrueFalseTotal
$mark ? : 70 539 609
$bright ? : 4 535 539
320 609 ▶ @lines = map { $self->_paint($_, $ink) } @lines if defined $ink;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ink 74 535 609
321  
322 609 ▶ return { width => $across ? 5 : 9, lines => \@lines };
Branch: 1 branch
BranchTrueFalseTotal
$across ? : 31 578 609
323 }
324  
325 1218 ▶sub _face {
Subroutine _face: called CC=5 SCAR=16.1
326 1218 my ($self, $pips, $turned) = @_;
327 1218 ▶ my $pip = $CHARS{ $self->ascii ? 'ascii' : 'wide' }{pip};
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 940 278 1218
328 1218 my @grid = map { [ (' ') x 3 ] } 0 .. 2;
329 1218 ▶ for my $spot (@{ $PIPS[$pips] || [] }) {
Condition50%1/2
$Game::Dominoes::Terminal::PIPS[$pips] || []
l!l
12180
MC/DC0%0/1
$Game::Dominoes::Terminal::PIPS[$pips] || []
$Game::Dominoes::Terminal::PIPS[$pips]
Truth table50%1/2
A
A= $Game::Dominoes::Terminal::PIPS[$pips]
Aresult
11
00
330 2920 my ($row, $col) = @$spot;
331 2920 ▶ ($row, $col) = ($col, 2 - $row) if $turned;
Branch: 1 branch
BranchTrueFalseTotal
if $turned 90 2830 2920
332 2920 $grid[$row][$col] = $pip;
333 }
334 1218 return [ map { join '', @$_ } @grid ];
335 }
336  
337 543 ▶sub _paint {
Subroutine _paint: called CC=2 SCAR=8.1
338 543 my ($self, $text, $code) = @_;
339 543 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 543 0 543
340 0 return "\e[${code}m" . $text . "\e[0m";
341 }
342  
343 121 ▶sub _wrap {
Subroutine _wrap: called CC=9 SCAR=22.0
344 121 my ($self, $cells, $indent) = @_;
345 121 ▶ $indent = 2 unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 119 2 121
346 121 ▶ my $room = ($self->width || $ENV{COLUMNS} || 80) - $indent;
Condition80%4/5
$self->width || $ENV{'COLUMNS'} || 80
l!l
9427
$self->width or $ENV{"COLUMNS"}
l!l&&r!l&&!r
94027
MC/DC50%1/2
$self->width || $ENV{'COLUMNS'} || 80
$self->width $ENV{"COLUMNS"}
Truth table66%2/3
A or B
A= $self->widthB= $ENV{"COLUMNS"}
ABresult
1X1
011
000
347 121 ▶ $room = 20 if $room < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 20 0 121 121
348  
349 121 my @run;
350 121 my ($cells_in, $x, @at) = ([], $indent);
351 121 my $from = 0;
352 121 for my $i (0 .. $#$cells) {
353 609 my $cell = $cells->[$i];
354 609 ▶ if (@$cells_in && $x + $cell->{width} - $indent > $room) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cells_in and $x + $cell->{"width"} - $indent > $room) 9 600 609
Condition100%3/3
@$cells_in and $x + $cell->{"width"} - $indent > $room
!ll&&!rl&&r
1214799
MC/DC100%2/2
@$cells_in and $x + $cell->{"width"} - $indent > $room
@$cells_in $x + $cell->{"width"} - $indent > $room
Truth table100%3/3
A and B
A= @$cells_inB= $x + $cell->{"width"} - $indent > $room
ABresult
0X0
100
111
355 9 push @run, { cells => $cells_in, x => [@at], from => $from, to => $i - 1 };
356 9 ($cells_in, $x, @at) = ([], $indent);
357 9 $from = $i;
358 }
359 609 push @$cells_in, $cell;
360 609 push @at, $x;
361 609 $x += $cell->{width};
362 }
363 121 ▶ push @run, { cells => $cells_in, x => [@at], from => $from, to => $#$cells }
Branch: 1 branch
BranchTrueFalseTotal
if @$cells_in 121 0 121
364 if @$cells_in;
365 121 return @run;
366 }
367  
368 130 ▶sub _band {
Subroutine _band: called CC=8 SCAR=20.8
369 130 my ($self, $run, $indent) = @_;
370 130 my @cells = @{ $run->{cells} };
371 130 my $height = 0;
372 130 for my $cell (@cells) {
373 609 ▶ $height = scalar @{ $cell->{lines} } if @{ $cell->{lines} } > $height;
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->{"lines"};} > $height 145 464 609
374 }
375  
376 130 my @rows = map { ' ' x $indent } 1 .. $height;
377 130 for my $cell (@cells) {
378 609 my $top = int(($height - scalar @{ $cell->{lines} }) / 2);
379 609 for my $row (0 .. $height - 1) {
380 3826 my $line = $cell->{lines}[ $row - $top ];
381 $rows[$row] .= ($row >= $top && defined $line)
382 3826 ▶ ? $line : ' ' x $cell->{width};
Branch: 1 branch
BranchTrueFalseTotal
$row >= $top && defined $line ? : 3698 128 3826
Condition100%3/3
$row >= $top and defined $line
!ll&&!rl&&r
64643698
MC/DC100%2/2
$row >= $top and defined $line
$row >= $top defined $line
Truth table100%3/3
A and B
A= $row >= $topB= defined $line
ABresult
0X0
100
111
383 }
384 }
385  
386 130 s/\s+\z// for @rows;
387  
388 130 return @rows;
389 }
390  
391 2 ▶sub _stem {
Subroutine _stem: called CC=2 SCAR=7.1
392 2 my ($self, $indent) = @_;
393 2 ▶ my $c = $CHARS{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 2 0 2
394 2 return (' ' x ($indent + 2)) . $c->{v};
395 }
396  
397 45 ▶sub _ends_line {
Subroutine _ends_line: called CC=2 SCAR=6.9
398 45 my ($self, $view) = @_;
399 45 my $count = $view->{count};
400 45 my $short = (5 - $count % 5) % 5;
401 45 my $text = 'ends ' . join(' ', @{ $view->{ends} }) . ' count ' . $count;
402 45 ▶ $text .= $short ? " ($short more would score)" : ' (scores)';
Branch: 1 branch
BranchTrueFalseTotal
$short ? : 26 19 45
403 45 return $text;
404 }
405  
406 80 ▶sub _scores_line {
Subroutine _scores_line: called CC=1 SCAR=0.0
407 80 my ($self, $view) = @_;
408 return 'scores ' . join(' ', map {
409 200 "seat $_: $view->{scores}{$_}"
410 80 } sort { $a <=> $b } keys %{ $view->{scores} });
411 }
412  
413 77 ▶sub _hands_line {
Subroutine _hands_line: called CC=1 SCAR=0.0
414 77 my ($self, $view) = @_;
415 return 'tiles ' . join(' ', map {
416 192 "seat $_: $view->{counts}{$_}"
417 128 } sort { $a <=> $b } keys %{ $view->{counts} })
418 77 . ' boneyard: ' . $view->{boneyard};
419 }
420  
421 81 ▶sub hand_lines {
Subroutine hand_lines: called CC=4 SCAR=13.9
Pod documented
422 81 my ($self, $view) = @_;
423 81 ▶ my @tiles = @{ $view->{hand} || [] };
Condition50%1/2
$view->{'hand'} || []
l!l
810
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
424 81 ▶ return ('your tiles: (none)') unless @tiles;
Branch: 1 branch
BranchTrueFalseTotal
unless @tiles 0 81 81
425 81 ▶ return ('your tiles: ' . join ' ', map { $self->_tile($_) } @tiles)
Branch: 1 branch
BranchTrueFalseTotal
if $self->compact 7 74 81
426 if $self->compact;
427  
428 my @cell = map {
429 74 my $art = $self->_tile_art($_->high, $_->low, 0, 0);
430 529 push @{ $art->{lines} }, sprintf '%-9s', ' ' . $_->stringify . ' ';
431 529 $art;
432 } @tiles;
433 74 return ('your tiles:', map { $self->_band($_, 2) } $self->_wrap(\@cell));
434 }
435  
436 28 ▶sub show {
Subroutine show: called CC=4 SCAR=13.9
Pod documented
437 28 my ($self, $view) = @_;
438 28 $self->_say('');
439 28 $self->_say("hand $view->{hand_number} " . $self->_scores_line($view));
440 28 $self->_say($self->_hands_line($view));
441 28 $self->_say('');
442 28 $self->_say($_) for $self->table($view);
443 28 $self->_say('');
444 28 ▶ $self->_say($self->_ends_line($view)) if $view->{layout}->count;
Branch: 1 branch
BranchTrueFalseTotal
if $view->{"layout"}->count 1 27 28
445 28 $self->_say($_) for $self->hand_lines($view);
446 28 return;
447 }
448  
449 51 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
450 51 my ($self, @set) = @_;
451 51 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 2 49 51
$set[0] ? : 0 2 2
452 51 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 26 25 51
$self->_picking ? : 23 3 26
453 25 return $self->keys_available;
454 }
455  
456 27 ▶sub keys_available {
Subroutine keys_available: called CC=5 SCAR=22.5
Pod documented
457 27 my ($self) = @_;
458 27 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 27 27
459 27 ▶ return 0 unless defined $self->in && eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and eval { do { -t $self->in } } 27 0 27
Condition33%1/3
defined $self->in and eval { do { -t $self->in } }
!ll&&!rl&&r
0270
MC/DC0%0/2
defined $self->in and eval { do { -t $self->in } }
defined $self->in eval { do { -t $self->in } }
Truth table33%1/3
A and B
A= defined $self->inB= eval { do { -t $self->in } }
ABresult
0X0
100
111
460 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
461 }
462  
463 23 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=20.4
Pod documented
464 23 my ($self) = @_;
465 23 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 23 23
466  
467 23 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 22 1 23
468 22 $self->raw(1);
469 22 return $self;
470 }
471  
472 1 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 1 0 1
473  
474 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
475  
476 0 $self->raw(1);
477 0 return $self;
478 }
479  
480 22 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.8
Pod documented
481 22 my ($self) = @_;
482 22 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 22 22
483 22 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 22 22
484 22 $self->raw(0);
485 22 return $self;
486 }
487  
488 132 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
489 132 my ($self, $wait) = @_;
490  
491 132 my $pending = $self->pending;
492 132 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 130 132
493 130 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 130 0 130
494  
495 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
496 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
497  
498 0 select undef, undef, undef, 0.05;
499 0 return Term::ReadKey::ReadKey(-1, $self->in);
500 }
501  
502 68 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
503 68 my ($self) = @_;
504 68 my $char = $self->read_char(1);
505 68 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 6 62 68
506 62 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Dominoes::Terminal::CONTROL{$char} 13 49 62
507 49 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 32 17 49
508 17 return $char;
509 }
510  
511 32 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
512 32 my ($self) = @_;
513  
514 32 my $opener = $self->read_char(0);
515 32 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 32 32
516  
517 32 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 30 32
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
2912
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
518 2 unshift @{ $self->pending }, $opener;
519 2 return 'escape';
520 }
521  
522 30 my $tail = '';
523 30 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 32 0 32
524 32 my $char = $self->read_char(0);
525 32 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 32 32
526 32 $tail .= $char;
527 32 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 30 2 32
528 }
529  
530 30 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Dominoes::Terminal::SEQUENCE{$tail} || 'escape'
l!l
300
MC/DC0%0/1
$Game::Dominoes::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Dominoes::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Dominoes::Terminal::SEQUENCE{$tail}
Aresult
11
00
531 }
532  
533 41 ▶sub preview {
Subroutine preview: called CC=1 SCAR=0.0
Pod documented
534 41 my ($self, $view, $move) = @_;
535  
536 41 my $trial = $view->{layout}->clone;
537 41 my $play = $trial->place($move->{tile}, $move->{arm});
538  
539 return ({
540 41 %$view,
541 layout => $trial,
542 count => Game::Dominoes::Scoring::count($trial),
543 ends => [ $trial->open_ends ],
544 }, $play);
545 }
546  
547 49 ▶sub choice_lines {
Subroutine choice_lines: called CC=10 SCAR=23.0
Pod documented
548 49 my ($self, $moves, $at) = @_;
549  
550 49 my $first = 0;
551 49 ▶ if (@$moves > $WINDOW) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$moves > $Game::Dominoes::Terminal::WINDOW) 15 34 49
552 15 $first = $at - int($WINDOW / 2);
553 15 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 13 2 15
554 15 ▶ $first = @$moves - $WINDOW if $first > @$moves - $WINDOW;
Branch: 1 branch
BranchTrueFalseTotal
if $first > @$moves - $Game::Dominoes::Terminal::WINDOW 2 13 15
555 }
556 49 my $last = $first + $WINDOW - 1;
557 49 ▶ $last = $#$moves if $last > $#$moves;
Branch: 1 branch
BranchTrueFalseTotal
if $last > $moves->$#* 34 15 49
558  
559 49 my @lines;
560 49 ▶ push @lines, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 2 47 49
561 49 for my $i ($first .. $last) {
562 306 my $move = $moves->[$i];
563 my $line = sprintf '%2d. %-8s %s', $i + 1,
564 $move->{tile}->stringify . '@' . $move->{arm},
565 306 ▶ $move->{points} ? "scores $move->{points}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$move->{'points'} ? : 47 259 306
566 306 $line =~ s/\s+\z//;
567 306 ▶ push @lines, $i == $at
Branch: 1 branch
BranchTrueFalseTotal
$i == $at ? : 49 257 306
568 ? ' ' . $self->_paint("> $line", '1;7')
569 : " $line";
570 }
571 49 ▶ push @lines, sprintf ' %d further down', $#$moves - $last
Branch: 1 branch
BranchTrueFalseTotal
if $last < $moves->$#* 13 36 49
572 if $last < $#$moves;
573  
574 49 return @lines;
575 }
576  
577 49 ▶sub legend {
Subroutine legend: called CC=1 SCAR=0.0
Pod documented
578 return (
579 49 'arrows to choose, enter to play, a number to jump to it',
580 'v for the table as it stands, ? for the commands, t to type, q to quit',
581 );
582 }
583  
584 23 ▶sub pick {
Subroutine pick: called CC=25 SCAR=33.2
Pod documented
585 23 my ($self, $view) = @_;
586  
587 23 my $seat = $view->{seat};
588 23 my $moves = $self->game->legal($seat);
589  
590 23 ▶ return $self->_typed_turn($view) unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 23 23
591  
592 23 ▶ unless ($self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->enter_raw) 1 22 23
593 1 $self->picking(0);
594 1 return $self->_typed_turn($view);
595 }
596  
597 22 my $at = 0;
598 22 my $live = 0;
599 22 my @notice;
600  
601 22 while (1) {
602 49 $self->_show_choice($view, $moves, $at, \@notice, $live);
603 49 @notice = ();
604  
605 49 my $key = $self->read_key;
606  
607 49 ▶ 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") 12 37 49
Condition77%7/9
not defined $key or $key eq "eof" or $key eq "interrupt" or $key eq "q"
l!l&&r!l&&!r
5737
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
5044
not defined $key or $key eq "eof"
l!l&&r!l&&!r
5044
MC/DC50%2/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 table60%3/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
608 12 $self->leave_raw;
609 12 $self->quit(1);
610 12 return 0;
611 }
612  
613 37 ▶ if ($key eq 'up' || $key eq 'left') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "up" or $key eq "left") 7 30 37
Condition66%2/3
$key eq "up" or $key eq "left"
l!l&&r!l&&!r
7030
MC/DC50%1/2
$key eq "up" or $key eq "left"
$key eq "up" $key eq "left"
Truth table66%2/3
A or B
A= $key eq "up"B= $key eq "left"
ABresult
1X1
011
000
614 7 $at = ($at - 1) % @$moves;
615 7 next;
616 }
617 30 ▶ if ($key eq 'down' || $key eq 'right' || $key eq 'tab') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "down" or $key eq "right" or $key eq "tab") 15 15 30
Condition66%4/6
$key eq "down" or $key eq "right" or $key eq "tab"
l!l&&r!l&&!r
15015
$key eq "down" or $key eq "right"
l!l&&r!l&&!r
15015
MC/DC33%1/3
$key eq "down" or $key eq "right" or $key eq "tab"
$key eq "down" $key eq "right" $key eq "tab"
Truth table50%2/4
A or B or C
A= $key eq "down"B= $key eq "right"C= $key eq "tab"
ABCresult
1XX1
01X1
0011
0000
618 15 $at = ($at + 1) % @$moves;
619 15 next;
620 }
621 15 ▶ 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 15 15
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
0015
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
622 15 ▶ if ($key eq 'end' || $key eq 'page_down') { $at = $#$moves; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end" or $key eq "page_down") 1 14 15
Condition66%2/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
1014
MC/DC50%1/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table66%2/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
623  
624 14 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 8 6 14
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
806
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
625 8 $self->leave_raw;
626 8 return $self->_play($seat, $moves->[$at]);
627 }
628  
629 6 ▶ if ($key =~ /\A[1-9]\z/ && $key <= @$moves) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-9]\z/ and $key <= @$moves) 1 5 6
Condition66%2/3
$key =~ /\A[1-9]\z/ and $key <= @$moves
!ll&&!rl&&r
501
MC/DC50%1/2
$key =~ /\A[1-9]\z/ and $key <= @$moves
$key =~ /\A[1-9]\z/ $key <= @$moves
Truth table66%2/3
A and B
A= $key =~ /\A[1-9]\z/B= $key <= @$moves
ABresult
0X0
100
111
630 1 $at = $key - 1;
631 1 next;
632 }
633  
634 5 ▶ if ($key eq 'v') { $live = !$live; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "v") 2 3 5
635 3 ▶ if ($key eq '?') { @notice = $self->help; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 3 3
636  
637 3 ▶ if ($key eq 't' || $key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "t" or $key eq ":") 2 1 3
Condition100%3/3
$key eq "t" or $key eq ":"
l!l&&r!l&&!r
111
MC/DC100%2/2
$key eq "t" or $key eq ":"
$key eq "t" $key eq ":"
Truth table100%3/3
A or B
A= $key eq "t"B= $key eq ":"
ABresult
1X1
011
000
638 2 $self->leave_raw;
639 2 ▶ $self->picking(0) if $key eq 't';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "t" 1 1 2
640 2 return $self->_typed_turn($view);
641 }
642  
643 1 @notice = ('that key does nothing here. ? for the ones that do');
644 }
645 }
646  
647 49 ▶sub _clear {
Subroutine _clear: called CC=2 SCAR=9.9
648 49 my ($self) = @_;
649 49 ▶ return unless eval { -t $self->out };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->out } } 49 0 49
650 0 my $out = $self->out;
651 0 print {$out} "\e[2J\e[3J\e[H";
652 0 return;
653 }
654  
655 49 ▶sub _show_choice {
Subroutine _show_choice: called CC=12 SCAR=24.8
656 49 my ($self, $view, $moves, $at, $notice, $live) = @_;
657  
658 49 $self->_clear;
659  
660 49 $self->_say('');
661 49 $self->_say("hand $view->{hand_number} " . $self->_scores_line($view));
662 49 $self->_say($self->_hands_line($view));
663  
664 49 ▶ if (my @said = $self->recent_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@said) = $self->recent_lines) 12 37 49
665 12 $self->_say('');
666 12 $self->_say(' ', $_) for @said;
667 }
668  
669 49 $self->_say('');
670  
671 49 my ($shown, $title);
672 49 ▶ if ($live) {
Branch: 1 branch
BranchTrueFalseTotal
if ($live) { } 8 41 49
673 8 $shown = $view;
674 8 $title = ' the table as it stands:';
675 }
676 else {
677 41 my ($peek) = $self->preview($view, $moves->[$at]);
678 41 $shown = $peek;
679 $title = ' if you play ' . $moves->[$at]{tile}->stringify
680 41 . ' on ' . $moves->[$at]{arm} . ':';
681 }
682  
683 49 $self->_say($title);
684 49 ▶ $self->_say($_) for $self->table($shown, $live ? undef : {
Branch: 1 branch
BranchTrueFalseTotal
$live ? : 8 41 49
685 41 %{ $self->table_marks },
686 $moves->[$at]{tile}->id => 'preview',
687 });
688 49 $self->_say('');
689 49 ▶ $self->_say($self->_ends_line($shown)) if $shown->{layout}->count;
Branch: 1 branch
BranchTrueFalseTotal
if $shown->{"layout"}->count 42 7 49
690  
691 49 $self->_say('');
692 49 $self->_say($_) for $self->hand_lines($view);
693 49 $self->_say('');
694 49 $self->_say($_) for $self->choice_lines($moves, $at);
695  
696 49 ▶ if (@$notice) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$notice) 1 48 49
697 1 $self->_say('');
698 1 $self->_say(' ', $_) for @$notice;
699 }
700  
701 49 $self->_say('');
702 49 $self->_say(' ', $_) for $self->legend;
703  
704 49 return;
705 }
706  
707 28 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=2 SCAR=6.9
708 28 my ($self, $view) = @_;
709 28 $self->show($view);
710 28 $self->_say($_) for $self->_legal_lines($view);
711 28 my $line = $self->_ask("seat $view->{seat}> ");
712 28 return $self->command($line, $view);
713 }
714  
715 31 ▶sub _legal_lines {
Subroutine _legal_lines: called CC=3 SCAR=11.0
716 31 my ($self, $view) = @_;
717 31 my $moves = $self->game->legal($view->{seat});
718 31 ▶ return (' (nothing to play)') unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 31 31
719 31 my $i = 0;
720 return map {
721 31 my $line = sprintf(' %2d. %-8s %s', ++$i,
722 $_->{tile}->stringify . '@' . $_->{arm},
723 212 ▶ $_->{points} ? "scores $_->{points}" : '');
Branch: 1 branch
BranchTrueFalseTotal
$_->{'points'} ? : 42 170 212
724 212 $line =~ s/\s+\z//;
725 212 $line;
726 } @$moves;
727 }
728  
729 28 ▶sub command {
Subroutine command: called CC=36 SCAR=45.2
Pod documented
730 28 my ($self, $line, $view) = @_;
731 28 my $game = $self->game;
732 28 my $seat = $view->{seat};
733  
734 28 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 27 28
735 27 $line =~ s/\A\s+//;
736 27 $line =~ s/\s+\z//;
737 27 ▶ return 1 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 27 27
738  
739 27 my ($word, @rest) = split /\s+/, $line;
740 27 $word = lc $word;
741  
742 27 ▶ if ($word eq 'quit' || $word eq 'q') { $self->quit(1); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "quit" or $word eq "q") 8 19 27
Condition66%2/3
$word eq "quit" or $word eq "q"
l!l&&r!l&&!r
8019
MC/DC50%1/2
$word eq "quit" or $word eq "q"
$word eq "quit" $word eq "q"
Truth table66%2/3
A or B
A= $word eq "quit"B= $word eq "q"
ABresult
1X1
011
000
743 19 ▶ if ($word eq 'help' || $word eq '?') { $self->_say($_) for $self->help; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help" or $word eq "?") 1 18 19
Condition66%2/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
1018
MC/DC50%1/2
$word eq "help" or $word eq "?"
$word eq "help" $word eq "?"
Truth table66%2/3
A or B
A= $word eq "help"B= $word eq "?"
ABresult
1X1
011
000
744 18 ▶ if ($word eq 'table' || $word eq 'board') { $self->_say($_) for $self->table($view); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "table" or $word eq "board") 3 15 18
Condition66%2/3
$word eq "table" or $word eq "board"
l!l&&r!l&&!r
3015
MC/DC50%1/2
$word eq "table" or $word eq "board"
$word eq "table" $word eq "board"
Truth table66%2/3
A or B
A= $word eq "table"B= $word eq "board"
ABresult
1X1
011
000
745 15 ▶ if ($word eq 'hand' || $word eq 'tiles') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "hand" or $word eq "tiles") 3 12 15
Condition66%2/3
$word eq "hand" or $word eq "tiles"
l!l&&r!l&&!r
3012
MC/DC50%1/2
$word eq "hand" or $word eq "tiles"
$word eq "hand" $word eq "tiles"
Truth table66%2/3
A or B
A= $word eq "hand"B= $word eq "tiles"
ABresult
1X1
011
000
746 3 $self->_say($_) for $self->hand_lines($view);
747 3 return 1;
748 }
749 12 ▶ if ($word eq 'ends' || $word eq 'count') { $self->_say($self->_ends_line($view)); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "ends" or $word eq "count") 2 10 12
Condition66%2/3
$word eq "ends" or $word eq "count"
l!l&&r!l&&!r
2010
MC/DC50%1/2
$word eq "ends" or $word eq "count"
$word eq "ends" $word eq "count"
Truth table66%2/3
A or B
A= $word eq "ends"B= $word eq "count"
ABresult
1X1
011
000
750 10 ▶ if ($word eq 'scores') { $self->_say($self->_scores_line($view)); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "scores") 3 7 10
751 7 ▶ if ($word eq 'legal' || $word eq 'moves') { $self->_say($_) for $self->_legal_lines($view); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "legal" or $word eq "moves") 3 4 7
Condition66%2/3
$word eq "legal" or $word eq "moves"
l!l&&r!l&&!r
304
MC/DC50%1/2
$word eq "legal" or $word eq "moves"
$word eq "legal" $word eq "moves"
Truth table66%2/3
A or B
A= $word eq "legal"B= $word eq "moves"
ABresult
1X1
011
000
752 4 ▶ if ($word eq 'ascii') { $self->ascii(!$self->ascii); $self->_encode; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "ascii") 0 4 4
753 4 ▶ if ($word eq 'type') { $self->picking(0); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "type") 0 4 4
754  
755 4 ▶ if ($word eq 'pick') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "pick") 0 4 4
756 0 ▶ if ($self->keys_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keys_available) 0 0 0
757 0 $self->picking(1);
758 0 return $self->pick($view);
759 }
760 0 $self->_say('the keys are not available here, so this game is typed.'
761 . ' Term::ReadKey and a terminal are what it takes');
762 0 return 1;
763 }
764  
765 4 ▶ if ($word eq 'compact') { $self->compact(!$self->compact); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "compact") 0 4 4
766 4 ▶ if ($word eq 'colour' || $word eq 'color') { $self->colour(!$self->colour); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "colour" or $word eq "color") 0 4 4
Condition33%1/3
$word eq "colour" or $word eq "color"
l!l&&r!l&&!r
004
MC/DC0%0/2
$word eq "colour" or $word eq "color"
$word eq "colour" $word eq "color"
Truth table33%1/3
A or B
A= $word eq "colour"B= $word eq "color"
ABresult
1X1
011
000
767 4 ▶ if ($word eq 'history' || $word eq 'log') { $self->_say($game->to_text); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "history" or $word eq "log") 0 4 4
Condition33%1/3
$word eq "history" or $word eq "log"
l!l&&r!l&&!r
004
MC/DC0%0/2
$word eq "history" or $word eq "log"
$word eq "history" $word eq "log"
Truth table33%1/3
A or B
A= $word eq "history"B= $word eq "log"
ABresult
1X1
011
000
768  
769 4 ▶ if ($word eq 'hint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "hint") 1 3 4
770 1 my $bot = Game::Dominoes::Bot->new(level => 4, seed => 1);
771 1 my $move = $bot->choose($game, $seat);
772 $self->_say($move
773 ? 'try ' . $move->{tile}->stringify . '@' . $move->{arm}
774 1 ▶ : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
$move ? : 1 0 1
775 1 return 1;
776 }
777  
778 3 ▶ if ($word eq 'play' || $word eq 'p') { return $self->_play($seat, join(' ', @rest)) }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "play" or $word eq "p") 0 3 3
Condition33%1/3
$word eq "play" or $word eq "p"
l!l&&r!l&&!r
003
MC/DC0%0/2
$word eq "play" or $word eq "p"
$word eq "play" $word eq "p"
Truth table33%1/3
A or B
A= $word eq "play"B= $word eq "p"
ABresult
1X1
011
000
779  
780 3 ▶ return $self->_play($seat, $line) if $line =~ /\A[0-6]-[0-6]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A[0-6]-[0-6]/ 2 1 3
781  
782 1 ▶ if ($word =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /\A\d+\z/) 0 1 1
783 0 my $moves = $game->legal($seat);
784 0 my $pick = $moves->[ $word - 1 ];
785 0 ▶ unless ($pick) { $self->_say('no such move'); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($pick) 0 0 0
786 0 return $self->_play($seat, $pick);
787 }
788  
789 1 $self->_say("I do not know '$word'. Try help.");
790 1 return 1;
791 }
792  
793 10 ▶sub _play {
Subroutine _play: called CC=9 SCAR=22.8
794 10 my ($self, $seat, $move) = @_;
795  
796 10 ▶ if (!ref $move && $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $move and $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/) 0 10 10
Condition66%2/3
not ref $move and $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/
!ll&&!rl&&r
820
MC/DC0%0/2
not ref $move and $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/
not ref $move $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/
Truth table66%2/3
not(A) and B
A= not ref $moveB= $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/
ABresult
0X0
100
111
797 0 $move = "$1\@" . uc $2;
798 }
799  
800 10 ▶ if (!ref $move && $move =~ /\A([0-6]-[0-6])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $move and $move =~ /\A([0-6]-[0-6])\z/) 2 8 10
Condition66%2/3
not ref $move and $move =~ /\A([0-6]-[0-6])\z/
!ll&&!rl&&r
802
MC/DC50%1/2
not ref $move and $move =~ /\A([0-6]-[0-6])\z/
not ref $move $move =~ /\A([0-6]-[0-6])\z/
Truth table66%2/3
not(A) and B
A= not ref $moveB= $move =~ /\A([0-6]-[0-6])\z/
ABresult
0X0
100
111
801 2 my $want = Game::Dominoes::Notation::parse_tile($1);
802 2 my @fit = grep { $_->{tile}->id == $want->id } @{ $self->game->legal($seat) };
803  
804 2 ▶ if (@fit > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fit > 1) 0 2 2
805 $self->_say('which arm? ' . join(' ',
806 0 map { $_->{tile}->stringify . '@' . $_->{arm} } @fit));
807 0 return 1;
808 }
809 2 ▶ $move = @fit ? { tile => $want, arm => $fit[0]{arm} } : { tile => $want };
Branch: 1 branch
BranchTrueFalseTotal
@fit ? : 1 1 2
810 }
811  
812 10 my $out = $self->game->play($seat, $move);
813 10 ▶ if (ref $out eq 'Game::Dominoes::Error') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out eq "Game::Dominoes::Error") 1 9 10
814 1 $self->_say('no: ' . $out->message);
815 1 return 1;
816 }
817 9 $self->remember($seat, $out);
818 9 ▶ $self->_say('played ' . $out->tile->stringify . ' on ' . $out->arm
Branch: 1 branch
BranchTrueFalseTotal
$out->points ? : 3 6 9
819 . ($out->points ? ' for ' . $out->points : ''));
820 9 return 1;
821 }
822  
823 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod documented
824 return (
825 1 ' play <tile> [arm] or just 6-4, or the number from `legal`',
826 ' legal table hand ends scores history hint',
827 ' pick type ascii colour compact quit',
828 ' arms are L and R along the line, U and D off the spinner',
829 ' a double is laid crosswise, which is how the table is drawn',
830 ' a tile in a heavy frame is one played since you last looked',
831 );
832 }
833  
834 21 ▶sub _handover {
Subroutine _handover: called CC=3 SCAR=18.1
835 21 my ($self, $seat) = @_;
836 21 ▶ return 1 unless $self->handover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handover 21 0 21
837 0 $self->_say('');
838 0 $self->_say(('') x 30);
839 0 my $line = $self->_ask("Pass to seat $seat, then press return: ");
840 0 ▶ return defined $line ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 0 0 0
841 }
842  
843 25 ▶sub start {
Subroutine start: called CC=15 SCAR=27.2
Pod documented
844 25 my ($self) = @_;
845 25 my $game = $self->game;
846  
847 25 $self->_say('Dominoes: All Fives, ' . $game->players . ' seats, to ' . $game->target);
848 25 $self->_say('Type help for the commands.');
849  
850 25 my $last_seat;
851 25 ▶ while ($game->status eq 'active' && !$self->quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->status eq "active" and not $self->quit) 128 4 132
Condition66%2/3
$game->status eq "active" and not $self->quit
!ll&&!rl&&r
40128
MC/DC50%1/2
$game->status eq "active" and not $self->quit
$game->status eq "active" not $self->quit
Truth table66%2/3
A and not(B)
A= $game->status eq "active"B= not $self->quit
ABresult
0X0
100
111
852 128 my $seat = $game->turn;
853 128 ▶ last unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 0 128 128
854  
855 128 ▶ if (my $bot = $self->bots->{$seat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bot = $self->bots->{$seat}) 80 48 128
856 80 ▶ my $move = $bot->choose($game, $seat) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $bot->choose($game, $seat) or last 80 0 80
857 80 my $out = $game->play($seat, $move);
858 80 ▶ last if ref $out eq 'Game::Dominoes::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Dominoes::Error" 0 80 80
859 80 $self->remember($seat, $out);
860 80 ▶ $self->_say("seat $seat played " . $out->tile->stringify
Branch: 1 branch
BranchTrueFalseTotal
$out->points ? : 29 51 80
861 . ' on ' . $out->arm . ($out->points ? ' for ' . $out->points : ''));
862 80 next;
863 }
864  
865 48 ▶ if (!defined $last_seat || $last_seat != $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last_seat or $last_seat != $seat) 21 27 48
Condition66%2/3
not defined $last_seat or $last_seat != $seat
l!l&&r!l&&!r
21027
MC/DC50%1/2
not defined $last_seat or $last_seat != $seat
not defined $last_seat $last_seat != $seat
Truth table66%2/3
not(A) or B
A= not defined $last_seatB= $last_seat != $seat
ABresult
1X1
011
000
866 21 ▶ last unless $self->_handover($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_handover($seat) 0 21 21
867 21 $last_seat = $seat;
868 }
869  
870 48 my $view = $game->view($seat);
871  
872 48 ▶ if ($self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking) 23 25 48
873 23 ▶ last unless $self->pick($view);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pick($view) 15 8 23
874 8 next;
875 }
876  
877 25 ▶ last unless $self->_typed_turn($view);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_typed_turn($view) 6 19 25
878 }
879  
880 25 ▶ if (my $result = $game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $game->result) 4 21 25
881 4 $self->_say('');
882 4 $self->_say($result->stringify);
883 4 return $result;
884 }
885  
886 21 $self->_say('');
887 21 $self->_say('stopped');
888 21 return $game->result;
889 }
890  
891 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
892 0 return <<'USAGE';
893 dominoes - draw dominoes with All Fives scoring
894  
895 dominoes [options]
896  
897 --players=N 2, 3 or 4 (default 2)
898 --seat=N which seat the human takes (default 1)
899 --hotseat every seat is a person, with a hand-over between turns
900 --bot-vs-bot nobody is a person
901 --level=N bot level 1 to 5, repeatable, one per bot seat
902 --target=N default 250 at two seats, 200 at three or four
903 --scale=N divide every score, for the cribbage board variation
904 (use --scale=5 --target=61 together, never one alone)
905 --variant=S all_fives, draw or block
906 --reserve=N tiles at the back of the boneyard nobody may draw
907 --seed=S reproduce a game exactly
908 --ascii plain ASCII only
909 --compact tiles as [6|4] on one line instead of drawn
910 --width=N what the drawn table may use before it wraps (default 80)
911 --colour force colour on or off (--nocolour)
912 --nopick type the moves out instead of choosing them with the keys
913 --version print the version
914 --help this
915  
916 Arms are L and R along the main line, U and D off the spinner.
917  
918 On a terminal the legal moves are listed under the table and chosen with
919 the arrow keys: the table above the list is the one the move under the
920 cursor would make, with its new count and what it would score, so a
921 placement is seen before it is played. Enter plays it, v shows the table as
922 it stands, t goes back to typing and ? prints the commands. Needs
923 Term::ReadKey; without it, or off a terminal, the moves are typed out.
924  
925 The tile each seat just played is marked on the table, because at four
926 seats three of them land between your turns.
927  
928 Exit status is 0 if your seat won or nobody was playing a seat, 1 if you
929 lost or stopped early, 2 for a bad option.
930 USAGE
931 }
932  
933 0 ▶sub _bad {
Subroutine _bad: not called CC=1 SCAR=6.9
934 0 my ($why) = @_;
935 0 $why =~ s/ at \S+ line \d+\.?\s*\z//;
936 0 $why =~ s/\A[\w:]+: //;
937 0 chomp $why;
938 0 print {\*STDERR} "dominoes: $why\n";
939 0 return 2;
940 }
941  
942 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
943 0 my ($class, @argv) = @_;
944 0 my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case));
945 0 my $status = $class->_run(\@argv);
946 0 Getopt::Long::Configure($saved);
947 0 return $status;
948 }
949  
950 0 ▶sub _run {
Subroutine _run: not called CC=34 SCAR=70.8
951 0 my ($class, $argv) = @_;
952 0 my %opt = (players => 2, seat => 1, level => [], variant => 'all_fives');
953  
954 Getopt::Long::GetOptionsFromArray($argv,
955 'players=i' => \$opt{players},
956 'seat=i' => \$opt{seat},
957 'hotseat' => \$opt{hotseat},
958 'bot-vs-bot' => \$opt{botonly},
959 'level=i@' => $opt{level},
960 'target=i' => \$opt{target},
961 'scale=i' => \$opt{scale},
962 'variant=s' => \$opt{variant},
963 'seed=s' => \$opt{seed},
964 'reserve=i' => \$opt{reserve},
965 'ascii' => \$opt{ascii},
966 'compact' => \$opt{compact},
967 'width=i' => \$opt{width},
968 'colour!' => \$opt{colour},
969 'color!' => \$opt{colour},
970 'pick!' => \$opt{pick},
971 'version' => \$opt{version},
972 'help|h|?' => \$opt{help},
973 0 ▶ ) or do { print {\*STDERR} _usage(); return 2 };
Branch: 1 branch
BranchTrueFalseTotal
&Getopt::Long::GetOptionsFromArray($argv, "players=i", \$opt{"players"}, "seat=i", \$opt{"seat"}, "hotseat", \$opt{"hotseat"}, "bot-vs-bot", \$opt{"botonly"}, "level=i\@", $opt{"level"}, "target=i", \$opt{"target"}, "scale=i", \$opt{"scale"}, "variant=s", \$opt{"variant"}, "seed=s", \$opt{"seed"}, "reserve=i", \$opt{"reserve"}, "ascii", \$opt{"ascii"}, "compact", \$opt{"compact"}, "width=i", \$opt{"width"}, "colour!", \$opt{"colour"}, "color!", \$opt{"colour"}, "pick!", \$opt{"pick"}, "version", \$opt{"version"}, "help|h|?", \$opt{"help"}) or do { print {\*STDERR;} _usage(); return 2 } 0 0 0
974  
975 0 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 0 0 0
976  
977 0 ▶ if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 0 0 0
978 0 print "dominoes (Game::Dominoes) $Game::Dominoes::VERSION\n";
979 0 return 0;
980 }
981  
982 0 ▶ if (@{$argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv;}) 0 0 0
983 0 print {\*STDERR} "dominoes: there is nothing to do with '$argv->[0]': options begin with --\n", _usage();
984 0 return 2;
985 }
986  
987 0 ▶ return _bad('--players must be 2, 3 or 4') unless $opt{players} =~ /\A[234]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"players"} =~ /\A[234]\z/ 0 0 0
988 return _bad("--seat must be a seat from 1 to $opt{players}")
989 unless $opt{seat} =~ /\A\d+\z/
990 0 ▶ && $opt{seat} >= 1 && $opt{seat} <= $opt{players};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1 and $opt{"seat"} <= $opt{"players"} 0 0 0
Condition0%0/6
$opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1 and $opt{"seat"} <= $opt{"players"}
!ll&&!rl&&r
000
$opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1
!ll&&!rl&&r
000
MC/DC0%0/3
$opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1 and $opt{"seat"} <= $opt{"players"}
$opt{"seat"} =~ /\A\d+\z/ $opt{"seat"} >= 1 $opt{"seat"} <= $opt{"players"}
Truth table0%0/4
A and B and C
A= $opt{"seat"} =~ /\A\d+\z/B= $opt{"seat"} >= 1C= $opt{"seat"} <= $opt{"players"}
ABCresult
0XX0
10X0
1100
1111
991  
992 my $seed = defined $opt{seed}
993 ? substr(($opt{seed} x 32), 0, 32)
994 0 ▶ : pack('N8', map { int rand 2**32 } 1 .. 8);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 0 0 0
995  
996 0 my $game = eval {
997 Game::Dominoes->new(
998 seed => $seed,
999 players => $opt{players},
1000 variant => $opt{variant},
1001 (defined $opt{target} ? (target => $opt{target}) : ()),
1002 (defined $opt{scale} ? (scale => $opt{scale}) : ()),
1003 0 ▶ (defined $opt{reserve} ? (reserve => $opt{reserve}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $opt{'target'} ? : 0 0 0
defined $opt{'scale'} ? : 0 0 0
defined $opt{'reserve'} ? : 0 0 0
1004 );
1005 };
1006  
1007 0 ▶ return _bad($@ || 'could not deal') unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 0 0 0
Condition0%0/2
$@ || 'could not deal'
l!l
00
MC/DC0%0/1
$@ || 'could not deal'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
1008  
1009 my @bot_seats =
1010 $opt{hotseat} ? ()
1011 : $opt{botonly} ? ($game->seats)
1012 0 ▶ : (grep { $_ != $opt{seat} } $game->seats);
Branch: 2 branches
BranchTrueFalseTotal
$opt{'hotseat'} ? : 0 0 0
$opt{'botonly'} ? : 0 0 0
1013  
1014 0 my @levels = @{ $opt{level} };
1015 0 my %bots;
1016 0 my $i = 0;
1017 0 for my $seat (@bot_seats) {
1018 0 ▶ my $level = $levels[$i] // $levels[-1] // 3;
Condition0%0/5
($levels[$i] // $levels[-1]) // 3
l!l
00
$levels[$i] // $levels[-1]
l!l&&r!l&&!r
000
MC/DC0%0/2
($levels[$i] // $levels[-1]) // 3
$levels[$i] $levels[-1]
Truth table0%0/3
A // B
A= $levels[$i]B= $levels[-1]
ABresult
1X1
011
000
1019 0 $bots{$seat} = Game::Dominoes::Bot->new(level => $level, seed => $seat * 7919);
1020 0 $i++;
1021 }
1022  
1023 0 my $terminal = eval {
1024 $class->new(
1025 game => $game,
1026 bots => \%bots,
1027 (defined $opt{colour} ? (colour => $opt{colour}) : ()),
1028 ($opt{ascii} ? (ascii => 1) : ()),
1029 ($opt{compact} ? (compact => 1) : ()),
1030 (defined $opt{width} ? (width => $opt{width}) : ()),
1031 0 ▶ (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 6 branches
BranchTrueFalseTotal
defined $opt{'colour'} ? : 0 0 0
$opt{'ascii'} ? : 0 0 0
$opt{'compact'} ? : 0 0 0
defined $opt{'width'} ? : 0 0 0
defined $opt{'pick'} ? : 0 0 0
$opt{'pick'} ? : 0 0 0
1032 );
1033 };
1034  
1035 0 ▶ return _bad($@ || 'could not start') unless $terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $terminal 0 0 0
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
1036  
1037 0 local @SIG{qw(INT TERM HUP)};
1038 0 for my $sig (qw(INT TERM HUP)) {
1039 0 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
1040 }
1041  
1042 0 my $result = eval { $terminal->start };
1043 0 my $died = $@;
1044 0 $terminal->leave_raw;
1045 0 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 0 0
1046  
1047 0 ▶ return 1 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 0 0 0
1048 0 ▶ return 0 if $opt{hotseat} || $opt{botonly};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"hotseat"} or $opt{"botonly"} 0 0 0
Condition0%0/3
$opt{"hotseat"} or $opt{"botonly"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"hotseat"} or $opt{"botonly"}
$opt{"hotseat"} $opt{"botonly"}
Truth table0%0/3
A or B
A= $opt{"hotseat"}B= $opt{"botonly"}
ABresult
1X1
011
000
1049 0 ▶ return 0 if grep { $_ == $opt{seat} } @{ $result->winners };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $opt{"seat"};} @{$result->winners;} 0 0 0
1050 0 return 1;
1051 }
1052  
1053 3 1;
1054  
1055 __END__
↑ Summary Dominoes/Bot.pm »