blib/lib/Game/Backgammon/Terminal.pm

filter: Statementstmt 97.0% 461 / 475 Branchbran 85.4% 229 / 268 Conditioncond 73.6% 67 / 91 MC/DCmcdc 56.3% 31 / 55 Subroutinesub 97.4% 75 / 77 Podpod 95.0% 19 / 20 totaltotal 89.4% 882 / 986 CC 180 SCAR 24.8
CC
180
Cov
91%
run
31.2
_pick_turn
26.7
BUILD
21.9
CRAP 11.9
« blib/lib/Game/Backgammon/Notation.pm ↑ Summary blib/lib/Game/Backgammon.pm »
1 package Game::Backgammon::Terminal;
2  
3 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 23 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 23 ▶use Game::Backgammon;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Game::Backgammon::Board ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use Game::Backgammon::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use Game::Terminal::Kit::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Game::Terminal::Kit::Picker;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 23 our $VERSION = '0.05';
22  
23 23 my %CHECKER = (
24 white => { ascii => 'O', wide => "\x{25CB}", colour => '1;37', ink => '1;38;5;231' },
25 black => { ascii => 'X', wide => "\x{25CF}", colour => '1;36', ink => '1;38;5;16' },
26 );
27  
28 23 my %GROUND = (
29 light => '48;5;180',
30 dark => '48;5;94',
31 from => '48;5;136',
32 to => '48;5;28',
33 hit => '48;5;124',
34 played => '48;5;24',
35 );
36  
37 23 ▶use constant ROWS => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 has game => (
40 is => 'rw',
41 isa => Object
42 23 );
43  
44 has out => (
45 is => 'ro',
46 isa => Any,
47 17 ▶ default => sub { \*STDOUT }
Subroutine __ANON__: called CC=1 SCAR=0.0
48 23 );
49  
50 has in => (
51 is => 'ro',
52 isa => Any,
53 39 ▶ default => sub { \*STDIN }
Subroutine __ANON__: called CC=1 SCAR=0.0
54 23 );
55  
56 has mode => (
57 is => 'ro',
58 isa => Str,
59 default => 'bot'
60 23 );
61  
62 has level => (
63 is => 'ro',
64 isa => Int,
65 default => 3
66 23 );
67  
68 has seat => (
69 is => 'ro',
70 isa => Str,
71 default => 'white'
72 23 );
73  
74 has view => (
75 is => 'rw',
76 isa => Str,
77 default => ''
78 23 );
79  
80 has ascii => (
81 is => 'rw',
82 isa => Bool,
83 default => 0
84 23 );
85  
86 has [qw/colour interactive picking/] => (
87 is => 'rw'
88 23 );
89  
90 has [qw/keyboard screen log picker/] => (
91 is => 'rw'
92 23 );
93  
94 has highlight => (
95 is => 'rw',
96 isa => HashRef,
97 49 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
98 23 );
99  
100 has keysource => (
101 is => 'rw',
102 isa => CodeRef
103 23 );
104  
105 has pending => (
106 is => 'rw',
107 isa => ArrayRef,
108 49 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
109 23 );
110  
111 has save_file => (
112 is => 'rw'
113 23 );
114  
115 has history => (
116 is => 'rw'
117 23 );
118  
119 49 ▶sub BUILD {
Subroutine BUILD: called CC=20 SCAR=30.8
Pod undocumented
120 49 my ($self) = @_;
121 49 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 23 26 49
-t $self->in ? : 0 23 23
122 49 ▶ $self->colour($self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 24 25 49
$self->interactive && !Game::Terminal::Kit::Screen::no_colour() ? : 1 23 24
Condition66%2/3
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
!ll&&!rl&&r
2301
MC/DC50%1/2
$self->interactive and not Game::Terminal::Kit::Screen::no_colour()
$self->interactive not Game::Terminal::Kit::Screen::no_colour()
Truth table66%2/3
A and not(B)
A= $self->interactiveB= not Game::Terminal::Kit::Screen::no_colour()
ABresult
0X0
100
111
123 unless defined $self->colour;
124 49 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 49 0 49
$self->keysource ? : 21 28 49
125 in => $self->in,
126 pending => $self->pending,
127 ($self->keysource ? (keysource => $self->keysource) : ()),
128 )) unless $self->keyboard;
129 49 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 4 branches
BranchTrueFalseTotal
unless $self->screen 49 0 49
$self->interactive ? : 1 48 49
$self->colour ? : 2 47 49
$self->ascii ? : 25 24 49
130 out => $self->out,
131 interactive => $self->interactive ? 1 : 0,
132 colour => $self->colour ? 1 : 0,
133 ascii => $self->ascii ? 1 : 0,
134 )) unless $self->screen;
135 49 ▶ $self->log(Game::Terminal::Kit::Log->new) unless $self->log;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->log 49 0 49
136 49 ▶ $self->picker(Game::Terminal::Kit::Picker->new(
Branch: 1 branch
BranchTrueFalseTotal
unless $self->picker 49 0 49
137 window => 0,
138 log => $self->log,
139 screen => $self->screen,
140 )) unless $self->picker;
141 49 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 49 0 49
142 49 ▶ $self->history->name('backgammon') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 49 49
143 49 $self->history->version($VERSION);
144 49 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 7 42 49
145 49 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 23 26 49
$self->interactive ? : 0 23 23
146 49 ▶ $self->picking(0) unless $self->_keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_keys_available 28 21 49
147 49 $self->screen->utf8->flush_on;
148 49 return;
149 }
150  
151 49 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
152 return Game::Terminal::Kit::History->new(
153 name => 'backgammon',
154 version => $VERSION,
155 190 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
156 18 ▶ load => sub { Game::Backgammon->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
157 49 );
158 }
159  
160 31522 ▶sub say_to { my ($self, @what) = @_; $self->screen->say(join '', @what); return }
Subroutine say_to: called CC=1 SCAR=0.0
Pod documented
161  
162 10490 ▶sub board_lines {
Subroutine board_lines: called CC=4 SCAR=14.0
Pod documented
163 10490 my ($self, $view) = @_;
164 10490 ▶ $view ||= $self->view || $self->game->turn;
Condition50%3/6
$view ||= $self->view || $self->game->turn
l!l&&r!l&&!r
1048820
$self->view || $self->game->turn
l!l&&r!l&&!r
020
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
165 10490 my $board = $self->game->board;
166 10490 my $other = Game::Backgammon::Board::other($view);
167  
168 10490 my @lines = (
169 $self->_border(13 .. 24),
170 $self->_half($view, [13 .. 24], 0),
171 ' |' . (' ' x 18) . '|' . $self->_label('BAR') . '|'
172 . (' ' x 18) . '|' . $self->_label('OFF') . '|',
173 $self->_half($view, [reverse 1 .. 12], 1),
174 $self->_border(reverse 1 .. 12)
175 );
176  
177 10490 $lines[1] .= sprintf ' %-5s %3d pips', ucfirst $other, $board->pip_count($other);
178 10490 $lines[11] .= sprintf ' %-5s %3d pips', ucfirst $view, $board->pip_count($view);
179 10490 ▶ if (my $last = $self->game->history->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $self->game->history->[-1]) 10440 50 10490
180 10440 $lines[6] .= sprintf ' last: %s %s', $last->player, $last->notation;
181 }
182 10490 push @lines, sprintf ' the points are numbered from %s', $view;
183 10490 return \@lines;
184 }
185  
186 10336 ▶sub render {
Subroutine render: called CC=1 SCAR=0.0
Pod documented
187 10336 my ($self, $view) = @_;
188 10336 return join "\n", @{ $self->board_lines($view) };
189 }
190  
191 20980 ▶sub _border {
Subroutine _border: called CC=1 SCAR=0.0
192 20980 my ($self, @points) = @_;
193 20980 my @cell = map { my $c = sprintf '%2d-', $_; $c =~ s/\A /-/; $c } @points;
194 20980 return ' ' . $self->_label(
195 '+' . join('', @cell[0 .. 5]) . '+---+' . join('', @cell[6 .. 11]) . '+---+'
196 );
197 }
198  
199 20980 ▶sub _half {
Subroutine _half: called CC=8 SCAR=20.8
200 20980 my ($self, $view, $points, $bottom) = @_;
201 20980 my $board = $self->game->board;
202 20980 my $other = Game::Backgammon::Board::other($view);
203 20980 my @lines;
204 20980 for my $row (0 .. ROWS - 1) {
205 104900 ▶ my $edge = $bottom ? ROWS - 1 - $row : $row;
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 52450 52450 104900
206 104900 ▶ my $middle = $bottom ? $row : ROWS - 1 - $row;
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 52450 52450 104900
207 104900 my @cell;
208 104900 for my $n (@$points) {
209 1258800 my ($mine, $theirs) = $board->point_for($view, $n);
210 1258800 ▶ push @cell, $mine
Branch: 1 branch
BranchTrueFalseTotal
$mine ? : 184550 1074250 1258800
211 ? $self->_cell($view, $mine, $edge, $n)
212 : $self->_cell($other, $theirs, $edge, $n);
213 }
214 104900 ▶ my $bar = $bottom
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 52450 52450 104900
215 ? $self->_cell($other, $board->bar($other), $middle)
216 : $self->_cell($view, $board->bar($view), $middle);
217 104900 ▶ my $off = $bottom
Branch: 1 branch
BranchTrueFalseTotal
$bottom ? : 52450 52450 104900
218 ? $self->_cell($view, $board->off($view), $edge)
219 : $self->_cell($other, $board->off($other), $edge);
220 104900 push @lines, ' ' . $self->_label('|') . join('', @cell[0 .. 5])
221 . $self->_label('|') . $bar . $self->_label('|')
222 . join('', @cell[6 .. 11]) . $self->_label('|') . $off
223 . $self->_label('|');
224 }
225 20980 return @lines;
226 }
227  
228 1468600 ▶sub _cell {
Subroutine _cell: called CC=8 SCAR=20.8
229 1468600 my ($self, $player, $count, $edge, $point) = @_;
230 1468600 ▶ return $self->_ground($point, ' ') unless $count && $edge < ROWS;
Branch: 1 branch
BranchTrueFalseTotal
unless $count and $edge < 5 1041045 427555 1468600
Condition66%2/3
$count and $edge < 5
!ll&&!rl&&r
10410450427555
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
231 427555 ▶ return $self->_ground(
Branch: 2 branches
BranchTrueFalseTotal
if $edge == 4 and $count > 5 20377 407178 427555
$count > 9 ? : 16401 3976 20377
Condition100%3/3
$edge == 4 and $count > 5
!ll&&!rl&&r
3420446513420377
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
232 $point, $count > 9 ? "$count " : " $count ", $player
233 ) if $edge == ROWS - 1 && $count > ROWS;
234 407178 ▶ return $self->_ground($point, ' ') unless $edge < $count;
Branch: 1 branch
BranchTrueFalseTotal
unless $edge < $count 234804 172374 407178
235 return $self->_ground(
236 172374 ▶ $point, ' ' . $CHECKER{$player}{$self->ascii ? 'ascii' : 'wide'} . ' ', $player
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 164837 7537 172374
237 );
238 }
239  
240 1468600 ▶sub _ground {
Subroutine _ground: called CC=8 SCAR=20.8
241 1468600 my ($self, $point, $text, $player) = @_;
242 return $player
243 ? $self->_paint($text, $CHECKER{$player}{colour})
244 1468600 ▶ : $text
Branch: 2 branches
BranchTrueFalseTotal
unless $self->colour and defined $point 1467760 840 1468600
$player ? : 192545 1275215 1467760
Condition100%3/3
$self->colour and defined $point
!ll&&!rl&&r
1467620140840
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
245 unless $self->colour && defined $point;
246  
247 840 my $mark = $self->highlight->{$point};
248 840 ▶ $mark = $point % 2 ? 'dark' : 'light' unless $mark && $GROUND{$mark};
Branch: 2 branches
BranchTrueFalseTotal
unless $mark and $GROUND{$mark} 805 35 840
$point % 2 ? : 415 390 805
Condition66%2/3
$mark and $GROUND{$mark}
!ll&&!rl&&r
805035
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
249 840 my @code = ($GROUND{$mark});
250 840 ▶ push @code, $CHECKER{$player}{ink} if $player;
Branch: 1 branch
BranchTrueFalseTotal
if $player 206 634 840
251 840 return "\e[" . join(';', @code) . "m$text\e[0m";
252 }
253  
254 566460 ▶sub _label { my ($self, $text) = @_; return $self->_paint($text, '2') }
Subroutine _label: called CC=1 SCAR=0.0
255  
256 759005 ▶sub _paint {
Subroutine _paint: called CC=1 SCAR=0.0
257 759005 my ($self, $text, $code) = @_;
258 759005 return $self->screen->paint($text, $code);
259 }
260  
261 10335 ▶sub clear { my ($self) = @_; $self->screen->clear; return }
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
262  
263 10251 ▶sub raw { my ($self) = @_; return $self->keyboard->raw }
Subroutine raw: called CC=1 SCAR=0.0
Pod documented
264  
265 49 ▶sub _keys_available { my ($self) = @_; return $self->keyboard->keys_available }
Subroutine _keys_available: called CC=1 SCAR=0.0
266  
267 23 ▶sub _enter_raw { my ($self) = @_; return $self->keyboard->enter_raw ? $self : undef }
Subroutine _enter_raw: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 23 0 23
268  
269 0 ▶sub _leave_raw { my ($self) = @_; $self->keyboard->leave_raw; return $self }
Subroutine _leave_raw: not called CC=1 SCAR=6.9
270  
271 163 ▶sub _read_key { my ($self) = @_; return $self->keyboard->read_key }
Subroutine _read_key: called CC=1 SCAR=0.0
272  
273 10372 ▶sub step {
Subroutine step: called CC=13 SCAR=26.0
Pod documented
274 10372 my ($self) = @_;
275 10372 my $game = $self->game;
276 10372 ▶ return 0 if $game->status ne 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $game->status ne "active" 0 10372 10372
277  
278 10372 my $who = $game->turn;
279 10372 my $person = $self->_at_keys($who);
280 10372 ▶ my $picking = $self->picking && $self->raw && $person;
Condition83%5/6
$self->picking && $self->raw && $person
!ll&&!rl&&r
2511005863
$self->picking and $self->raw
!ll&&!rl&&r
251010121
MC/DC66%2/3
$self->picking && $self->raw && $person
$self->picking $self->raw $person
Truth table75%3/4
A and B && C
A= $self->pickingB= $self->rawC= $person
ABCresult
0XX0
10X0
1100
1111
281 10372 ▶ unless ($picking) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($picking) 10309 63 10372
282 10309 $self->clear;
283 10309 $self->say_to($self->render($who));
284 10309 $self->say_to(sprintf ' %s to play %s', $who, join '-', @{ $game->dice });
285 10309 $self->_say_notes;
286 }
287  
288 10372 my $turns = $game->legal_turns;
289 10372 ▶ if ($turns->[0]->is_forfeit && @$turns == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($turns->[0]->is_forfeit and @$turns == 1) 28 10344 10372
Condition66%2/3
$turns->[0]->is_forfeit and @$turns == 1
!ll&&!rl&&r
10344028
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
290 28 ▶ if ($picking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($picking) 1 27 28
291 1 $self->clear;
292 1 ▶ $self->say_to($self->render($self->view || $who));
Condition33%1/3
$self->view || $who
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->view || $who
$self->view $who
Truth table33%1/3
A || B
A= $self->viewB= $who
ABresult
1X1
011
000
293 1 $self->say_to(sprintf ' %s to play %s', $who, join '-', @{ $game->dice });
294 }
295 28 $self->say_to(' no play');
296 28 $game->play($turns->[0]);
297 28 return $self->_played;
298 }
299  
300 10344 ▶ $self->person_turn if $person;
Branch: 1 branch
BranchTrueFalseTotal
if $person 143 10201 10344
301 10344 my $turn = $self->_ask($who, $turns);
302 10344 ▶ return 0 unless $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $turn 22 10322 10344
303 10322 ▶ return 1 unless ref $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $turn 12 10310 10322
304 0 ▶ $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 } 10310 0 10310
305 10310 $self->say_to(' ' . $turn->notation);
306 10310 return $self->_played;
307 }
308  
309 10372 ▶sub _at_keys {
Subroutine _at_keys: called CC=4 SCAR=13.9
310 10372 my ($self, $who) = @_;
311 10372 ▶ return 1 if $self->mode eq 'hotseat';
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "hotseat" 5 10367 10372
312 10367 ▶ return $self->mode eq 'bot' && $who eq $self->seat ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->mode eq 'bot' && $who eq $self->seat ? : 148 10219 10367
Condition100%3/3
$self->mode eq "bot" and $who eq $self->seat
!ll&&!rl&&r
10089130148
MC/DC100%2/2
$self->mode eq "bot" and $who eq $self->seat
$self->mode eq "bot" $who eq $self->seat
Truth table100%3/3
A and B
A= $self->mode eq "bot"B= $who eq $self->seat
ABresult
0X0
100
111
313 }
314  
315 10338 ▶sub _played {
Subroutine _played: called CC=3 SCAR=11.0
316 10338 my ($self) = @_;
317 10338 ▶ return 1 if $self->game->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->status eq "active" 10334 4 10338
318 4 ▶ $self->person_turn unless $self->mode eq 'watch';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->mode eq "watch" 3 1 4
319 4 return 0;
320 }
321  
322 10310 ▶sub _say_notes {
Subroutine _say_notes: called CC=2 SCAR=6.9
323 10310 my ($self) = @_;
324 10310 $self->say_to(" $_") for $self->log->lines;
325 10310 $self->log->shown;
326 10310 return;
327 }
328  
329 146 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
330 146 my ($self) = @_;
331 146 $self->history->turn($self->game);
332 146 return $self;
333 }
334  
335 13 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
336 13 my ($self) = @_;
337 13 my $game = $self->history->undo;
338 13 ▶ return $self->_notify('There is nothing to take back.') unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 3 10 13
339 10 $self->swap($game);
340 10 $self->_notify('Taken back.');
341 10 return 1;
342 }
343  
344 4 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=6.9
Pod documented
345 4 my ($self) = @_;
346 4 my $game = $self->history->redo;
347 4 ▶ return $self->_notify('There is nothing to play again.') unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 1 3 4
348 3 $self->swap($game);
349 3 $self->_notify('Played again.');
350 3 return 1;
351 }
352  
353 13 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
354 13 my ($self, $game) = @_;
355 13 $self->game($game);
356 13 $self->highlight({});
357 13 $self->log->forget;
358 13 return $self;
359 }
360  
361 17 ▶sub _notify {
Subroutine _notify: called CC=1 SCAR=0.0
362 17 my ($self, $line) = @_;
363 17 $self->log->note($line);
364 17 return 0;
365 }
366  
367 10344 ▶sub _ask {
Subroutine _ask: called CC=18 SCAR=28.9
368 10344 my ($self, $who, $turns) = @_;
369 10344 ▶ 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 10201 143 10344
Condition100%6/6
$self->mode eq "watch" or $self->mode eq "bot" and $who ne $self->seat
l!l&&r!l&&!r
10071130143
$self->mode eq "bot" and $who ne $self->seat
!ll&&!rl&&r
5138130
MC/DC100%3/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 table100%4/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
370 if $self->mode eq 'watch'
371 || ($self->mode eq 'bot' && $who ne $self->seat);
372  
373 143 ▶ if ($self->picking && $self->raw) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->picking and $self->raw) 62 81 143
Condition66%2/3
$self->picking and $self->raw
!ll&&!rl&&r
81062
MC/DC50%1/2
$self->picking and $self->raw
$self->picking $self->raw
Truth table66%2/3
A and B
A= $self->pickingB= $self->raw
ABresult
0X0
100
111
374 62 my $turn = $self->_pick_turn($who, $turns);
375 62 $self->highlight({});
376 62 return $turn;
377 }
378  
379 81 $self->say_to($_) for @{ $self->offer_lines($turns) };
380 81 while (1) {
381 96 my $line = $self->keyboard->read_line(" $who> ", $self->out);
382 96 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 1 95 96
383 95 ▶ return undef if $line eq 'q' || $line eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "q" or $line eq "quit" 18 77 95
Condition66%2/3
$line eq "q" or $line eq "quit"
l!l&&r!l&&!r
18077
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
384 77 ▶ if ($line eq 'undo' || $line eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "undo" or $line eq "redo") 6 71 77
Condition100%3/3
$line eq "undo" or $line eq "redo"
l!l&&r!l&&!r
5171
MC/DC100%2/2
$line eq "undo" or $line eq "redo"
$line eq "undo" $line eq "redo"
Truth table100%3/3
A or B
A= $line eq "undo"B= $line eq "redo"
ABresult
1X1
011
000
385 6 ▶ return 'swapped' if $line eq 'undo' ? $self->take_back : $self->play_again;
Branch: 2 branches
BranchTrueFalseTotal
if $line eq "undo" ? $self->take_back : $self->play_again 5 1 6
$line eq 'undo' ? : 5 1 6
386 1 $self->_say_notes;
387 1 next;
388 }
389 71 ▶ if (my (undef, $file) = Game::Terminal::Kit::Save::command($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $file) = Game::Terminal::Kit::Save::command($line)) 11 60 71
390 11 $self->say_to(' ' . $self->save($file));
391 11 next;
392 }
393 60 ▶ 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 57 3 60
Condition100%6/6
$line =~ /\A\d+\z/ and $line >= 1 and $line <= @$turns
!ll&&!rl&&r
2157
$line =~ /\A\d+\z/ and $line >= 1
!ll&&!rl&&r
1158
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
394 3 $self->say_to(' pick a number from the list, or q to stop');
395 }
396 }
397  
398 209 ▶sub _matching {
Subroutine _matching: called CC=5 SCAR=16.1
399 209 my ($self, $turns, $chosen) = @_;
400 209 my @match;
401 209 TURN: for my $turn (@$turns) {
402 3011 my $moves = $turn->moves;
403 3011 ▶ next TURN if @$moves < @$chosen;
Branch: 1 branch
BranchTrueFalseTotal
if @$moves < @$chosen 0 3011 3011
404 3011 for my $at (0 .. $#$chosen) {
405 2667 ▶ next TURN if $moves->[$at]->notation ne $chosen->[$at]->notation;
Branch: 1 branch
BranchTrueFalseTotal
if $moves->[$at]->notation ne $chosen->[$at]->notation 1628 1039 2667
406 }
407 1383 push @match, $turn;
408 }
409 209 return \@match;
410 }
411  
412 209 ▶sub _next_moves {
Subroutine _next_moves: called CC=4 SCAR=13.9
413 209 my ($self, $match, $chosen) = @_;
414 209 my (@move, %seen);
415 209 for my $turn (@$match) {
416 1466 ▶ my $move = $turn->moves->[ scalar @$chosen ] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $turn->moves->[scalar @$chosen] or next 1407 59 1466
417 1407 ▶ next if $seen{ $move->notation }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$move->notation}++ 874 533 1407
418 533 push @move, $move;
419 }
420 209 return \@move;
421 }
422  
423 197 ▶sub _complete {
Subroutine _complete: called CC=3 SCAR=11.0
424 197 my ($self, $match, $chosen) = @_;
425 197 for my $turn (@$match) {
426 1133 ▶ return $turn if @{ $turn->moves } == @$chosen;
Branch: 1 branch
BranchTrueFalseTotal
if @{$turn->moves;} == @$chosen 58 1075 1133
427 }
428 139 return undef;
429 }
430  
431 149 ▶sub _move_marks {
Subroutine _move_marks: called CC=8 SCAR=20.8
432 149 my ($self, $view, $player, $chosen, $hover) = @_;
433 149 my %mark;
434 480 ▶ my $seen = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
435 480 my ($point) = @_;
436 480 ▶ return undef if $point eq 'bar' || $point eq 'off';
Branch: 1 branch
BranchTrueFalseTotal
if $point eq "bar" or $point eq "off" 51 429 480
Condition100%3/3
$point eq "bar" or $point eq "off"
l!l&&r!l&&!r
501429
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
437 429 ▶ return $view eq $player ? $point : 25 - $point;
Branch: 1 branch
BranchTrueFalseTotal
$view eq $player ? : 427 2 429
438 149 };
439 149 for my $move (@$chosen) {
440 92 for my $end ($move->from, $move->to) {
441 184 ▶ my $point = $seen->($end) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $point = &$seen($end) or next 160 24 184
442 160 $mark{$point} = 'played';
443 }
444 }
445 149 ▶ if ($hover) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hover) 148 1 149
446 148 ▶ if (my $to = $seen->($hover->to)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to = &$seen($hover->to)) 147 1 148
447 147 ▶ $mark{$to} = $hover->hit ? 'hit' : 'to';
Branch: 1 branch
BranchTrueFalseTotal
$hover->hit ? : 18 129 147
448 }
449 148 ▶ if (my $from = $seen->($hover->from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $from = &$seen($hover->from)) 122 26 148
450 122 $mark{$from} = 'from';
451 }
452 }
453 149 return \%mark;
454 }
455  
456 139 ▶sub _move_items {
Subroutine _move_items: called CC=1 SCAR=0.0
457 139 my ($self, $next) = @_;
458 139 return [ map { { label => $_->notation, about => $self->_describe($_), value => $_ } } @$next ];
459 }
460  
461 67 ▶sub _pick_turn {
Subroutine _pick_turn: called CC=25 SCAR=32.9
462 67 my ($self, $who, $turns) = @_;
463 67 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 67 67
464 67 ▶ my $view = $self->view || $who;
Condition33%1/3
$self->view || $who
l!l&&r!l&&!r
0670
MC/DC0%0/2
$self->view || $who
$self->view $who
Truth table33%1/3
A || B
A= $self->viewB= $who
ABresult
1X1
011
000
465 67 my $picker = $self->picker;
466 67 my $log = $self->log;
467 67 my (@chosen, $done);
468 67 my $fresh = 1;
469  
470 67 while (1) {
471 199 ▶ if ($fresh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fresh) 194 5 199
472 194 my $match = $self->_matching($turns, \@chosen);
473 194 my $next = $self->_next_moves($match, \@chosen);
474 194 $done = $self->_complete($match, \@chosen);
475 194 ▶ return $done if $done && !@$next;
Branch: 1 branch
BranchTrueFalseTotal
if $done and not @$next 55 139 194
Condition66%2/3
$done and not @$next
!ll&&!rl&&r
139055
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
476 139 $picker->reset($self->_move_items($next));
477 $picker->game_keys({
478 k => 'up', j => 'down', v => 'view', '?' => 'help',
479 139 %{ Game::Terminal::Kit::History->game_keys },
480 139 ▶ %{ Game::Terminal::Kit::Save::game_keys() },
Branch: 1 branch
BranchTrueFalseTotal
$done ? : 0 139 139
481 ($done ? (f => 'finish') : ()),
482 });
483 139 $fresh = 0;
484 }
485  
486 $self->highlight(
487 144 $self->_move_marks($view, $who, \@chosen, $picker->current)
488 );
489 144 $self->_draw_choice($who, $view, $picker, \@chosen, $done);
490  
491 144 my ($event, $value) = $picker->step($self->_read_key);
492 144 ▶ if ($event eq 'quit') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "quit") 5 139 144
493 5 $self->highlight({});
494 5 return undef;
495 }
496 139 ▶ if ($event eq 'chosen') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "chosen") 125 14 139
497 125 push @chosen, $value;
498 125 $fresh = 1;
499 125 next;
500 }
501 14 ▶ if ($event eq 'back') {
Branch: 1 branch
BranchTrueFalseTotal
if ($event eq "back") 2 12 14
502 2 ▶ $log->note('nothing taken back: no move chosen yet') unless @chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless @chosen 1 1 2
503 2 pop @chosen;
504 2 $fresh = 1;
505 2 next;
506 }
507 12 ▶ next unless $event eq 'key';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "key" 1 11 12
508 11 ▶ return $done if $value eq 'finish';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "finish" 0 11 11
509 11 ▶ if ($value eq 'undo' || $value eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "undo" or $value eq "redo") 9 2 11
Condition100%3/3
$value eq "undo" or $value eq "redo"
l!l&&r!l&&!r
632
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
510 9 ▶ next unless $value eq 'undo' ? $self->take_back : $self->play_again;
Branch: 2 branches
BranchTrueFalseTotal
unless $value eq "undo" ? $self->take_back : $self->play_again 2 7 9
$value eq 'undo' ? : 6 3 9
511 7 ▶ $log->note('The moves chosen this turn were dropped.') if @chosen;
Branch: 1 branch
BranchTrueFalseTotal
if @chosen 1 6 7
512 7 return 'swapped';
513 }
514 2 ▶ if ($value eq 'save') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "save") 1 1 2
515 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->screen,
516 1 default => $self->save_default, head => [ @{ $self->board_lines($view) }, '' ]);
517 1 ▶ $log->note($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 0 1
518 1 next;
519 }
520 1 ▶ $picker->move($value eq 'up' ? -1 : 1) if $value eq 'up' || $value eq 'down';
Branch: 2 branches
BranchTrueFalseTotal
if $value eq "up" or $value eq "down" 1 0 1
$value eq 'up' ? : 0 1 1
Condition33%1/3
$value eq "up" or $value eq "down"
l!l&&r!l&&!r
010
MC/DC0%0/2
$value eq "up" or $value eq "down"
$value eq "up" $value eq "down"
Truth table33%1/3
A or B
A= $value eq "up"B= $value eq "down"
ABresult
1X1
011
000
521 1 ▶ $self->view($view = Game::Backgammon::Board::other($view)) if $value eq 'view';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "view" 0 1 1
522 0 ▶ $log->note(@{ $self->_key_lines }) if $value eq 'help';
Branch: 1 branch
BranchTrueFalseTotal
if $value eq "help" 0 1 1
523 }
524 }
525  
526 0 ▶sub _key_lines {
Subroutine _key_lines: not called CC=1 SCAR=6.9
527 return [
528 0 'up and down choose a move, enter plays it',
529 'backspace or escape takes back the move chosen before it',
530 'a digit jumps to that move, v turns the board round',
531 'f finishes the turn where one can end, q stops the game',
532 'u takes back your last turn, r plays it again',
533 'S saves the game, to carry on with --resume or watch with --replay',
534 ];
535 }
536  
537 144 ▶sub _draw_choice {
Subroutine _draw_choice: called CC=3 SCAR=11.0
538 144 my ($self, $who, $view, $picker, $chosen, $done) = @_;
539 144 my $log = $self->log;
540 $self->screen->show([
541 144 @{ $self->board_lines($view) },
542 144 ▶ sprintf(' %s to play %s', $who, join '-', @{ $self->game->dice }),
Branch: 1 branch
BranchTrueFalseTotal
@$chosen ? : 70 74 144
543 (@$chosen
544 91 ? ' so far: ' . join ' ', map { $_->notation } @$chosen
545 : ' nothing chosen yet'),
546 '',
547 463 ▶ (map { " $_" } $picker->rows),
Branch: 1 branch
BranchTrueFalseTotal
$done ? : 0 144 144
548 ($done ? ('', ' f finishes the turn here') : ()),
549 144 (map { " $_" } $log->lines),
550 '',
551 ' up and down to choose, enter to play it, S to save, ? for the keys',
552 ]);
553 144 $log->shown;
554 144 return;
555 }
556  
557 441 ▶sub _describe {
Subroutine _describe: called CC=5 SCAR=16.1
558 441 my ($self, $move) = @_;
559 441 my @note;
560 441 ▶ push @note, 'from the bar' if $move->is_bar;
Branch: 1 branch
BranchTrueFalseTotal
if $move->is_bar 35 406 441
561 441 ▶ push @note, 'bears off' if $move->is_off;
Branch: 1 branch
BranchTrueFalseTotal
if $move->is_off 0 441 441
562 441 ▶ push @note, 'hits' if $move->hit;
Branch: 1 branch
BranchTrueFalseTotal
if $move->hit 52 389 441
563 441 ▶ push @note, sprintf 'with the %d', $move->die unless @note;
Branch: 1 branch
BranchTrueFalseTotal
unless @note 359 82 441
564 441 return join ', ', @note;
565 }
566  
567 83 ▶sub offer_lines {
Subroutine offer_lines: called CC=6 SCAR=17.9
Pod documented
568 83 my ($self, $turns) = @_;
569 83 my @item = map { sprintf '%2d) %s', $_ + 1, $turns->[$_]->notation } 0 .. $#$turns;
570 83 my $width = 0;
571 83 for my $item (@item) {
572 1231 ▶ $width = length $item if length $item > $width;
Branch: 1 branch
BranchTrueFalseTotal
if length $item > $width 302 929 1231
573 }
574 83 ▶ my $columns = int(74 / ($width + 2)) || 1;
Condition50%1/2
int 74 / ($width + 2) || 1
l!l
830
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
575 83 ▶ $columns = 4 if $columns > 4;
Branch: 1 branch
BranchTrueFalseTotal
if $columns > 4 6 77 83
576 83 my @lines;
577 83 ▶ while (@item) {
Branch: 1 branch
BranchTrueFalseTotal
if (@item) 503 83 586
578 503 my @row = splice @item, 0, $columns;
579 503 my $line = ' ' . join ' ', map { sprintf '%-*s', $width, $_ } @row;
580 503 $line =~ s/\s+\z//;
581 503 push @lines, $line;
582 }
583 83 return \@lines;
584 }
585  
586 25 ▶sub start {
Subroutine start: called CC=6 SCAR=18.0
Pod documented
587 25 my ($self) = @_;
588 25 ▶ $self->game(Game::Backgammon->new(seed => $self->_seed)) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 8 17 25
589 25 $self->history->start($self->game);
590 25 ▶ $self->picking(0) if $self->picking && !$self->_enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->_enter_raw 0 25 25
Condition66%2/3
$self->picking and not $self->_enter_raw
!ll&&!rl&&r
2230
MC/DC0%0/2
$self->picking and not $self->_enter_raw
$self->picking not $self->_enter_raw
Truth table66%2/3
A and not(B)
A= $self->pickingB= not $self->_enter_raw
ABresult
0X0
100
111
591  
592 25 ▶ Game::Terminal::Kit::Run::guard(sub { $self->_turns }, $self->keyboard, $self->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
593  
594 25 $self->clear;
595 25 $self->highlight({});
596 25 ▶ $self->say_to($self->render($self->mode eq 'bot' ? $self->seat : undef));
Branch: 1 branch
BranchTrueFalseTotal
$self->mode eq 'bot' ? : 23 2 25
597 25 my $r = $self->game->result;
598 25 ▶ $self->say_to($r ? $r->stringify : 'stopped');
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 4 21 25
599 25 return $r;
600 }
601  
602 25 ▶sub _turns {
Subroutine _turns: called CC=3 SCAR=11.0
603 25 my ($self) = @_;
604 25 my $guard = 0;
605 25 ▶ while ($self->step) { last if ++$guard > 10_000 }
Branch: 2 branches
BranchTrueFalseTotal
if ($self->step) 10325 24 10349
if ++$guard > 10000 1 10324 10325
606 25 return;
607 }
608  
609 8 ▶sub _seed {
Subroutine _seed: called CC=3 SCAR=11.3
610 8 require Digest::SHA;
611 8 ▶ 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) 8 0 8
612 8 read $ur, my $bytes, 32;
613 8 close $ur;
614 8 ▶ return length($bytes) == 32 ? $bytes : Digest::SHA::sha256(rand() . $$ . time);
Branch: 1 branch
BranchTrueFalseTotal
length $bytes == 32 ? : 8 0 8
615 }
616  
617 12 ▶sub settings {
Subroutine settings: called CC=3 SCAR=11.1
Pod documented
618 12 my ($self) = @_;
619 12 ▶ return { mode => $self->mode, level => $self->level, seat => $self->seat,
Branch: 1 branch
BranchTrueFalseTotal
length($self->view // '') ? : 0 12 12
Condition100%2/2
$self->view // ""
l!l
111
MC/DC100%1/1
$self->view // ""
$self->view
Truth table100%2/2
A
A= $self->view
Aresult
11
00
620 (length($self->view // '') ? (view => $self->view) : ()) };
621 }
622  
623 7 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=7.1
Pod documented
624 7 my ($self) = @_;
625 7 my $file = $self->history->file;
626 7 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('backgammon');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 7 0 7
627 }
628  
629 12 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
630 12 my ($self, $file) = @_;
631 12 ▶ $file = $self->save_default unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 6 6 12
Condition66%2/3
defined $file and length $file
!ll&&!rl&&r
606
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
632 12 my $history = $self->history;
633 12 $history->settings($self->settings);
634 12 $history->version($VERSION);
635 12 my $path = eval { $history->saved($self->game)->write_file($file) };
636 12 ▶ return 'the game was not saved: ' . Game::Terminal::Kit::Save::why($@) unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 12 12
637 12 $history->file($path);
638 12 return "the game is saved to $path";
639 }
640  
641 9 ▶sub read_save {
Subroutine read_save: called CC=1 SCAR=0.0
Pod documented
642 9 my ($class, $file) = @_;
643 9 return Game::Terminal::Kit::Save->read_game($file, 'backgammon');
644 }
645  
646 3 ▶sub replay_lines {
Subroutine replay_lines: called CC=4 SCAR=14.0
Pod documented
647 3 my ($self, $game) = @_;
648 3 my $was = $self->game;
649 3 $self->game($game);
650 3 my @lines = eval {
651 3 my $result = $game->result;
652 3 ▶ (@{ $self->board_lines($self->mode eq 'bot' ? $self->seat : undef) },
Branch: 2 branches
BranchTrueFalseTotal
$self->mode eq 'bot' ? : 3 0 3
$result ? : 0 3 3
653 $result ? ' ' . $result->stringify
654 3 : sprintf(' %s to play %s', $game->turn, join '-', @{ $game->dice }));
655 };
656 3 my $died = $@;
657 3 $self->game($was);
658 3 ▶ die $died unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 3 3
659 3 return @lines;
660 }
661  
662 9 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
663 9 return <<'USAGE';
664 backgammon [--mode bot|hotseat|watch] [--level 1-3] [--seat white|black]
665 [--view white|black] [--ascii] [--no-colour] [--no-pick]
666 [--save FILE] [--resume FILE | --replay FILE]
667  
668 bot play the computer (the default)
669 hotseat two people at one keyboard
670 watch the computer against itself
671  
672 --view pin the board to one side's numbering. By default it is drawn
673 from the side on roll, because a turn is written in the mover's
674 own numbers and the board should agree with the list.
675 --ascii O and X instead of the round checkers.
676 --no-pick choose a whole turn by number from the list, as 0.01 did, rather
677 than one checker at a time with the keys.
678 --save FILE where S, or "save", writes the game: FILE, else the file
679 it was resumed from, else backgammon.save
680 --resume FILE carry on a saved game, with its undo and redo
681 --replay FILE watch a saved game a turn at a time; enter plays on
682 --help this
683  
684 The board is the usual one: your home board is the bottom right quarter and
685 you run into it anticlockwise, the bar is the middle column and the tray the
686 one on the right.
687  
688 On a terminal a turn is built one checker at a time: the moves you can make
689 with the dice you have left are listed, up and down choose one and enter plays
690 it, backspace or escape takes the last one back, and the board shows where the checker
691 under the cursor is going. A roll that can be played three hundred ways is
692 still only a handful of choices at each step. v turns the board round, u takes
693 back your last turn and r plays it again, S saves the game, and q stops.
694 Without a terminal, or with --no-pick, every legal turn is numbered and you
695 pick one, or type undo, redo or save.
696  
697 Exit status is 0 when the game ended or was stopped, 2 for an option the game
698 will not take.
699 USAGE
700 }
701  
702 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
703 return [
704 2 { name => 'mode', type => 'enum', choices => [qw(bot hotseat watch)], help => 'you against the program, two people at one keyboard, or two programs' },
705 { name => 'level', type => 'int', min => 1, max => 3, help => 'how hard the program plays' },
706 { name => 'seat', type => 'enum', choices => [qw(white black)], help => 'your side' },
707 ];
708 }
709  
710 26 ▶sub run {
Subroutine run: called CC=27 SCAR=34.4
Pod documented
711 26 my ($class, @argv) = @_;
712 26 my %default = (mode => 'bot', level => 3, seat => 'white', view => '');
713 26 my %o = (ascii => 0);
714 26 my ($colour, $pick, $help);
715  
716 26 my $ok = eval {
717 26 ▶ local $SIG{__WARN__} = sub { print STDERR "backgammon: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
718 Getopt::Long::GetOptionsFromArray(\@argv,
719 'mode=s' => \$o{mode},
720 'level=i' => \$o{level},
721 'seat=s' => \$o{seat},
722 'view=s' => \$o{view},
723 'ascii' => \$o{ascii},
724 'colour!' => \$colour,
725 'pick!' => \$pick,
726 'help' => \$help,
727 'save=s' => \$o{save},
728 'resume=s' => \$o{resume},
729 'replay=s' => \$o{replay},
730 26 );
731 };
732 26 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 25 26
733 25 ▶ if ($help) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($help) 8 17 25
734 0 ▶ if (@argv) { print STDERR "backgammon: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 17 17
735 17 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%o)) { print STDERR "backgammon: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%o)) 2 15 17
736 15 my ($file, $resume, $replay) = delete @o{qw(save resume replay)};
737  
738 15 my $save;
739 15 ▶ if (defined $resume || defined $replay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $resume or defined $replay) 8 7 15
Condition100%3/3
defined $resume or defined $replay
l!l&&r!l&&!r
627
MC/DC100%2/2
defined $resume or defined $replay
defined $resume defined $replay
Truth table100%3/3
A or B
A= defined $resumeB= defined $replay
ABresult
1X1
011
000
740 8 ▶ ($save, my $why) = $class->read_save(defined $resume ? $resume : $replay);
Branch: 1 branch
BranchTrueFalseTotal
defined $resume ? : 6 2 8
741 8 ▶ unless ($save) { print STDERR "backgammon: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 2 6 8
742 6 my $settings = $save->settings;
743 6 ▶ defined $o{$_} or $o{$_} = $settings->{$_} for grep { defined $settings->{$_} } qw(mode level seat view);
Branch: 1 branch
BranchTrueFalseTotal
defined $o{$_} or $o{$_} = $settings->{$_} 1 17 18
744 }
745 13 ▶ defined $o{$_} or $o{$_} = $default{$_} for keys %default;
Branch: 1 branch
BranchTrueFalseTotal
defined $o{$_} or $o{$_} = $default{$_} 25 27 52
746  
747 13 my %refused = (
748 mode => [ qr/\A(?:bot|hotseat|watch)\z/, 'bot, hotseat or watch' ],
749 seat => [ qr/\A(?:white|black)\z/, 'white or black' ],
750 view => [ qr/\A(?:white|black|)\z/, 'white or black' ],
751 level => [ qr/\A[123]\z/, '1 to 3' ],
752 );
753 13 for my $k (qw(mode seat view level)) {
754 52 ▶ next if $o{$k} =~ $refused{$k}[0];
Branch: 1 branch
BranchTrueFalseTotal
if $o{$k} =~ /$refused{$k}[0]/ 52 0 52
755 0 print STDERR "backgammon: --$k takes $refused{$k}[1], not '$o{$k}'\n", _usage();
756 0 return 2;
757 }
758  
759 13 ▶ my $terminal = $class->new(
Branch: 3 branches
BranchTrueFalseTotal
defined $colour ? : 0 13 13
defined $pick ? : 0 13 13
defined $file ? : 6 7 13
760 %o,
761 (defined $colour ? (colour => $colour) : ()),
762 (defined $pick ? (picking => $pick) : ()),
763 (defined $file ? (save_file => $file) : ()),
764 );
765  
766 13 ▶ if (defined $replay) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $replay) 1 12 13
767 my $watch = Game::Terminal::Kit::Replay->new(
768 save => $save,
769 3 ▶ load => sub { Game::Backgammon->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
770 3 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
771 1 ▶ head => [ 'backgammon, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 1 0 1
772 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
773 screen => $terminal->screen,
774 );
775 my ($event, $step) = Game::Terminal::Kit::Run::guard(
776 1 ▶ sub { $watch->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
777 1 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 0 1
778 0 $save = $save->from_step($step);
779 }
780 12 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 5 7 12
781 5 my $game = eval { $terminal->history->resume($save) };
782 5 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 5 5
783 0 print STDERR "backgammon: '" . $save->file . "' cannot be carried on: "
784 . Game::Terminal::Kit::Save::why($@) . ".\n";
785 0 return 2;
786 }
787 5 ▶ $terminal->history->file($file) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 4 5
788 5 $terminal->game($game);
789 }
790  
791 my $result = Game::Terminal::Kit::Run::guard(
792 12 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen,
Subroutine __ANON__: called CC=1 SCAR=0.0
793 );
794 12 ▶ 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 12 12
Condition33%2/6
$result and $terminal->mode eq "bot" and $result->winner ne $terminal->seat
!ll&&!rl&&r
1200
$result and $terminal->mode eq "bot"
!ll&&!rl&&r
1200
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 table25%1/4
A and B and C
A= $resultB= $terminal->mode eq "bot"C= $result->winner ne $terminal->seat
ABCresult
0XX0
10X0
1100
1111
795 12 return 0;
796 }
797  
798 23 1;
799  
800 __END__
« blib/lib/Game/Backgammon/Notation.pm ↑ Summary blib/lib/Game/Backgammon.pm »