blib/lib/Game/Checkers/Terminal.pm

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