blib/lib/Game/Dominoes/Terminal.pm

filter: Statementstmt 93.9% 709 / 755 Branchbran 76.6% 299 / 390 Conditioncond 56.5% 78 / 138 MC/DCmcdc 35.3% 29 / 82 Subroutinesub 98.8% 83 / 84 Podpod 96.8% 31 / 32 totaltotal 82.9% 1229 / 1481 CC 262 SCAR 39.7
CC
262
Cov
85%
_run
222.9
command
69.3
BUILD
20.8
CRAP 53.2
↑ Summary blib/lib/Game/Dominoes.pm »
1 package Game::Dominoes::Terminal;
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 28 ▶use Game::Dominoes;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 ▶use Game::Dominoes::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 28 ▶use Game::Dominoes::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 28 ▶use Game::Dominoes::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 28 ▶use Game::Dominoes::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 28 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 28 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 28 ▶use Game::Terminal::Kit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 28 ▶use Game::Terminal::Kit::Picker;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 28 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 28 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 28 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 28 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 28 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 28 our $VERSION = '0.05';
24  
25 28 our @PIPS = (
26 [],
27 [[1, 1]],
28 [[0, 0], [2, 2]],
29 [[0, 0], [1, 1], [2, 2]],
30 [[0, 0], [0, 2], [2, 0], [2, 2]],
31 [[0, 0], [0, 2], [1, 1], [2, 0], [2, 2]],
32 [[0, 0], [0, 2], [1, 0], [1, 2], [2, 0], [2, 2]],
33 );
34  
35 28 our %CHARS = (
36 wide => {
37 pip => "\x{25CF}",
38 h => "\x{2500}", v => "\x{2502}",
39 tl => "\x{250C}", tr => "\x{2510}", bl => "\x{2514}", br => "\x{2518}",
40 t => "\x{252C}", b => "\x{2534}", l => "\x{251C}", r => "\x{2524}",
41 },
42 ascii => {
43 pip => '*',
44 h => '-', v => '|',
45 tl => '+', tr => '+', bl => '+', br => '+',
46 t => '+', b => '+', l => '+', r => '+',
47 },
48 );
49  
50 28 our %MARKED = (
51 recent => {
52 wide => {
53 h => "\x{2550}", v => "\x{2551}",
54 tl => "\x{2554}", tr => "\x{2557}", bl => "\x{255A}", br => "\x{255D}",
55 t => "\x{2566}", b => "\x{2569}", l => "\x{2560}", r => "\x{2563}",
56 },
57 ascii => {
58 h => '=', v => '#',
59 tl => '#', tr => '#', bl => '#', br => '#',
60 t => '#', b => '#', l => '#', r => '#',
61 },
62 },
63 preview => {
64 wide => {
65 h => "\x{2501}", v => "\x{2503}",
66 tl => "\x{250F}", tr => "\x{2513}", bl => "\x{2517}", br => "\x{251B}",
67 t => "\x{2533}", b => "\x{253B}", l => "\x{2523}", r => "\x{252B}",
68 },
69 ascii => {
70 h => '=', v => '!',
71 tl => '+', tr => '+', bl => '+', br => '+',
72 t => '+', b => '+', l => '+', r => '+',
73 },
74 },
75 );
76  
77 28 our %MARK_INK = (
78 new => '1;32',
79 recent => '32',
80 preview => '1;36',
81 );
82  
83 28 our %MARK_FRAME = (
84 new => 'recent',
85 recent => 'recent',
86 preview => 'preview',
87 );
88  
89 28 our %PICK_KEY = (
90 'v' => 'live',
91 '?' => 'help',
92 't' => 'type',
93 ':' => 'command',
94 );
95  
96 28 our $WINDOW = 8;
97  
98 28 has game => (is => 'rw', isa => Object);
99  
100 28 has bots => (is => 'rw', isa => HashRef, default => {});
101  
102 28 has in => (is => 'rw');
103 28 has out => (is => 'rw');
104  
105 28 has colour => (is => 'rw', isa => Bool);
106 28 has ascii => (is => 'rw', isa => Bool, default => 0);
107  
108 28 has compact => (is => 'rw', isa => Bool, default => 0);
109  
110 28 has width => (is => 'rw', isa => Int, default => 0);
111  
112 28 has handover => (is => 'rw', isa => Bool);
113  
114 28 has quit => (is => 'rw', isa => Bool, default => 0);
115  
116 28 has recent => (is => 'rw', isa => ArrayRef, default => []);
117  
118 28 has keysource => (is => 'rw');
119  
120 28 has pending => (is => 'rw', isa => ArrayRef, default => []);
121  
122 28 has keyboard => (is => 'rw');
123 28 has screen => (is => 'rw');
124 28 has log => (is => 'rw');
125  
126 28 has history => (is => 'rw');
127 28 has save_file => (is => 'rw');
128  
129 28 has _picking => (is => 'rw', init_arg => 'picking', private => 1);
130 28 has _seen => (is => 'rw', isa => Int, default => 0, private => 1);
131 28 has _told => (is => 'rw', isa => HashRef, default => {}, private => 1);
132  
133 63 ▶sub BUILD {
Subroutine BUILD: called CC=19 SCAR=30.3
Pod undocumented
134 63 my ($self) = @_;
135 63 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 19 44 63
136 63 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 18 45 63
137 63 ▶ $self->game(Game::Dominoes->new(seed => "\0" x 32)) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 0 63 63
138  
139 63 ▶ my $tty = eval { -t $self->out } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->out } } ? : 0 63 63
140 63 ▶ unless (defined $self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->colour) 17 46 63
141 17 ▶ $self->colour(($tty && !Game::Terminal::Kit::Screen::no_colour()) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$tty && !Game::Terminal::Kit::Screen::no_colour() ? : 0 17 17
Condition33%1/3
$tty and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
1700
MC/DC0%0/2
$tty and not Game::Terminal::Kit::Screen::no_colour()
$tty not Game::Terminal::Kit::Screen::no_colour()
Truth table33%1/3
A and not(B)
A= $ttyB= not Game::Terminal::Kit::Screen::no_colour()
ABresult
0X0
100
111
142 }
143 63 ▶ unless (defined $self->handover) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->handover) 23 40 63
144 23 ▶ $self->handover($self->_humans > 1 ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_humans > 1 ? : 3 20 23
145 }
146 63 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 63 0 63
$self->keysource ? : 34 29 63
147 in => $self->in,
148 pending => $self->pending,
149 ($self->keysource ? (keysource => $self->keysource) : ()),
150 )) unless $self->keyboard;
151 63 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 3 branches
BranchTrueFalseTotal
unless $self->screen 63 0 63
$self->colour ? : 0 63 63
$self->ascii ? : 37 26 63
152 out => $self->out,
153 interactive => $tty,
154 colour => $self->colour ? 1 : 0,
155 ascii => $self->ascii ? 1 : 0,
156 )) unless $self->screen;
157 63 ▶ $self->log(Game::Terminal::Kit::Log->new(keep => $self->game->players))
Branch: 1 branch
BranchTrueFalseTotal
unless $self->log 63 0 63
158 unless $self->log;
159 63 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 58 5 63
160 63 ▶ $self->history->name('dominoes') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 63 63
161 63 $self->history->version($VERSION);
162 63 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 8 55 63
163 63 $self->history->start($self->game);
164 63 $self->_seen(scalar @{ $self->game->history });
165 63 $self->screen->utf8->flush_on;
166 63 return $self;
167 }
168  
169 64 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
170 return Game::Terminal::Kit::History->new(
171 name => 'dominoes',
172 version => $VERSION,
173 308 ▶ save => sub { $_[0]->to_text(with_seed => 1) },
Subroutine __ANON__: called CC=1 SCAR=0.0
174 16 ▶ load => sub { Game::Dominoes->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
175 64 );
176 }
177  
178 2519 ▶sub _in_step {
Subroutine _in_step: called CC=3 SCAR=11.0
179 2519 my ($self) = @_;
180 2519 my $screen = $self->screen;
181 2519 ▶ $screen->colour($self->colour ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 0 2519 2519
182 2519 ▶ $screen->ascii($self->ascii ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 2265 254 2519
183 2519 return $screen;
184 }
185  
186 23 ▶sub _humans {
Subroutine _humans: called CC=1 SCAR=0.0
187 23 my ($self) = @_;
188 23 return scalar grep { !$self->bots->{$_} } $self->game->seats;
189 }
190  
191 274 ▶sub remember {
Subroutine remember: called CC=7 SCAR=20.2
Pod documented
192 274 my ($self, $seat, $play) = @_;
193 274 ▶ 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 274 274
Condition33%2/6
ref $play and $play->can("tile") and $play->tile
!ll&&!rl&&r
00274
ref $play and $play->can("tile")
!ll&&!rl&&r
00274
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
194  
195 274 my $recent = $self->recent;
196 274 push @$recent, { seat => $seat, play => $play };
197  
198 274 ▶ shift @$recent while @$recent > $self->game->players;
Branch: 1 branch
BranchTrueFalseTotal
@$recent > $self->game->players and do { shift @$recent } 184 274 458
199  
200 274 $self->log->keep($self->game->players);
201 274 ▶ $self->log->record("seat $seat played " . $play->tile->stringify
Branch: 2 branches
BranchTrueFalseTotal
$play->points ? : 83 191 274
$play->spinner ? : 29 245 274
202 . ' on ' . $play->arm
203 . ($play->points ? ' for ' . $play->points : '')
204 . ($play->spinner ? ', the spinner' : ''));
205  
206 274 return $self;
207 }
208  
209 223 ▶sub table_marks {
Subroutine table_marks: called CC=3 SCAR=11.0
Pod documented
210 223 my ($self) = @_;
211 223 my $recent = $self->recent;
212 223 my %mark;
213 223 for my $i (0 .. $#$recent) {
214 $mark{ $recent->[$i]{play}->tile->id }
215 218 ▶ = $i == $#$recent ? 'new' : 'recent';
Branch: 1 branch
BranchTrueFalseTotal
$i == $recent->$#* ? : 114 104 218
216 }
217 223 return \%mark;
218 }
219  
220 135 ▶sub recent_lines {
Subroutine recent_lines: called CC=1 SCAR=0.0
Pod documented
221 135 my ($self) = @_;
222 135 return map { @$_ } @{ $self->log->recent };
223 }
224  
225 2253 ▶sub _say {
Subroutine _say: called CC=1 SCAR=0.0
226 2253 my ($self, @text) = @_;
227 2253 $self->screen->say(join '', @text);
228 2253 return;
229 }
230  
231 77 ▶sub _ask {
Subroutine _ask: called CC=1 SCAR=0.0
232 77 my ($self, $prompt) = @_;
233 77 return $self->keyboard->read_line($prompt, $self->out);
234 }
235  
236 222 ▶sub table {
Subroutine table: called CC=7 SCAR=19.6
Pod documented
237 222 my ($self, $view, $marks) = @_;
238 222 my $layout = $view->{layout};
239 222 ▶ $marks ||= $self->table_marks;
Condition66%2/3
$marks ||= $self->table_marks
l!l&&r!l&&!r
127950
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
240 222 ▶ return (' (nothing on the table yet)') unless $layout->count;
Branch: 1 branch
BranchTrueFalseTotal
unless $layout->count 45 177 222
241 177 ▶ return $self->_compact_table($layout, $marks) if $self->compact;
Branch: 1 branch
BranchTrueFalseTotal
if $self->compact 1 176 177
242  
243 176 my $spinner = $layout->spinner_index;
244 176 my $line = $layout->line;
245 my @cell = map {
246 176 $self->_tile_art($line->[$_]{left}, $line->[$_]{right},
247 $line->[$_]{tile}->is_double,
248 defined $spinner && $_ == $spinner,
249 $marks->{ $line->[$_]{tile}->id })
250 717 ▶ } 0 .. $#$line;
Condition100%3/3
defined $spinner && $_ == $spinner
!ll&&!rl&&r
198414105
MC/DC100%2/2
defined $spinner && $_ == $spinner
defined $spinner $_ == $spinner
Truth table100%3/3
A && B
A= defined $spinnerB= $_ == $spinner
ABresult
0X0
100
111
251 176 my @band = $self->_wrap(\@cell);
252 176 my ($first, $last) = ($band[0], $band[-1]);
253  
254 41 ▶ my $at = sub {
Subroutine __ANON__: called CC=5 SCAR=20.9
255 41 my ($run) = @_;
256 return 2 unless defined $spinner && $run && $spinner >= $run->{from}
257 41 ▶ && $spinner <= $run->{to};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spinner and $run and $spinner >= $run->{"from"} and $spinner <= $run->{"to"} 0 41 41
Condition33%3/9
defined $spinner and $run and $spinner >= $run->{"from"} and $spinner <= $run->{"to"}
!ll&&!rl&&r
0041
defined $spinner and $run and $spinner >= $run->{"from"}
!ll&&!rl&&r
0041
defined $spinner and $run
!ll&&!rl&&r
0041
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
258 41 return $run->{x}[ $spinner - $run->{from} ];
259 176 };
260  
261 176 my @rows;
262 176 ▶ if (@{ $layout->u }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$layout->u;}) 36 140 176
263 my @arm = map {
264 $self->_tile_art($_->{inner}, $_->{outer}, $_->{tile}->is_double, 0,
265 $marks->{ $_->{tile}->id })
266 36 } @{ $layout->u };
267 36 my $indent = $at->($first);
268 36 push @rows, map { $self->_band($_, $indent) } $self->_wrap(\@arm, $indent);
269 36 push @rows, $self->_stem($indent);
270 }
271  
272 176 push @rows, map { $self->_band($_, 2) } @band;
273  
274 176 ▶ if (@{ $layout->d }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$layout->d;}) 5 171 176
275 my @arm = map {
276 $self->_tile_art($_->{inner}, $_->{outer}, $_->{tile}->is_double, 0,
277 $marks->{ $_->{tile}->id })
278 5 } @{ $layout->d };
279 5 my $indent = $at->($last);
280 5 push @rows, $self->_stem($indent);
281 5 push @rows, map { $self->_band($_, $indent) } $self->_wrap(\@arm, $indent);
282 }
283 176 return @rows;
284 }
285  
286 1 ▶sub _compact_table {
Subroutine _compact_table: called CC=4 SCAR=14.0
287 1 my ($self, $layout, $marks) = @_;
288 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
289 1 my @rows;
290 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->u })
291 1 ▶ if @{ $layout->u };
Branch: 1 branch
BranchTrueFalseTotal
if @{$layout->u;} 1 0 1
292 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->line });
293 1 push @rows, ' ' . join(' ', map { $self->_tile($_->{tile}, $marks) } @{ $layout->d })
294 1 ▶ if @{ $layout->d };
Branch: 1 branch
BranchTrueFalseTotal
if @{$layout->d;} 1 0 1
295 1 return @rows;
296 }
297  
298 54 ▶sub _tile {
Subroutine _tile: called CC=6 SCAR=18.4
299 54 my ($self, $tile, $marks) = @_;
300  
301 54 ▶ my $mark = $marks ? $marks->{ $tile->id } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$marks ? : 5 49 54
302 54 ▶ my $text = ($mark ? '{' : '[') . $tile->high . '|' . $tile->low
Branch: 2 branches
BranchTrueFalseTotal
$mark ? : 0 54 54
$mark ? : 0 54 54
303 . ($mark ? '}' : ']');
304  
305 54 ▶ return $self->_paint($text, $MARK_INK{$mark}) if $mark;
Branch: 1 branch
BranchTrueFalseTotal
if $mark 0 54 54
306 54 ▶ return $tile->is_double ? $self->_paint($text, '1;33') : $text;
Branch: 1 branch
BranchTrueFalseTotal
$tile->is_double ? : 15 39 54
307 }
308  
309 2311 ▶sub _tile_art {
Subroutine _tile_art: called CC=9 SCAR=22.0
310 2311 my ($self, $one, $two, $across, $bright, $mark) = @_;
311 2311 ▶ my $kind = $self->ascii ? 'ascii' : 'wide';
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1727 584 2311
312 my $c = $mark && $MARK_FRAME{$mark}
313 ? $MARKED{ $MARK_FRAME{$mark} }{$kind}
314 2311 ▶ : $CHARS{$kind};
Branch: 1 branch
BranchTrueFalseTotal
$mark && $Game::Dominoes::Terminal::MARK_FRAME{$mark} ? : 339 1972 2311
Condition66%2/3
$mark and $Game::Dominoes::Terminal::MARK_FRAME{$mark}
!ll&&!rl&&r
19720339
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
315 2311 my @face = ($self->_face($one, $across), $self->_face($two, $across));
316  
317 2311 my @lines;
318 2311 ▶ if ($across) {
Branch: 1 branch
BranchTrueFalseTotal
if ($across) { } 149 2162 2311
319 149 push @lines, $c->{tl} . ($c->{h} x 3) . $c->{tr};
320 149 push @lines, map { $c->{v} . $_ . $c->{v} } @{ $face[0] };
321 149 push @lines, $c->{l} . ($c->{h} x 3) . $c->{r};
322 149 push @lines, map { $c->{v} . $_ . $c->{v} } @{ $face[1] };
323 149 push @lines, $c->{bl} . ($c->{h} x 3) . $c->{br};
324 }
325 else {
326 2162 push @lines, $c->{tl} . ($c->{h} x 3) . $c->{t} . ($c->{h} x 3) . $c->{tr};
327 push @lines, map {
328 2162 $c->{v} . $face[0][$_] . $c->{v} . $face[1][$_] . $c->{v}
329 6486 } 0 .. 2;
330 2162 push @lines, $c->{bl} . ($c->{h} x 3) . $c->{b} . ($c->{h} x 3) . $c->{br};
331 }
332  
333 2311 ▶ my $ink = $mark ? $MARK_INK{$mark} : ($bright ? '1;33' : undef);
Branch: 2 branches
BranchTrueFalseTotal
$mark ? : 339 1972 2311
$bright ? : 43 1929 1972
334 2311 ▶ @lines = map { $self->_paint($_, $ink) } @lines if defined $ink;
Branch: 1 branch
BranchTrueFalseTotal
if defined $ink 382 1929 2311
335  
336 2311 ▶ return { width => $across ? 5 : 9, lines => \@lines };
Branch: 1 branch
BranchTrueFalseTotal
$across ? : 149 2162 2311
337 }
338  
339 4622 ▶sub _face {
Subroutine _face: called CC=5 SCAR=16.1
340 4622 my ($self, $pips, $turned) = @_;
341 4622 ▶ my $pip = $CHARS{ $self->ascii ? 'ascii' : 'wide' }{pip};
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 3454 1168 4622
342 4622 my @grid = map { [ (' ') x 3 ] } 0 .. 2;
343 4622 ▶ for my $spot (@{ $PIPS[$pips] || [] }) {
Condition50%1/2
$Game::Dominoes::Terminal::PIPS[$pips] || []
l!l
46220
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
344 12813 my ($row, $col) = @$spot;
345 12813 ▶ ($row, $col) = ($col, 2 - $row) if $turned;
Branch: 1 branch
BranchTrueFalseTotal
if $turned 530 12283 12813
346 12813 $grid[$row][$col] = $pip;
347 }
348 4622 return [ map { join '', @$_ } @grid ];
349 }
350  
351 2417 ▶sub _paint {
Subroutine _paint: called CC=1 SCAR=0.0
352 2417 my ($self, $text, $code) = @_;
353 2417 return $self->_in_step->paint($text, $code);
354 }
355  
356 430 ▶sub _wrap {
Subroutine _wrap: called CC=8 SCAR=20.9
357 430 my ($self, $cells, $indent) = @_;
358 430 ▶ $indent = 2 unless defined $indent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 389 41 430
359 430 ▶ my $room = ($self->width || ($self->screen->size)[0]) - $indent;
Condition66%2/3
$self->width || ($self->screen->size)[0]
l!l&&r!l&&!r
3171130
MC/DC0%0/2
$self->width || ($self->screen->size)[0]
$self->width ($self->screen->size)[0]
Truth table66%2/3
A || B
A= $self->widthB= ($self->screen->size)[0]
ABresult
1X1
011
000
360 430 ▶ $room = 20 if $room < 20;
Branch: 1 branch
BranchTrueFalseTotal
if $room < 20 0 430 430
361  
362 430 my @run;
363 430 my ($cells_in, $x, @at) = ([], $indent);
364 430 my $from = 0;
365 430 for my $i (0 .. $#$cells) {
366 2311 my $cell = $cells->[$i];
367 2311 ▶ if (@$cells_in && $x + $cell->{width} - $indent > $room) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cells_in and $x + $cell->{"width"} - $indent > $room) 23 2288 2311
Condition100%3/3
@$cells_in and $x + $cell->{"width"} - $indent > $room
!ll&&!rl&&r
430185823
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
368 23 push @run, { cells => $cells_in, x => [@at], from => $from, to => $i - 1 };
369 23 ($cells_in, $x, @at) = ([], $indent);
370 23 $from = $i;
371 }
372 2311 push @$cells_in, $cell;
373 2311 push @at, $x;
374 2311 $x += $cell->{width};
375 }
376 430 ▶ push @run, { cells => $cells_in, x => [@at], from => $from, to => $#$cells }
Branch: 1 branch
BranchTrueFalseTotal
if @$cells_in 430 0 430
377 if @$cells_in;
378 430 return @run;
379 }
380  
381 453 ▶sub _band {
Subroutine _band: called CC=8 SCAR=20.8
382 453 my ($self, $run, $indent) = @_;
383 453 my @cells = @{ $run->{cells} };
384 453 my $height = 0;
385 453 for my $cell (@cells) {
386 2311 ▶ $height = scalar @{ $cell->{lines} } if @{ $cell->{lines} } > $height;
Branch: 1 branch
BranchTrueFalseTotal
if @{$cell->{"lines"};} > $height 546 1765 2311
387 }
388  
389 453 my @rows = map { ' ' x $indent } 1 .. $height;
390 453 for my $cell (@cells) {
391 2311 my $top = int(($height - scalar @{ $cell->{lines} }) / 2);
392 2311 for my $row (0 .. $height - 1) {
393 15417 my $line = $cell->{lines}[ $row - $top ];
394 $rows[$row] .= ($row >= $top && defined $line)
395 15417 ▶ ? $line : ' ' x $cell->{width};
Branch: 1 branch
BranchTrueFalseTotal
$row >= $top && defined $line ? : 13641 1776 15417
Condition100%3/3
$row >= $top and defined $line
!ll&&!rl&&r
88888813641
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
396 }
397 }
398  
399 453 s/\s+\z// for @rows;
400  
401 453 return @rows;
402 }
403  
404 41 ▶sub _stem {
Subroutine _stem: called CC=2 SCAR=7.1
405 41 my ($self, $indent) = @_;
406 41 ▶ my $c = $CHARS{ $self->ascii ? 'ascii' : 'wide' };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 41 0 41
407 41 return (' ' x ($indent + 2)) . $c->{v};
408 }
409  
410 175 ▶sub _ends_line {
Subroutine _ends_line: called CC=2 SCAR=6.9
411 175 my ($self, $view) = @_;
412 175 my $count = $view->{count};
413 175 my $short = (5 - $count % 5) % 5;
414 175 my $text = 'ends ' . join(' ', @{ $view->{ends} }) . ' count ' . $count;
415 175 ▶ $text .= $short ? " ($short more would score)" : ' (scores)';
Branch: 1 branch
BranchTrueFalseTotal
$short ? : 102 73 175
416 175 return $text;
417 }
418  
419 218 ▶sub _scores_line {
Subroutine _scores_line: called CC=1 SCAR=0.0
420 218 my ($self, $view) = @_;
421 return 'scores ' . join(' ', map {
422 482 "seat $_: $view->{scores}{$_}"
423 218 } sort { $a <=> $b } keys %{ $view->{scores} });
424 }
425  
426 212 ▶sub _hands_line {
Subroutine _hands_line: called CC=1 SCAR=0.0
427 212 my ($self, $view) = @_;
428 return 'tiles ' . join(' ', map {
429 468 "seat $_: $view->{counts}{$_}"
430 291 } sort { $a <=> $b } keys %{ $view->{counts} })
431 212 . ' boneyard: ' . $view->{boneyard};
432 }
433  
434 220 ▶sub hand_lines {
Subroutine hand_lines: called CC=4 SCAR=13.9
Pod documented
435 220 my ($self, $view) = @_;
436 220 ▶ my @tiles = @{ $view->{hand} || [] };
Condition50%1/2
$view->{'hand'} || []
l!l
2200
MC/DC0%0/1
$view->{'hand'} || []
$view->{'hand'}
Truth table50%1/2
A
A= $view->{'hand'}
Aresult
11
00
437 220 ▶ return ('your tiles: (none)') unless @tiles;
Branch: 1 branch
BranchTrueFalseTotal
unless @tiles 0 220 220
438 220 ▶ return ('your tiles: ' . join ' ', map { $self->_tile($_) } @tiles)
Branch: 1 branch
BranchTrueFalseTotal
if $self->compact 7 213 220
439 if $self->compact;
440  
441 my @cell = map {
442 213 my $art = $self->_tile_art($_->high, $_->low, 0, 0);
443 1490 push @{ $art->{lines} }, sprintf '%-9s', ' ' . $_->stringify . ' ';
444 1490 $art;
445 } @tiles;
446 213 return ('your tiles:', map { $self->_band($_, 2) } $self->_wrap(\@cell));
447 }
448  
449 77 ▶sub show {
Subroutine show: called CC=4 SCAR=13.9
Pod documented
450 77 my ($self, $view) = @_;
451 77 $self->_say('');
452 77 $self->_say("hand $view->{hand_number} " . $self->_scores_line($view));
453 77 $self->_say($self->_hands_line($view));
454 77 $self->_say('');
455 77 $self->_say($_) for $self->table($view);
456 77 $self->_say('');
457 77 ▶ $self->_say($self->_ends_line($view)) if $view->{layout}->count;
Branch: 1 branch
BranchTrueFalseTotal
if $view->{"layout"}->count 42 35 77
458 77 $self->_say($_) for $self->hand_lines($view);
459 77 return;
460 }
461  
462 203 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
463 203 my ($self, @set) = @_;
464 203 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 2 201 203
$set[0] ? : 0 2 2
465 203 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 138 65 203
$self->_picking ? : 120 18 138
466 65 return $self->keys_available;
467 }
468  
469 2 ▶sub raw {
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
470 2 my ($self) = @_;
471 2 return $self->keyboard->raw;
472 }
473  
474 66 ▶sub keys_available {
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
475 66 my ($self) = @_;
476 66 return $self->keyboard->keys_available;
477 }
478  
479 102 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=6.9
Pod documented
480 102 my ($self) = @_;
481 102 ▶ return $self->keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 101 1 102
482 }
483  
484 101 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
485 101 my ($self) = @_;
486 101 $self->keyboard->leave_raw;
487 101 return $self;
488 }
489  
490 154 ▶sub read_key {
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
491 154 my ($self) = @_;
492 154 return $self->keyboard->read_key;
493 }
494  
495 127 ▶sub preview {
Subroutine preview: called CC=1 SCAR=0.0
Pod documented
496 127 my ($self, $view, $move) = @_;
497  
498 127 my $trial = $view->{layout}->clone;
499 127 my $play = $trial->place($move->{tile}, $move->{arm});
500  
501 return ({
502 127 %$view,
503 layout => $trial,
504 count => Game::Dominoes::Scoring::count($trial),
505 ends => [ $trial->open_ends ],
506 }, $play);
507 }
508  
509 101 ▶sub picker {
Subroutine picker: called CC=3 SCAR=11.0
Pod documented
510 101 my ($self, $moves, $at) = @_;
511  
512 my @items = map {
513 101 my $label = sprintf '%-8s %s', $_->{tile}->stringify . '@' . $_->{arm},
514 495 ▶ $_->{points} ? "scores $_->{points}" : '';
Branch: 1 branch
BranchTrueFalseTotal
$_->{'points'} ? : 98 397 495
515 495 $label =~ s/\s+\z//;
516 495 +{ label => $label, value => $_ };
517 } @$moves;
518  
519 return Game::Terminal::Kit::Picker->new(
520 items => \@items,
521 window => $WINDOW,
522 101 ▶ game_keys => { %PICK_KEY, %{ $self->history->game_keys }, %{ Game::Terminal::Kit::Save::game_keys() } },
Branch: 1 branch
BranchTrueFalseTotal
defined $at ? : 0 101 101
523 log => $self->log,
524 screen => $self->_in_step,
525 (defined $at ? (at => $at) : ()),
526 );
527 }
528  
529 0 ▶sub choice_lines {
Subroutine choice_lines: not called CC=1 SCAR=6.9
Pod documented
530 0 my ($self, $moves, $at) = @_;
531 0 return $self->_rows($self->picker($moves, $at));
532 }
533  
534 135 ▶sub _rows {
Subroutine _rows: called CC=2 SCAR=6.9
535 135 my ($self, $picker) = @_;
536 135 ▶ return map { /\A\s+\d+ further / ? " $_" : " $_" } $picker->rows;
Branch: 1 branch
BranchTrueFalseTotal
/\A\s+\d+ further / ? : 37 688 725
537 }
538  
539 135 ▶sub legend {
Subroutine legend: called CC=1 SCAR=0.0
Pod documented
540 return (
541 135 'arrows to choose, enter to play, a number to jump to it',
542 'v for the table as it stands, ? for the commands, t to type, q to quit',
543 'u to take back your last move, r to play it again, S to save',
544 );
545 }
546  
547 102 ▶sub pick {
Subroutine pick: called CC=18 SCAR=29.0
Pod documented
548 102 my ($self, $view) = @_;
549  
550 102 my $seat = $view->{seat};
551 102 my $moves = $self->game->legal($seat);
552  
553 102 ▶ return $self->_typed_turn($view) unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 102 102
554  
555 102 ▶ unless ($self->enter_raw) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->enter_raw) 1 101 102
556 1 $self->picking(0);
557 1 return $self->_typed_turn($view);
558 }
559  
560 101 my $picker = $self->picker($moves);
561 101 my $log = $self->log;
562 101 my $live = 0;
563  
564 101 $self->screen->clear(scrollback => 1);
565  
566 101 while (1) {
567 135 $self->_show_choice($view, $picker, $live);
568  
569 135 my ($event, $value) = $picker->step($self->read_key);
570  
571 135 ▶ if ($event eq 'quit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "quit") 25 110 135
572 25 $self->leave_raw;
573 25 $self->quit(1);
574 25 return 0;
575 }
576  
577 110 ▶ if ($event eq 'chosen') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "chosen") 68 42 110
578 68 $self->leave_raw;
579 68 return $self->_play($seat, $value);
580 }
581  
582 42 ▶ if ($event eq 'back' || ($event eq 'none' && @{ $log->notes })) {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "back" or $event eq "none" and @{$log->notes;}) 1 41 42
Condition66%4/6
$event eq "back" or $event eq "none" and @{$log->notes;}
l!l&&r!l&&!r
0141
$event eq "none" and @{$log->notes;}
!ll&&!rl&&r
4101
MC/DC33%1/3
$event eq "back" or $event eq "none" and @{$log->notes;}
$event eq "back" $event eq "none" @{$log->notes;}
Truth table50%2/4
A or B and C
A= $event eq "back"B= $event eq "none"C= @{$log->notes;}
ABCresult
1XX1
0111
00X0
0100
583 1 $log->shown->note('that key does nothing here. ? for the ones that do');
584 1 next;
585 }
586  
587 41 ▶ next unless $event eq 'key';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "key" 25 16 41
588  
589 16 ▶ if ($value eq 'live') { $live = !$live; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "live") 2 14 16
590 0 ▶ if ($value eq 'help') { $log->note($self->help); next }
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "help") 0 14 14
591 14 ▶ if ($value eq 'save') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "save") 1 13 14
592 1 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->_in_step,
593 default => $self->save_default,
594 head => [ '', $self->table($view), '', $self->hand_lines($view), '' ]);
595 1 ▶ $log->note($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 0 1
596 1 next;
597 }
598 13 ▶ if ($value eq 'undo' || $value eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "undo" or $value eq "redo") 11 2 13
Condition100%3/3
$value eq "undo" or $value eq "redo"
l!l&&r!l&&!r
832
MC/DC100%2/2
$value eq "undo" or $value eq "redo"
$value eq "undo" $value eq "redo"
Truth table100%3/3
A or B
A= $value eq "undo"B= $value eq "redo"
ABresult
1X1
011
000
599 11 ▶ my $moved = $value eq 'undo' ? $self->take_back : $self->play_again;
Branch: 1 branch
BranchTrueFalseTotal
$value eq 'undo' ? : 8 3 11
600 11 ▶ next unless $moved;
Branch: 1 branch
BranchTrueFalseTotal
unless $moved 5 6 11
601 6 $self->leave_raw;
602 6 return 1;
603 }
604  
605 2 $self->leave_raw;
606 2 ▶ $self->picking(0) if $value eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "type" 1 1 2
607 2 return $self->_typed_turn($view);
608 }
609 }
610  
611 135 ▶sub _show_choice {
Subroutine _show_choice: called CC=6 SCAR=17.9
612 135 my ($self, $view, $picker, $live) = @_;
613  
614 135 my $log = $self->log;
615 135 my $move = $picker->current;
616  
617 135 my @lines = ('', "hand $view->{hand_number} " . $self->_scores_line($view),
618 $self->_hands_line($view));
619  
620 135 ▶ if (my @said = $self->recent_lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@said) = $self->recent_lines) 79 56 135
621 79 push @lines, '', map { " $_" } @said;
622 }
623  
624 135 my ($shown, $title);
625 135 ▶ if ($live) {
Branch: 1 branch
BranchTrueFalseTotal
if ($live) { } 8 127 135
626 8 $shown = $view;
627 8 $title = ' the table as it stands:';
628 }
629 else {
630 127 my ($peek) = $self->preview($view, $move);
631 127 $shown = $peek;
632 $title = ' if you play ' . $move->{tile}->stringify
633 127 . ' on ' . $move->{arm} . ':';
634 }
635  
636 push @lines, '', $title, $self->table($shown, $live ? undef : {
637 127 %{ $self->table_marks },
638 135 ▶ $move->{tile}->id => 'preview',
Branch: 1 branch
BranchTrueFalseTotal
$live ? : 8 127 135
639 });
640 135 push @lines, '';
641 135 ▶ push @lines, $self->_ends_line($shown) if $shown->{layout}->count;
Branch: 1 branch
BranchTrueFalseTotal
if $shown->{"layout"}->count 128 7 135
642  
643 135 push @lines, '', $self->hand_lines($view), '', $self->_rows($picker);
644  
645 135 ▶ if (my @notice = @{ $log->notes }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@notice) = @{$log->notes;}) 20 115 135
646 20 push @lines, '', map { " $_" } @notice;
647 }
648  
649 135 push @lines, '', map { " $_" } $self->legend;
650  
651 135 $self->screen->show(\@lines);
652 135 $log->shown;
653  
654 135 return;
655 }
656  
657 77 ▶sub _typed_turn {
Subroutine _typed_turn: called CC=2 SCAR=6.9
658 77 my ($self, $view) = @_;
659 77 $self->show($view);
660 77 $self->_say($_) for $self->_legal_lines($view);
661 77 my $line = $self->_ask("seat $view->{seat}> ");
662 77 return $self->command($line, $view);
663 }
664  
665 80 ▶sub _legal_lines {
Subroutine _legal_lines: called CC=3 SCAR=11.0
666 80 my ($self, $view) = @_;
667 80 my $moves = $self->game->legal($view->{seat});
668 80 ▶ return (' (nothing to play)') unless @$moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @$moves 0 80 80
669 80 my $i = 0;
670 return map {
671 80 my $line = sprintf(' %2d. %-8s %s', ++$i,
672 $_->{tile}->stringify . '@' . $_->{arm},
673 406 ▶ $_->{points} ? "scores $_->{points}" : '');
Branch: 1 branch
BranchTrueFalseTotal
$_->{'points'} ? : 84 322 406
674 406 $line =~ s/\s+\z//;
675 406 $line;
676 } @$moves;
677 }
678  
679 77 ▶sub command {
Subroutine command: called CC=39 SCAR=42.4
Pod documented
680 77 my ($self, $line, $view) = @_;
681 77 my $game = $self->game;
682 77 my $seat = $view->{seat};
683  
684 77 ▶ return 0 unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 76 77
685 76 $line =~ s/\A\s+//;
686 76 $line =~ s/\s+\z//;
687 76 ▶ return 1 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 76 76
688  
689 76 my ($word, @rest) = split /\s+/, $line;
690 76 $word = lc $word;
691  
692 76 ▶ if ($word eq 'quit' || $word eq 'q') { $self->quit(1); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "quit" or $word eq "q") 25 51 76
Condition100%3/3
$word eq "quit" or $word eq "q"
l!l&&r!l&&!r
121351
MC/DC100%2/2
$word eq "quit" or $word eq "q"
$word eq "quit" $word eq "q"
Truth table100%3/3
A or B
A= $word eq "quit"B= $word eq "q"
ABresult
1X1
011
000
693 51 ▶ 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 50 51
Condition66%2/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
1050
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
694 50 ▶ 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 47 50
Condition66%2/3
$word eq "table" or $word eq "board"
l!l&&r!l&&!r
3047
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
695 47 ▶ if ($word eq 'hand' || $word eq 'tiles') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "hand" or $word eq "tiles") 3 44 47
Condition66%2/3
$word eq "hand" or $word eq "tiles"
l!l&&r!l&&!r
3044
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
696 3 $self->_say($_) for $self->hand_lines($view);
697 3 return 1;
698 }
699 44 ▶ 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 42 44
Condition66%2/3
$word eq "ends" or $word eq "count"
l!l&&r!l&&!r
2042
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
700 42 ▶ if ($word eq 'scores') { $self->_say($self->_scores_line($view)); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "scores") 3 39 42
701 39 ▶ 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 36 39
Condition66%2/3
$word eq "legal" or $word eq "moves"
l!l&&r!l&&!r
3036
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
702 0 ▶ if ($word eq 'ascii') { $self->ascii(!$self->ascii); $self->_in_step->utf8; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "ascii") 0 36 36
703 0 ▶ if ($word eq 'type') { $self->picking(0); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "type") 0 36 36
704  
705 36 ▶ if ($word eq 'pick') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "pick") 0 36 36
706 0 ▶ if ($self->keys_available) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keys_available) 0 0 0
707 0 $self->picking(1);
708 0 return $self->pick($view);
709 }
710 0 $self->_say('the keys are not available here, so this game is typed.'
711 . ' Term::ReadKey and a terminal are what it takes');
712 0 return 1;
713 }
714  
715 0 ▶ if ($word eq 'compact') { $self->compact(!$self->compact); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "compact") 0 36 36
716 0 ▶ 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 36 36
Condition33%1/3
$word eq "colour" or $word eq "color"
l!l&&r!l&&!r
0036
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
717 0 ▶ 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 36 36
Condition33%1/3
$word eq "history" or $word eq "log"
l!l&&r!l&&!r
0036
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
718 36 ▶ if (my (undef, $file) = Game::Terminal::Kit::Save::command($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $file) = Game::Terminal::Kit::Save::command($line)) 12 24 36
719 12 $self->_say($self->save($file));
720 12 return 1;
721 }
722 24 ▶ if ($word eq 'undo') { $self->take_back; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "undo") 4 20 24
723 20 ▶ if ($word eq 'redo') { $self->play_again; return 1 }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "redo") 1 19 20
724  
725 19 ▶ if ($word eq 'hint') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "hint") 1 18 19
726 1 my $bot = Game::Dominoes::Bot->new(level => 4, seed => 1);
727 1 my $move = $bot->choose($game, $seat);
728 $self->_say($move
729 ? 'try ' . $move->{tile}->stringify . '@' . $move->{arm}
730 1 ▶ : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
$move ? : 1 0 1
731 1 return 1;
732 }
733  
734 0 ▶ 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 18 18
Condition33%1/3
$word eq "play" or $word eq "p"
l!l&&r!l&&!r
0018
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
735  
736 18 ▶ return $self->_play($seat, $line) if $line =~ /\A[0-6]-[0-6]/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A[0-6]-[0-6]/ 2 16 18
737  
738 16 ▶ if ($word =~ /\A\d+\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /\A\d+\z/) 13 3 16
739 13 my $moves = $game->legal($seat);
740 13 my $pick = $moves->[ $word - 1 ];
741 0 ▶ unless ($pick) { $self->_say('no such move'); return 1 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($pick) 0 13 13
742 13 return $self->_play($seat, $pick);
743 }
744  
745 3 $self->_say("I do not know '$word'. Try help.");
746 3 return 1;
747 }
748  
749 83 ▶sub _play {
Subroutine _play: called CC=9 SCAR=22.8
750 83 my ($self, $seat, $move) = @_;
751  
752 83 ▶ 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 83 83
Condition66%2/3
not ref $move and $move =~ /\A([0-6]-[0-6])\s+([LRUDlrud])\z/
!ll&&!rl&&r
8120
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
753 0 $move = "$1\@" . uc $2;
754 }
755  
756 83 ▶ 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 81 83
Condition66%2/3
not ref $move and $move =~ /\A([0-6]-[0-6])\z/
!ll&&!rl&&r
8102
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
757 2 my $want = Game::Dominoes::Notation::parse_tile($1);
758 2 my @fit = grep { $_->{tile}->id == $want->id } @{ $self->game->legal($seat) };
759  
760 2 ▶ if (@fit > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fit > 1) 0 2 2
761 $self->_say('which arm? ' . join(' ',
762 0 map { $_->{tile}->stringify . '@' . $_->{arm} } @fit));
763 0 return 1;
764 }
765 2 ▶ $move = @fit ? { tile => $want, arm => $fit[0]{arm} } : { tile => $want };
Branch: 1 branch
BranchTrueFalseTotal
@fit ? : 1 1 2
766 }
767  
768 83 my $out = $self->game->play($seat, $move);
769 83 ▶ if (ref $out eq 'Game::Dominoes::Error') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $out eq "Game::Dominoes::Error") 1 82 83
770 1 $self->_say('no: ' . $out->message);
771 1 return 1;
772 }
773 82 $self->remember($seat, $out);
774 82 ▶ $self->_say('played ' . $out->tile->stringify . ' on ' . $out->arm
Branch: 1 branch
BranchTrueFalseTotal
$out->points ? : 13 69 82
775 . ($out->points ? ' for ' . $out->points : ''));
776 82 return 1;
777 }
778  
779 1 ▶sub help {
Subroutine help: called CC=1 SCAR=0.0
Pod documented
780 return (
781 1 ' play <tile> [arm] or just 6-4, or the number from `legal`',
782 ' legal table hand ends scores history hint',
783 ' undo redo take back your last move and the replies to it, or play it again',
784 ' save [FILE] save the game, to carry on with --resume or watch with --replay',
785 ' pick type ascii colour compact quit',
786 ' arms are L and R along the line, U and D off the spinner',
787 ' a double is laid crosswise, which is how the table is drawn',
788 ' a tile in a heavy frame is one played since you last looked',
789 );
790 }
791  
792 55 ▶sub _handover {
Subroutine _handover: called CC=3 SCAR=18.1
793 55 my ($self, $seat) = @_;
794 55 ▶ return 1 unless $self->handover;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->handover 55 0 55
795 0 $self->_say('');
796 0 $self->_say(('') x 30);
797 0 my $line = $self->_ask("Pass to seat $seat, then press return: ");
798 0 ▶ return defined $line ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 0 0 0
799 }
800  
801 176 ▶sub person_turn {
Subroutine person_turn: called CC=9 SCAR=22.0
Pod documented
802 176 my ($self) = @_;
803 176 my $game = $self->game;
804 176 my $events = $game->history;
805 176 my ($drew, $dealt);
806 176 for my $event (@{$events}[ $self->_seen .. $#$events ]) {
807 191 ▶ $drew = 1 if $event->{kind} eq 'draw' && !$self->bots->{ $event->{seat} };
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "draw" and not $self->bots->{$event->{"seat"}} 9 182 191
Condition100%3/3
$event->{"kind"} eq "draw" and not $self->bots->{$event->{"seat"}}
!ll&&!rl&&r
17939
MC/DC100%2/2
$event->{"kind"} eq "draw" and not $self->bots->{$event->{"seat"}}
$event->{"kind"} eq "draw" not $self->bots->{$event->{"seat"}}
Truth table100%3/3
A and not(B)
A= $event->{"kind"} eq "draw"B= not $self->bots->{$event->{"seat"}}
ABresult
0X0
100
111
808 191 ▶ $dealt = 1 if $event->{kind} eq 'hand_end';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "hand_end" 2 189 191
809 }
810 176 $self->_seen(scalar @$events);
811 176 $self->history->turn($game);
812 176 ▶ return $self unless $drew || $dealt;
Branch: 1 branch
BranchTrueFalseTotal
unless $drew or $dealt 165 11 176
Condition100%3/3
$drew or $dealt
l!l&&r!l&&!r
92165
MC/DC100%2/2
$drew or $dealt
$drew $dealt
Truth table100%3/3
A or B
A= $drewB= $dealt
ABresult
1X1
011
000
813  
814 11 $self->history->clear;
815 11 ▶ $self->_once($drew ? 'draw' : 'deal', $drew
Branch: 2 branches
BranchTrueFalseTotal
$drew ? : 9 2 11
$drew ? : 9 2 11
816 ? 'Drawing from the boneyard: nothing before it can be taken back.'
817 : 'A new hand: nothing before it can be taken back.');
818 11 return $self;
819 }
820  
821 12 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
822 12 my ($self) = @_;
823 12 my $game = $self->history->undo;
824 12 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 3 9 12
825 3 $self->_remark('There is nothing to take back.');
826 3 return 0;
827 }
828 9 $self->swap($game);
829 9 $self->_remark('Taken back.');
830 9 return 1;
831 }
832  
833 4 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=6.9
Pod documented
834 4 my ($self) = @_;
835 4 my $game = $self->history->redo;
836 4 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 2 2 4
837 2 $self->_remark('There is nothing to play again.');
838 2 return 0;
839 }
840 2 $self->swap($game);
841 2 $self->_remark('Played again.');
842 2 return 1;
843 }
844  
845 11 ▶sub swap {
Subroutine swap: called CC=3 SCAR=11.0
Pod documented
846 11 my ($self, $game) = @_;
847 11 $self->game($game);
848 11 $self->_seen(scalar @{ $game->history });
849 11 $self->log->forget;
850 11 $self->recent([]);
851 11 my @plays = grep { $_->{kind} eq 'play' } @{ $game->history };
852 11 ▶ splice @plays, 0, @plays - $game->players if @plays > $game->players;
Branch: 1 branch
BranchTrueFalseTotal
if @plays > $game->players 2 9 11
853 11 $self->remember($_->{seat}, $_->{play}) for @plays;
854 11 return $self;
855 }
856  
857 24 ▶sub _remark {
Subroutine _remark: called CC=2 SCAR=6.9
858 24 my ($self, $text) = @_;
859 24 ▶ return $self->log->note($text) if $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking 18 6 24
860 6 $self->_say($text);
861 6 return $self;
862 }
863  
864 11 ▶sub _once {
Subroutine _once: called CC=2 SCAR=6.9
865 11 my ($self, $what, $text) = @_;
866 11 ▶ return $self if $self->_told->{$what}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_told->{$what}++ 3 8 11
867 8 return $self->_remark($text);
868 }
869  
870 56 ▶sub start {
Subroutine start: called CC=15 SCAR=27.1
Pod documented
871 56 my ($self) = @_;
872 56 my $game = $self->game;
873 56 $self->history->start($game);
874 56 $self->_seen(scalar @{ $game->history });
875  
876 56 $self->_say('Dominoes: All Fives, ' . $game->players . ' seats, to ' . $game->target);
877 56 $self->_say('Type help for the commands.');
878  
879 56 my $last_seat;
880 56 ▶ while ($self->game->status eq 'active' && !$self->quit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active" and not $self->quit) 358 5 363
Condition66%2/3
$self->game->status eq "active" and not $self->quit
!ll&&!rl&&r
50358
MC/DC50%1/2
$self->game->status eq "active" and not $self->quit
$self->game->status eq "active" not $self->quit
Truth table66%2/3
A and not(B)
A= $self->game->status eq "active"B= not $self->quit
ABresult
0X0
100
111
881 358 $game = $self->game;
882 358 my $seat = $game->turn;
883 358 ▶ last unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 0 358 358
884  
885 358 ▶ if (my $bot = $self->bots->{$seat}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $bot = $self->bots->{$seat}) 182 176 358
886 182 ▶ my $move = $bot->choose($game, $seat) or last;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $bot->choose($game, $seat) or last 182 0 182
887 182 my $out = $game->play($seat, $move);
888 182 ▶ last if ref $out eq 'Game::Dominoes::Error';
Branch: 1 branch
BranchTrueFalseTotal
if ref $out eq "Game::Dominoes::Error" 0 182 182
889 182 $self->remember($seat, $out);
890 182 ▶ $self->_say("seat $seat played " . $out->tile->stringify
Branch: 1 branch
BranchTrueFalseTotal
$out->points ? : 70 112 182
891 . ' on ' . $out->arm . ($out->points ? ' for ' . $out->points : ''));
892 182 next;
893 }
894  
895 176 ▶ if (!defined $last_seat || $last_seat != $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $last_seat or $last_seat != $seat) 55 121 176
Condition100%3/3
not defined $last_seat or $last_seat != $seat
l!l&&r!l&&!r
514121
MC/DC100%2/2
not defined $last_seat or $last_seat != $seat
not defined $last_seat $last_seat != $seat
Truth table100%3/3
not(A) or B
A= not defined $last_seatB= $last_seat != $seat
ABresult
1X1
011
000
896 55 ▶ last unless $self->_handover($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_handover($seat) 0 55 55
897 55 $last_seat = $seat;
898 }
899  
900 176 $self->person_turn;
901 176 my $view = $game->view($seat);
902  
903 176 ▶ if ($self->picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking) 102 74 176
904 102 ▶ last unless $self->pick($view);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->pick($view) 28 74 102
905 74 next;
906 }
907  
908 74 ▶ last unless $self->_typed_turn($view);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_typed_turn($view) 23 51 74
909 }
910  
911 56 $game = $self->game;
912 56 ▶ if (my $result = $game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $game->result) 5 51 56
913 5 $self->_say('');
914 5 $self->_say($result->stringify);
915 5 return $result;
916 }
917  
918 51 $self->_say('');
919 51 $self->_say('stopped');
920 51 return $game->result;
921 }
922  
923 16 ▶sub _person_seat {
Subroutine _person_seat: called CC=2 SCAR=7.0
924 16 my ($self) = @_;
925 16 my ($seat) = grep { !$self->bots->{$_} } $self->game->seats;
926 16 ▶ return defined $seat ? $seat : ($self->game->seats)[0];
Branch: 1 branch
BranchTrueFalseTotal
defined $seat ? : 16 0 16
927 }
928  
929 13 ▶sub settings {
Subroutine settings: called CC=4 SCAR=14.3
Pod documented
930 13 my ($self) = @_;
931 13 my $bots = $self->bots;
932 13 my @seats = $self->game->seats;
933 13 my @bot_seats = grep { $bots->{$_} } @seats;
934 return {
935 seat => $self->_person_seat,
936 hotseat => @bot_seats ? 0 : 1,
937 'bot-vs-bot' => @bot_seats == @seats ? 1 : 0,
938 13 ▶ (@bot_seats ? (level => join ',', map { $bots->{$_}->level } @bot_seats) : ()),
Branch: 3 branches
BranchTrueFalseTotal
@bot_seats ? : 13 0 13
@bot_seats == @seats ? : 0 13 13
@bot_seats ? : 13 0 13
939 };
940 }
941  
942 8 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=7.1
Pod documented
943 8 my ($self) = @_;
944 8 my $file = $self->history->file;
945 8 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('dominoes');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 8 0 8
946 }
947  
948 13 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
949 13 my ($self, $file) = @_;
950 13 ▶ $file = $self->save_default unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 7 6 13
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
706
MC/DC50%1/2
defined $file and length $file
defined $file length $file
Truth table66%2/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
951 13 my $history = $self->history;
952 13 $history->settings($self->settings);
953 13 $history->version($VERSION);
954 13 my $path = eval { $history->saved($self->game)->write_file($file) };
955 13 ▶ return 'the game was not saved: ' . Game::Terminal::Kit::Save::why($@) unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 13 13
956 13 $history->file($path);
957 13 return "the game is saved to $path";
958 }
959  
960 9 ▶sub read_save {
Subroutine read_save: called CC=1 SCAR=0.0
Pod documented
961 9 my ($class, $file) = @_;
962 9 return Game::Terminal::Kit::Save->read_game($file, 'dominoes');
963 }
964  
965 3 ▶sub replay_lines {
Subroutine replay_lines: called CC=4 SCAR=14.1
Pod documented
966 3 my ($self, $game) = @_;
967 3 my $was = $self->game;
968 3 $self->game($game);
969 3 my @lines = eval {
970 3 my $view = $game->view($self->_person_seat);
971 3 my $result = $game->result;
972 ("hand $view->{hand_number} " . $self->_scores_line($view), '',
973 $self->table($view), '',
974 3 ▶ ($view->{layout}->count ? ($self->_ends_line($view), '') : ()),
Branch: 2 branches
BranchTrueFalseTotal
$view->{'layout'}->count ? : 3 0 3
$result ? : 0 3 3
975 $self->hand_lines($view),
976 ($result ? ('', $result->stringify) : ()));
977 };
978 3 my $died = $@;
979 3 $self->game($was);
980 3 ▶ die $died unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 3 3
981 3 return @lines;
982 }
983  
984 16 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
985 16 return <<'USAGE';
986 dominoes - draw dominoes with All Fives scoring
987  
988 dominoes [options]
989  
990 --players=N 2, 3 or 4 (default 2)
991 --seat=N which seat the human takes (default 1)
992 --hotseat every seat is a person, with a hand-over between turns
993 --bot-vs-bot nobody is a person
994 --level=N bot level 1 to 5, repeatable, one per bot seat
995 --target=N default 250 at two seats, 200 at three or four
996 --scale=N divide every score, for the cribbage board variation
997 (use --scale=5 --target=61 together, never one alone)
998 --variant=S all_fives, draw or block
999 --reserve=N tiles at the back of the boneyard nobody may draw
1000 --seed=S reproduce a game exactly
1001 --ascii plain ASCII only
1002 --compact tiles as [6|4] on one line instead of drawn
1003 --width=N what the drawn table may use before it wraps (default: the
1004 terminal's width)
1005 --colour force colour on or off (--nocolour)
1006 --nopick type the moves out instead of choosing them with the keys
1007 --save=FILE where S, or save, writes the game: FILE, else the file it
1008 was resumed from, else dominoes.save
1009 --resume=FILE carry on a saved game, with its undo and redo
1010 --replay=FILE watch a saved game a turn at a time; enter plays on
1011 --version print the version
1012 --help this
1013  
1014 Arms are L and R along the main line, U and D off the spinner.
1015  
1016 On a terminal the legal moves are listed under the table and chosen with
1017 the arrow keys: the table above the list is the one the move under the
1018 cursor would make, with its new count and what it would score, so a
1019 placement is seen before it is played. Enter plays it, v shows the table as
1020 it stands, t goes back to typing and ? prints the commands. Needs
1021 Term::ReadKey; without it, or off a terminal, the moves are typed out.
1022  
1023 u takes back your last move and every reply to it, and r plays them again;
1024 typed, they are undo and redo. S saves the game; typed, it is save. Nothing before a tile you drew from the
1025 boneyard, or before a new hand, can be taken back.
1026  
1027 The tile each seat just played is marked on the table, because at four
1028 seats three of them land between your turns.
1029  
1030 Exit status is 0 if your seat won or nobody was playing a seat, 1 if you
1031 lost or stopped early, 2 for a bad option.
1032 USAGE
1033 }
1034  
1035 8 ▶sub _bad {
Subroutine _bad: called CC=1 SCAR=0.0
1036 8 my ($why) = @_;
1037 8 $why =~ s/ at \S+ line \d+\.?\s*\z//;
1038 8 $why =~ s/\A[\w:]+: //;
1039 8 chomp $why;
1040 8 print {\*STDERR} "dominoes: $why\n";
1041 8 return 2;
1042 }
1043  
1044 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
1045 return [
1046 2 { name => 'players', type => 'int', min => 2, max => 4, help => 'seats at the table' },
1047 { name => 'seat', type => 'int', min => 1, max => 4, help => 'which seat you take' },
1048 { name => 'level', type => 'int', min => 1, max => 5, help => "the bots' level" },
1049 { name => 'variant', type => 'enum', choices => [qw(all_fives draw block)], help => 'the rules' },
1050 { name => 'target', type => 'int', min => 50, max => 500, help => 'the score that wins' },
1051 { name => 'hotseat', type => 'flag', help => 'every seat is a person' },
1052 { name => 'bot-vs-bot', type => 'flag', help => 'nobody is a person' },
1053 { name => 'seed', type => 'str', help => 'reproduce a game exactly' },
1054 ];
1055 }
1056  
1057 38 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod documented
1058 38 my ($class, @argv) = @_;
1059 38 my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case));
1060 38 my $status = $class->_run(\@argv);
1061 38 Getopt::Long::Configure($saved);
1062 38 return $status;
1063 }
1064  
1065 38 ▶sub _run {
Subroutine _run: called CC=62 SCAR=54.1
1066 38 my ($class, $argv) = @_;
1067 38 my %opt = (level => []);
1068  
1069 Getopt::Long::GetOptionsFromArray($argv,
1070 'players=i' => \$opt{players},
1071 'seat=i' => \$opt{seat},
1072 'hotseat' => \$opt{hotseat},
1073 'bot-vs-bot' => \$opt{botonly},
1074 'level=i@' => $opt{level},
1075 'target=i' => \$opt{target},
1076 'scale=i' => \$opt{scale},
1077 'variant=s' => \$opt{variant},
1078 'seed=s' => \$opt{seed},
1079 'reserve=i' => \$opt{reserve},
1080 'ascii' => \$opt{ascii},
1081 'compact' => \$opt{compact},
1082 'width=i' => \$opt{width},
1083 'colour!' => \$opt{colour},
1084 'color!' => \$opt{colour},
1085 'pick!' => \$opt{pick},
1086 'version' => \$opt{version},
1087 'help|h|?' => \$opt{help},
1088 'save=s' => \$opt{save},
1089 'resume=s' => \$opt{resume},
1090 'replay=s' => \$opt{replay},
1091 38 ▶ ) 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"}, "save=s", \$opt{"save"}, "resume=s", \$opt{"resume"}, "replay=s", \$opt{"replay"}) or do { print {\*STDERR;} _usage(); return 2 } 37 1 38
1092  
1093 37 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 15 22 37
1094  
1095 22 ▶ if ($opt{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 0 22 22
1096 0 print "dominoes (Game::Dominoes) $Game::Dominoes::VERSION\n";
1097 0 return 0;
1098 }
1099  
1100 22 ▶ if (@{$argv}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$argv;}) 0 22 22
1101 0 print {\*STDERR} "dominoes: there is nothing to do with '$argv->[0]': options begin with --\n", _usage();
1102 0 return 2;
1103 }
1104 22 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) { return _bad($why) }
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) 2 20 22
1105  
1106 20 my ($game, $save, $history);
1107 20 ▶ my $from = defined $opt{resume} ? $opt{resume} : $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 10 10 20
1108 20 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 12 8 20
1109 12 my %new = (players => 'players', variant => 'variant', target => 'target', scale => 'scale',
1110 reserve => 'reserve', seed => 'seed');
1111 12 for my $name (grep { defined $opt{$_} } sort keys %new) {
1112 4 ▶ return _bad("--$name begins a new game, and --" . (defined $opt{resume} ? 'resume' : 'replay')
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 4 0 4
1113 . ' starts from a saved one');
1114 }
1115 8 ($save, my $why) = $class->read_save($from);
1116 8 ▶ return _bad($why) unless $save;
Branch: 1 branch
BranchTrueFalseTotal
unless $save 2 6 8
1117 6 my $settings = $save->settings;
1118 6 ▶ $opt{seat} = $settings->{seat} if !defined $opt{seat} && defined $settings->{seat};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opt{"seat"} and defined $settings->{"seat"} 6 0 6
Condition33%1/3
not defined $opt{"seat"} and defined $settings->{"seat"}
!ll&&!rl&&r
006
MC/DC0%0/2
not defined $opt{"seat"} and defined $settings->{"seat"}
not defined $opt{"seat"} defined $settings->{"seat"}
Truth table33%1/3
not(A) and B
A= not defined $opt{"seat"}B= defined $settings->{"seat"}
ABresult
0X0
100
111
1119 6 ▶ $opt{hotseat} = 1 if !defined $opt{hotseat} && !defined $opt{botonly} && $settings->{hotseat};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"hotseat"} 0 6 6
Condition33%2/6
not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"hotseat"}
!ll&&!rl&&r
060
defined $opt{'hotseat'} || defined $opt{'botonly'}
l!l&&r!l&&!r
006
MC/DC0%0/3
not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"hotseat"}
defined $opt{'hotseat'} defined $opt{'botonly'} $settings->{"hotseat"}
Truth table25%1/4
not(A || B) and C
A= defined $opt{'hotseat'}B= defined $opt{'botonly'}C= $settings->{"hotseat"}
ABCresult
1XX0
01X0
0000
0011
1120 6 ▶ $opt{botonly} = 1 if !defined $opt{hotseat} && !defined $opt{botonly} && $settings->{'bot-vs-bot'};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"bot-vs-bot"} 0 6 6
Condition33%2/6
not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"bot-vs-bot"}
!ll&&!rl&&r
060
defined $opt{'hotseat'} || defined $opt{'botonly'}
l!l&&r!l&&!r
006
MC/DC0%0/3
not defined $opt{"hotseat"} || defined $opt{"botonly"} and $settings->{"bot-vs-bot"}
defined $opt{'hotseat'} defined $opt{'botonly'} $settings->{"bot-vs-bot"}
Truth table25%1/4
not(A || B) and C
A= defined $opt{'hotseat'}B= defined $opt{'botonly'}C= $settings->{"bot-vs-bot"}
ABCresult
1XX0
01X0
0000
0011
1121 6 ▶ @{ $opt{level} } = split /,/, $settings->{level} if !@{ $opt{level} } && defined $settings->{level};
Branch: 1 branch
BranchTrueFalseTotal
if not @{$opt{"level"};} and defined $settings->{"level"} 5 1 6
Condition66%2/3
not @{$opt{"level"};} and defined $settings->{"level"}
!ll&&!rl&&r
105
MC/DC50%1/2
not @{$opt{"level"};} and defined $settings->{"level"}
not @{$opt{"level"};} defined $settings->{"level"}
Truth table66%2/3
not(A) and B
A= not @{$opt{"level"};}B= defined $settings->{"level"}
ABresult
0X0
100
111
1122 6 $history = _new_history();
1123 6 ▶ unless (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{'replay'}) { } 1 5 6
1124 5 $game = eval { $history->resume($save) };
1125 5 ▶ return _bad("'" . $save->file . "' cannot be carried on: " . Game::Terminal::Kit::Save::why($@)) unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 0 5 5
1126 }
1127 else {
1128 1 $game = eval { Game::Dominoes->from_text(($save->steps)[0]) };
1129 1 ▶ return _bad("'" . $save->file . "' cannot be watched: " . Game::Terminal::Kit::Save::why($@)) unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 0 1 1
1130 }
1131 6 $opt{players} = $game->players;
1132 }
1133 14 ▶ $opt{players} = 2 unless defined $opt{players};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"players"} 8 6 14
1134 14 ▶ $opt{seat} = 1 unless defined $opt{seat};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"seat"} 8 6 14
1135 14 ▶ $opt{variant} = 'all_fives' unless defined $opt{variant};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"variant"} 14 0 14
1136  
1137 14 ▶ 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 14 14
1138 return _bad("--seat must be a seat from 1 to $opt{players}")
1139 unless $opt{seat} =~ /\A\d+\z/
1140 14 ▶ && $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 14 14
Condition33%2/6
$opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1 and $opt{"seat"} <= $opt{"players"}
!ll&&!rl&&r
0014
$opt{"seat"} =~ /\A\d+\z/ and $opt{"seat"} >= 1
!ll&&!rl&&r
0014
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 table25%1/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
1141  
1142 14 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 8 6 14
1143 my $seed = defined $opt{seed}
1144 ? substr(($opt{seed} x 32), 0, 32)
1145 0 ▶ : pack('N8', map { int rand 2**32 } 1 .. 8);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 8 0 8
1146  
1147 8 $game = eval {
1148 Game::Dominoes->new(
1149 seed => $seed,
1150 players => $opt{players},
1151 variant => $opt{variant},
1152 (defined $opt{target} ? (target => $opt{target}) : ()),
1153 (defined $opt{scale} ? (scale => $opt{scale}) : ()),
1154 8 ▶ (defined $opt{reserve} ? (reserve => $opt{reserve}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
defined $opt{'target'} ? : 0 8 8
defined $opt{'scale'} ? : 0 8 8
defined $opt{'reserve'} ? : 0 8 8
1155 );
1156 };
1157  
1158 8 ▶ return _bad($@ || 'could not deal') unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 0 8 8
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
1159 }
1160  
1161 my @bot_seats =
1162 $opt{hotseat} ? ()
1163 : $opt{botonly} ? ($game->seats)
1164 14 ▶ : (grep { $_ != $opt{seat} } $game->seats);
Branch: 2 branches
BranchTrueFalseTotal
$opt{'hotseat'} ? : 0 14 14
$opt{'botonly'} ? : 0 14 14
1165  
1166 14 my @levels = @{ $opt{level} };
1167 14 my %bots;
1168 14 my $i = 0;
1169 14 for my $seat (@bot_seats) {
1170 14 ▶ my $level = $levels[$i] // $levels[-1] // 3;
Condition40%2/5
($levels[$i] // $levels[-1]) // 3
l!l
140
$levels[$i] // $levels[-1]
l!l&&r!l&&!r
1400
MC/DC0%0/2
($levels[$i] // $levels[-1]) // 3
$levels[$i] $levels[-1]
Truth table33%1/3
A // B
A= $levels[$i]B= $levels[-1]
ABresult
1X1
011
000
1171 14 $bots{$seat} = Game::Dominoes::Bot->new(level => $level, seed => $seat * 7919);
1172 14 $i++;
1173 }
1174  
1175 14 my $terminal = eval {
1176 $class->new(
1177 game => $game,
1178 bots => \%bots,
1179 ($history && !defined $opt{replay} ? (history => $history) : ()),
1180 (defined $opt{save} ? (save_file => $opt{save}) : ()),
1181 (defined $opt{colour} ? (colour => $opt{colour}) : ()),
1182 ($opt{ascii} ? (ascii => 1) : ()),
1183 ($opt{compact} ? (compact => 1) : ()),
1184 (defined $opt{width} ? (width => $opt{width}) : ()),
1185 14 ▶ (defined $opt{pick} ? (picking => $opt{pick} ? 1 : 0) : ()),
Branch: 8 branches
BranchTrueFalseTotal
$history && !defined($opt{'replay'}) ? : 5 9 14
defined $opt{'save'} ? : 7 7 14
defined $opt{'colour'} ? : 0 14 14
$opt{'ascii'} ? : 0 14 14
$opt{'compact'} ? : 0 14 14
defined $opt{'width'} ? : 0 14 14
defined $opt{'pick'} ? : 0 14 14
$opt{'pick'} ? : 0 0 0
Condition100%3/3
$history and not defined $opt{"replay"}
!ll&&!rl&&r
815
MC/DC100%2/2
$history and not defined $opt{"replay"}
$history not defined $opt{"replay"}
Truth table100%3/3
A and not(B)
A= $historyB= not defined $opt{"replay"}
ABresult
0X0
100
111
1186 );
1187 };
1188  
1189 14 ▶ return _bad($@ || 'could not start') unless $terminal;
Branch: 1 branch
BranchTrueFalseTotal
unless $terminal 0 14 14
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
1190  
1191 14 ▶ if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"replay"}) 1 13 14
1192 my $replay = Game::Terminal::Kit::Replay->new(
1193 save => $save,
1194 3 ▶ load => sub { Game::Dominoes->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1195 3 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1196 1 ▶ head => [ 'dominoes, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 1 0 1
1197 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
1198 screen => $terminal->screen,
1199 );
1200 my ($event, $step) = Game::Terminal::Kit::Run::guard(
1201 1 ▶ sub { $replay->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
1202 1 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 0 1
1203 0 my $resumed = eval { $terminal->history->resume($save->from_step($step)) };
1204 0 ▶ return _bad("'" . $save->file . "' cannot be carried on: " . Game::Terminal::Kit::Save::why($@)) unless $resumed;
Branch: 1 branch
BranchTrueFalseTotal
unless $resumed 0 0 0
1205 0 $terminal->swap($resumed);
1206 }
1207  
1208 my $result = Game::Terminal::Kit::Run::guard(
1209 13 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen,
Subroutine __ANON__: called CC=1 SCAR=0.0
1210 );
1211  
1212 13 ▶ return 1 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 13 0 13
1213 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
1214 0 ▶ return 0 if grep { $_ == $opt{seat} } @{ $result->winners };
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $opt{"seat"};} @{$result->winners;} 0 0 0
1215 0 return 1;
1216 }
1217  
1218 28 1;
1219  
1220 __END__
↑ Summary blib/lib/Game/Dominoes.pm »