blib/lib/Game/Checkers/Terminal.pm

filter: Statementstmt 82.0% 492 / 600 Branchbran 67.6% 242 / 358 Conditioncond 37.1% 55 / 148 MC/DCmcdc 17.0% 15 / 88 Subroutinesub 84.3% 54 / 64 Podpod 97.8% 45 / 46 totaltotal 69.2% 903 / 1304 CC 248 SCAR 29.0
CC
248
Cov
71%
offer_draw
72.0
pick
56.8
run
53.3
CRAP 18.3
« blib/lib/Game/Checkers/Bot.pm ↑ Summary blib/lib/Game/Checkers/Notation.pm »
1 package Game::Checkers::Terminal;
2  
3 10 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
5  
6 10 ▶use Object::Proto::Sugar -types;
Subroutine __ANON__: called CC=1 SCAR=0.0
7 10 ▶use Game::Checkers;
Subroutine __ANON__: called CC=1 SCAR=0.0
8 10 ▶use Game::Checkers::Bot;
Subroutine __ANON__: called CC=1 SCAR=0.0
9 10 ▶use Game::Checkers::Notation;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 10 ▶use Game::Checkers::Squares;
Subroutine __ANON__: called CC=1 SCAR=0.0
11 10 ▶use Getopt::Long ();
Subroutine __ANON__: called CC=1 SCAR=0.0
12  
13 10 our $VERSION = '0.03';
14  
15 10 our %COMMAND;
16  
17 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 %COMMAND = (
19 help => 'this list',
20 '?' => 'this list',
21 moves => 'the legal moves, numbered: play one by its number',
22 pick => 'choose a move with the arrow keys instead of typing it',
23 type => 'go back to typing moves out',
24 hint => 'what the bot would play here',
25 board => 'draw the board again',
26 fen => 'the position as FEN',
27 undo => 'take back your last move, and the reply to it',
28 'save FILE' => 'write the game out as PDN',
29 'load FILE' => 'read a PDN game back in',
30 'level N' => 'set the strength of the bot, 1 to 5',
31 flip => 'draw the board from the other side',
32 ascii => 'letters instead of draughts symbols',
33 draw => 'offer a draw',
34 resign => 'give the game up',
35 quit => 'stop playing',
36 );
37 }
38  
39 10 my %PIECE = (
40 1 => { ascii => 'b', wide => "\x{26C2}" },
41 2 => { ascii => 'B', wide => "\x{26C3}" },
42 -1 => { ascii => 'w', wide => "\x{26C0}" },
43 -2 => { ascii => 'W', wide => "\x{26C1}" },
44 );
45  
46 # the squares nobody plays on are the light ones, so an unplayed square needs no
47 # mark of its own once the board is painted
48 10 my %GROUND = (
49 light => '48;5;180',
50 dark => '48;5;94',
51 from => '48;5;136',
52 to => '48;5;28',
53 captured => '48;5;124',
54 cursor => '48;5;24',
55 );
56  
57 10 my %INK = (
58 black => '1;38;5;16',
59 white => '1;38;5;231',
60 );
61  
62 # resign and draw are not on a key on purpose: a game should not end because a
63 # finger slipped on the row below the arrow keys
64 10 my %KEY = (
65 u => 'undo',
66 f => 'flip',
67 a => 'ascii',
68 q => 'quit',
69 );
70  
71 # Term::ReadKey hands over one character at a time and leaves an escape
72 # sequence as the characters it is made of, so the arrow keys are named here
73 10 my %SEQUENCE = (
74 'A' => 'up',
75 'B' => 'down',
76 'C' => 'right',
77 'D' => 'left',
78 'H' => 'home',
79 'F' => 'end',
80 '1~' => 'home',
81 '4~' => 'end',
82 '5~' => 'page_up',
83 '6~' => 'page_down',
84 '7~' => 'home',
85 '8~' => 'end',
86 );
87  
88 10 my %CONTROL = (
89 "\r" => 'enter',
90 "\n" => 'enter',
91 "\t" => 'tab',
92 "\x7f" => 'backspace',
93 "\x08" => 'backspace',
94 "\x03" => 'interrupt',
95 "\x04" => 'eof',
96 );
97  
98 has game => (
99 is => 'rw',
100 isa => Object
101 10 );
102  
103 has bot => (
104 is => 'rw'
105 10 );
106  
107 has human => (
108 is => 'rw',
109 isa => Str,
110 default => 'black'
111 10 );
112  
113 has [qw/in out/] => (
114 is => 'rw'
115 10 );
116  
117 has [qw/interactive colour/] => (
118 is => 'rw'
119 10 );
120  
121 has [qw/ascii flip/] => (
122 is => 'rw',
123 isa => Bool,
124 default => 0
125 10 );
126  
127 has redraw => (
128 is => 'rw',
129 isa => Bool,
130 default => 1
131 10 );
132  
133 has highlight => (
134 is => 'rw',
135 isa => HashRef,
136 default => {}
137 10 );
138  
139 has picking => (
140 is => 'rw',
141 isa => Bool
142 10 );
143  
144 has raw => (
145 is => 'rw',
146 isa => Bool,
147 default => 0
148 10 );
149  
150 has keysource => (
151 is => 'rw',
152 isa => CodeRef
153 10 );
154  
155 has pending => (
156 is => 'rw',
157 isa => ArrayRef,
158 default => []
159 10 );
160  
161 38 ▶sub BUILD {
Subroutine BUILD: called CC=13 SCAR=26.4
Pod undocumented
162 38 my ($self) = @_;
163 38 ▶ $self->game(Game::Checkers->new) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 30 8 38
164 38 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 26 12 38
165 38 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 2 36 38
166 38 ▶ $self->interactive(-t $self->in ? 1 : 0) unless defined $self->interactive;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->interactive 2 36 38
-t $self->in ? : 0 2 2
167 38 ▶ $self->colour($self->interactive && !$ENV{NO_COLOR} ? 1 : 0)
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->colour 0 38 38
$self->interactive && !$ENV{'NO_COLOR'} ? : 0 0 0
Condition0%0/3
$self->interactive and not $ENV{"NO_COLOR"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->interactive and not $ENV{"NO_COLOR"}
$self->interactive not $ENV{"NO_COLOR"}
Truth table0%0/3
A and not(B)
A= $self->interactiveB= not $ENV{"NO_COLOR"}
ABresult
0X0
100
111
168 unless defined $self->colour;
169 38 ▶ $self->picking($self->interactive ? 1 : 0) unless defined $self->picking;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $self->picking 22 16 38
$self->interactive ? : 1 21 22
170 38 ▶ $self->picking(0) unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 22 16 38
171 38 ▶ binmode $self->out, ':encoding(UTF-8)' unless $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ascii 2 36 38
172  
173 # a prompt is printed without a newline and has to be on the screen before
174 # the read, and the encoding layer holds on to output otherwise
175 38 my $previous = select $self->out;
176 38 $| = 1;
177 38 select $previous;
178 38 return $self;
179 }
180  
181 13 ▶sub start {
Subroutine start: called CC=4 SCAR=15.8
Pod documented
182 13 my ($self) = @_;
183 13 ▶ $self->picking(0) if $self->picking && !$self->enter_raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->picking and not $self->enter_raw 0 13 13
Condition33%1/3
$self->picking and not $self->enter_raw
!ll&&!rl&&r
1300
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
184  
185 # a raw terminal that is never put back is a broken shell, so the interrupt
186 # is caught only to restore it, and then raised again as if it had not been
187 13 my $interrupt = $SIG{INT};
188 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
189 0 $self->leave_raw;
190 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
191 0 kill 'INT', $$;
192 13 };
193  
194 13 $self->say('Checkers. Type help for the commands.');
195 13 my $played = eval { $self->turns; 1 };
196 13 my $error = $@;
197 13 $self->leave_raw;
198 13 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 13 13
199 13 return $self->game->result;
200 }
201  
202 13 ▶sub turns {
Subroutine turns: called CC=10 SCAR=23.3
Pod documented
203 13 my ($self) = @_;
204 13 while (1) {
205 49 ▶ my $picking = $self->picking && $self->raw;
Condition33%1/3
$self->picking && $self->raw
!ll&&!rl&&r
4900
MC/DC0%0/2
$self->picking && $self->raw
$self->picking $self->raw
Truth table33%1/3
A && B
A= $self->pickingB= $self->raw
ABresult
0X0
100
111
206 # only a move or a switch redraws: a list of moves or a refusal would
207 # otherwise be wiped, or pushed off the top, by the board that follows
208 # it. A picked move draws its own screen, every keystroke of it
209 49 ▶ $self->render if $self->redraw && !$picking;
Branch: 1 branch
BranchTrueFalseTotal
if $self->redraw and not $picking 38 11 49
Condition66%2/3
$self->redraw and not $picking
!ll&&!rl&&r
11038
MC/DC50%1/2
$self->redraw and not $picking
$self->redraw not $picking
Truth table66%2/3
A and not(B)
A= $self->redrawB= not $picking
ABresult
0X0
100
111
210 49 ▶ if (my $result = $self->game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->game->result) 2 47 49
211 2 $self->highlight({});
212 2 ▶ $self->render if $picking;
Branch: 1 branch
BranchTrueFalseTotal
if $picking 0 2 2
213 2 $self->say($result->stringify);
214 2 last;
215 }
216 47 ▶ if ($self->bot_plays($self->game->turn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bot_plays($self->game->turn)) 5 42 47
217 5 $self->bot_move;
218 5 next;
219 }
220 42 ▶ my $line = $picking ? $self->pick : $self->read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 0 42 42
221 42 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 2 40 42
222 2 $self->say('Bye.');
223 2 last;
224 }
225 40 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 9 31 40
226 }
227 13 return $self;
228 }
229  
230 48 ▶sub bot_plays {
Subroutine bot_plays: called CC=5 SCAR=16.3
Pod documented
231 48 my ($self, $side) = @_;
232 48 ▶ return 0 if $self->human eq 'both' || !$self->bot;
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "both" or not $self->bot 29 19 48
Condition66%2/3
$self->human eq "both" or not $self->bot
l!l&&r!l&&!r
29019
MC/DC50%1/2
$self->human eq "both" or not $self->bot
$self->human eq "both" not $self->bot
Truth table66%2/3
A or not(B)
A= $self->human eq "both"B= not $self->bot
ABresult
1X1
011
000
233 19 ▶ return 1 if $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "none" 0 19 19
234 19 ▶ return $side eq $self->human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->human ? : 13 6 19
235 }
236  
237 5 ▶sub bot_move {
Subroutine bot_move: called CC=2 SCAR=7.0
Pod documented
238 5 my ($self) = @_;
239 5 ▶ my $move = $self->bot->choose($self->game) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $move = $self->bot->choose($self->game) or return undef 5 0 5
240 5 $self->game->move($move);
241 5 $self->say(sprintf '%s plays %s.', ucfirst $move->side, $move->coord_notation);
242 5 $self->redraw(1);
243 5 return $move;
244 }
245  
246 38 ▶sub keys_available {
Subroutine keys_available: called CC=5 SCAR=21.7
Pod documented
247 38 my ($self) = @_;
248 38 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 16 22 38
249 22 ▶ return 0 unless defined $self->in && -t $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->in and -t $self->in 22 0 22
Condition33%1/3
defined $self->in and -t $self->in
!ll&&!rl&&r
0220
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
250 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
251 }
252  
253 6 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=24.2
Pod documented
254 6 my ($self) = @_;
255 6 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 6 6
256 6 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 6 0 6
257 6 $self->raw(1);
258 6 return $self;
259 }
260 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
261 # cbreak rather than raw, so an interrupt is still an interrupt and not a
262 # key this has to know about
263 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
264 0 $self->raw(1);
265 0 return $self;
266 }
267  
268 16 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
269 16 my ($self) = @_;
270 16 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 15 1 16
271 1 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 1 1
272 1 $self->raw(0);
273 1 return $self;
274 }
275  
276 60 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=25.4
Pod documented
277 60 my ($self, $wait) = @_;
278 60 my $pending = $self->pending;
279 60 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 2 58 60
280 58 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 58 0 58
281 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
282 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
283 # nothing there yet, and the rest of an escape sequence is worth one short
284 # wait before the escape is taken for the key of that name
285 0 select undef, undef, undef, 0.05;
286 0 return Term::ReadKey::ReadKey(-1, $self->in);
287 }
288  
289 36 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
290 36 my ($self) = @_;
291 36 my $char = $self->read_char(1);
292 36 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 34 36
293 34 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 8 26 34
294 26 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 12 14 26
295 14 return $char;
296 }
297  
298 12 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
299 12 my ($self) = @_;
300 12 my $opener = $self->read_char(0);
301 12 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 12 12
302 # a pressed escape key that happens to be followed by a real keystroke must
303 # not eat it, so what is not the opening of a sequence goes back
304 12 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 2 10 12
Condition100%3/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
912
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
305 2 unshift @{$self->pending}, $opener;
306 2 return 'escape';
307 }
308 10 my $tail = '';
309 10 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 12 0 12
310 12 my $char = $self->read_char(0);
311 12 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 12 12
312 12 $tail .= $char;
313 12 ▶ last if $char =~ m/[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 10 2 12
314 }
315 10 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$SEQUENCE{$tail} || 'escape'
l!l
100
MC/DC0%0/1
$SEQUENCE{$tail} || 'escape'
$SEQUENCE{$tail}
Truth table50%1/2
A
A= $SEQUENCE{$tail}
Aresult
11
00
316 }
317  
318 5 ▶sub pick {
Subroutine pick: called CC=26 SCAR=40.4
Pod documented
319 5 my ($self) = @_;
320 5 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 5 5
321 5 my $legal = $self->game->legal_moves;
322 5 ▶ return undef unless @{$legal};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$legal;} 0 5 5
323  
324 5 my $at = 0;
325 5 my $notice = [];
326 5 while (1) {
327 8 $self->show_choice($legal, $at, $notice);
328 8 $notice = [];
329 8 my $key = $self->read_key;
330 8 ▶ 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 7 8
Condition66%4/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
107
not defined $key or $key eq "eof"
l!l&&r!l&&!r
107
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
331  
332 7 ▶ 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 7 7
Condition33%2/6
$key eq "up" or $key eq "left" or $key eq "k"
l!l&&r!l&&!r
007
$key eq "up" or $key eq "left"
l!l&&r!l&&!r
007
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
333 0 $at = ($at - 1) % @{$legal};
334 0 next;
335 }
336 7 ▶ 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") 2 5 7
Condition66%6/9
$key eq "down" or $key eq "right" or $key eq "tab" or $key eq "j"
l!l&&r!l&&!r
205
$key eq "down" or $key eq "right" or $key eq "tab"
l!l&&r!l&&!r
205
$key eq "down" or $key eq "right"
l!l&&r!l&&!r
205
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
337 2 $at = ($at + 1) % @{$legal};
338 2 next;
339 }
340 5 ▶ if ($key eq 'home' || $key eq 'page_up') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "home" or $key eq "page_up") 0 5 5
Condition33%1/3
$key eq "home" or $key eq "page_up"
l!l&&r!l&&!r
005
MC/DC0%0/2
$key eq "home" or $key eq "page_up"
$key eq "home" $key eq "page_up"
Truth table33%1/3
A or B
A= $key eq "home"B= $key eq "page_up"
ABresult
1X1
011
000
341 0 $at = 0;
342 0 next;
343 }
344 5 ▶ if ($key eq 'end' || $key eq 'page_down') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "end" or $key eq "page_down") 0 5 5
Condition33%1/3
$key eq "end" or $key eq "page_down"
l!l&&r!l&&!r
005
MC/DC0%0/2
$key eq "end" or $key eq "page_down"
$key eq "end" $key eq "page_down"
Truth table33%1/3
A or B
A= $key eq "end"B= $key eq "page_down"
ABresult
1X1
011
000
345 0 $at = $#{$legal};
346 0 next;
347 }
348 5 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 2 3 5
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
203
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
349 2 $self->highlight({});
350 2 $self->game->move($legal->[$at]);
351 2 $self->redraw(1);
352 2 return '';
353 }
354 3 ▶ if ($key =~ m/^[1-9]$/ && $key <= @{$legal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^[1-9]$/ and $key <= @{$legal;}) 1 2 3
Condition66%2/3
$key =~ /^[1-9]$/ and $key <= @{$legal;}
!ll&&!rl&&r
201
MC/DC50%1/2
$key =~ /^[1-9]$/ and $key <= @{$legal;}
$key =~ /^[1-9]$/ $key <= @{$legal;}
Truth table66%2/3
A and B
A= $key =~ /^[1-9]$/B= $key <= @{$legal;}
ABresult
0X0
100
111
355 1 $at = $key - 1;
356 1 next;
357 }
358 2 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 0 2 2
359 0 my $hint = $self->hint;
360 0 $notice = [$hint->{line}];
361 0 ▶ $at = $self->index_of($legal, $hint->{move}) if $hint->{move};
Branch: 1 branch
BranchTrueFalseTotal
if $hint->{"move"} 0 0 0
362 0 next;
363 }
364 2 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 2 2
365 0 $notice = $self->help_lines;
366 0 next;
367 }
368 2 ▶ if ($key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":") 1 1 2
369 1 $self->highlight({});
370 1 return $self->read_line(': ');
371 }
372 1 ▶ if ($KEY{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($KEY{$key}) 1 0 1
373 1 $self->highlight({});
374 1 return $KEY{$key};
375 }
376 0 $notice = ['That key does nothing here. Press ? for the ones that do.'];
377 }
378 }
379  
380 0 ▶sub index_of {
Subroutine index_of: not called CC=3 SCAR=24.8
Pod documented
381 0 my ($self, $legal, $move) = @_;
382 0 my $notation = $move->coord_notation;
383 0 for my $index (0 .. $#{$legal}) {
384 0 ▶ return $index if $legal->[$index]->coord_notation eq $notation;
Branch: 1 branch
BranchTrueFalseTotal
if $legal->[$index]->coord_notation eq $notation 0 0 0
385 }
386 0 return 0;
387 }
388  
389 8 ▶sub show_choice {
Subroutine show_choice: called CC=6 SCAR=18.3
Pod documented
390 8 my ($self, $legal, $at, $notice) = @_;
391 8 $self->highlight($self->move_marks($legal->[$at]));
392 8 $self->clear;
393 8 $self->say($_) for @{$self->board_lines}, '', @{$self->status_lines}, '',
394 8 @{$self->choice_lines($legal, $at)};
395 8 ▶ $self->say('') if $notice && @{$notice};
Branch: 1 branch
BranchTrueFalseTotal
if $notice and @{$notice;} 0 8 8
Condition33%1/3
$notice and @{$notice;}
!ll&&!rl&&r
080
MC/DC0%0/2
$notice and @{$notice;}
$notice @{$notice;}
Truth table33%1/3
A and B
A= $noticeB= @{$notice;}
ABresult
0X0
100
111
396 8 ▶ $self->say($_) for @{$notice || []};
Condition50%1/2
$notice || []
l!l
80
MC/DC0%0/1
$notice || []
$notice
Truth table50%1/2
A
A= $notice
Aresult
11
00
397 8 $self->say('');
398 8 $self->say($self->legend);
399 8 $self->redraw(0);
400 8 return $self;
401 }
402  
403 9 ▶sub move_marks {
Subroutine move_marks: called CC=3 SCAR=11.0
Pod documented
404 9 my ($self, $move) = @_;
405 9 my %mark;
406 9 $mark{$_} = 'cursor' for @{$move->path};
407 9 $mark{$_} = 'captured' for @{$move->captures};
408 9 $mark{$move->from} = 'from';
409 9 $mark{$move->to} = 'to';
410 9 return \%mark;
411 }
412  
413 10 ▶sub choice_lines {
Subroutine choice_lines: called CC=9 SCAR=22.0
Pod documented
414 10 my ($self, $legal, $at) = @_;
415 # the list is windowed so that a position with a lot of jumps in it cannot
416 # push the board off the top of the screen
417 10 my $window = 8;
418 10 my $first = 0;
419 10 ▶ if (@{$legal} > $window) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$legal;} > $window) 2 8 10
420 2 $first = $at - int($window / 2);
421 2 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 1 1 2
422 2 ▶ $first = @{$legal} - $window if $first > @{$legal} - $window;
Branch: 1 branch
BranchTrueFalseTotal
if $first > @{$legal;} - $window 1 1 2
423 }
424 10 my $last = $first + $window - 1;
425 10 ▶ $last = $#{$legal} if $last > $#{$legal};
Branch: 1 branch
BranchTrueFalseTotal
if $last > $#{$legal;} 8 2 10
426  
427 10 my @lines;
428 10 ▶ push @lines, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 1 9 10
429 10 for my $index ($first .. $last) {
430 72 my $move = $legal->[$index];
431 72 my $line = sprintf '%2d. %-11s %s',
432 $index + 1, $move->coord_notation, $self->describe($move);
433 72 $line =~ s/\s+$//;
434 72 ▶ push @lines, $index == $at
Branch: 1 branch
BranchTrueFalseTotal
$index == $at ? : 10 62 72
435 ? ' ' . $self->paint("> $line", '1;7')
436 : " $line";
437 }
438 1 push @lines, sprintf ' %d further down', $#{$legal} - $last
439 10 ▶ if $last < $#{$legal};
Branch: 1 branch
BranchTrueFalseTotal
if $last < $#{$legal;} 1 9 10
440 10 return \@lines;
441 }
442  
443 74 ▶sub describe {
Subroutine describe: called CC=3 SCAR=11.0
Pod documented
444 74 my ($self, $move) = @_;
445 74 my @note;
446 push @note, 'takes ' . join ', ',
447 2 map { Game::Checkers::Squares::coord_name($_) } @{$move->captures}
448 74 ▶ if @{$move->captures};
Branch: 1 branch
BranchTrueFalseTotal
if @{$move->captures;} 1 73 74
449 74 ▶ push @note, 'crowns' if $move->promoted;
Branch: 1 branch
BranchTrueFalseTotal
if $move->promoted 1 73 74
450 74 return join ', ', @note;
451 }
452  
453 8 ▶sub legend {
Subroutine legend: called CC=1 SCAR=0.0
Pod documented
454 8 return 'up and down to choose, enter to play, h hint, u undo, f flip, '
455 . 'q quit, : to type a command, ? for the rest';
456 }
457  
458 49 ▶sub read_line {
Subroutine read_line: called CC=4 SCAR=13.9
Pod documented
459 49 my ($self, $prompt) = @_;
460 49 my $out = $self->out;
461 49 ▶ print {$out} defined $prompt ? $prompt : $self->game->turn . '> ';
Branch: 1 branch
BranchTrueFalseTotal
defined $prompt ? : 7 42 49
462 49 ▶ return $self->keyed_line if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 1 48 49
463 48 my $line = readline $self->in;
464 48 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 46 48
465 46 chomp $line;
466 46 return $line;
467 }
468  
469 1 ▶sub keyed_line {
Subroutine keyed_line: called CC=9 SCAR=26.8
Pod documented
470 1 my ($self) = @_;
471 1 my $out = $self->out;
472 1 my $line = '';
473 1 while (1) {
474 8 my $key = $self->read_key;
475 8 ▶ 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" 0 8 8
Condition33%2/6
not defined $key or $key eq "eof" or $key eq "interrupt"
l!l&&r!l&&!r
008
not defined $key or $key eq "eof"
l!l&&r!l&&!r
008
MC/DC0%0/3
not defined $key or $key eq "eof" or $key eq "interrupt"
not defined $key $key eq "eof" $key eq "interrupt"
Truth table25%1/4
not(A) or B or C
A= not defined $keyB= $key eq "eof"C= $key eq "interrupt"
ABCresult
1XX1
01X1
0011
0000
476 8 ▶ last if $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" 1 7 8
477 7 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 0 7 7
478 0 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
479 0 chop $line;
480 # back over the character, blank it, and back over it again, which
481 # is all a raw terminal will do for a rubbed out letter
482 0 print {$out} "\b \b";
483 0 next;
484 }
485 7 ▶ next if length $key > 1 || $key lt ' ';
Branch: 1 branch
BranchTrueFalseTotal
if length $key > 1 or $key lt " " 0 7 7
Condition33%1/3
length $key > 1 or $key lt " "
l!l&&r!l&&!r
007
MC/DC0%0/2
length $key > 1 or $key lt " "
length $key > 1 $key lt " "
Truth table33%1/3
A or B
A= length $key > 1B= $key lt " "
ABresult
1X1
011
000
486 7 $line .= $key;
487 7 print {$out} $key;
488 }
489 1 print {$out} "\n";
490 1 return $line;
491 }
492  
493 1128 ▶sub say {
Subroutine say: called CC=2 SCAR=7.0
Pod documented
494 1128 my ($self, $line) = @_;
495 1128 my $out = $self->out;
496 1128 ▶ print {$out} (defined $line ? $line : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 1128 0 1128
497 1128 return $self;
498 }
499  
500 51 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
501 51 my ($self) = @_;
502 51 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 50 1 51
503 1 my $out = $self->out;
504 1 print {$out} "\e[2J\e[H";
505 1 return $self;
506 }
507  
508 41 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
509 41 my ($self) = @_;
510 41 $self->clear;
511 41 $self->say($_) for @{$self->board_lines}, '', @{$self->status_lines};
512 41 $self->redraw(0);
513 41 return $self;
514 }
515  
516 57 ▶sub board_lines {
Subroutine board_lines: called CC=10 SCAR=23.0
Pod documented
517 57 my ($self) = @_;
518 57 my $board = $self->game->board;
519 57 my $rule = ' +' . ('---+' x 8);
520 57 my @files = ('a' .. 'h');
521 57 my @rows = 0 .. 7;
522 57 my @cols = 0 .. 7;
523 57 ▶ if ($self->flip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flip) 3 54 57
524 3 @rows = reverse @rows;
525 3 @cols = reverse @cols;
526 }
527  
528 57 my @aside = @{$self->aside};
529 # five spaces puts a over the middle of the first cell, which the rule line
530 # opens at index 3. A painted board drops the rule and the bars, because the
531 # squares then meet, and widens the cell to five so a piece has a middle to
532 # sit in. Either way the first file stays at index five and the pitch stays
533 # the cell width, so the letters sit over the pieces
534 57 ▶ my @lines = $self->colour
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 4 53 57
535 ? (' ' . join(' ', @files[@cols]))
536 : (' ' . join(' ', @files[@cols]));
537 57 ▶ push @lines, $rule unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 53 4 57
538 57 for my $row (@rows) {
539 456 ▶ my $line = $self->colour
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 32 424 456
540 ? sprintf('%2d ', 8 - $row)
541 : sprintf('%2d |', 8 - $row);
542 456 for my $col (@cols) {
543 3648 $line .= $self->cell($board, $row, $col);
544 3648 ▶ $line .= '|' unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 3392 256 3648
545 }
546 456 ▶ if (my $note = shift @aside) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $note = shift @aside) 114 342 456
547 114 $line .= ' ' . $note;
548 }
549 456 push @lines, $line;
550 456 ▶ push @lines, $rule unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 424 32 456
551 }
552 57 return \@lines;
553 }
554  
555 3648 ▶sub cell {
Subroutine cell: called CC=14 SCAR=26.4
Pod documented
556 3648 my ($self, $board, $row, $col) = @_;
557 3648 my $square = Game::Checkers::Squares::square($row, $col);
558 3648 ▶ my $value = $square ? $board->at($square) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$square ? : 1824 1824 3648
559 3648 ▶ my $piece = $value ? $PIECE{$value}{$self->ascii ? 'ascii' : 'wide'} : undef;
Branch: 2 branches
BranchTrueFalseTotal
$value ? : 1265 2383 3648
$self->ascii ? : 1193 72 1265
560  
561 # an empty playing square is dotted, because half the squares are not in the
562 # game at all and nothing else unpainted says which half
563 3648 ▶ unless ($self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->colour) 3392 256 3648
564 3392 ▶ return ' ' unless $square;
Branch: 1 branch
BranchTrueFalseTotal
unless $square 1696 1696 3392
565 1696 ▶ return ' ' . (defined $piece ? $piece : '.') . ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $piece ? : 1169 527 1696
566 }
567  
568 256 ▶ my $mark = $square ? $self->highlight->{$square} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$square ? : 128 128 256
569 256 ▶ $mark = $square ? 'dark' : 'light' unless $mark && $GROUND{$mark};
Branch: 2 branches
BranchTrueFalseTotal
unless $mark and $GROUND{$mark} 254 2 256
$square ? : 126 128 254
Condition66%2/3
$mark and $GROUND{$mark}
!ll&&!rl&&r
25402
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
570 256 my @code = ($GROUND{$mark});
571 256 ▶ push @code, $INK{$value > 0 ? 'black' : 'white'} if defined $piece;
Branch: 2 branches
BranchTrueFalseTotal
if defined $piece 96 160 256
$value > 0 ? : 48 48 96
572 256 ▶ return "\e[" . join(';', @code) . 'm '
Branch: 1 branch
BranchTrueFalseTotal
defined $piece ? : 96 160 256
573 . (defined $piece ? $piece : ' ') . " \e[0m";
574 }
575  
576 10 ▶sub paint {
Subroutine paint: called CC=2 SCAR=8.1
Pod documented
577 10 my ($self, $text, $code) = @_;
578 10 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 10 0 10
579 0 return "\e[${code}m" . $text . "\e[0m";
580 }
581  
582 57 ▶sub aside {
Subroutine aside: called CC=2 SCAR=6.9
Pod documented
583 57 my ($self) = @_;
584 57 my $board = $self->game->board;
585 57 my @aside;
586 57 for my $side (qw/black white/) {
587 114 my $count = $board->count($side);
588 push @aside, sprintf '%-5s %2d (%d kings)',
589 114 ucfirst $side, $count->{total}, $count->{kings};
590 }
591 57 return \@aside;
592 }
593  
594 54 ▶sub status_lines {
Subroutine status_lines: called CC=8 SCAR=20.8
Pod documented
595 54 my ($self) = @_;
596 54 my $game = $self->game;
597 54 my @lines;
598  
599 54 ▶ if (my $last = $game->history->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $game->history->[-1]) 26 28 54
600 push @lines, sprintf 'Last: %s by %s%s',
601 $last->coord_notation, $last->side,
602 26 (@{$last->captures}
603 ? ', taking ' . join(', ',
604 7 map { Game::Checkers::Squares::coord_name($_) }
605 26 ▶ @{$last->captures})
Branch: 1 branch
BranchTrueFalseTotal
@{$last->captures;} ? : 7 19 26
606 : '');
607 }
608  
609 54 ▶ if (my $result = $game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $game->result) 3 51 54
610 3 push @lines, $result->stringify;
611 3 return \@lines;
612 }
613  
614 51 my $moves = scalar @{$game->legal_moves};
615 51 ▶ push @lines, $game->must_capture
Branch: 3 branches
BranchTrueFalseTotal
$game->must_capture ? : 10 41 51
$moves == 1 ? : 6 4 10
$moves == 1 ? : 0 41 41
616 ? sprintf('%s must capture, %d %s available',
617 ucfirst $game->turn, $moves, $moves == 1 ? 'jump' : 'jumps')
618 : sprintf('%s to move, %d %s',
619 ucfirst $game->turn, $moves, $moves == 1 ? 'move' : 'moves');
620  
621 51 ▶ push @lines, sprintf '%s has offered a draw.', ucfirst $game->draw_offered_by
Branch: 1 branch
BranchTrueFalseTotal
if $game->draw_offered_by 1 50 51
622 if $game->draw_offered_by;
623  
624 51 return \@lines;
625 }
626  
627 41 ▶sub command {
Subroutine command: called CC=22 SCAR=31.6
Pod documented
628 41 my ($self, $line) = @_;
629 41 $line =~ s/^\s+|\s+$//g;
630 41 ▶ return 0 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 41 41
631  
632 41 my ($word, $argument) = split ' ', $line, 2;
633 41 $word = lc $word;
634  
635 41 ▶ return $self->show_help if $word eq 'help' || $word eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "help" or $word eq "?" 0 41 41
Condition33%1/3
$word eq "help" or $word eq "?"
l!l&&r!l&&!r
0041
MC/DC0%0/2
$word eq "help" or $word eq "?"
$word eq "help" $word eq "?"
Truth table33%1/3
A or B
A= $word eq "help"B= $word eq "?"
ABresult
1X1
011
000
636 41 ▶ return $self->redraw(1) && 0 if $word eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "board" 0 41 41
Condition0%0/2
$self->redraw(1) && 0
!ll
00
MC/DC0%0/1
$self->redraw(1) && 0
$self->redraw(1)
Truth table0%0/2
A
A= $self->redraw(1)
Aresult
00
11
637 41 ▶ return $self->show_moves if $word eq 'moves';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "moves" 2 39 41
638 39 ▶ return $self->set_picking($word) if $word eq 'pick' || $word eq 'type';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "pick" or $word eq "type" 1 38 39
Condition66%2/3
$word eq "pick" or $word eq "type"
l!l&&r!l&&!r
0138
MC/DC50%1/2
$word eq "pick" or $word eq "type"
$word eq "pick" $word eq "type"
Truth table66%2/3
A or B
A= $word eq "pick"B= $word eq "type"
ABresult
1X1
011
000
639 38 ▶ return $self->show_hint if $word eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "hint" 1 37 38
640 37 ▶ return $self->say('FEN: ' . $self->game->to_fen) && 0 if $word eq 'fen';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "fen" 1 36 37
Condition50%1/2
$self->say('FEN: ' . $self->game->to_fen) && 0
!ll
01
MC/DC0%0/1
$self->say('FEN: ' . $self->game->to_fen) && 0
$self->say('FEN: ' . $self->game->to_fen)
Truth table50%1/2
A
A= $self->say('FEN: ' . $self->game->to_fen)
Aresult
00
11
641 36 ▶ return $self->take_back if $word eq 'undo';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "undo" 1 35 36
642 35 ▶ return $self->save($argument) if $word eq 'save';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "save" 1 34 35
643 34 ▶ return $self->load($argument) if $word eq 'load';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "load" 1 33 34
644 33 ▶ return $self->set_level($argument) if $word eq 'level';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "level" 1 32 33
645 32 ▶ return $self->toggle($word) if $word eq 'flip' || $word eq 'ascii';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "flip" or $word eq "ascii" 2 30 32
Condition100%3/3
$word eq "flip" or $word eq "ascii"
l!l&&r!l&&!r
1130
MC/DC100%2/2
$word eq "flip" or $word eq "ascii"
$word eq "flip" $word eq "ascii"
Truth table100%3/3
A or B
A= $word eq "flip"B= $word eq "ascii"
ABresult
1X1
011
000
646 30 ▶ return $self->offer_draw if $word eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "draw" 0 30 30
647 30 ▶ return $self->resign if $word eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "resign" 1 29 30
648 29 ▶ return $self->quit if $word eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "quit" 9 20 29
649  
650 20 ▶ return $self->play_number($word) if $word =~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^[0-9]+$/ 4 16 20
651 16 return $self->play($line);
652 }
653  
654 16 ▶sub play {
Subroutine play: called CC=6 SCAR=18.0
Pod documented
655 16 my ($self, $notation) = @_;
656 16 my $move = $self->game->move($notation);
657 16 ▶ return $self->redraw(1) && 0 unless ref $move eq 'Game::Checkers::Error';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "Game::Checkers::Error" 11 5 16
Condition50%1/2
$self->redraw(1) && 0
!ll
011
MC/DC0%0/1
$self->redraw(1) && 0
$self->redraw(1)
Truth table50%1/2
A
A= $self->redraw(1)
Aresult
00
11
658  
659 5 $self->say(ucfirst $move->message . '.');
660 5 ▶ $self->say('Type moves for the list, or help for the commands.')
Branch: 1 branch
BranchTrueFalseTotal
if $move->not_a_move 2 3 5
661 if $move->not_a_move;
662 $self->say('You could play: '
663 21 . join ', ', map { $_->coord_notation } @{$move->legal})
664 5 ▶ if @{$move->legal} && !$move->not_a_move;
Branch: 1 branch
BranchTrueFalseTotal
if @{$move->legal;} and not $move->not_a_move 3 2 5
Condition66%2/3
@{$move->legal;} and not $move->not_a_move
!ll&&!rl&&r
023
MC/DC50%1/2
@{$move->legal;} and not $move->not_a_move
@{$move->legal;} not $move->not_a_move
Truth table66%2/3
A and not(B)
A= @{$move->legal;}B= not $move->not_a_move
ABresult
0X0
100
111
665 5 return 0;
666 }
667  
668 4 ▶sub play_number {
Subroutine play_number: called CC=4 SCAR=15.5
Pod documented
669 4 my ($self, $number) = @_;
670 4 my $legal = $self->game->legal_moves;
671 return $self->say("There is no move $number. Type moves for the list.") && 0
672 4 ▶ unless $number >= 1 && $number <= @{$legal};
Branch: 1 branch
BranchTrueFalseTotal
unless $number >= 1 and $number <= @{$legal;} 0 4 4
Condition20%1/5
$number >= 1 and $number <= @{$legal;}
!ll&&!rl&&r
004
$self->say("There is no move $number. Type moves for the list.") && 0
!ll
00
MC/DC0%0/2
$number >= 1 and $number <= @{$legal;}
$number >= 1 $number <= @{$legal;}
MC/DC0%0/1
$self->say("There is no move $number. Type moves for the list.") && 0
$self->say("There is no move $number. Type moves for the list.")
Truth table33%1/3
A and B
A= $number >= 1B= $number <= @{$legal;}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->say("There is no move $number. Type moves for the list.")
Aresult
00
11
673 4 $self->game->move($legal->[$number - 1]);
674 4 $self->redraw(1);
675 4 return 0;
676 }
677  
678 2 ▶sub show_moves {
Subroutine show_moves: called CC=2 SCAR=6.9
Pod documented
679 2 my ($self) = @_;
680 2 my $legal = $self->game->legal_moves;
681 2 my $number = 0;
682 2 $self->say(sprintf '%2d. %s', ++$number, $_->coord_notation) for @{$legal};
683 2 $self->say('Play one by its number, or type it out.');
684 2 return 0;
685 }
686  
687 1 ▶sub hint {
Subroutine hint: called CC=4 SCAR=15.0
Pod documented
688 1 my ($self) = @_;
689 1 ▶ my $bot = $self->bot || Game::Checkers::Bot->new;
Condition33%1/3
$self->bot || 'Game::Checkers::Bot'->new
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->bot || 'Game::Checkers::Bot'->new
$self->bot 'Game::Checkers::Bot'->new
Truth table33%1/3
A || B
A= $self->botB= 'Game::Checkers::Bot'->new
ABresult
1X1
011
000
690 1 my $move = $bot->choose($self->game);
691 1 ▶ return { line => 'There is nothing to play.' } unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 1 1
692 1 my $score = $bot->last_search->{score};
693 return {
694 1 ▶ move => $move,
Branch: 1 branch
BranchTrueFalseTotal
defined $score ? : 1 0 1
695 line => sprintf('Try %s%s.', $move->coord_notation,
696 defined $score ? sprintf(' (it scores that %+.2f)', $score / 100) : '')
697 };
698 }
699  
700 1 ▶sub show_hint {
Subroutine show_hint: called CC=1 SCAR=0.0
Pod documented
701 1 my ($self) = @_;
702 1 $self->say($self->hint->{line});
703 1 return 0;
704 }
705  
706 1 ▶sub take_back {
Subroutine take_back: called CC=5 SCAR=18.1
Pod documented
707 1 my ($self) = @_;
708 1 my $game = $self->game;
709 1 ▶ return $self->say('There is nothing to take back.') && 0 unless $game->ply;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->ply 0 1 1
Condition0%0/2
$self->say('There is nothing to take back.') && 0
!ll
00
MC/DC0%0/1
$self->say('There is nothing to take back.') && 0
$self->say('There is nothing to take back.')
Truth table0%0/2
A
A= $self->say('There is nothing to take back.')
Aresult
00
11
710  
711 1 $game->undo;
712 # and the reply that provoked it, so the board comes back as you left it
713 1 ▶ $game->undo if $game->ply && $self->bot_plays($game->turn);
Branch: 1 branch
BranchTrueFalseTotal
if $game->ply and $self->bot_plays($game->turn) 1 0 1
Condition33%1/3
$game->ply and $self->bot_plays($game->turn)
!ll&&!rl&&r
001
MC/DC0%0/2
$game->ply and $self->bot_plays($game->turn)
$game->ply $self->bot_plays($game->turn)
Truth table33%1/3
A and B
A= $game->plyB= $self->bot_plays($game->turn)
ABresult
0X0
100
111
714 1 $self->say('Taken back.');
715 1 $self->redraw(1);
716 1 return 0;
717 }
718  
719 1 ▶sub save {
Subroutine save: called CC=5 SCAR=18.4
Pod documented
720 1 my ($self, $file) = @_;
721 1 ▶ return $self->say('Save needs a file name.') && 0 unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 1 1
Condition0%0/2
$self->say('Save needs a file name.') && 0
!ll
00
MC/DC0%0/1
$self->say('Save needs a file name.') && 0
$self->say('Save needs a file name.')
Truth table0%0/2
A
A= $self->say('Save needs a file name.')
Aresult
00
11
722 1 ▶ open my $handle, '>', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, ">", $file or return $self->say("Cannot write ${file}: $!") && 0 1 0 1
Condition0%0/2
$self->say("Cannot write ${file}: $!") && 0
!ll
00
MC/DC0%0/1
$self->say("Cannot write ${file}: $!") && 0
$self->say("Cannot write ${file}: $!")
Truth table0%0/2
A
A= $self->say("Cannot write ${file}: $!")
Aresult
00
11
723 or return $self->say("Cannot write $file: $!") && 0;
724 1 print {$handle} $self->game->to_pdn(Event => 'Terminal checkers');
725 1 close $handle;
726 1 $self->say("Saved to $file.");
727 1 return 0;
728 }
729  
730 1 ▶sub load {
Subroutine load: called CC=7 SCAR=22.0
Pod documented
731 1 my ($self, $file) = @_;
732 1 ▶ return $self->say('Load needs a file name.') && 0 unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 1 1
Condition0%0/2
$self->say('Load needs a file name.') && 0
!ll
00
MC/DC0%0/1
$self->say('Load needs a file name.') && 0
$self->say('Load needs a file name.')
Truth table0%0/2
A
A= $self->say('Load needs a file name.')
Aresult
00
11
733 1 ▶ open my $handle, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $file or return $self->say("Cannot read ${file}: $!") && 0 1 0 1
Condition0%0/2
$self->say("Cannot read ${file}: $!") && 0
!ll
00
MC/DC0%0/1
$self->say("Cannot read ${file}: $!") && 0
$self->say("Cannot read ${file}: $!")
Truth table0%0/2
A
A= $self->say("Cannot read ${file}: $!")
Aresult
00
11
734 or return $self->say("Cannot read $file: $!") && 0;
735 1 my $pdn = do { local $/; <$handle> };
736 1 close $handle;
737 1 my $game = eval { Game::Checkers->from_pdn($pdn) };
738 1 ▶ return $self->say("That is not a game I can read: $@") && 0 unless $game;
Branch: 1 branch
BranchTrueFalseTotal
unless $game 0 1 1
Condition0%0/2
$self->say("That is not a game I can read: $@") && 0
!ll
00
MC/DC0%0/1
$self->say("That is not a game I can read: $@") && 0
$self->say("That is not a game I can read: $@")
Truth table0%0/2
A
A= $self->say("That is not a game I can read: $@")
Aresult
00
11
739 1 $self->game($game);
740 1 $self->say("Loaded $file, " . $game->ply . ' moves in.');
741 1 $self->redraw(1);
742 1 return 0;
743 }
744  
745 1 ▶sub set_level {
Subroutine set_level: called CC=5 SCAR=18.9
Pod documented
746 1 my ($self, $level) = @_;
747 1 ▶ return $self->say('Level takes a number from 1 to 5.') && 0
Branch: 1 branch
BranchTrueFalseTotal
unless defined $level and $level =~ /^[1-5]$/ 0 1 1
Condition20%1/5
defined $level and $level =~ /^[1-5]$/
!ll&&!rl&&r
001
$self->say('Level takes a number from 1 to 5.') && 0
!ll
00
MC/DC0%0/2
defined $level and $level =~ /^[1-5]$/
defined $level $level =~ /^[1-5]$/
MC/DC0%0/1
$self->say('Level takes a number from 1 to 5.') && 0
$self->say('Level takes a number from 1 to 5.')
Truth table33%1/3
A and B
A= defined $levelB= $level =~ /^[1-5]$/
ABresult
0X0
100
111
Truth table0%0/2
A
A= $self->say('Level takes a number from 1 to 5.')
Aresult
00
11
748 unless defined $level && $level =~ m/^[1-5]$/;
749 1 ▶ $self->bot(Game::Checkers::Bot->new) unless $self->bot;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bot 0 1 1
750 1 $self->bot->level($level);
751 1 $self->say("The bot is at level $level.");
752 1 return 0;
753 }
754  
755 1 ▶sub set_picking {
Subroutine set_picking: called CC=4 SCAR=18.6
Pod documented
756 1 my ($self, $word) = @_;
757 1 ▶ if ($word eq 'type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "type") 1 0 1
758 # raw mode goes with it, so the terminal does its own line editing again
759 1 $self->leave_raw;
760 1 $self->picking(0);
761 1 $self->say('Type your moves. The command pick brings the keys back.');
762 1 return 0;
763 }
764 0 ▶ return $self->say('This terminal cannot be read a key at a time.') && 0
Branch: 1 branch
BranchTrueFalseTotal
unless $self->enter_raw 0 0 0
Condition0%0/2
$self->say('This terminal cannot be read a key at a time.') && 0
!ll
00
MC/DC0%0/1
$self->say('This terminal cannot be read a key at a time.') && 0
$self->say('This terminal cannot be read a key at a time.')
Truth table0%0/2
A
A= $self->say('This terminal cannot be read a key at a time.')
Aresult
00
11
765 unless $self->enter_raw;
766 0 $self->picking(1);
767 0 return 0;
768 }
769  
770 2 ▶sub toggle {
Subroutine toggle: called CC=2 SCAR=7.0
Pod documented
771 2 my ($self, $what) = @_;
772 2 ▶ $self->$what($self->$what ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->$what ? : 0 2 2
773 2 $self->redraw(1);
774 2 return 0;
775 }
776  
777 0 ▶sub offer_draw {
Subroutine offer_draw: not called CC=8 SCAR=42.8
Pod documented
778 0 my ($self) = @_;
779 0 my $game = $self->game;
780 0 ▶ my $side = $self->human eq 'both' ? $game->turn : $self->human;
Branch: 1 branch
BranchTrueFalseTotal
$self->human eq 'both' ? : 0 0 0
781 0 $game->offer_draw($side);
782 0 $self->redraw(1);
783 0 ▶ return 0 unless $self->bot;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bot 0 0 0
784  
785 # the bot answers from what it thought of the position last time it moved,
786 # which costs nothing and is the same number it played on
787 0 ▶ my $score = $self->bot->last_search ? $self->bot->last_search->{score} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->bot->last_search ? : 0 0 0
788 0 ▶ if (defined $score && $score <= 30) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $score and $score <= 30) { } 0 0 0
Condition0%0/3
defined $score and $score <= 30
!ll&&!rl&&r
000
MC/DC0%0/2
defined $score and $score <= 30
defined $score $score <= 30
Truth table0%0/3
A and B
A= defined $scoreB= $score <= 30
ABresult
0X0
100
111
789 0 ▶ $game->accept_draw($side eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 0 0 0
790 0 $self->say('The bot takes the draw.');
791 } else {
792 0 ▶ $game->decline_draw($side eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 0 0 0
793 0 $self->say('The bot plays on.');
794 }
795 0 return 0;
796 }
797  
798 1 ▶sub resign {
Subroutine resign: called CC=2 SCAR=7.0
Pod documented
799 1 my ($self) = @_;
800 1 my $game = $self->game;
801 1 ▶ $game->resign($self->human eq 'both' ? $game->turn : $self->human);
Branch: 1 branch
BranchTrueFalseTotal
$self->human eq 'both' ? : 0 1 1
802 1 $self->redraw(1);
803 1 return 0;
804 }
805  
806 9 ▶sub quit {
Subroutine quit: called CC=5 SCAR=17.8
Pod documented
807 9 my ($self) = @_;
808 9 ▶ return 1 unless $self->game->status eq 'active' && $self->game->ply;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game->status eq "active" and $self->game->ply 3 6 9
Condition66%2/3
$self->game->status eq "active" and $self->game->ply
!ll&&!rl&&r
036
MC/DC50%1/2
$self->game->status eq "active" and $self->game->ply
$self->game->status eq "active" $self->game->ply
Truth table66%2/3
A and B
A= $self->game->status eq "active"B= $self->game->ply
ABresult
0X0
100
111
809 6 my $answer = $self->read_line('Really quit, with the game unfinished? (y/n) ');
810 6 ▶ return 1 if !defined $answer || $answer =~ m/^\s*y/i;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $answer or $answer =~ /^\s*y/i 6 0 6
Condition33%1/3
not defined $answer or $answer =~ /^\s*y/i
l!l&&r!l&&!r
060
MC/DC0%0/2
not defined $answer or $answer =~ /^\s*y/i
not defined $answer $answer =~ /^\s*y/i
Truth table33%1/3
not(A) or B
A= not defined $answerB= $answer =~ /^\s*y/i
ABresult
1X1
011
000
811 0 return 0;
812 }
813  
814 0 ▶sub help_lines {
Subroutine help_lines: not called CC=2 SCAR=17.9
Pod documented
815 0 my ($self) = @_;
816 return [
817 'A move is its squares: a3-b4 to slide, c3xe5xg7 to jump.',
818 0 map { sprintf ' %-12s %s', $_, $COMMAND{$_} }
819 0 ▶ sort { length $a <=> length $b || $a cmp $b } keys %COMMAND
Condition0%0/3
length $a <=> length $b || $a cmp $b
l!l&&r!l&&!r
000
MC/DC0%0/2
length $a <=> length $b || $a cmp $b
length $a <=> length $b $a cmp $b
Truth table0%0/3
A || B
A= length $a <=> length $bB= $a cmp $b
ABresult
1X1
011
000
820 ];
821 }
822  
823 0 ▶sub show_help {
Subroutine show_help: not called CC=2 SCAR=17.9
Pod documented
824 0 my ($self) = @_;
825 0 $self->say($_) for @{$self->help_lines};
826 0 return 0;
827 }
828  
829 1 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
830 1 return <<'USAGE';
831 checkers [options]
832  
833 --level N the strength of the bot, 1 to 5, 3 by default. Give it
834 twice with --bot-vs-bot for one level a side.
835 --side SIDE black, white or random. Black moves first.
836 --hotseat two people at one keyboard, no bot.
837 --bot-vs-bot watch two bots play.
838 --fen STRING start from a position instead of the opening.
839 --load FILE read a PDN game and play on from the end of it.
840 --replay FILE step through a PDN game, one move a keypress.
841 --analyse print the bot's move for the position and stop.
842 --seed N the seed the bot varies its easy levels with.
843 --ascii letters instead of the draughts symbols.
844 --no-colour no ANSI colour. NO_COLOR in the environment does the same.
845 --no-pick type the moves out instead of choosing them with the keys.
846 --version, --help
847  
848 On a terminal the legal moves are listed under the board: the up and down keys
849 choose one, enter plays it, and the board shows where it goes. Press ? for the
850 other keys, or : to type a command.
851  
852 Typing a move works everywhere, and is all there is when the input is not a
853 terminal or Term::ReadKey is not installed. A move is its squares, read off the
854 edge of the board: a3-b4 to slide, c3xe5xg7 to jump. Type help for the commands.
855  
856 Exit status is 0 if you won, drew or quit, 1 if you lost, 2 for a bad option.
857 USAGE
858 }
859  
860 0 ▶sub _side {
Subroutine _side: not called CC=5 SCAR=34.0
861 0 my ($side) = @_;
862 0 $side = lc $side;
863 0 ▶ return $side if $side eq 'black' || $side eq 'white';
Branch: 1 branch
BranchTrueFalseTotal
if $side eq "black" or $side eq "white" 0 0 0
Condition0%0/3
$side eq "black" or $side eq "white"
l!l&&r!l&&!r
000
MC/DC0%0/2
$side eq "black" or $side eq "white"
$side eq "black" $side eq "white"
Truth table0%0/3
A or B
A= $side eq "black"B= $side eq "white"
ABresult
1X1
011
000
864 0 ▶ return int(rand 2) ? 'black' : 'white' if $side eq 'random';
Branch: 2 branches
BranchTrueFalseTotal
if $side eq "random" 0 0 0
int rand 2 ? : 0 0 0
865 0 return undef;
866 }
867  
868 3 ▶sub _build_game {
Subroutine _build_game: called CC=3 SCAR=11.1
869 3 my ($option) = @_;
870 3 ▶ return Game::Checkers->from_pdn(_read_pdn($option->{load})) if $option->{load};
Branch: 1 branch
BranchTrueFalseTotal
if $option->{"load"} 0 3 3
871 3 ▶ return Game::Checkers->new($option->{fen} ? (fen => $option->{fen}) : ());
Branch: 1 branch
BranchTrueFalseTotal
$option->{'fen'} ? : 1 2 3
872 }
873  
874 0 ▶sub _read_pdn {
Subroutine _read_pdn: not called CC=2 SCAR=17.9
875 0 my ($file) = @_;
876 0 ▶ open my $handle, '<', $file or die "cannot read $file: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<", $file or die "cannot read ${file}: $!\n" 0 0 0
877 0 my $pdn = do { local $/; <$handle> };
878 0 close $handle;
879 0 return $pdn;
880 }
881  
882 1 ▶sub _analyse {
Subroutine _analyse: called CC=4 SCAR=15.8
883 1 my ($game, $level, $seed) = @_;
884 1 my $bot = Game::Checkers::Bot->new(level => $level, seed => $seed);
885 1 my $move = $bot->choose($game);
886 1 ▶ unless ($move) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($move) 0 1 1
887 0 ▶ print 'no move: ', ($game->result ? $game->result->stringify : 'no legal move'), "\n";
Branch: 1 branch
BranchTrueFalseTotal
$game->result ? : 0 0 0
888 0 return;
889 }
890 1 my $search = $bot->last_search;
891 printf "move %s\nscore %s\ndepth %d\nnodes %d\nline %s\n",
892 $move->notation,
893 defined $search->{score} ? $search->{score} : 'forced',
894 $search->{depth},
895 $search->{nodes},
896 1 ▶ join ' ', @{$search->{pv}};
Branch: 1 branch
BranchTrueFalseTotal
defined $search->{'score'} ? : 1 0 1
897 1 return;
898 }
899  
900 0 ▶sub _replay {
Subroutine _replay: not called CC=3 SCAR=24.8
901 0 my ($class, $pdn, $option) = @_;
902 0 my $whole = Game::Checkers->from_pdn($pdn);
903 0 my $game = Game::Checkers->new(fen => $whole->fen);
904 my $terminal = $class->new(
905 game => $game,
906 human => 'both',
907 ascii => $option->{ascii},
908 picking => 0,
909 0 ▶ (defined $option->{colour} ? (colour => $option->{colour}) : ())
Branch: 1 branch
BranchTrueFalseTotal
defined $option->{'colour'} ? : 0 0 0
910 );
911 0 for my $move (@{$whole->history}) {
912 0 $terminal->render;
913 0 $terminal->read_line('Press enter for ' . $move->coord_notation . ' ');
914 0 $game->move($move->notation);
915 }
916 0 $terminal->render;
917 0 return;
918 }
919  
920 6 ▶sub run {
Subroutine run: called CC=23 SCAR=39.8
Pod documented
921 6 my ($class, @argv) = @_;
922 6 my %option = (
923 level => [],
924 side => 'black',
925 ascii => 0,
926 seed => 0,
927 );
928  
929 6 my $ok = eval {
930 6 ▶ local $SIG{__WARN__} = sub { print STDERR "checkers: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
931 6 Getopt::Long::GetOptionsFromArray(
932 \@argv,
933 \%option,
934 'level=i@',
935 'side=s',
936 'hotseat',
937 'bot-vs-bot',
938 'fen=s',
939 'load=s',
940 'replay=s',
941 'seed=i',
942 'ascii',
943 'colour!',
944 'pick!',
945 'analyse',
946 'version',
947 'help'
948 );
949 };
950 6 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 0 6 6
951 6 ▶ if ($option{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"help"}) 1 5 6
952 5 ▶ if (@argv) { print STDERR "checkers: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 0 5 5
953  
954 5 ▶ if ($option{version}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"version"}) 1 4 5
955 1 print "checkers, Game::Checkers $Game::Checkers::VERSION\n";
956 1 return 0;
957 }
958  
959 4 my @level = @{$option{level}};
960 4 ▶ push @level, 3 while @level < 2;
Branch: 1 branch
BranchTrueFalseTotal
@level < 2 and do { push @level, 3 } 6 4 10
961 4 ▶ if (grep { $_ < 1 || $_ > 5 } @level) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ < 1 or $_ > 5;} @level) 1 3 4
Condition66%2/3
$_ < 1 || $_ > 5
l!l&&r!l&&!r
017
MC/DC50%1/2
$_ < 1 || $_ > 5
$_ < 1 $_ > 5
Truth table66%2/3
A || B
A= $_ < 1B= $_ > 5
ABresult
1X1
011
000
962 1 print STDERR "checkers: level must be 1 to 5\n";
963 1 return 2;
964 }
965  
966 3 my $game = _build_game(\%option);
967  
968 3 ▶ if ($option{analyse}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option{"analyse"}) 1 2 3
969 1 _analyse($game, $level[0], $option{seed});
970 1 return 0;
971 }
972  
973 2 ▶ if (my $file = $option{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $option{"replay"}) 0 2 2
974 0 $class->_replay(_read_pdn($file), \%option);
975 0 return 0;
976 }
977  
978 my $human = $option{hotseat} ? 'both'
979 : $option{'bot-vs-bot'} ? 'none'
980 2 ▶ : _side($option{side});
Branch: 2 branches
BranchTrueFalseTotal
$option{'hotseat'} ? : 2 0 2
$option{'bot-vs-bot'} ? : 0 0 0
981 2 ▶ if (!defined $human) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $human) 0 2 2
982 0 print STDERR "checkers: side must be black, white or random\n";
983 0 return 2;
984 }
985  
986 my $terminal = $class->new(
987 game => $game,
988 human => $human,
989 ($human eq 'both' ? () : (
990 bot => Game::Checkers::Bot->new(level => $level[0], seed => $option{seed})
991 )),
992 ascii => $option{ascii},
993 (defined $option{colour} ? (colour => $option{colour}) : ()),
994 2 ▶ (defined $option{pick} ? (picking => $option{pick}) : ())
Branch: 3 branches
BranchTrueFalseTotal
$human eq 'both' ? : 2 0 2
defined $option{'colour'} ? : 2 0 2
defined $option{'pick'} ? : 0 2 2
995 );
996  
997 2 local @SIG{qw(INT TERM HUP)};
998 2 for my $sig (qw(INT TERM HUP)) {
999 6 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
1000 }
1001  
1002 2 my $result = eval { $terminal->start };
1003 2 my $died = $@;
1004 2 $terminal->leave_raw;
1005 2 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 2 2
1006 2 ▶ return 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 2 0 2
1007 0 ▶ return 0 if $result->is_draw || $human eq 'both' || $human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $result->is_draw or $human eq "both" or $human eq "none" 0 0 0
Condition0%0/6
$result->is_draw or $human eq "both" or $human eq "none"
l!l&&r!l&&!r
000
$result->is_draw or $human eq "both"
l!l&&r!l&&!r
000
MC/DC0%0/3
$result->is_draw or $human eq "both" or $human eq "none"
$result->is_draw $human eq "both" $human eq "none"
Truth table0%0/4
A or B or C
A= $result->is_drawB= $human eq "both"C= $human eq "none"
ABCresult
1XX1
01X1
0011
0000
1008 0 ▶ return $result->winner eq $human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$result->winner eq $human ? : 0 0 0
1009 }
1010  
1011  
1012 10 1;
1013  
1014 __END__
« blib/lib/Game/Checkers/Bot.pm ↑ Summary blib/lib/Game/Checkers/Notation.pm »