Backgammon/Terminal.pm

filter: Statementstmt 78.3% 329 / 420 Branchbran 67.8% 156 / 230 Conditioncond 51.6% 61 / 118 MC/DCmcdc 31.3% 21 / 67 Subroutinesub 86.0% 43 / 50 Podpod 88.8% 8 / 9 totaltotal 69.1% 618 / 894 CC 171 SCAR 32.4
CC
171
Cov
71%
run
182.0
_pick_turn
58.6
step
30.5
CRAP 25.6
↑ Summary Backgammon/Notation.pm »
1 package Game::Backgammon::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::Backgammon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Game::Backgammon::Board ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Game::Backgammon::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 3 our $VERSION = '0.03';
14  
15 3 my %CHECKER = (
16 white => { ascii => 'O', wide => "\x{25CB}", colour => '1;37', ink => '1;38;5;231' },
17 black => { ascii => 'X', wide => "\x{25CF}", colour => '1;36', ink => '1;38;5;16' },
18 );
19  
20 3 my %SEQUENCE = (
21 'A' => 'up',
22 'B' => 'down',
23 'C' => 'right',
24 'D' => 'left',
25 'H' => 'home',
26 'F' => 'end',
27 '1~' => 'home',
28 '4~' => 'end',
29 '5~' => 'page_up',
30 '6~' => 'page_down',
31 '7~' => 'home',
32 '8~' => 'end',
33 );
34  
35 3 my %CONTROL = (
36 "\r" => 'enter',
37 "\n" => 'enter',
38 "\t" => 'tab',
39 "\x7f" => 'backspace',
40 "\x08" => 'backspace',
41 "\x03" => 'interrupt',
42 "\x04" => 'eof',
43 );
44  
45 3 my %GROUND = (
46 light => '48;5;180',
47 dark => '48;5;94',
48 from => '48;5;136',
49 to => '48;5;28',
50 hit => '48;5;124',
51 played => '48;5;24',
52 );
53  
54 3 ▶use constant ROWS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 has game => (
57 is => 'rw',
58 isa => Object
59 3 );
60  
61 has out => (
62 is => 'ro',
63 isa => Any,
64 4 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
65 3 );
66  
67 has in => (
68 is => 'ro',
69 isa => Any,
70 18 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
71 3 );
72  
73 has mode => (
74 is => 'ro',
75 isa => Str,
76 default => 'bot'
77 3 );
78  
79 has level => (
80 is => 'ro',
81 isa => Int,
82 default => 3
83 3 );
84  
85 has seat => (
86 is => 'ro',
87 isa => Str,
88 default => 'white'
89 3 );
90  
91 has view => (
92 is => 'rw',
93 isa => Str,
94 default => ''
95 3 );
96  
97 has ascii => (
98 is => 'rw',
99 isa => Bool,
100 default => 0
101 3 );
102  
103 has [qw/colour interactive picking/] => (
104 is => 'rw'
105 3 );
106  
107 has highlight => (
108 is => 'rw',
109 isa => HashRef,
110 23 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
111 3 );
112  
113 has raw => (
114 is => 'rw',
115 isa => Bool,
116 default => 0
117 3 );
118  
119 has keysource => (
120 is => 'rw',
121 isa => CodeRef
122 3 );
123  
124 has pending => (
125 is => 'rw',
126 isa => ArrayRef,
127 23 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
128 3 );
129  
130 23 ▶sub BUILD {
Subroutine BUILD: called CC=10 SCAR=23.5
Pod undocumented
131 23 my ($self) = @_;
132 23 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 5 18 23
-t $self->in ? : 0 5 5
133 23 ▶ $self->colour($self->interactive && !$ENV{NO_COLOR} ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 5 18 23
$self->interactive && !$ENV{'NO_COLOR'} ? : 0 5 5
Condition33%1/3
$self->interactive and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
500
MC/DC0%0/2
$self->interactive and not $ENV{"NO_COLOR"}
$self->interactive not $ENV{"NO_COLOR"}
Truth table33%1/3
A and not(B)
A= $self->interactiveB= not $ENV{"NO_COLOR"}
ABresult
0X0
100
111
134 unless defined $self->colour;
135 23 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 10 13 23
$self->interactive ? : 0 10 10
136 23 ▶ $self->picking(0) unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 10 13 23
137 23 ▶ binmode $self->out, ':encoding(UTF-8)' unless $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ascii 6 17 23
138  
139 23 my $previous = select $self->out;
140 23 $| = 1;
141 23 select $previous;
142 23 return;
143 }
144  
145 837 ▶sub say_to { my ($self, @what) = @_; my $fh = $self->out; print {$fh} @what, "\n"; return }
Subroutine say_to: called CC=1 SCAR=0.0
Pod documented
146  
147 221 ▶sub board_lines {
Subroutine board_lines: called CC=4 SCAR=14.0
Pod documented
148 221 my ($self, $view) = @_;
149 221 ▶ $view ||= $self->view || $self->game->turn;
Condition50%3/6
$view ||= $self->view || $self->game->turn
l!l&&r!l&&!r
22010
$self->view || $self->game->turn
l!l&&r!l&&!r
010
MC/DC0%0/3
$view ||= $self->view || $self->game->turn
$view $self->view $self->game->turn
Truth table0%0/4
A ||= B || C
A= $viewB= $self->viewC= $self->game->turn
ABCresult
1XX1
01X1
0011
0000
150 221 my $board = $self->game->board;
151 221 my $other = Game::Backgammon::Board::other($view);
152  
153 221 my @lines = (
154 $self->_border(13 .. 24),
155 $self->_half($view, [13 .. 24], 0),
156 ' |' . (' ' x 18) . '|' . $self->_label('BAR') . '|'
157 . (' ' x 18) . '|' . $self->_label('OFF') . '|',
158 $self->_half($view, [reverse 1 .. 12], 1),
159 $self->_border(reverse 1 .. 12)
160 );
161  
162 221 $lines[1] .= sprintf ' %-5s %3d pips', ucfirst $other, $board->pip_count($other);
163 221 $lines[11] .= sprintf ' %-5s %3d pips', ucfirst $view, $board->pip_count($view);
164 221 ▶ if (my $last = $self->game->history->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->game->history->[-1]) 195 26 221
165 195 $lines[6] .= sprintf ' last: %s %s', $last->player, $last->notation;
166 }
167 221 push @lines, sprintf ' the points are numbered from %s', $view;
168 221 return \@lines;
169 }
170  
171 215 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
172 215 my ($self, $view) = @_;
173 215 return join "\n", @{ $self->board_lines($view) };
174 }
175  
176 442 ▶sub _border {
Subroutine _border: called CC=1 SCAR=0.0
177 442 my ($self, @points) = @_;
178 442 my @cell = map { my $c = sprintf '%2d-', $_; $c =~ s/\A /-/; $c } @points;
179 442 return ' ' . $self->_label(
180 '+' . join('', @cell[0 .. 5]) . '+---+' . join('', @cell[6 .. 11]) . '+---+'
181 );
182 }
183  
184 442 ▶sub _half {
Subroutine _half: called CC=8 SCAR=20.8
185 442 my ($self, $view, $points, $bottom) = @_;
186 442 my $board = $self->game->board;
187 442 my $other = Game::Backgammon::Board::other($view);
188 442 my @lines;
189 442 for my $row (0 .. ROWS - 1) {
190 2210 ▶ my $edge = $bottom ? ROWS - 1 - $row : $row;
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 1105 1105 2210
191 2210 ▶ my $middle = $bottom ? $row : ROWS - 1 - $row;
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 1105 1105 2210
192 2210 my @cell;
193 2210 for my $n (@$points) {
194 26520 my ($mine, $theirs) = $board->point_for($view, $n);
195 26520 ▶ push @cell, $mine
Branch: 1 branch
BranchTrueFalseTotal
$mine ? : 5080 21440 26520
196 ? $self->_cell($view, $mine, $edge, $n)
197 : $self->_cell($other, $theirs, $edge, $n);
198 }
199 2210 ▶ my $bar = $bottom
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 1105 1105 2210
200 ? $self->_cell($other, $board->bar($other), $middle)
201 : $self->_cell($view, $board->bar($view), $middle);
202 2210 ▶ my $off = $bottom
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 1105 1105 2210
203 ? $self->_cell($view, $board->off($view), $edge)
204 : $self->_cell($other, $board->off($other), $edge);
205 2210 push @lines, ' ' . $self->_label('|') . join('', @cell[0 .. 5])
206 . $self->_label('|') . $bar . $self->_label('|')
207 . join('', @cell[6 .. 11]) . $self->_label('|') . $off
208 . $self->_label('|');
209 }
210 442 return @lines;
211 }
212  
213 30940 ▶sub _cell {
Subroutine _cell: called CC=8 SCAR=20.8
214 30940 my ($self, $player, $count, $edge, $point) = @_;
215 30940 ▶ return $self->_ground($point, ' ') unless $count && $edge < ROWS;
Branch: 1 branch
BranchTrueFalseTotal
unless $count and $edge < 5 19490 11450 30940
Condition66%2/3
$count and $edge < 5
!ll&&!rl&&r
19490011450
MC/DC50%1/2
$count and $edge < 5
$count $edge < 5
Truth table66%2/3
A and B
A= $countB= $edge < 5
ABresult
0X0
100
111
216 11450 ▶ return $self->_ground(
Branch: 2 branches
BranchTrueFalseTotal
if $edge == 4 and $count > 5 155 11295 11450
$count > 9 ? : 62 93 155
Condition100%3/3
$edge == 4 and $count > 5
!ll&&!rl&&r
91602135155
MC/DC100%2/2
$edge == 4 and $count > 5
$edge == 4 $count > 5
Truth table100%3/3
A and B
A= $edge == 4B= $count > 5
ABresult
0X0
100
111
217 $point, $count > 9 ? "$count " : " $count ", $player
218 ) if $edge == ROWS - 1 && $count > ROWS;
219 11295 ▶ return $self->_ground($point, ' ') unless $edge < $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $edge < $count 5412 5883 11295
220 return $self->_ground(
221 5883 ▶ $point, ' ' . $CHECKER{$player}{$self->ascii ? 'ascii' : 'wide'} . ' ', $player
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 530 5353 5883
222 );
223 }
224  
225 30940 ▶sub _ground {
Subroutine _ground: called CC=8 SCAR=20.8
226 30940 my ($self, $point, $text, $player) = @_;
227 return $player
228 ? $self->_paint($text, $CHECKER{$player}{colour})
229 30940 ▶ : $text
Branch: 2 branches
BranchTrueFalseTotal
unless $self->colour and defined $point 30700 240 30940
$player ? : 5978 24722 30700
Condition100%3/3
$self->colour and defined $point
!ll&&!rl&&r
3066040240
MC/DC100%2/2
$self->colour and defined $point
$self->colour defined $point
Truth table100%3/3
A and B
A= $self->colourB= defined $point
ABresult
0X0
100
111
230 unless $self->colour && defined $point;
231  
232 240 my $mark = $self->highlight->{$point};
233 240 ▶ $mark = $point % 2 ? 'dark' : 'light' unless $mark && $GROUND{$mark};
Branch: 2 branches
BranchTrueFalseTotal
unless $mark and $GROUND{$mark} 235 5 240
$point % 2 ? : 115 120 235
Condition66%2/3
$mark and $GROUND{$mark}
!ll&&!rl&&r
23505
MC/DC50%1/2
$mark and $GROUND{$mark}
$mark $GROUND{$mark}
Truth table66%2/3
A and B
A= $markB= $GROUND{$mark}
ABresult
0X0
100
111
234 240 my @code = ($GROUND{$mark});
235 240 ▶ push @code, $CHECKER{$player}{ink} if $player;
Branch: 1 branch
BranchTrueFalseTotal
if $player 60 180 240
236 240 return "\e[" . join(';', @code) . "m$text\e[0m";
237 }
238  
239 11934 ▶sub _label { my ($self, $text) = @_; return $self->_paint($text, '2') }
Subroutine _label: called CC=1 SCAR=0.0
240  
241 17924 ▶sub _paint {
Subroutine _paint: called CC=2 SCAR=6.9
242 17924 my ($self, $text, $code) = @_;
243 17924 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 17816 108 17924
244 108 return "\e[${code}m" . $text . "\e[0m";
245 }
246  
247 214 ▶sub clear {
Subroutine clear: called CC=2 SCAR=10.9
Pod documented
248 214 my ($self) = @_;
249 214 ▶ return unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 214 0 214
250 0 my $fh = $self->out;
251 0 print {$fh} "\e[2J\e[H";
252 0 return;
253 }
254  
255 23 ▶sub _keys_available {
Subroutine _keys_available: called CC=5 SCAR=21.7
256 23 my ($self) = @_;
257 23 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 13 10 23
258 10 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 10 0 10
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0100
MC/DC0%0/2
defined $self->in and -t $self->in
defined $self->in -t $self->in
Truth table33%1/3
A and B
A= defined $self->inB= -t $self->in
ABresult
0X0
100
111
259 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
260 }
261  
262 13 ▶sub _enter_raw {
Subroutine _enter_raw: called CC=5 SCAR=24.2
263 13 my ($self) = @_;
264 13 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 13 13
265 13 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 13 0 13
266 13 $self->raw(1);
267 13 return $self;
268 }
269 0 ▶ return undef unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 0 0 0
270 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
271 0 $self->raw(1);
272 0 return $self;
273 }
274  
275 5 ▶sub _leave_raw {
Subroutine _leave_raw: called CC=3 SCAR=18.1
276 5 my ($self) = @_;
277 5 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 5 0 5
278 0 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 0 0
279 0 $self->raw(0);
280 0 return $self;
281 }
282  
283 51 ▶sub _read_char {
Subroutine _read_char: called CC=6 SCAR=27.1
284 51 my ($self, $wait) = @_;
285 51 my $pending = $self->pending;
286 51 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 2 49 51
287 49 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 49 0 49
288 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
289 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
290 0 select undef, undef, undef, 0.05;
291 0 return Term::ReadKey::ReadKey(-1, $self->in);
292 }
293  
294 31 ▶sub _read_key {
Subroutine _read_key: called CC=4 SCAR=13.9
295 31 my ($self) = @_;
296 31 my $char = $self->_read_char(1);
297 31 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 29 31
298 29 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 14 15 29
299 15 ▶ return $self->_read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 10 5 15
300 5 return $char;
301 }
302  
303 10 ▶sub _read_sequence {
Subroutine _read_sequence: called CC=8 SCAR=21.0
304 10 my ($self) = @_;
305 10 my $opener = $self->_read_char(0);
306 10 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 10 10
307 10 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 8 10
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
712
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
308 2 unshift @{ $self->pending }, $opener;
309 2 return 'escape';
310 }
311 8 my $tail = '';
312 8 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 10 0 10
313 10 my $char = $self->_read_char(0);
314 10 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 10 10
315 10 $tail .= $char;
316 10 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 8 2 10
317 }
318 8 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$SEQUENCE{$tail} || 'escape'
l!l
80
MC/DC0%0/1
$SEQUENCE{$tail} || 'escape'
$SEQUENCE{$tail}
Truth table50%1/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
319 }
320  
321 197 ▶sub step {
Subroutine step: called CC=15 SCAR=34.2
Pod documented
322 197 my ($self) = @_;
323 197 my $game = $self->game;
324 197 ▶ return 0 if $game->status ne 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $game->status ne "active" 0 197 197
325  
326 197 my $who = $game->turn;
327 197 ▶ my $picking = $self->picking && $self->raw
Condition16%2/12
$self->picking && $self->raw && ($self->mode eq 'hotseat' || $self->mode eq 'bot' && $who eq $self->seat)
!ll&&!rl&&r
19700
$self->picking and $self->raw
!ll&&!rl&&r
19700
$self->mode eq "hotseat" or $self->mode eq "bot" and $who eq $self->seat
l!l&&r!l&&!r
000
$self->mode eq "bot" and $who eq $self->seat
!ll&&!rl&&r
000
MC/DC0%0/5
$self->picking && $self->raw && ($self->mode eq 'hotseat' || $self->mode eq 'bot' && $who eq $self->seat)
$self->picking $self->raw $self->mode eq "hotseat" $self->mode eq "bot" $who eq $self->seat
Truth table16%1/6
A and B && C or D and E
A= $self->pickingB= $self->rawC= $self->mode eq "hotseat"D= $self->mode eq "bot"E= $who eq $self->seat
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
328 && ($self->mode eq 'hotseat' || ($self->mode eq 'bot' && $who eq $self->seat));
329 197 ▶ unless ($picking) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($picking) 197 0 197
330 197 $self->clear;
331 197 $self->say_to($self->render($who));
332 197 $self->say_to(sprintf ' %s to play %s', $who, join '-', @{ $game->dice });
333 }
334  
335 197 my $turns = $game->legal_turns;
336 197 ▶ if ($turns->[0]->is_forfeit && @$turns == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turns->[0]->is_forfeit and @$turns == 1) 24 173 197
Condition66%2/3
$turns->[0]->is_forfeit and @$turns == 1
!ll&&!rl&&r
173024
MC/DC50%1/2
$turns->[0]->is_forfeit and @$turns == 1
$turns->[0]->is_forfeit @$turns == 1
Truth table66%2/3
A and B
A= $turns->[0]->is_forfeitB= @$turns == 1
ABresult
0X0
100
111
337 24 ▶ if ($picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($picking) 0 24 24
338 0 $self->clear;
339 0 ▶ $self->say_to($self->render($self->view || $who));
Condition0%0/3
$self->view || $who
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->view || $who
$self->view $who
Truth table0%0/3
A || B
A= $self->viewB= $who
ABresult
1X1
011
000
340 0 $self->say_to(sprintf ' %s to play %s', $who, join '-', @{ $game->dice });
341 }
342 24 $self->say_to(' no play');
343 24 $game->play($turns->[0]);
344 24 ▶ return $game->status eq 'active' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$game->status eq 'active' ? : 24 0 24
345 }
346  
347 173 my $turn = $self->_ask($who, $turns);
348 173 ▶ return 0 unless $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $turn 2 171 173
349 171 ▶ $game->play($turn) or do { $self->say_to(' ' . $@->message); return 1 };
Branch: 1 branch
BranchTrueFalseTotal
$game->play($turn) or do { $self->say_to(' ' . $@->message); return 1 } 171 0 171
350 171 $self->say_to(' ' . $turn->notation);
351 171 ▶ return $game->status eq 'active' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$game->status eq 'active' ? : 168 3 171
352 }
353  
354 173 ▶sub _ask {
Subroutine _ask: called CC=13 SCAR=26.3
355 173 my ($self, $who, $turns) = @_;
356 173 ▶ return Game::Backgammon::Bot->new(level => $self->level)->choose($self->game)
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "watch" or $self->mode eq "bot" and $who ne $self->seat 127 46 173
Condition83%5/6
$self->mode eq "watch" or $self->mode eq "bot" and $who ne $self->seat
l!l&&r!l&&!r
735446
$self->mode eq "bot" and $who ne $self->seat
!ll&&!rl&&r
04654
MC/DC66%2/3
$self->mode eq "watch" or $self->mode eq "bot" and $who ne $self->seat
$self->mode eq "watch" $self->mode eq "bot" $who ne $self->seat
Truth table75%3/4
A or B and C
A= $self->mode eq "watch"B= $self->mode eq "bot"C= $who ne $self->seat
ABCresult
1XX1
0111
00X0
0100
357 if $self->mode eq 'watch'
358 || ($self->mode eq 'bot' && $who ne $self->seat);
359  
360 46 ▶ if ($self->picking && $self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking and $self->raw) 0 46 46
Condition33%1/3
$self->picking and $self->raw
!ll&&!rl&&r
4600
MC/DC0%0/2
$self->picking and $self->raw
$self->picking $self->raw
Truth table33%1/3
A and B
A= $self->pickingB= $self->raw
ABresult
0X0
100
111
361 0 my $turn = $self->_pick_turn($who, $turns);
362 0 $self->highlight({});
363 0 return $turn;
364 }
365  
366 46 $self->say_to($_) for @{ $self->offer_lines($turns) };
367 46 my $fh = $self->in;
368 46 my $out = $self->out;
369 46 while (1) {
370 49 print {$out} " $who> ";
371 49 my $line = <$fh>;
372 49 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 48 49
373 48 $line =~ s/\s+\z//;
374 48 ▶ return undef if $line eq 'q' || $line eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "q" or $line eq "quit" 1 47 48
Condition66%2/3
$line eq "q" or $line eq "quit"
l!l&&r!l&&!r
1047
MC/DC50%1/2
$line eq "q" or $line eq "quit"
$line eq "q" $line eq "quit"
Truth table66%2/3
A or B
A= $line eq "q"B= $line eq "quit"
ABresult
1X1
011
000
375 47 ▶ return $turns->[ $line - 1 ] if $line =~ /\A\d+\z/ && $line >= 1 && $line <= @$turns;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\d+\z/ and $line >= 1 and $line <= @$turns 44 3 47
Condition100%6/6
$line =~ /\A\d+\z/ and $line >= 1 and $line <= @$turns
!ll&&!rl&&r
2144
$line =~ /\A\d+\z/ and $line >= 1
!ll&&!rl&&r
1145
MC/DC100%3/3
$line =~ /\A\d+\z/ and $line >= 1 and $line <= @$turns
$line =~ /\A\d+\z/ $line >= 1 $line <= @$turns
Truth table100%4/4
A and B and C
A= $line =~ /\A\d+\z/B= $line >= 1C= $line <= @$turns
ABCresult
0XX0
10X0
1100
1111
376 3 $self->say_to(' pick a number from the list, or q to stop');
377 }
378 }
379  
380 30 ▶sub _matching {
Subroutine _matching: called CC=5 SCAR=16.1
381 30 my ($self, $turns, $chosen) = @_;
382 30 my @match;
383 30 TURN: for my $turn (@$turns) {
384 1061 my $moves = $turn->moves;
385 1061 ▶ next TURN if @$moves < @$chosen;
Branch: 1 branch
BranchTrueFalseTotal
if @$moves < @$chosen 0 1061 1061
386 1061 for my $at (0 .. $#$chosen) {
387 765 ▶ next TURN if $moves->[$at]->notation ne $chosen->[$at]->notation;
Branch: 1 branch
BranchTrueFalseTotal
if $moves->[$at]->notation ne $chosen->[$at]->notation 670 95 765
388 }
389 391 push @match, $turn;
390 }
391 30 return \@match;
392 }
393  
394 30 ▶sub _next_moves {
Subroutine _next_moves: called CC=4 SCAR=13.9
395 30 my ($self, $match, $chosen) = @_;
396 30 my (@move, %seen);
397 30 for my $turn (@$match) {
398 474 ▶ my $move = $turn->moves->[ scalar @$chosen ] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $turn->moves->[scalar @$chosen] or next 467 7 474
399 467 ▶ next if $seen{ $move->notation }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$move->notation}++ 320 147 467
400 147 push @move, $move;
401 }
402 30 return \@move;
403 }
404  
405 18 ▶sub _complete {
Subroutine _complete: called CC=3 SCAR=11.0
406 18 my ($self, $match, $chosen) = @_;
407 18 for my $turn (@$match) {
408 141 ▶ return $turn if @{ $turn->moves } == @$chosen;
Branch: 1 branch
BranchTrueFalseTotal
if @{$turn->moves;} == @$chosen 6 135 141
409 }
410 12 return undef;
411 }
412  
413 17 ▶sub _move_marks {
Subroutine _move_marks: called CC=8 SCAR=20.8
414 17 my ($self, $view, $player, $chosen, $hover) = @_;
415 17 my %mark;
416 42 ▶ my $seen = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
417 42 my ($point) = @_;
418 42 ▶ return undef if $point eq 'bar' || $point eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $point eq "bar" or $point eq "off" 2 40 42
Condition100%3/3
$point eq "bar" or $point eq "off"
l!l&&r!l&&!r
1140
MC/DC100%2/2
$point eq "bar" or $point eq "off"
$point eq "bar" $point eq "off"
Truth table100%3/3
A or B
A= $point eq "bar"B= $point eq "off"
ABresult
1X1
011
000
419 40 ▶ return $view eq $player ? $point : 25 - $point;
Branch: 1 branch
BranchTrueFalseTotal
$view eq $player ? : 38 2 40
420 17 };
421 17 for my $move (@$chosen) {
422 5 for my $end ($move->from, $move->to) {
423 10 ▶ my $point = $seen->($end) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $point = &$seen($end) or next 10 0 10
424 10 $mark{$point} = 'played';
425 }
426 }
427 17 ▶ if ($hover) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hover) 16 1 17
428 16 ▶ if (my $to = $seen->($hover->to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to = &$seen($hover->to)) 15 1 16
429 15 ▶ $mark{$to} = $hover->hit ? 'hit' : 'to';
Branch: 1 branch
BranchTrueFalseTotal
$hover->hit ? : 0 15 15
430 }
431 16 ▶ if (my $from = $seen->($hover->from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = &$seen($hover->from)) 15 1 16
432 15 $mark{$from} = 'from';
433 }
434 }
435 17 return \%mark;
436 }
437  
438 5 ▶sub _pick_turn {
Subroutine _pick_turn: called CC=28 SCAR=40.7
439 5 my ($self, $who, $turns) = @_;
440 5 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 5 5
441 5 ▶ my $view = $self->view || $who;
Condition33%1/3
$self->view || $who
l!l&&r!l&&!r
050
MC/DC0%0/2
$self->view || $who
$self->view $who
Truth table33%1/3
A || B
A= $self->viewB= $who
ABresult
1X1
011
000
442 5 my @chosen;
443 5 my $at = 0;
444 5 my @notice;
445  
446 5 while (1) {
447 15 my $match = $self->_matching($turns, \@chosen);
448 15 my $next = $self->_next_moves($match, \@chosen);
449 15 my $done = $self->_complete($match, \@chosen);
450 15 ▶ return $done if $done && !@$next;
Branch: 1 branch
BranchTrueFalseTotal
if $done and not @$next 3 12 15
Condition66%2/3
$done and not @$next
!ll&&!rl&&r
1203
MC/DC50%1/2
$done and not @$next
$done not @$next
Truth table66%2/3
A and not(B)
A= $doneB= not @$next
ABresult
0X0
100
111
451  
452 12 ▶ $at = $#$next if $at > $#$next;
Branch: 1 branch
BranchTrueFalseTotal
if $at > $next->$#* 0 12 12
453 12 ▶ $at = 0 if $at < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $at < 0 0 12 12
454 12 $self->highlight(
455 $self->_move_marks($view, $who, \@chosen, $next->[$at])
456 );
457 12 $self->_draw_choice($who, $view, $next, $at, \@chosen, $done, \@notice);
458 12 @notice = ();
459  
460 12 my $key = $self->_read_key;
461 12 ▶ return undef if !defined $key || $key eq 'eof' || $key eq 'interrupt';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $key or $key eq "eof" or $key eq "interrupt" 1 11 12
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
1011
not defined $key or $key eq "eof"
l!l&&r!l&&!r
1011
MC/DC33%1/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table50%2/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
462  
463 11 ▶ if ($key eq 'up' || $key eq 'left' || $key eq 'k') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "up" or $key eq "left" or $key eq "k") 0 11 11
Condition33%2/6
$key eq "up" or $key eq "left" or $key eq "k"
l!l&&r!l&&!r
0011
$key eq "up" or $key eq "left"
l!l&&r!l&&!r
0011
MC/DC0%0/3
$key eq "up" or $key eq "left" or $key eq "k"
$key eq "up" $key eq "left" $key eq "k"
Truth table25%1/4
A or B or C
A= $key eq "up"B= $key eq "left"C= $key eq "k"
ABCresult
1XX1
01X1
0011
0000
464 0 $at = ($at - 1) % @$next;
465 0 next;
466 }
467 11 ▶ if ($key eq 'down' || $key eq 'right' || $key eq 'tab' || $key eq 'j') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "down" or $key eq "right" or $key eq "tab" or $key eq "j") 1 10 11
Condition66%6/9
$key eq "down" or $key eq "right" or $key eq "tab" or $key eq "j"
l!l&&r!l&&!r
1010
$key eq "down" or $key eq "right" or $key eq "tab"
l!l&&r!l&&!r
1010
$key eq "down" or $key eq "right"
l!l&&r!l&&!r
1010
MC/DC25%1/4
$key eq "down" or $key eq "right" or $key eq "tab" or $key eq "j"
$key eq "down" $key eq "right" $key eq "tab" $key eq "j"
Truth table40%2/5
A or B or C or D
A= $key eq "down"B= $key eq "right"C= $key eq "tab"D= $key eq "j"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
468 1 $at = ($at + 1) % @$next;
469 1 next;
470 }
471 10 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 7 3 10
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
703
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
472 7 push @chosen, $next->[$at];
473 7 $at = 0;
474 7 next;
475 }
476 3 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 2 1 3
477 2 ▶ @notice = ('nothing taken back: no move chosen yet') unless @chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless @chosen 1 1 2
478 2 pop @chosen;
479 2 $at = 0;
480 2 next;
481 }
482 1 ▶ if ($key =~ /\A[1-9]\z/ && $key <= @$next) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A[1-9]\z/ and $key <= @$next) 0 1 1
Condition33%1/3
$key =~ /\A[1-9]\z/ and $key <= @$next
!ll&&!rl&&r
100
MC/DC0%0/2
$key =~ /\A[1-9]\z/ and $key <= @$next
$key =~ /\A[1-9]\z/ $key <= @$next
Truth table33%1/3
A and B
A= $key =~ /\A[1-9]\z/B= $key <= @$next
ABresult
0X0
100
111
483 0 $at = $key - 1;
484 0 next;
485 }
486 1 ▶ if ($key eq 'f' && $done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "f" and $done) 0 1 1
Condition33%1/3
$key eq "f" and $done
!ll&&!rl&&r
100
MC/DC0%0/2
$key eq "f" and $done
$key eq "f" $done
Truth table33%1/3
A and B
A= $key eq "f"B= $done
ABresult
0X0
100
111
487 0 return $done;
488 }
489 1 ▶ if ($key eq 'v') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "v") 0 1 1
490 0 $self->view($view = Game::Backgammon::Board::other($view));
491 0 next;
492 }
493 1 ▶ if ($key eq 'q') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "q") 1 0 1
494 1 $self->highlight({});
495 1 return undef;
496 }
497 0 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
498 0 @notice = @{ $self->_key_lines };
499 0 next;
500 }
501 0 @notice = ('that key does nothing here, ? for the ones that do');
502 }
503 }
504  
505 0 ▶sub _key_lines {
Subroutine _key_lines: not called CC=1 SCAR=6.9
506 return [
507 0 'up and down choose a move, enter plays it',
508 'backspace takes back the move chosen before it',
509 'a digit jumps to that move, v turns the board round',
510 'f finishes the turn where one can end, q stops the game',
511 ];
512 }
513  
514 12 ▶sub _draw_choice {
Subroutine _draw_choice: called CC=5 SCAR=16.2
515 12 my ($self, $who, $view, $next, $at, $chosen, $done, $notice) = @_;
516 12 $self->clear;
517 12 $self->say_to($self->render($view));
518 12 $self->say_to(sprintf ' %s to play %s', $who, join '-', @{ $self->game->dice });
519 $self->say_to(
520 @$chosen
521 12 ▶ ? ' so far: ' . join ' ', map { $_->notation } @$chosen
Branch: 1 branch
BranchTrueFalseTotal
@$chosen ? : 4 8 12
522 : ' nothing chosen yet'
523 );
524 12 $self->say_to('');
525 12 $self->say_to($_) for @{ $self->_choice_lines($next, $at) };
526 12 ▶ if ($done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done) 0 12 12
527 0 $self->say_to('');
528 0 $self->say_to(' f finishes the turn here');
529 }
530 12 $self->say_to(' ' . $_) for @$notice;
531 12 $self->say_to('');
532 12 $self->say_to(' up and down to choose, enter to play it, ? for the keys');
533 12 return;
534 }
535  
536 12 ▶sub _choice_lines {
Subroutine _choice_lines: called CC=3 SCAR=11.0
537 12 my ($self, $next, $at) = @_;
538 12 my @lines;
539 12 for my $index (0 .. $#$next) {
540 55 my $move = $next->[$index];
541 55 my $text = sprintf '%2d) %-9s %s',
542 $index + 1, $move->notation, $self->_describe($move);
543 55 $text =~ s/\s+\z//;
544 55 ▶ push @lines, $index == $at
Branch: 1 branch
BranchTrueFalseTotal
$index == $at ? : 12 43 55
545 ? ' ' . $self->_paint("> $text", '1;7')
546 : ' ' . $text;
547 }
548 12 return \@lines;
549 }
550  
551 55 ▶sub _describe {
Subroutine _describe: called CC=5 SCAR=17.0
552 55 my ($self, $move) = @_;
553 55 my @note;
554 55 ▶ push @note, 'from the bar' if $move->is_bar;
Branch: 1 branch
BranchTrueFalseTotal
if $move->is_bar 0 55 55
555 55 ▶ push @note, 'bears off' if $move->is_off;
Branch: 1 branch
BranchTrueFalseTotal
if $move->is_off 0 55 55
556 55 ▶ push @note, 'hits' if $move->hit;
Branch: 1 branch
BranchTrueFalseTotal
if $move->hit 0 55 55
557 55 ▶ push @note, sprintf 'with the %d', $move->die unless @note;
Branch: 1 branch
BranchTrueFalseTotal
unless @note 55 0 55
558 55 return join ', ', @note;
559 }
560  
561 48 ▶sub offer_lines {
Subroutine offer_lines: called CC=6 SCAR=17.9
Pod documented
562 48 my ($self, $turns) = @_;
563 48 my @item = map { sprintf '%2d) %s', $_ + 1, $turns->[$_]->notation } 0 .. $#$turns;
564 48 my $width = 0;
565 48 for my $item (@item) {
566 322 ▶ $width = length $item if length $item > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $item > $width 134 188 322
567 }
568 48 ▶ my $columns = int(74 / ($width + 2)) || 1;
Condition50%1/2
int 74 / ($width + 2) || 1
l!l
480
MC/DC0%0/1
int 74 / ($width + 2) || 1
int 74 / ($width + 2)
Truth table50%1/2
A
A= int 74 / ($width + 2)
Aresult
11
00
569 48 ▶ $columns = 4 if $columns > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $columns > 4 6 42 48
570 48 my @lines;
571 48 ▶ while (@item) {
Branch: 1 branch
BranchTrueFalseTotal
if (@item) 116 48 164
572 116 my @row = splice @item, 0, $columns;
573 116 my $line = ' ' . join ' ', map { sprintf '%-*s', $width, $_ } @row;
574 116 $line =~ s/\s+\z//;
575 116 push @lines, $line;
576 }
577 48 return \@lines;
578 }
579  
580 5 ▶sub start {
Subroutine start: called CC=7 SCAR=21.0
Pod documented
581 5 my ($self) = @_;
582 5 ▶ $self->game(Game::Backgammon->new(seed => $self->_seed)) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 0 5 5
583 5 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 5 5
Condition33%1/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
500
MC/DC0%0/2
$self->picking and not $self->_enter_raw
$self->picking not $self->_enter_raw
Truth table33%1/3
A and not(B)
A= $self->pickingB= not $self->_enter_raw
ABresult
0X0
100
111
584  
585 5 my $interrupt = $SIG{INT};
586 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
587 0 $self->_leave_raw;
588 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
589 0 kill 'INT', $$;
590 5 };
591  
592 5 my $played = eval { $self->_turns; 1 };
593 5 my $error = $@;
594 5 $self->_leave_raw;
595 5 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 5 5
596  
597 5 $self->clear;
598 5 $self->highlight({});
599 5 ▶ $self->say_to($self->render($self->mode eq 'bot' ? $self->seat : undef));
Branch: 1 branch
BranchTrueFalseTotal
$self->mode eq 'bot' ? : 4 1 5
600 5 my $r = $self->game->result;
601 5 ▶ $self->say_to($r ? $r->stringify : 'stopped');
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 3 2 5
602 5 return $r;
603 }
604  
605 5 ▶sub _turns {
Subroutine _turns: called CC=3 SCAR=11.0
606 5 my ($self) = @_;
607 5 my $guard = 0;
608 5 ▶ while ($self->step) { last if ++$guard > 10_000 }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->step) 192 5 197
if ++$guard > 10000 0 192 192
609 5 return;
610 }
611  
612 0 ▶sub _seed {
Subroutine _seed: not called CC=3 SCAR=24.8
613 0 require Digest::SHA;
614 0 ▶ open my $ur, '<:raw', '/dev/urandom' or return Digest::SHA::sha256(rand() . $$ . time);
Branch: 1 branch
BranchTrueFalseTotal
open my $ur, "<:raw", "/dev/urandom" or return &Digest::SHA::sha256((rand()) . $$ . time) 0 0 0
615 0 read $ur, my $bytes, 32;
616 0 close $ur;
617 0 ▶ return length($bytes) == 32 ? $bytes : Digest::SHA::sha256(rand() . $$ . time);
Branch: 1 branch
BranchTrueFalseTotal
length $bytes == 32 ? : 0 0 0
618 }
619  
620 0 ▶sub _usage {
Subroutine _usage: not called CC=1 SCAR=6.9
621 0 return <<'USAGE';
622 backgammon [--mode bot|hotseat|watch] [--level 1-3] [--seat white|black]
623 [--view white|black] [--ascii] [--no-colour] [--no-pick]
624  
625 bot play the computer (the default)
626 hotseat two people at one keyboard
627 watch the computer against itself
628  
629 --view pin the board to one side's numbering. By default it is drawn
630 from the side on roll, because a turn is written in the mover's
631 own numbers and the board should agree with the list.
632 --ascii O and X instead of the round checkers.
633 --no-pick choose a whole turn by number from the list, as 0.01 did, rather
634 than one checker at a time with the keys.
635 --help this
636  
637 The board is the usual one: your home board is the bottom right quarter and
638 you run into it anticlockwise, the bar is the middle column and the tray the
639 one on the right.
640  
641 On a terminal a turn is built one checker at a time: the moves you can make
642 with the dice you have left are listed, up and down choose one and enter plays
643 it, backspace takes the last one back, and the board shows where the checker
644 under the cursor is going. A roll that can be played three hundred ways is
645 still only a handful of choices at each step. v turns the board round and q
646 stops. Without a terminal, or with --no-pick, every legal turn is numbered and
647 you pick one.
648  
649 Exit status is 0 when the game ended or was stopped, 2 for an option the game
650 will not take.
651 USAGE
652 }
653  
654 0 ▶sub run {
Subroutine run: not called CC=13 SCAR=52.0
Pod documented
655 0 my ($class, @argv) = @_;
656 0 my %o = (mode => 'bot', level => 3, seat => 'white', view => '', ascii => 0);
657 0 my ($colour, $pick, $help);
658  
659 0 my $ok = eval {
660 0 ▶ local $SIG{__WARN__} = sub { print STDERR "backgammon: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=6.9
661 Getopt::Long::GetOptionsFromArray(\@argv,
662 'mode=s' => \$o{mode},
663 'level=i' => \$o{level},
664 'seat=s' => \$o{seat},
665 'view=s' => \$o{view},
666 'ascii' => \$o{ascii},
667 0 'colour!' => \$colour,
668 'pick!' => \$pick,
669 'help' => \$help,
670 );
671 };
672 0 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 0 0
673 0 ▶ if ($help) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 0 0 0
674 0 ▶ if (@argv) { print STDERR "backgammon: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 0 0
675  
676 0 my %refused = (
677 mode => [ qr/\A(?:bot|hotseat|watch)\z/, 'bot, hotseat or watch' ],
678 seat => [ qr/\A(?:white|black)\z/, 'white or black' ],
679 view => [ qr/\A(?:white|black|)\z/, 'white or black' ],
680 level => [ qr/\A[123]\z/, '1 to 3' ],
681 );
682 0 for my $k (qw(mode seat view level)) {
683 0 ▶ next if $o{$k} =~ $refused{$k}[0];
Branch: 1 branch
BranchTrueFalseTotal
if $o{$k} =~ /$refused{$k}[0]/ 0 0 0
684 0 print STDERR "backgammon: --$k takes $refused{$k}[1], not '$o{$k}'\n", _usage();
685 0 return 2;
686 }
687  
688 0 ▶ my $terminal = $class->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $colour ? : 0 0 0
defined $pick ? : 0 0 0
689 %o,
690 (defined $colour ? (colour => $colour) : ()),
691 (defined $pick ? (picking => $pick) : ()),
692 );
693  
694 0 local @SIG{qw(INT TERM HUP)};
695 0 for my $sig (qw(INT TERM HUP)) {
696 0 ▶ $SIG{$sig} = sub { $terminal->_leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
697 }
698  
699 0 my $result;
700 0 my $played = eval { $result = $terminal->start; 1 };
701 0 my $died = $@;
702 0 $terminal->_leave_raw;
703 0 ▶ die $died unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 0 0
704 0 ▶ return 1 if $result && $terminal->mode eq 'bot' && $result->winner ne $terminal->seat;
Branch: 1 branch
BranchTrueFalseTotal
if $result and $terminal->mode eq "bot" and $result->winner ne $terminal->seat 0 0 0
Condition0%0/6
$result and $terminal->mode eq "bot" and $result->winner ne $terminal->seat
!ll&&!rl&&r
000
$result and $terminal->mode eq "bot"
!ll&&!rl&&r
000
MC/DC0%0/3
$result and $terminal->mode eq "bot" and $result->winner ne $terminal->seat
$result $terminal->mode eq "bot" $result->winner ne $terminal->seat
Truth table0%0/4
A and B and C
A= $resultB= $terminal->mode eq "bot"C= $result->winner ne $terminal->seat
ABCresult
0XX0
10X0
1100
1111
705 0 return 0;
706 }
707  
708 3 1;
709  
710 __END__
↑ Summary Backgammon/Notation.pm »