blib/lib/Game/Checkers/Terminal.pm

filter: Statementstmt 86.4% 440 / 509 Branchbran 71.8% 217 / 302 Conditioncond 38.9% 53 / 136 MC/DCmcdc 17.2% 14 / 81 Subroutinesub 90.7% 49 / 54 Podpod 97.7% 44 / 45 totaltotal 72.4% 817 / 1127 CC 214 SCAR 27.6
CC
214
Cov
75%
offer_draw
72.0
pick
56.8
command
23.7
CRAP 15.8
« 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  
12 10 our $VERSION = '0.02';
13  
14 10 our %COMMAND;
15  
16 10 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 %COMMAND = (
18 help => 'this list',
19 '?' => 'this list',
20 moves => 'the legal moves, numbered: play one by its number',
21 pick => 'choose a move with the arrow keys instead of typing it',
22 type => 'go back to typing moves out',
23 hint => 'what the bot would play here',
24 board => 'draw the board again',
25 fen => 'the position as FEN',
26 undo => 'take back your last move, and the reply to it',
27 'save FILE' => 'write the game out as PDN',
28 'load FILE' => 'read a PDN game back in',
29 'level N' => 'set the strength of the bot, 1 to 5',
30 flip => 'draw the board from the other side',
31 ascii => 'letters instead of draughts symbols',
32 draw => 'offer a draw',
33 resign => 'give the game up',
34 quit => 'stop playing',
35 );
36 }
37  
38 10 my %PIECE = (
39 1 => { ascii => 'b', wide => "\x{26C2}" },
40 2 => { ascii => 'B', wide => "\x{26C3}" },
41 -1 => { ascii => 'w', wide => "\x{26C0}" },
42 -2 => { ascii => 'W', wide => "\x{26C1}" },
43 );
44  
45 # the squares nobody plays on are the light ones, so an unplayed square needs no
46 # mark of its own once the board is painted
47 10 my %GROUND = (
48 light => '48;5;180',
49 dark => '48;5;94',
50 from => '48;5;136',
51 to => '48;5;28',
52 captured => '48;5;124',
53 cursor => '48;5;24',
54 );
55  
56 10 my %INK = (
57 black => '1;38;5;16',
58 white => '1;38;5;231',
59 );
60  
61 # resign and draw are not on a key on purpose: a game should not end because a
62 # finger slipped on the row below the arrow keys
63 10 my %KEY = (
64 u => 'undo',
65 f => 'flip',
66 a => 'ascii',
67 q => 'quit',
68 );
69  
70 # Term::ReadKey hands over one character at a time and leaves an escape
71 # sequence as the characters it is made of, so the arrow keys are named here
72 10 my %SEQUENCE = (
73 'A' => 'up',
74 'B' => 'down',
75 'C' => 'right',
76 'D' => 'left',
77 'H' => 'home',
78 'F' => 'end',
79 '1~' => 'home',
80 '4~' => 'end',
81 '5~' => 'page_up',
82 '6~' => 'page_down',
83 '7~' => 'home',
84 '8~' => 'end',
85 );
86  
87 10 my %CONTROL = (
88 "\r" => 'enter',
89 "\n" => 'enter',
90 "\t" => 'tab',
91 "\x7f" => 'backspace',
92 "\x08" => 'backspace',
93 "\x03" => 'interrupt',
94 "\x04" => 'eof',
95 );
96  
97 has game => (
98 is => 'rw',
99 isa => Object
100 10 );
101  
102 has bot => (
103 is => 'rw'
104 10 );
105  
106 has human => (
107 is => 'rw',
108 isa => Str,
109 default => 'black'
110 10 );
111  
112 has [qw/in out/] => (
113 is => 'rw'
114 10 );
115  
116 has [qw/interactive colour/] => (
117 is => 'rw'
118 10 );
119  
120 has [qw/ascii flip/] => (
121 is => 'rw',
122 isa => Bool,
123 default => 0
124 10 );
125  
126 has redraw => (
127 is => 'rw',
128 isa => Bool,
129 default => 1
130 10 );
131  
132 has highlight => (
133 is => 'rw',
134 isa => HashRef,
135 default => {}
136 10 );
137  
138 has picking => (
139 is => 'rw',
140 isa => Bool
141 10 );
142  
143 has raw => (
144 is => 'rw',
145 isa => Bool,
146 default => 0
147 10 );
148  
149 has keysource => (
150 is => 'rw',
151 isa => CodeRef
152 10 );
153  
154 has pending => (
155 is => 'rw',
156 isa => ArrayRef,
157 default => []
158 10 );
159  
160 38 ▶sub BUILD {
Subroutine BUILD: called CC=13 SCAR=26.4
Pod undocumented
161 38 my ($self) = @_;
162 38 ▶ $self->game(Game::Checkers->new) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 30 8 38
163 38 ▶ $self->in(\*STDIN) unless $self->in;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->in 26 12 38
164 38 ▶ $self->out(\*STDOUT) unless $self->out;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->out 2 36 38
165 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
166 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
167 unless defined $self->colour;
168 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
169 38 ▶ $self->picking(0) unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 22 16 38
170 38 ▶ binmode $self->out, ':encoding(UTF-8)' unless $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ascii 2 36 38
171  
172 # a prompt is printed without a newline and has to be on the screen before
173 # the read, and the encoding layer holds on to output otherwise
174 38 my $previous = select $self->out;
175 38 $| = 1;
176 38 select $previous;
177 38 return $self;
178 }
179  
180 13 ▶sub start {
Subroutine start: called CC=4 SCAR=15.8
Pod documented
181 13 my ($self) = @_;
182 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
183  
184 # a raw terminal that is never put back is a broken shell, so the interrupt
185 # is caught only to restore it, and then raised again as if it had not been
186 13 my $interrupt = $SIG{INT};
187 0 ▶ local $SIG{INT} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
188 0 $self->leave_raw;
189 0 ▶ $SIG{INT} = defined $interrupt ? $interrupt : 'DEFAULT';
Branch: 1 branch
BranchTrueFalseTotal
defined $interrupt ? : 0 0 0
190 0 kill 'INT', $$;
191 13 };
192  
193 13 $self->say('Checkers. Type help for the commands.');
194 13 my $played = eval { $self->turns; 1 };
195 13 my $error = $@;
196 13 $self->leave_raw;
197 13 ▶ die $error unless $played;
Branch: 1 branch
BranchTrueFalseTotal
unless $played 0 13 13
198 13 return $self->game->result;
199 }
200  
201 13 ▶sub turns {
Subroutine turns: called CC=10 SCAR=23.3
Pod documented
202 13 my ($self) = @_;
203 13 while (1) {
204 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
205 # only a move or a switch redraws: a list of moves or a refusal would
206 # otherwise be wiped, or pushed off the top, by the board that follows
207 # it. A picked move draws its own screen, every keystroke of it
208 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
209 49 ▶ if (my $result = $self->game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->game->result) 2 47 49
210 2 $self->highlight({});
211 2 ▶ $self->render if $picking;
Branch: 1 branch
BranchTrueFalseTotal
if $picking 0 2 2
212 2 $self->say($result->stringify);
213 2 last;
214 }
215 47 ▶ if ($self->bot_plays($self->game->turn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bot_plays($self->game->turn)) 5 42 47
216 5 $self->bot_move;
217 5 next;
218 }
219 42 ▶ my $line = $picking ? $self->pick : $self->read_line;
Branch: 1 branch
BranchTrueFalseTotal
$picking ? : 0 42 42
220 42 ▶ unless (defined $line) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $line) 2 40 42
221 2 $self->say('Bye.');
222 2 last;
223 }
224 40 ▶ last if $self->command($line);
Branch: 1 branch
BranchTrueFalseTotal
if $self->command($line) 9 31 40
225 }
226 13 return $self;
227 }
228  
229 48 ▶sub bot_plays {
Subroutine bot_plays: called CC=5 SCAR=16.3
Pod documented
230 48 my ($self, $side) = @_;
231 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
232 19 ▶ return 1 if $self->human eq 'none';
Branch: 1 branch
BranchTrueFalseTotal
if $self->human eq "none" 0 19 19
233 19 ▶ return $side eq $self->human ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$side eq $self->human ? : 13 6 19
234 }
235  
236 5 ▶sub bot_move {
Subroutine bot_move: called CC=2 SCAR=7.0
Pod documented
237 5 my ($self) = @_;
238 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
239 5 $self->game->move($move);
240 5 $self->say(sprintf '%s plays %s.', ucfirst $move->side, $move->coord_notation);
241 5 $self->redraw(1);
242 5 return $move;
243 }
244  
245 38 ▶sub keys_available {
Subroutine keys_available: called CC=5 SCAR=21.7
Pod documented
246 38 my ($self) = @_;
247 38 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 16 22 38
248 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
249 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
250 }
251  
252 6 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=24.2
Pod documented
253 6 my ($self) = @_;
254 6 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 6 6
255 6 ▶ if ($self->keysource) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 6 0 6
256 6 $self->raw(1);
257 6 return $self;
258 }
259 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
260 # cbreak rather than raw, so an interrupt is still an interrupt and not a
261 # key this has to know about
262 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
263 0 $self->raw(1);
264 0 return $self;
265 }
266  
267 14 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
268 14 my ($self) = @_;
269 14 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 13 1 14
270 1 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 1 1
271 1 $self->raw(0);
272 1 return $self;
273 }
274  
275 60 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=25.4
Pod documented
276 60 my ($self, $wait) = @_;
277 60 my $pending = $self->pending;
278 60 ▶ return shift @{$pending} if @{$pending};
Branch: 1 branch
BranchTrueFalseTotal
if @{$pending;} 2 58 60
279 58 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 58 0 58
280 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
281 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
282 # nothing there yet, and the rest of an escape sequence is worth one short
283 # wait before the escape is taken for the key of that name
284 0 select undef, undef, undef, 0.05;
285 0 return Term::ReadKey::ReadKey(-1, $self->in);
286 }
287  
288 36 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
289 36 my ($self) = @_;
290 36 my $char = $self->read_char(1);
291 36 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 34 36
292 34 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $CONTROL{$char} 8 26 34
293 26 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 12 14 26
294 14 return $char;
295 }
296  
297 12 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.0
Pod documented
298 12 my ($self) = @_;
299 12 my $opener = $self->read_char(0);
300 12 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 12 12
301 # a pressed escape key that happens to be followed by a real keystroke must
302 # not eat it, so what is not the opening of a sequence goes back
303 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
304 2 unshift @{$self->pending}, $opener;
305 2 return 'escape';
306 }
307 10 my $tail = '';
308 10 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 12 0 12
309 12 my $char = $self->read_char(0);
310 12 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 12 12
311 12 $tail .= $char;
312 12 ▶ last if $char =~ m/[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 10 2 12
313 }
314 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
315 }
316  
317 5 ▶sub pick {
Subroutine pick: called CC=26 SCAR=40.4
Pod documented
318 5 my ($self) = @_;
319 5 ▶ return undef unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 0 5 5
320 5 my $legal = $self->game->legal_moves;
321 5 ▶ return undef unless @{$legal};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$legal;} 0 5 5
322  
323 5 my $at = 0;
324 5 my $notice = [];
325 5 while (1) {
326 8 $self->show_choice($legal, $at, $notice);
327 8 $notice = [];
328 8 my $key = $self->read_key;
329 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
330  
331 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
332 0 $at = ($at - 1) % @{$legal};
333 0 next;
334 }
335 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
336 2 $at = ($at + 1) % @{$legal};
337 2 next;
338 }
339 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
340 0 $at = 0;
341 0 next;
342 }
343 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
344 0 $at = $#{$legal};
345 0 next;
346 }
347 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
348 2 $self->highlight({});
349 2 $self->game->move($legal->[$at]);
350 2 $self->redraw(1);
351 2 return '';
352 }
353 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
354 1 $at = $key - 1;
355 1 next;
356 }
357 2 ▶ if ($key eq 'h') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 0 2 2
358 0 my $hint = $self->hint;
359 0 $notice = [$hint->{line}];
360 0 ▶ $at = $self->index_of($legal, $hint->{move}) if $hint->{move};
Branch: 1 branch
BranchTrueFalseTotal
if $hint->{"move"} 0 0 0
361 0 next;
362 }
363 2 ▶ if ($key eq '?') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 2 2
364 0 $notice = $self->help_lines;
365 0 next;
366 }
367 2 ▶ if ($key eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq ":") 1 1 2
368 1 $self->highlight({});
369 1 return $self->read_line(': ');
370 }
371 1 ▶ if ($KEY{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($KEY{$key}) 1 0 1
372 1 $self->highlight({});
373 1 return $KEY{$key};
374 }
375 0 $notice = ['That key does nothing here. Press ? for the ones that do.'];
376 }
377 }
378  
379 0 ▶sub index_of {
Subroutine index_of: not called CC=3 SCAR=24.8
Pod documented
380 0 my ($self, $legal, $move) = @_;
381 0 my $notation = $move->coord_notation;
382 0 for my $index (0 .. $#{$legal}) {
383 0 ▶ return $index if $legal->[$index]->coord_notation eq $notation;
Branch: 1 branch
BranchTrueFalseTotal
if $legal->[$index]->coord_notation eq $notation 0 0 0
384 }
385 0 return 0;
386 }
387  
388 8 ▶sub show_choice {
Subroutine show_choice: called CC=6 SCAR=18.3
Pod documented
389 8 my ($self, $legal, $at, $notice) = @_;
390 8 $self->highlight($self->move_marks($legal->[$at]));
391 8 $self->clear;
392 8 $self->say($_) for @{$self->board_lines}, '', @{$self->status_lines}, '',
393 8 @{$self->choice_lines($legal, $at)};
394 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
395 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
396 8 $self->say('');
397 8 $self->say($self->legend);
398 8 $self->redraw(0);
399 8 return $self;
400 }
401  
402 9 ▶sub move_marks {
Subroutine move_marks: called CC=3 SCAR=11.0
Pod documented
403 9 my ($self, $move) = @_;
404 9 my %mark;
405 9 $mark{$_} = 'cursor' for @{$move->path};
406 9 $mark{$_} = 'captured' for @{$move->captures};
407 9 $mark{$move->from} = 'from';
408 9 $mark{$move->to} = 'to';
409 9 return \%mark;
410 }
411  
412 10 ▶sub choice_lines {
Subroutine choice_lines: called CC=9 SCAR=22.0
Pod documented
413 10 my ($self, $legal, $at) = @_;
414 # the list is windowed so that a position with a lot of jumps in it cannot
415 # push the board off the top of the screen
416 10 my $window = 8;
417 10 my $first = 0;
418 10 ▶ if (@{$legal} > $window) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$legal;} > $window) 2 8 10
419 2 $first = $at - int($window / 2);
420 2 ▶ $first = 0 if $first < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $first < 0 1 1 2
421 2 ▶ $first = @{$legal} - $window if $first > @{$legal} - $window;
Branch: 1 branch
BranchTrueFalseTotal
if $first > @{$legal;} - $window 1 1 2
422 }
423 10 my $last = $first + $window - 1;
424 10 ▶ $last = $#{$legal} if $last > $#{$legal};
Branch: 1 branch
BranchTrueFalseTotal
if $last > $#{$legal;} 8 2 10
425  
426 10 my @lines;
427 10 ▶ push @lines, sprintf ' %d further up', $first if $first;
Branch: 1 branch
BranchTrueFalseTotal
if $first 1 9 10
428 10 for my $index ($first .. $last) {
429 72 my $move = $legal->[$index];
430 72 my $line = sprintf '%2d. %-11s %s',
431 $index + 1, $move->coord_notation, $self->describe($move);
432 72 $line =~ s/\s+$//;
433 72 ▶ push @lines, $index == $at
Branch: 1 branch
BranchTrueFalseTotal
$index == $at ? : 10 62 72
434 ? ' ' . $self->paint("> $line", '1;7')
435 : " $line";
436 }
437 1 push @lines, sprintf ' %d further down', $#{$legal} - $last
438 10 ▶ if $last < $#{$legal};
Branch: 1 branch
BranchTrueFalseTotal
if $last < $#{$legal;} 1 9 10
439 10 return \@lines;
440 }
441  
442 74 ▶sub describe {
Subroutine describe: called CC=3 SCAR=11.0
Pod documented
443 74 my ($self, $move) = @_;
444 74 my @note;
445 push @note, 'takes ' . join ', ',
446 2 map { Game::Checkers::Squares::coord_name($_) } @{$move->captures}
447 74 ▶ if @{$move->captures};
Branch: 1 branch
BranchTrueFalseTotal
if @{$move->captures;} 1 73 74
448 74 ▶ push @note, 'crowns' if $move->promoted;
Branch: 1 branch
BranchTrueFalseTotal
if $move->promoted 1 73 74
449 74 return join ', ', @note;
450 }
451  
452 8 ▶sub legend {
Subroutine legend: called CC=1 SCAR=0.0
Pod documented
453 8 return 'up and down to choose, enter to play, h hint, u undo, f flip, '
454 . 'q quit, : to type a command, ? for the rest';
455 }
456  
457 49 ▶sub read_line {
Subroutine read_line: called CC=4 SCAR=13.9
Pod documented
458 49 my ($self, $prompt) = @_;
459 49 my $out = $self->out;
460 49 ▶ print {$out} defined $prompt ? $prompt : $self->game->turn . '> ';
Branch: 1 branch
BranchTrueFalseTotal
defined $prompt ? : 7 42 49
461 49 ▶ return $self->keyed_line if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 1 48 49
462 48 my $line = readline $self->in;
463 48 ▶ return undef unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 2 46 48
464 46 chomp $line;
465 46 return $line;
466 }
467  
468 1 ▶sub keyed_line {
Subroutine keyed_line: called CC=9 SCAR=26.8
Pod documented
469 1 my ($self) = @_;
470 1 my $out = $self->out;
471 1 my $line = '';
472 1 while (1) {
473 8 my $key = $self->read_key;
474 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
475 8 ▶ last if $key eq 'enter';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "enter" 1 7 8
476 7 ▶ if ($key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "backspace") 0 7 7
477 0 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 0 0
478 0 chop $line;
479 # back over the character, blank it, and back over it again, which
480 # is all a raw terminal will do for a rubbed out letter
481 0 print {$out} "\b \b";
482 0 next;
483 }
484 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
485 7 $line .= $key;
486 7 print {$out} $key;
487 }
488 1 print {$out} "\n";
489 1 return $line;
490 }
491  
492 1128 ▶sub say {
Subroutine say: called CC=2 SCAR=7.0
Pod documented
493 1128 my ($self, $line) = @_;
494 1128 my $out = $self->out;
495 1128 ▶ print {$out} (defined $line ? $line : ''), "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $line ? : 1128 0 1128
496 1128 return $self;
497 }
498  
499 51 ▶sub clear {
Subroutine clear: called CC=2 SCAR=6.9
Pod documented
500 51 my ($self) = @_;
501 51 ▶ return $self unless $self->interactive;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->interactive 50 1 51
502 1 my $out = $self->out;
503 1 print {$out} "\e[2J\e[H";
504 1 return $self;
505 }
506  
507 41 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
508 41 my ($self) = @_;
509 41 $self->clear;
510 41 $self->say($_) for @{$self->board_lines}, '', @{$self->status_lines};
511 41 $self->redraw(0);
512 41 return $self;
513 }
514  
515 57 ▶sub board_lines {
Subroutine board_lines: called CC=10 SCAR=23.0
Pod documented
516 57 my ($self) = @_;
517 57 my $board = $self->game->board;
518 57 my $rule = ' +' . ('---+' x 8);
519 57 my @files = ('a' .. 'h');
520 57 my @rows = 0 .. 7;
521 57 my @cols = 0 .. 7;
522 57 ▶ if ($self->flip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->flip) 3 54 57
523 3 @rows = reverse @rows;
524 3 @cols = reverse @cols;
525 }
526  
527 57 my @aside = @{$self->aside};
528 # five spaces puts a over the middle of the first cell, which the rule line
529 # opens at index 3. A painted board drops the rule and the bars, because the
530 # squares then meet, and widens the cell to five so a piece has a middle to
531 # sit in. Either way the first file stays at index five and the pitch stays
532 # the cell width, so the letters sit over the pieces
533 57 ▶ my @lines = $self->colour
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 4 53 57
534 ? (' ' . join(' ', @files[@cols]))
535 : (' ' . join(' ', @files[@cols]));
536 57 ▶ push @lines, $rule unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 53 4 57
537 57 for my $row (@rows) {
538 456 ▶ my $line = $self->colour
Branch: 1 branch
BranchTrueFalseTotal
$self->colour ? : 32 424 456
539 ? sprintf('%2d ', 8 - $row)
540 : sprintf('%2d |', 8 - $row);
541 456 for my $col (@cols) {
542 3648 $line .= $self->cell($board, $row, $col);
543 3648 ▶ $line .= '|' unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 3392 256 3648
544 }
545 456 ▶ if (my $note = shift @aside) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $note = shift @aside) 114 342 456
546 114 $line .= ' ' . $note;
547 }
548 456 push @lines, $line;
549 456 ▶ push @lines, $rule unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 424 32 456
550 }
551 57 return \@lines;
552 }
553  
554 3648 ▶sub cell {
Subroutine cell: called CC=14 SCAR=26.4
Pod documented
555 3648 my ($self, $board, $row, $col) = @_;
556 3648 my $square = Game::Checkers::Squares::square($row, $col);
557 3648 ▶ my $value = $square ? $board->at($square) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$square ? : 1824 1824 3648
558 3648 ▶ my $piece = $value ? $PIECE{$value}{$self->ascii ? 'ascii' : 'wide'} : undef;
Branch: 2 branches
BranchTrueFalseTotal
$value ? : 1265 2383 3648
$self->ascii ? : 1193 72 1265
559  
560 # an empty playing square is dotted, because half the squares are not in the
561 # game at all and nothing else unpainted says which half
562 3648 ▶ unless ($self->colour) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->colour) 3392 256 3648
563 3392 ▶ return ' ' unless $square;
Branch: 1 branch
BranchTrueFalseTotal
unless $square 1696 1696 3392
564 1696 ▶ return ' ' . (defined $piece ? $piece : '.') . ' ';
Branch: 1 branch
BranchTrueFalseTotal
defined $piece ? : 1169 527 1696
565 }
566  
567 256 ▶ my $mark = $square ? $self->highlight->{$square} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$square ? : 128 128 256
568 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
569 256 my @code = ($GROUND{$mark});
570 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
571 256 ▶ return "\e[" . join(';', @code) . 'm '
Branch: 1 branch
BranchTrueFalseTotal
defined $piece ? : 96 160 256
572 . (defined $piece ? $piece : ' ') . " \e[0m";
573 }
574  
575 10 ▶sub paint {
Subroutine paint: called CC=2 SCAR=8.1
Pod documented
576 10 my ($self, $text, $code) = @_;
577 10 ▶ return $text unless $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->colour 10 0 10
578 0 return "\e[${code}m" . $text . "\e[0m";
579 }
580  
581 57 ▶sub aside {
Subroutine aside: called CC=2 SCAR=6.9
Pod documented
582 57 my ($self) = @_;
583 57 my $board = $self->game->board;
584 57 my @aside;
585 57 for my $side (qw/black white/) {
586 114 my $count = $board->count($side);
587 push @aside, sprintf '%-5s %2d (%d kings)',
588 114 ucfirst $side, $count->{total}, $count->{kings};
589 }
590 57 return \@aside;
591 }
592  
593 54 ▶sub status_lines {
Subroutine status_lines: called CC=8 SCAR=20.8
Pod documented
594 54 my ($self) = @_;
595 54 my $game = $self->game;
596 54 my @lines;
597  
598 54 ▶ if (my $last = $game->history->[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $last = $game->history->[-1]) 26 28 54
599 push @lines, sprintf 'Last: %s by %s%s',
600 $last->coord_notation, $last->side,
601 26 (@{$last->captures}
602 ? ', taking ' . join(', ',
603 7 map { Game::Checkers::Squares::coord_name($_) }
604 26 ▶ @{$last->captures})
Branch: 1 branch
BranchTrueFalseTotal
@{$last->captures;} ? : 7 19 26
605 : '');
606 }
607  
608 54 ▶ if (my $result = $game->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $game->result) 3 51 54
609 3 push @lines, $result->stringify;
610 3 return \@lines;
611 }
612  
613 51 my $moves = scalar @{$game->legal_moves};
614 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
615 ? sprintf('%s must capture, %d %s available',
616 ucfirst $game->turn, $moves, $moves == 1 ? 'jump' : 'jumps')
617 : sprintf('%s to move, %d %s',
618 ucfirst $game->turn, $moves, $moves == 1 ? 'move' : 'moves');
619  
620 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
621 if $game->draw_offered_by;
622  
623 51 return \@lines;
624 }
625  
626 41 ▶sub command {
Subroutine command: called CC=22 SCAR=31.6
Pod documented
627 41 my ($self, $line) = @_;
628 41 $line =~ s/^\s+|\s+$//g;
629 41 ▶ return 0 unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 41 41
630  
631 41 my ($word, $argument) = split ' ', $line, 2;
632 41 $word = lc $word;
633  
634 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
635 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
636 41 ▶ return $self->show_moves if $word eq 'moves';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "moves" 2 39 41
637 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
638 38 ▶ return $self->show_hint if $word eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "hint" 1 37 38
639 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
640 36 ▶ return $self->take_back if $word eq 'undo';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "undo" 1 35 36
641 35 ▶ return $self->save($argument) if $word eq 'save';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "save" 1 34 35
642 34 ▶ return $self->load($argument) if $word eq 'load';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "load" 1 33 34
643 33 ▶ return $self->set_level($argument) if $word eq 'level';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "level" 1 32 33
644 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
645 30 ▶ return $self->offer_draw if $word eq 'draw';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "draw" 0 30 30
646 30 ▶ return $self->resign if $word eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "resign" 1 29 30
647 29 ▶ return $self->quit if $word eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if $word eq "quit" 9 20 29
648  
649 20 ▶ return $self->play_number($word) if $word =~ m/^[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $word =~ /^[0-9]+$/ 4 16 20
650 16 return $self->play($line);
651 }
652  
653 16 ▶sub play {
Subroutine play: called CC=6 SCAR=18.0
Pod documented
654 16 my ($self, $notation) = @_;
655 16 my $move = $self->game->move($notation);
656 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
657  
658 5 $self->say(ucfirst $move->message . '.');
659 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
660 if $move->not_a_move;
661 $self->say('You could play: '
662 21 . join ', ', map { $_->coord_notation } @{$move->legal})
663 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
664 5 return 0;
665 }
666  
667 4 ▶sub play_number {
Subroutine play_number: called CC=4 SCAR=15.5
Pod documented
668 4 my ($self, $number) = @_;
669 4 my $legal = $self->game->legal_moves;
670 return $self->say("There is no move $number. Type moves for the list.") && 0
671 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
672 4 $self->game->move($legal->[$number - 1]);
673 4 $self->redraw(1);
674 4 return 0;
675 }
676  
677 2 ▶sub show_moves {
Subroutine show_moves: called CC=2 SCAR=6.9
Pod documented
678 2 my ($self) = @_;
679 2 my $legal = $self->game->legal_moves;
680 2 my $number = 0;
681 2 $self->say(sprintf '%2d. %s', ++$number, $_->coord_notation) for @{$legal};
682 2 $self->say('Play one by its number, or type it out.');
683 2 return 0;
684 }
685  
686 1 ▶sub hint {
Subroutine hint: called CC=4 SCAR=15.0
Pod documented
687 1 my ($self) = @_;
688 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
689 1 my $move = $bot->choose($self->game);
690 1 ▶ return { line => 'There is nothing to play.' } unless $move;
Branch: 1 branch
BranchTrueFalseTotal
unless $move 0 1 1
691 1 my $score = $bot->last_search->{score};
692 return {
693 1 ▶ move => $move,
Branch: 1 branch
BranchTrueFalseTotal
defined $score ? : 1 0 1
694 line => sprintf('Try %s%s.', $move->coord_notation,
695 defined $score ? sprintf(' (it scores that %+.2f)', $score / 100) : '')
696 };
697 }
698  
699 1 ▶sub show_hint {
Subroutine show_hint: called CC=1 SCAR=0.0
Pod documented
700 1 my ($self) = @_;
701 1 $self->say($self->hint->{line});
702 1 return 0;
703 }
704  
705 1 ▶sub take_back {
Subroutine take_back: called CC=5 SCAR=18.1
Pod documented
706 1 my ($self) = @_;
707 1 my $game = $self->game;
708 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
709  
710 1 $game->undo;
711 # and the reply that provoked it, so the board comes back as you left it
712 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
713 1 $self->say('Taken back.');
714 1 $self->redraw(1);
715 1 return 0;
716 }
717  
718 1 ▶sub save {
Subroutine save: called CC=5 SCAR=18.4
Pod documented
719 1 my ($self, $file) = @_;
720 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
721 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
722 or return $self->say("Cannot write $file: $!") && 0;
723 1 print {$handle} $self->game->to_pdn(Event => 'Terminal checkers');
724 1 close $handle;
725 1 $self->say("Saved to $file.");
726 1 return 0;
727 }
728  
729 1 ▶sub load {
Subroutine load: called CC=7 SCAR=22.0
Pod documented
730 1 my ($self, $file) = @_;
731 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
732 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
733 or return $self->say("Cannot read $file: $!") && 0;
734 1 my $pdn = do { local $/; <$handle> };
735 1 close $handle;
736 1 my $game = eval { Game::Checkers->from_pdn($pdn) };
737 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
738 1 $self->game($game);
739 1 $self->say("Loaded $file, " . $game->ply . ' moves in.');
740 1 $self->redraw(1);
741 1 return 0;
742 }
743  
744 1 ▶sub set_level {
Subroutine set_level: called CC=5 SCAR=18.9
Pod documented
745 1 my ($self, $level) = @_;
746 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
747 unless defined $level && $level =~ m/^[1-5]$/;
748 1 ▶ $self->bot(Game::Checkers::Bot->new) unless $self->bot;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bot 0 1 1
749 1 $self->bot->level($level);
750 1 $self->say("The bot is at level $level.");
751 1 return 0;
752 }
753  
754 1 ▶sub set_picking {
Subroutine set_picking: called CC=4 SCAR=18.6
Pod documented
755 1 my ($self, $word) = @_;
756 1 ▶ if ($word eq 'type') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "type") 1 0 1
757 # raw mode goes with it, so the terminal does its own line editing again
758 1 $self->leave_raw;
759 1 $self->picking(0);
760 1 $self->say('Type your moves. The command pick brings the keys back.');
761 1 return 0;
762 }
763 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
764 unless $self->enter_raw;
765 0 $self->picking(1);
766 0 return 0;
767 }
768  
769 2 ▶sub toggle {
Subroutine toggle: called CC=2 SCAR=7.0
Pod documented
770 2 my ($self, $what) = @_;
771 2 ▶ $self->$what($self->$what ? 0 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->$what ? : 0 2 2
772 2 $self->redraw(1);
773 2 return 0;
774 }
775  
776 0 ▶sub offer_draw {
Subroutine offer_draw: not called CC=8 SCAR=42.8
Pod documented
777 0 my ($self) = @_;
778 0 my $game = $self->game;
779 0 ▶ my $side = $self->human eq 'both' ? $game->turn : $self->human;
Branch: 1 branch
BranchTrueFalseTotal
$self->human eq 'both' ? : 0 0 0
780 0 $game->offer_draw($side);
781 0 $self->redraw(1);
782 0 ▶ return 0 unless $self->bot;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->bot 0 0 0
783  
784 # the bot answers from what it thought of the position last time it moved,
785 # which costs nothing and is the same number it played on
786 0 ▶ my $score = $self->bot->last_search ? $self->bot->last_search->{score} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->bot->last_search ? : 0 0 0
787 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
788 0 ▶ $game->accept_draw($side eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 0 0 0
789 0 $self->say('The bot takes the draw.');
790 } else {
791 0 ▶ $game->decline_draw($side eq 'black' ? 'white' : 'black');
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'black' ? : 0 0 0
792 0 $self->say('The bot plays on.');
793 }
794 0 return 0;
795 }
796  
797 1 ▶sub resign {
Subroutine resign: called CC=2 SCAR=7.0
Pod documented
798 1 my ($self) = @_;
799 1 my $game = $self->game;
800 1 ▶ $game->resign($self->human eq 'both' ? $game->turn : $self->human);
Branch: 1 branch
BranchTrueFalseTotal
$self->human eq 'both' ? : 0 1 1
801 1 $self->redraw(1);
802 1 return 0;
803 }
804  
805 9 ▶sub quit {
Subroutine quit: called CC=5 SCAR=17.8
Pod documented
806 9 my ($self) = @_;
807 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
808 6 my $answer = $self->read_line('Really quit, with the game unfinished? (y/n) ');
809 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
810 0 return 0;
811 }
812  
813 0 ▶sub help_lines {
Subroutine help_lines: not called CC=2 SCAR=17.9
Pod documented
814 0 my ($self) = @_;
815 return [
816 'A move is its squares: a3-b4 to slide, c3xe5xg7 to jump.',
817 0 map { sprintf ' %-12s %s', $_, $COMMAND{$_} }
818 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
819 ];
820 }
821  
822 0 ▶sub show_help {
Subroutine show_help: not called CC=2 SCAR=17.9
Pod documented
823 0 my ($self) = @_;
824 0 $self->say($_) for @{$self->help_lines};
825 0 return 0;
826 }
827  
828 10 1;
829  
830 __END__
« blib/lib/Game/Checkers/Bot.pm ↑ Summary blib/lib/Game/Checkers/Notation.pm »