Chess.pm

filter: Statementstmt 89.3% 564 / 631 Branchbran 84.0% 333 / 396 Conditioncond 77.0% 67 / 87 MC/DCmcdc 63.6% 35 / 55 Subroutinesub 88.7% 79 / 89 Podpod 97.6% 41 / 42 totaltotal 86.0% 1119 / 1300 CC 245 SCAR 30.8
CC
245
Cov
87%
run
73.0
start
42.4
pick_move
35.1
CRAP 21.7
↑ Summary Chess/Game.pm »
1 package Term::Chess;
2  
3 7 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use Chess::Plisco qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use Term::Chess::Game;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use Term::Chess::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 7 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 7 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 7 ▶use Game::Terminal::Kit::Steer;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 7 our $VERSION = '0.04';
24  
25 7 my %OUTLINE = (
26 CP_KING() => "\x{2654}", CP_QUEEN() => "\x{2655}", CP_ROOK() => "\x{2656}",
27 CP_BISHOP() => "\x{2657}", CP_KNIGHT() => "\x{2658}", CP_PAWN() => "\x{2659}",
28 );
29  
30 7 my %FILLED = (
31 CP_KING() => "\x{265A}", CP_QUEEN() => "\x{265B}", CP_ROOK() => "\x{265C}",
32 CP_BISHOP() => "\x{265D}", CP_KNIGHT() => "\x{265E}", CP_PAWN() => "\x{265F}",
33 );
34  
35 7 my %LETTER = (
36 CP_KING() => 'K', CP_QUEEN() => 'Q', CP_ROOK() => 'R',
37 CP_BISHOP() => 'B', CP_KNIGHT() => 'N', CP_PAWN() => 'P',
38 );
39  
40 7 my %WORD = (
41 CP_KING() => 'king', CP_QUEEN() => 'queen', CP_ROOK() => 'rook',
42 CP_BISHOP() => 'bishop', CP_KNIGHT() => 'knight', CP_PAWN() => 'pawn',
43 );
44  
45 7 my %WHY = (
46 checkmate => 'checkmate',
47 stalemate => 'stalemate',
48 fifty_moves => 'fifty moves without a capture or a pawn move',
49 insufficient_material => 'neither side can mate',
50 repetition => 'the same position three times',
51 resignation => 'resignation',
52 );
53  
54 7 my %PAINT = (
55 white_piece => '38;5;231',
56 black_piece => '38;5;16',
57 light => '48;5;180',
58 dark => '48;5;94',
59 );
60  
61 7 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
62 7 CELL => 2,
63 GAP => 1,
64 ROW_WEIGHT => 100,
65 7 };
66  
67 117 ▶has _in => (is => 'rw', init_arg => 'in', private => 1, default => sub { \*STDIN });
Subroutine __ANON__: called CC=1 SCAR=0.0
68  
69 7 has game => (is => 'rw');
70  
71 7 has level => (is => 'ro');
72  
73 7 has seat => (is => 'ro', isa => Str, default => 'p1');
74  
75 71 ▶has _fh => (is => 'rw', init_arg => 'out', private => 1, default => sub { \*STDOUT });
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 7 has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0);
78  
79 7 has _colour => (is => 'rw', init_arg => 'colour', private => 1);
80  
81 7 has _keysource => (is => 'rw', isa => Any, init_arg => 'keysource', private => 1);
82  
83 135 ▶has _pending => (is => 'rw', isa => ArrayRef, init_arg => 'pending', private => 1, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 7 has [qw/keyboard screen/] => (is => 'rw');
86  
87 7 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
88  
89 7 has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind');
90  
91 7 has _size => (is => 'rw', isa => Any, init_arg => 'size');
92  
93 7 has history => (is => 'rw');
94  
95 7 has save_file => (is => 'rw');
96  
97 135 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.1
Pod undocumented
98 135 my ($self) = @_;
99  
100 135 ▶ my $size = defined $self->_size ? $self->_size : 2;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->_size ? : 104 31 135
101 135 ▶ Carp::croak("Term::Chess: size is 1, 2 or 3, not '$size'")
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /\A[123]\z/u 2 133 135
102 unless $size =~ /\A[123]\z/;
103 133 $self->_size($size + 0);
104 133 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->_keysource ? : 35 98 133
105 in => $self->_in,
106 pending => $self->_pending,
107 ($self->_keysource ? (keysource => $self->_keysource) : ()),
108 ));
109 133 ▶ $self->screen(Game::Terminal::Kit::Screen->new(
Branch: 4 branches
BranchTrueFalseTotal
$self->_ascii ? : 64 69 133
Game::Terminal::Kit::Screen::no_colour() ? : 4 129 133
defined $self->_colour ? : 115 14 129
$self->_colour ? : 11 104 115
110 out => $self->_fh,
111 ascii => $self->_ascii ? 1 : 0,
112 colour => Game::Terminal::Kit::Screen::no_colour() ? 0
113 : defined $self->_colour ? ($self->_colour ? 1 : 0)
114 : 1,
115 ));
116 133 $self->screen->utf8;
117 133 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 133 0 133
118 133 ▶ $self->history->name('chess') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 133 133
119 133 $self->history->version($VERSION);
120 133 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 0 133 133
121 133 return;
122 }
123  
124 133 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
125 return Game::Terminal::Kit::History->new(
126 name => 'chess',
127 version => $VERSION,
128 150 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 9 ▶ load => sub { Term::Chess::Game->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
130 133 );
131 }
132  
133 2 ▶sub in {
Subroutine in: called CC=2 SCAR=6.9
Pod documented
134 2 my $s = shift;
135 2 ▶ $s->keyboard->in(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 1 2
136 2 return $s->keyboard->in;
137 }
138  
139 100 ▶sub out {
Subroutine out: called CC=2 SCAR=6.9
Pod documented
140 100 my $s = shift;
141 100 ▶ if (@_) { $s->screen->out(shift); $s->screen->utf8 }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 97 100
142 100 return $s->screen->out;
143 }
144  
145 9130 ▶sub ascii {
Subroutine ascii: called CC=4 SCAR=17.1
Pod documented
146 9130 my $s = shift;
147 0 ▶ if (@_) { $s->screen->ascii(shift() ? 1 : 0); $s->screen->utf8 }
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 9130 9130
shift() ? : 0 0 0
148 9130 ▶ return $s->screen->ascii ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s->screen->ascii ? : 6031 3099 9130
149 }
150  
151 13540 ▶sub colour {
Subroutine colour: called CC=4 SCAR=13.9
Pod documented
152 13540 my $s = shift;
153 13540 ▶ $s->screen->colour(shift() ? 1 : 0) if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 2 13538 13540
shift() ? : 1 1 2
154 13540 ▶ return $s->screen->colour ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$s->screen->colour ? : 1483 12057 13540
155 }
156  
157 1 ▶sub size { $_[0]->_size }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
158  
159 9 ▶sub keysource {
Subroutine keysource: called CC=2 SCAR=7.1
Pod documented
160 9 my $s = shift;
161 9 ▶ $s->keyboard->keysource(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 0 9
162 9 return $s->keyboard->keysource;
163 }
164  
165 0 ▶sub pending {
Subroutine pending: not called CC=2 SCAR=17.9
Pod documented
166 0 my $s = shift;
167 0 ▶ $s->keyboard->pending(shift) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
168 0 return $s->keyboard->pending;
169 }
170  
171 2 ▶sub raw { $_[0]->keyboard->raw ? 1 : 0 }
Subroutine raw: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->keyboard->raw ? : 1 1 2
172  
173 128 ▶sub _say { my ($self, @t) = @_; $self->screen->say(@t); return }
Subroutine _say: called CC=1 SCAR=0.0
174 7 ▶sub _out { my ($self, @t) = @_; my $fh = $self->screen->out; print {$fh} @t }
Subroutine _out: called CC=1 SCAR=0.0
175  
176 46717 ▶sub light {
Subroutine light: called CC=2 SCAR=6.9
Pod documented
177 46717 my ($self, $square) = @_;
178 46717 my ($file, $rank) = (ord(substr $square, 0, 1) - ord('a'), substr($square, 1, 1));
179 46717 ▶ return (($file + $rank) % 2) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
($file + $rank) % 2 ? : 23358 23359 46717
180 }
181  
182 6787 ▶sub glyph {
Subroutine glyph: called CC=6 SCAR=17.9
Pod documented
183 6787 my ($self, $piece, $colour) = @_;
184 6787 ▶ return '' unless $piece;
Branch: 1 branch
BranchTrueFalseTotal
unless $piece 1 6786 6787
185 6786 ▶ if ($self->ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ascii) 4383 2403 6786
186 4383 my $l = $LETTER{$piece};
187 4383 ▶ return $colour == CP_WHITE ? $l : lc $l;
Branch: 1 branch
BranchTrueFalseTotal
$colour == 0 ? : 2224 2159 4383
188 }
189 2403 ▶ return $FILLED{$piece} if $self->colour;
Branch: 1 branch
BranchTrueFalseTotal
if $self->colour 721 1682 2403
190 1682 ▶ return $colour == CP_WHITE ? $OUTLINE{$piece} : $FILLED{$piece};
Branch: 1 branch
BranchTrueFalseTotal
$colour == 0 ? : 846 836 1682
191 }
192  
193 46648 ▶sub _paint {
Subroutine _paint: called CC=4 SCAR=13.9
194 46648 my ($self, $text, $light, $colour) = @_;
195 46648 ▶ my $paint = $PAINT{ $light ? 'light' : 'dark' };
Branch: 1 branch
BranchTrueFalseTotal
$light ? : 23324 23324 46648
196 46648 ▶ $paint .= ';' . $PAINT{ $colour == CP_WHITE ? 'white_piece' : 'black_piece' } if defined $colour;
Branch: 2 branches
BranchTrueFalseTotal
if defined $colour 6744 39904 46648
$colour == 0 ? : 3411 3333 6744
197 46648 return $self->screen->paint($text, $paint);
198 }
199  
200 21952 ▶sub _cell {
Subroutine _cell: called CC=8 SCAR=20.8
201 21952 my ($self, $square, $piece, $colour, $spot, $glyph_row) = @_;
202 21952 my $light = $self->light($square);
203 21952 my $size = $self->_size;
204 21952 my $w = 2 * $size;
205 21952 my $char;
206 21952 ▶ if (!$glyph_row) { $char = ' ' }
Branch: 3 branches
BranchTrueFalseTotal
if (not $glyph_row) { } 3200 18752 21952
elsif ($piece) { } 6744 12008 18752
elsif ($spot) { } 881 11127 12008
207 6744 elsif ($piece) { $char = $self->glyph($piece, $colour) }
208 881 elsif ($spot) { $char = '+' }
209 11127 ▶ else { $char = $self->colour ? ' ' : $light ? '.' : ':' }
Branch: 2 branches
BranchTrueFalseTotal
$self->colour ? : 758 10369 11127
$light ? : 5378 4991 10369
210 21952 my $text = (' ' x $size) . $char . (' ' x ($w - $size - 1));
211 21952 ▶ return $self->_paint($text, $light, $piece && $glyph_row ? $colour : undef);
Branch: 1 branch
BranchTrueFalseTotal
$piece && $glyph_row ? : 6744 15208 21952
Condition100%3/3
$piece and $glyph_row
!ll&&!rl&&r
1368415246744
MC/DC100%2/2
$piece and $glyph_row
$piece $glyph_row
Truth table100%3/3
A and B
A= $pieceB= $glyph_row
ABresult
0X0
100
111
212 }
213  
214 2344 ▶sub _marks {
Subroutine _marks: called CC=8 SCAR=20.8
215 2344 my ($self, $rank, $files, $o) = @_;
216 2344 my @gap = (' ') x 9;
217 2344 ▶ my $take = $self->ascii ? 'x' : "\x{00D7}";
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1648 696 2344
218  
219 2344 for my $i (0 .. 7) {
220 18752 my $sq = $files->[$i] . $rank;
221 18752 ▶ next unless $o->{_take}{$sq};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"_take"}{$sq} 18729 23 18752
222 23 $gap[$i] = $take;
223 23 $gap[$i + 1] = $take;
224 }
225 2344 for my $pair ([ $o->{from}, '(', ')' ], [ $o->{cursor}, '[', ']' ]) {
226 4688 my ($sq, $left, $right) = @$pair;
227 4688 ▶ next unless defined $sq && substr($sq, 1, 1) == $rank;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sq and substr($sq, 1, 1) == $rank 4405 283 4688
Condition100%3/3
defined $sq and substr($sq, 1, 1) == $rank
!ll&&!rl&&r
24241981283
MC/DC100%2/2
defined $sq and substr($sq, 1, 1) == $rank
defined $sq substr($sq, 1, 1) == $rank
Truth table100%3/3
A and B
A= defined $sqB= substr($sq, 1, 1) == $rank
ABresult
0X0
100
111
228 283 my ($i) = grep { $files->[$_] eq substr($sq, 0, 1) } 0 .. 7;
229 283 ▶ next unless defined $i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i 0 283 283
230 283 $gap[$i] = $left;
231 283 $gap[$i + 1] = $right;
232 }
233 2344 return @gap;
234 }
235  
236 293 ▶sub board_lines {
Subroutine board_lines: called CC=19 SCAR=29.5
Pod documented
237 293 my ($self, %o) = @_;
238 293 ▶ my $pos = $o{position} || ($self->game ? $self->game->position : Chess::Plisco::Engine::Position->new);
Branch: 1 branch
BranchTrueFalseTotal
$self->game ? : 249 43 292
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->position : 'Chess::Plisco::Engine::Position'->new)
l!l&&r!l&&!r
12920
MC/DC0%0/2
$o{'position'} || ($self->game ? $self->game->position : 'Chess::Plisco::Engine::Position'->new)
$o{'position'} ($self->game ? $self->game->position : 'Chess::Plisco::Engine::Position'->new)
Truth table66%2/3
A || B
A= $o{'position'}B= ($self->game ? $self->game->position : 'Chess::Plisco::Engine::Position'->new)
ABresult
1X1
011
000
239 293 ▶ my $flip = $o{flip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o{'flip'} ? : 35 258 293
240 293 my $size = $self->_size;
241 293 my $glyph_row = int($size / 2);
242  
243 293 my (%spot, %take);
244 293 ▶ for my $sq (@{ $o{spots} || [] }) {
Condition100%2/2
$o{'spots'} || []
l!l
93200
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
Aresult
11
00
245 903 my ($p) = $pos->pieceAtSquare($sq);
246 903 ▶ $p || $o{_ep}{$sq} ? ($take{$sq} = 1) : ($spot{$sq} = 1);
Branch: 1 branch
BranchTrueFalseTotal
$p || $o{'_ep'}{$sq} ? : 22 881 903
Condition66%2/3
$p or $o{"_ep"}{$sq}
l!l&&r!l&&!r
220881
MC/DC50%1/2
$p or $o{"_ep"}{$sq}
$p $o{"_ep"}{$sq}
Truth table66%2/3
A or B
A= $pB= $o{"_ep"}{$sq}
ABresult
1X1
011
000
247 }
248 293 ▶ $take{$_} = 1 for keys %{ $o{_ep} || {} };
Condition100%2/2
$o{'_ep'} || {}
l!l
84209
MC/DC100%1/1
$o{'_ep'} || {}
$o{'_ep'}
Truth table100%2/2
A
A= $o{'_ep'}
Aresult
11
00
249 293 $o{_take} = \%take;
250  
251 293 ▶ my @ranks = $flip ? (1 .. 8) : reverse(1 .. 8);
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 35 258 293
252 293 ▶ my @files = $flip ? reverse('a' .. 'h') : ('a' .. 'h');
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 35 258 293
253 293 my @lines;
254  
255 293 for my $rank (@ranks) {
256 2344 my @marks = $self->_marks($rank, \@files, \%o);
257 2344 for my $r (0 .. $size - 1) {
258 2744 ▶ my $on = $r == $glyph_row ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$r == $glyph_row ? : 2344 400 2744
259 2744 ▶ my @gap = $on ? @marks : (' ') x 9;
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 2344 400 2744
260 2744 ▶ my $row = $on ? sprintf('%d ', $rank) : ' ';
Branch: 1 branch
BranchTrueFalseTotal
$on ? : 2344 400 2744
261 2744 $row .= $self->_paint($gap[0], $self->light($files[0] . $rank));
262 2744 for my $i (0 .. 7) {
263 21952 my $sq = $files[$i] . $rank;
264 21952 my ($piece, $colour) = $pos->pieceAtSquare($sq);
265 21952 $row .= $self->_cell($sq, $piece, $colour, $spot{$sq}, $on);
266 21952 $row .= $self->_paint($gap[$i + 1], $self->light($sq));
267 }
268 2744 push @lines, $row;
269 }
270 }
271  
272 293 my $legend = ' ';
273 293 $legend .= (' ' x $size) . $_ . (' ' x $size) for @files;
274 293 push @lines, $legend;
275 293 return @lines;
276 }
277  
278 88 ▶sub draw {
Subroutine draw: called CC=1 SCAR=0.0
Pod documented
279 88 my ($self, %o) = @_;
280 88 $self->screen->say($self->board_lines(%o));
281 88 return $self;
282 }
283  
284 52 ▶sub clear {
Subroutine clear: called CC=3 SCAR=11.0
Pod documented
285 52 my ($self, @notice) = @_;
286 52 ▶ $self->_out("\e[H\e[2J") if $self->rewind;
Branch: 1 branch
BranchTrueFalseTotal
if $self->rewind 7 45 52
287 52 ▶ $self->screen->say(@notice) if @notice;
Branch: 1 branch
BranchTrueFalseTotal
if @notice 32 20 52
288 52 return $self;
289 }
290  
291 41 ▶sub keys_available { $_[0]->keyboard->keys_available }
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
292  
293 109 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
294 109 my ($self, @set) = @_;
295 109 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 46 63 109
$set[0] ? : 0 46 46
296 109 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 70 39 109
$self->_picking ? : 11 59 70
297 39 return $self->keys_available;
298 }
299  
300 216 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=18.1
Pod documented
301 216 my ($self, @set) = @_;
302 216 ▶ $self->_rewind($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 1 215 216
$set[0] ? : 1 0 1
303 216 ▶ return $self->_rewind ? 1 : 0 if defined $self->_rewind;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_rewind 186 30 216
$self->_rewind ? : 12 174 186
304 30 ▶ return eval { -t $self->out } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->out } } ? : 0 30 30
305 }
306  
307 9 ▶sub enter_raw {
Subroutine enter_raw: called CC=2 SCAR=7.2
Pod documented
308 9 my ($self) = @_;
309 9 ▶ return $self->keyboard->enter_raw ? $self : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->keyboard->enter_raw ? : 9 0 9
310 }
311  
312 53 ▶sub leave_raw {
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
313 53 my ($self) = @_;
314 53 $self->keyboard->leave_raw;
315 53 return $self;
316 }
317  
318 169 ▶sub read_key { $_[0]->keyboard->read_key }
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
319  
320 100 ▶sub piece_name {
Subroutine piece_name: called CC=3 SCAR=11.0
Pod documented
321 100 my ($self, $piece, $colour) = @_;
322 100 ▶ return 'nothing' unless $piece;
Branch: 1 branch
BranchTrueFalseTotal
unless $piece 1 99 100
323 99 ▶ return ($colour == CP_WHITE ? 'white ' : 'black ') . $WORD{$piece};
Branch: 1 branch
BranchTrueFalseTotal
$colour == 0 ? : 75 24 99
324 }
325  
326 29 ▶sub describe {
Subroutine describe: called CC=6 SCAR=17.9
Pod documented
327 29 my ($self, $g, $uci, $lead) = @_;
328 29 my $pos = $g->position;
329 29 my $m = eval { $pos->parseMove($uci) };
330 29 ▶ return "$lead $uci" unless $m;
Branch: 1 branch
BranchTrueFalseTotal
unless $m 2 27 29
331 27 my $san = $pos->SAN($m);
332 27 my $line = "$lead $san ($uci)";
333 27 my $captured = $pos->moveCaptured($m);
334 27 ▶ $line .= ', taking the ' . $WORD{ $pos->moveEnPassant($m) ? CP_PAWN() : $captured } if $captured;
Branch: 2 branches
BranchTrueFalseTotal
if $captured 3 24 27
$pos->moveEnPassant($m) ? : 1 2 3
335 27 ▶ $line .= ', checkmate' if $san =~ /#\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $san =~ /#\z/u 2 25 27
336 27 ▶ $line .= ', check' if $san =~ /\+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $san =~ /\+\z/u 1 26 27
337 27 return $line;
338 }
339  
340 223 ▶sub _drawn_order {
Subroutine _drawn_order: called CC=2 SCAR=6.9
341 223 my ($self, $flip, @squares) = @_;
342 223 ▶ my $row = sub { my $r = substr($_[0], 1, 1); $flip ? $r : 9 - $r };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 286 6286 6572
343 223 ▶ my $col = sub { my $f = ord(substr $_[0], 0, 1) - ord('a'); $flip ? 7 - $f : $f };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 212 2664 2876
344 223 ▶ return [ sort { $row->($a) <=> $row->($b) || $col->($a) <=> $col->($b) } @squares ];
Condition66%2/3
&$row($a) <=> &$row($b) || &$col($a) <=> &$col($b)
l!l&&r!l&&!r
184814380
MC/DC0%0/2
&$row($a) <=> &$row($b) || &$col($a) <=> &$col($b)
&$row($a) <=> &$row($b) &$col($a) <=> &$col($b)
Truth table66%2/3
A || B
A= &$row($a) <=> &$row($b)B= &$col($a) <=> &$col($b)
ABresult
1X1
011
000
345 }
346  
347 53 ▶sub origins {
Subroutine origins: called CC=1 SCAR=0.0
Pod documented
348 53 my ($self, $pos, $flip) = @_;
349 53 my %seen;
350 53 my @from = grep { !$seen{$_}++ } map { $pos->shiftToSquare($pos->moveFrom($_)) } $pos->legalMoves;
351 53 return $self->_drawn_order($flip, @from);
352 }
353  
354 170 ▶sub targets {
Subroutine targets: called CC=1 SCAR=0.0
Pod documented
355 170 my ($self, $pos, $from, $flip) = @_;
356 170 my %seen;
357 1211 my @to = grep { !$seen{$_}++ }
358 1211 map { $pos->shiftToSquare($pos->moveTo($_)) }
359 170 grep { $pos->shiftToSquare($pos->moveFrom($_)) eq $from } $pos->legalMoves;
360 170 return $self->_drawn_order($flip, @to);
361 }
362  
363 98 ▶sub moves_between {
Subroutine moves_between: called CC=2 SCAR=6.9
Pod documented
364 98 my ($self, $pos, $from, $to) = @_;
365 98 ▶ return grep { $pos->shiftToSquare($pos->moveFrom($_)) eq $from
Condition100%3/3
$pos->shiftToSquare($pos->moveFrom($_)) eq $from && $pos->shiftToSquare($pos->moveTo($_)) eq $to
!ll&&!rl&&r
933908150
MC/DC100%2/2
$pos->shiftToSquare($pos->moveFrom($_)) eq $from && $pos->shiftToSquare($pos->moveTo($_)) eq $to
$pos->shiftToSquare($pos->moveFrom($_)) eq $from $pos->shiftToSquare($pos->moveTo($_)) eq $to
Truth table100%3/3
A && B
A= $pos->shiftToSquare($pos->moveFrom($_)) eq $fromB= $pos->shiftToSquare($pos->moveTo($_)) eq $to
ABresult
0X0
100
111
366 && $pos->shiftToSquare($pos->moveTo($_)) eq $to } $pos->legalMoves;
367 }
368  
369 166 ▶sub step {
Subroutine step: called CC=9 SCAR=22.0
Pod documented
370 166 my ($self, $squares, $at, $key, $flip) = @_;
371 166 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 29 137 166
372 137 ▶ return $#$squares if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 14 123 137
373 123 ▶ return Game::Terminal::Kit::Steer::ring(scalar @$squares, $at, 1)
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq "tab" 75 48 123
Condition100%3/3
$key eq "right" or $key eq "tab"
l!l&&r!l&&!r
621348
MC/DC100%2/2
$key eq "right" or $key eq "tab"
$key eq "right" $key eq "tab"
Truth table100%3/3
A or B
A= $key eq "right"B= $key eq "tab"
ABresult
1X1
011
000
374 if $key eq 'right' || $key eq 'tab';
375 48 ▶ return Game::Terminal::Kit::Steer::ring(scalar @$squares, $at, -1)
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" 13 35 48
376 if $key eq 'left';
377  
378 my @points = map {
379 35 my $rank = substr $_, 1, 1;
380 294 ▶ [ ord(substr $_, 0, 1), ROW_WEIGHT * ($flip ? $rank : 9 - $rank) ]
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 15 279 294
381 } @$squares;
382 35 ▶ my $next = Game::Terminal::Kit::Steer::nearest(\@points, $at, 0, $key eq 'up' ? -1 : 1);
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'up' ? : 16 19 35
383 35 ▶ return defined $next ? $next : $at;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 9 26 35
384 }
385  
386 74 ▶sub victims {
Subroutine victims: called CC=3 SCAR=11.0
Pod documented
387 74 my ($self, $pos, $from) = @_;
388 74 my %ep;
389 74 for my $m (grep { $pos->shiftToSquare($pos->moveFrom($_)) eq $from } $pos->legalMoves) {
390 925 ▶ next unless $pos->moveEnPassant($m);
Branch: 1 branch
BranchTrueFalseTotal
unless $pos->moveEnPassant($m) 923 2 925
391 2 my $to = $pos->shiftToSquare($pos->moveTo($m));
392 2 $ep{ substr($to, 0, 1) . substr($from, 1, 1) } = 1;
393 }
394 74 return \%ep;
395 }
396  
397 74 ▶sub move_line {
Subroutine move_line: called CC=13 SCAR=25.7
Pod documented
398 74 my ($self, $pos, $from, $to) = @_;
399 74 my @moves = $self->moves_between($pos, $from, $to);
400 74 ▶ return "$to: not a move" unless @moves;
Branch: 1 branch
BranchTrueFalseTotal
unless @moves 1 73 74
401 73 ▶ my ($m) = sort { ($pos->movePromote($b) || 0) <=> ($pos->movePromote($a) || 0) } @moves;
Condition50%2/4
$pos->movePromote($b) || 0
l!l
360
$pos->movePromote($a) || 0
l!l
360
MC/DC0%0/1
$pos->movePromote($b) || 0
$pos->movePromote($b)
MC/DC0%0/1
$pos->movePromote($a) || 0
$pos->movePromote($a)
Truth table50%1/2
A
A= $pos->movePromote($b)
Aresult
11
00
Truth table50%1/2
A
A= $pos->movePromote($a)
Aresult
11
00
402  
403 73 my $captured = $pos->moveCaptured($m);
404 73 my ($prey, $prey_colour) = $pos->pieceAtSquare($to);
405 73 my $line = "$to: ";
406 73 ▶ if ($pos->moveEnPassant($m)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($pos->moveEnPassant($m)) { } 1 72 73
elsif ($captured) { } 6 66 72
elsif ($pos->movePiece($m) == 6 and abs ord(substr($from, 0, 1)) - ord(substr($to, 0, 1)) == 2) { } 2 64 66
407 1 $line .= 'takes the pawn en passant';
408 }
409 elsif ($captured) {
410 6 $line .= 'takes the ' . $self->piece_name($prey, $prey_colour);
411 }
412 ▶ elsif ($pos->movePiece($m) == CP_KING
Condition100%3/3
$pos->movePiece($m) == 6 and abs ord(substr($from, 0, 1)) - ord(substr($to, 0, 1)) == 2
!ll&&!rl&&r
6222
MC/DC100%2/2
$pos->movePiece($m) == 6 and abs ord(substr($from, 0, 1)) - ord(substr($to, 0, 1)) == 2
$pos->movePiece($m) == 6 abs ord(substr($from, 0, 1)) - ord(substr($to, 0, 1)) == 2
Truth table100%3/3
A and B
A= $pos->movePiece($m) == 6B= abs ord(substr($from, 0, 1)) - ord(substr($to, 0, 1)) == 2
ABresult
0X0
100
111
413 && abs(ord(substr $from, 0, 1) - ord(substr $to, 0, 1)) == 2) {
414 2 $line .= 'castles';
415 }
416 else {
417 64 $line .= 'a quiet move';
418 }
419 73 ▶ $line .= ', and promotes' if @moves > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @moves > 1 9 64 73
420  
421 73 my $san = $pos->SAN($m);
422 73 ▶ $line .= $san =~ /#\z/ ? ', and it is mate'
Branch: 2 branches
BranchTrueFalseTotal
$san =~ /#\z/u ? : 1 72 73
$san =~ /\+\z/u ? : 19 53 72
423 : $san =~ /\+\z/ ? ', and it checks'
424 : '';
425 73 ▶ $line .= ', but it loses material' if $captured && $pos->SEE($m) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $captured and $pos->SEE($m) < 0 2 71 73
Condition100%3/3
$captured and $pos->SEE($m) < 0
!ll&&!rl&&r
6652
MC/DC100%2/2
$captured and $pos->SEE($m) < 0
$captured $pos->SEE($m) < 0
Truth table100%3/3
A and B
A= $capturedB= $pos->SEE($m) < 0
ABresult
0X0
100
111
426 73 return $line;
427 }
428  
429 92 ▶sub piece_line {
Subroutine piece_line: called CC=3 SCAR=11.0
Pod documented
430 92 my ($self, $pos, $from, $flip) = @_;
431 92 my ($piece, $colour) = $pos->pieceAtSquare($from);
432 92 my $n = scalar @{ $self->targets($pos, $from, $flip) };
433 92 ▶ my $line = sprintf 'the %s on %s, %d %s',
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 25 67 92
434 $self->piece_name($piece, $colour), $from, $n, ($n == 1 ? 'move' : 'moves');
435 92 ▶ $line .= ', and it is attacked' if $pos->attacked($pos->squareToShift($from));
Branch: 1 branch
BranchTrueFalseTotal
if $pos->attacked($pos->squareToShift($from)) 1 91 92
436 92 return $line;
437 }
438  
439 47 ▶sub pick_move {
Subroutine pick_move: called CC=35 SCAR=35.6
Pod documented
440 47 my ($self, $g, $seat) = @_;
441 47 my $pos = $g->position;
442 47 ▶ my $flip = $seat eq $g->black ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seat eq $g->black ? : 6 41 47
443 47 my $from = $self->origins($pos, $flip);
444 47 ▶ return undef unless @$from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$from 0 47 47
445  
446 47 my ($at, $held, $to, $toat, $asking, $drawn) = (0, undef, undef, 0, 0, 0);
447  
448 47 while (1) {
449 161 my %o = (flip => $flip);
450 161 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) { } 72 89 161
451 72 $to = $self->targets($pos, $held, $flip);
452 72 %o = (%o, from => $held, cursor => $to->[$toat], spots => $to,
453 _ep => $self->victims($pos, $held));
454 }
455 else {
456 89 %o = (%o, cursor => $from->[$at]);
457 }
458  
459 161 my @lines = $self->board_lines(%o);
460 161 ▶ push @lines, ' ' . ($asking ? 'queen, rook, bishop or knight? (q r b n, enter is queen)'
Branch: 2 branches
BranchTrueFalseTotal
$asking ? : 10 151 161
defined $held ? : 62 89 151
461 : defined $held ? $self->move_line($pos, $held, $to->[$toat])
462 : $self->piece_line($pos, $from->[$at], $flip));
463 161 ▶ push @lines, ' ' . ($asking ? 'escape to choose another square'
Branch: 2 branches
BranchTrueFalseTotal
$asking ? : 10 151 161
defined $held ? : 62 89 151
464 : defined $held ? 'arrows to choose, enter or space to play, escape to pick another'
465 : 'arrows to choose a piece, enter or space to take it, S to save, ? for the keys');
466  
467 161 my $screen = $self->screen;
468 161 $screen->interactive($self->rewind);
469 161 $screen->rewind($drawn);
470 161 $screen->say(@lines);
471 161 $drawn = scalar @lines;
472  
473 161 my $key = $self->read_key;
474 161 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 160 161
475 160 ▶ return 'quit' if $key eq 'q' && !$asking;
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" and not $asking 15 145 160
Condition100%3/3
$key eq "q" and not $asking
!ll&&!rl&&r
144115
MC/DC100%2/2
$key eq "q" and not $asking
$key eq "q" not $asking
Truth table100%3/3
A and not(B)
A= $key eq "q"B= not $asking
ABresult
0X0
100
111
476 145 ▶ return 'quit' if $key eq 'interrupt' || $key eq 'eof';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "interrupt" or $key eq "eof" 0 145 145
Condition33%1/3
$key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
00145
MC/DC0%0/2
$key eq "interrupt" or $key eq "eof"
$key eq "interrupt" $key eq "eof"
Truth table33%1/3
A or B
A= $key eq "interrupt"B= $key eq "eof"
ABresult
1X1
011
000
477  
478 145 ▶ if ($asking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asking) 10 135 145
479 10 ▶ if ($key eq 'escape' || $key eq 'backspace') { $asking = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 1 9 10
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
109
MC/DC50%1/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table66%2/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
480 9 ▶ my $letter = ($key eq 'enter' || $key eq ' ') ? 'q'
Branch: 2 branches
BranchTrueFalseTotal
$key eq 'enter' || $key eq ' ' ? : 2 7 9
$key =~ /\A[qrbn]\z/u ? : 5 2 7
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
117
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
481 : $key =~ /\A[qrbn]\z/ ? $key
482 : undef;
483 9 ▶ next unless defined $letter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $letter 2 7 9
484 7 return $held . $to->[$toat] . $letter;
485 }
486  
487 135 ▶ my $word = $self->history->game_keys->{$key} // Game::Terminal::Kit::Save::game_keys()->{$key};
Condition66%2/3
$self->history->game_keys->{$key} // Game::Terminal::Kit::Save::game_keys()->{$key}
l!l&&r!l&&!r
100125
MC/DC50%1/2
$self->history->game_keys->{$key} // Game::Terminal::Kit::Save::game_keys()->{$key}
$self->history->game_keys->{$key} Game::Terminal::Kit::Save::game_keys()->{$key}
Truth table66%2/3
A // B
A= $self->history->game_keys->{$key}B= Game::Terminal::Kit::Save::game_keys()->{$key}
ABresult
1X1
011
000
488 135 ▶ return $word if defined $word;
Branch: 1 branch
BranchTrueFalseTotal
if defined $word 10 125 135
489  
490 125 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 49 76 125
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
43676
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
491 49 ▶ if (!defined $held) { ($held, $toat) = ($from->[$at], 0); next }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $held) 27 22 49
492 22 my @moves = $self->moves_between($pos, $held, $to->[$toat]);
493 22 ▶ if (@moves > 1) { $asking = 1; next }
Branch: 1 branch
BranchTrueFalseTotal
if (@moves > 1) 8 14 22
494 14 return $pos->moveCoordinateNotation($moves[0]);
495 }
496 76 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 2 74 76
Condition100%3/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
1174
MC/DC100%2/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table100%3/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
497 2 $held = undef;
498 2 next;
499 }
500 74 ▶ if ($key =~ /\A(?:up|down|left|right|home|end|tab)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A(?:up|down|left|right|home|end|tab)\z/u) 68 6 74
501 68 ▶ defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 29 39 68
502 ? ($toat = $self->step($to, $toat, $key, $flip))
503 : ($at = $self->step($from, $at, $key, $flip));
504 68 next;
505 }
506 6 ▶ if ($key eq '?') { $self->_keys; $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 2 4 6
507 4 ▶ if ($key eq 'l') { $self->_legal($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "l") 1 3 4
508 3 ▶ if ($key eq 'f') { $self->_say($pos->toFEN); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "f") 1 2 3
509 2 ▶ if ($key eq 'g') { $self->_history($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g") 1 1 2
510 1 ▶ if ($key eq 'h') { $self->_hint($g, $seat); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 1 0 1
511 }
512 }
513  
514 2 ▶sub _keys {
Subroutine _keys: called CC=2 SCAR=6.9
515 2 my ($self) = @_;
516 2 $self->_say($_) for
517 'arrows move the cursor; it only stops where a move is',
518 'enter take the piece under the cursor, then play to the square',
519 'space the same as enter',
520 'escape put the piece back and choose another',
521 'tab the next one, whichever you are choosing',
522 'q r b n what a pawn becomes, when it is asked',
523 'h the strongest move this engine can find',
524 'l f g every move you have, the FEN, the moves so far',
525 'u take back your last move and the bot\'s reply',
526 'r play again what u took back',
527 'S save the game, to carry on with --resume or watch with --replay',
528 'q give up and leave';
529 2 return $self;
530 }
531  
532  
533 52 ▶sub start {
Subroutine start: called CC=41 SCAR=37.5
Pod documented
534 52 my ($self) = @_;
535  
536 52 ▶ if (!$self->game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->game) 1 51 52
537 1 my $made = eval { Term::Chess::Game->new(
538 seed => substr('chess-terminal-default' . ('.' x 32), 0, 32)) };
539 1 $self->game($made);
540 }
541 52 ▶ return 2 unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 0 52 52
542 52 $self->history->start($self->game);
543 52 my $seat = $self->seat;
544  
545 52 ▶ my $pick = $self->picking && $self->enter_raw ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking && $self->enter_raw ? : 7 45 52
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
4507
MC/DC50%1/2
$self->picking and $self->enter_raw
$self->picking $self->enter_raw
Truth table66%2/3
A and B
A= $self->pickingB= $self->enter_raw
ABresult
0X0
100
111
546 52 ▶ $self->picking(0) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 45 7 52
547 52 my $notice;
548 52 ▶ $self->_say($pick
Branch: 1 branch
BranchTrueFalseTotal
$pick ? : 7 45 52
549 ? 'chess. arrows to choose a move, enter or space to play it, ? for the keys.'
550 : 'chess. a move is coordinates like e2e4, or SAN like Nf3. "help" for the rest.');
551 52 ▶ $self->draw(flip => $self->_flip) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 45 7 52
552  
553 52 ▶ while ($self->game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active") 109 9 118
554 109 my $g = $self->game;
555 109 my $flip = $self->_flip;
556 109 ▶ if ($g->turn ne $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->turn ne $seat) 22 87 109
557 22 my $mv = do { local $Term::Chess::Bot::LEVEL = $self->level;
558 22 $g->bot->choose($g, $g->turn) };
559 22 ▶ last unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 22 22
560 22 $notice = $self->describe($g, $mv, 'the bot plays');
561 22 my $refusal = $g->play($mv);
562 22 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 21 22
563 1 $self->_say($notice, 'the bot offered a move the game refused: ' . $refusal->code);
564 1 last;
565 }
566 21 $self->clear($notice);
567 21 ▶ $self->draw(flip => $flip) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 14 7 21
568 21 next;
569 }
570  
571 87 $self->person_turn;
572  
573 87 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 21 66 87
574 21 my $chosen = $self->pick_move($g, $seat);
575 21 ▶ last if !defined $chosen || $chosen eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $chosen or $chosen eq "quit" 7 14 21
Condition66%2/3
not defined $chosen or $chosen eq "quit"
l!l&&r!l&&!r
0714
MC/DC50%1/2
not defined $chosen or $chosen eq "quit"
not defined $chosen $chosen eq "quit"
Truth table66%2/3
not(A) or B
A= not defined $chosenB= $chosen eq "quit"
ABresult
1X1
011
000
576 14 ▶ if ($chosen eq 'undo') { $notice = undef if $self->take_back; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($chosen eq "undo") 5 9 14
if $self->take_back 3 2 5
577 9 ▶ if ($chosen eq 'redo') { $notice = undef if $self->play_again; next }
Branch: 2 branches
BranchTrueFalseTotal
if ($chosen eq "redo") 3 6 9
if $self->play_again 1 2 3
578 0 ▶ if ($chosen eq 'save') { $self->_ask_save; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($chosen eq "save") 0 6 6
579 6 my $refusal = $g->play($chosen);
580 6 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')')
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 0 6 6
581 if $refusal;
582 6 next;
583 }
584  
585 66 my $line = $self->keyboard->read_line($self->_prompt($g), $self->out);
586 66 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 66 66
587 66 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 66 66
588  
589 66 ▶ last if $line eq 'quit' || $line eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" or $line eq "q" 33 33 66
Condition66%2/3
$line eq "quit" or $line eq "q"
l!l&&r!l&&!r
33033
MC/DC50%1/2
$line eq "quit" or $line eq "q"
$line eq "quit" $line eq "q"
Truth table66%2/3
A or B
A= $line eq "quit"B= $line eq "q"
ABresult
1X1
011
000
590 33 ▶ $self->_help, next if $line eq 'help' || $line eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "help" or $line eq "?" 2 31 33
Condition66%2/3
$line eq "help" or $line eq "?"
l!l&&r!l&&!r
2031
MC/DC50%1/2
$line eq "help" or $line eq "?"
$line eq "help" $line eq "?"
Truth table66%2/3
A or B
A= $line eq "help"B= $line eq "?"
ABresult
1X1
011
000
591 31 ▶ $self->_legal($g), next if $line eq 'legal';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "legal" 1 30 31
592 30 ▶ $self->_say($g->position->toFEN), next if $line eq 'fen';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "fen" 1 29 30
593 29 ▶ $self->_history($g), next if $line eq 'log';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "log" 2 27 29
594 27 ▶ $self->draw(flip => $flip), next if $line eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "board" 1 26 27
595 26 ▶ $self->_hint($g, $seat), next if $line eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "hint" 1 25 26
596 25 ▶ $self->take_back, next if $line eq 'undo';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "undo" 4 21 25
597 21 ▶ $self->play_again, next if $line eq 'redo';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "redo" 1 20 21
598 20 ▶ if (my (undef, $file) = Game::Terminal::Kit::Save::command($line)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(undef, $file) = Game::Terminal::Kit::Save::command($line)) 0 20 20
599 0 $self->_say($self->save($file));
600 0 next;
601 }
602 20 ▶ if ($line eq 'resign') { $g->resign($seat); last }
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "resign") 2 18 20
603  
604 18 my $refusal = $g->play($line);
605 18 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 2 16 18
606 2 $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')');
607 2 next;
608 }
609 16 $self->clear;
610 16 $self->draw(flip => $flip);
611 }
612  
613 52 ▶ $self->person_turn if $self->game->status ne 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $self->game->status ne "active" 11 41 52
614 52 $self->leave_raw;
615 52 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 7 45 52
616 7 ▶ $self->clear(defined $notice ? $notice : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $notice ? : 3 4 7
617 7 $self->draw(flip => $self->_flip);
618 }
619 52 return $self->_verdict($self->game, $seat);
620 }
621  
622 165 ▶sub _flip {
Subroutine _flip: called CC=2 SCAR=6.9
623 165 my ($self) = @_;
624 165 ▶ return $self->seat eq $self->game->black ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->seat eq $self->game->black ? : 17 148 165
625 }
626  
627 98 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
628 98 my ($self) = @_;
629 98 $self->history->turn($self->game);
630 98 return $self;
631 }
632  
633 9 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
634 9 my ($self) = @_;
635 9 my $game = $self->history->undo;
636 9 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 3 6 9
637 3 $self->_say('nothing to take back');
638 3 return 0;
639 }
640 6 $self->swap($game, 'taken back');
641 6 return 1;
642 }
643  
644 4 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=6.9
Pod documented
645 4 my ($self) = @_;
646 4 my $game = $self->history->redo;
647 4 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 2 2 4
648 2 $self->_say('nothing to play again');
649 2 return 0;
650 }
651 2 $self->swap($game, 'played again');
652 2 return 1;
653 }
654  
655 8 ▶sub swap {
Subroutine swap: called CC=2 SCAR=6.9
Pod documented
656 8 my ($self, $game, @notice) = @_;
657 8 $self->game($game);
658 8 $self->clear(@notice);
659 8 ▶ $self->draw(flip => $self->_flip) unless $self->picking;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->picking 4 4 8
660 8 return $self;
661 }
662  
663 66 ▶sub _prompt {
Subroutine _prompt: called CC=1 SCAR=0.0
664 66 my ($self, $g) = @_;
665 66 return $g->side . ' to move> ';
666 }
667  
668 2 ▶sub _help {
Subroutine _help: called CC=2 SCAR=6.9
669 2 my ($self) = @_;
670 2 $self->_say($_) for
671 'a move is coordinates, the from square then the to square, like e2e4',
672 'or standard notation like Nf3, exd5, O-O, a8=Q',
673 'board draw it again',
674 'legal every move you have',
675 'fen the position as a FEN',
676 'log the moves so far',
677 'hint the strongest move this engine can find',
678 'undo take back your last move and the bot\'s reply',
679 'redo play again what undo took back',
680 'save save the game, to carry on with --resume; save FILE to that file',
681 'resign give up the game',
682 'quit leave without finishing';
683 2 return $self;
684 }
685  
686 2 ▶sub _legal {
Subroutine _legal: called CC=1 SCAR=0.0
687 2 my ($self, $g) = @_;
688 2 my @m = @{ $g->legal };
689 2 $self->_say(scalar(@m) . ' moves: ' . join ' ', @m);
690 2 return $self;
691 }
692  
693 3 ▶sub _history {
Subroutine _history: called CC=2 SCAR=6.9
694 3 my ($self, $g) = @_;
695 3 my $san = $g->san_log;
696 3 ▶ $self->_say(length $san ? $san : 'no moves yet');
Branch: 1 branch
BranchTrueFalseTotal
length $san ? : 1 2 3
697 3 return $self;
698 }
699  
700 2 ▶sub _hint {
Subroutine _hint: called CC=2 SCAR=7.0
701 2 my ($self, $g, $seat) = @_;
702 2 my $mv = $g->bot->hint($g, $seat);
703 2 ▶ $self->_say(defined $mv ? $self->describe($g, $mv, 'try') : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 2 0 2
704 2 return $self;
705 }
706  
707 52 ▶sub _verdict {
Subroutine _verdict: called CC=7 SCAR=19.7
708 52 my ($self, $g, $seat) = @_;
709 52 ▶ return 0 if $g->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $g->status eq "active" 41 11 52
710 11 my $r = $g->result;
711 11 ▶ my $why = $WHY{ $r->{reason} || '' } || $r->{reason} || 'over';
Condition42%3/7
$WHY{$r->{'reason'} or ''} || $r->{'reason'} || 'over'
l!l
110
$WHY{$r->{"reason"} or ""} or $r->{"reason"}
l!l&&r!l&&!r
1100
$r->{'reason'} || ''
l!l
110
MC/DC0%0/2
$WHY{$r->{'reason'} or ''} || $r->{'reason'} || 'over'
$WHY{$r->{"reason"} or ""} $r->{"reason"}
MC/DC0%0/1
$r->{'reason'} || ''
$r->{'reason'}
Truth table33%1/3
A or B
A= $WHY{$r->{"reason"} or ""}B= $r->{"reason"}
ABresult
1X1
011
000
Truth table50%1/2
A
A= $r->{'reason'}
Aresult
11
00
712  
713 11 ▶ if (!defined $r->{winner}) { $self->_say("drawn: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"winner"}) 4 7 11
714 7 ▶ if ($r->{winner} eq $seat) { $self->_say("you won: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"winner"} eq $seat) 3 4 7
715 4 $self->_say("you lost: $why");
716 4 return 1;
717 }
718  
719 0 ▶sub _ask_save {
Subroutine _ask_save: not called CC=2 SCAR=17.9
720 0 my ($self) = @_;
721 0 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->screen,
722 default => $self->save_default,
723 head => [ $self->board_lines(flip => $self->_flip), '' ]);
724 0 ▶ $self->_say($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 0 0 0
725 0 return $self;
726 }
727  
728 0 ▶sub settings {
Subroutine settings: not called CC=3 SCAR=24.8
Pod documented
729 0 my ($self) = @_;
730 return {
731 0 ▶ seat => $self->seat,
Branch: 2 branches
BranchTrueFalseTotal
defined $self->_size ? : 0 0 0
defined $self->level ? : 0 0 0
732 (defined $self->_size ? (size => $self->_size) : ()),
733 (defined $self->level ? (level => $self->level) : ()),
734 };
735 }
736  
737 0 ▶sub save_default {
Subroutine save_default: not called CC=2 SCAR=17.9
Pod documented
738 0 my ($self) = @_;
739 0 my $file = $self->history->file;
740 0 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('chess');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 0 0
741 }
742  
743 0 ▶sub save {
Subroutine save: not called CC=4 SCAR=30.0
Pod documented
744 0 my ($self, $file) = @_;
745 0 ▶ $file = $self->save_default unless defined $file && length $file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file 0 0 0
Condition0%0/3
defined $file and length $file
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and length $file
defined $file length $file
Truth table0%0/3
A and B
A= defined $fileB= length $file
ABresult
0X0
100
111
746 0 my $history = $self->history;
747 0 $history->settings($self->settings);
748 0 $history->version($VERSION);
749 0 my $path = eval { $history->saved($self->game)->write_file($file) };
750 0 ▶ return 'not saved: ' . Game::Terminal::Kit::Save::why($@) unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 0 0
751 0 $history->file($path);
752 0 return "saved to $path";
753 }
754  
755 0 ▶sub read_save {
Subroutine read_save: not called CC=1 SCAR=6.9
Pod documented
756 0 my ($class, $file) = @_;
757 0 return Game::Terminal::Kit::Save->read_game($file, 'chess');
758 }
759  
760 0 ▶sub replay_lines {
Subroutine replay_lines: not called CC=4 SCAR=30.0
Pod documented
761 0 my ($self, $game) = @_;
762 0 my $was = $self->game;
763 0 $self->game($game);
764 0 my @lines = eval {
765 0 my @moves = @{ $game->log };
766 0 ▶ ($self->board_lines(flip => $self->_flip),
Branch: 2 branches
BranchTrueFalseTotal
@moves ? : 0 0 0
$game->status eq 'active' ? : 0 0 0
767 ' ' . (@moves ? scalar(@moves) . ' moves, the last ' . $moves[-1] : 'no moves yet')
768 . ($game->status eq 'active' ? '' : ', and the game is over'));
769 };
770 0 my $died = $@;
771 0 $self->game($was);
772 0 ▶ die $died unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 0 0
773 0 return @lines;
774 }
775  
776 17 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
777 17 return <<'USAGE';
778 chess - chess on a terminal, against a bot
779  
780 usage: chess [options]
781  
782 --level N node budget for the bot, or a rung index into the ladder
783 --white SEAT p1 or p2; whoever is White moves first
784 --seat SEAT which seat you play (default p1)
785 --seed STR up to thirty-two bytes, for a reproducible game
786 --fen FEN start from a position instead of the opening
787 --moves LIST play these moves first, coordinates or SAN, comma or space separated
788 --size N 1, 2 or 3: squares of 2, 4 or 6 columns and as many rows (default 2)
789 --ascii the FEN letters instead of the pieces
790 --ansi colour; --no-ansi turns it off (NO_COLOR beats both)
791 --pick choose a move with the arrow keys; --no-pick to type it
792 --save FILE where S, or "save", writes the game: FILE, else the file it
793 was resumed from, else chess.save
794 --resume FILE carry on a saved game, with its undo and redo
795 --replay FILE watch a saved game a turn at a time; enter plays on
796 --help this
797  
798 exit status:
799 0 you won, or the game drew, or you quit
800 1 you lost
801 2 a bad option
802  
803 With a terminal and Term::ReadKey the arrow keys walk the pieces that have a
804 move and then that piece's squares, with the caption saying what each one does.
805 Without either, or with --no-pick, a move is typed as coordinates, like e2e4,
806 or in standard notation, like Nf3 or O-O, and "help" lists the rest.
807  
808 The engine is Chess::Plisco, bounded in NODES and never in seconds, so the
809 same position answers the same on a busy machine as on an idle one.
810 USAGE
811 }
812  
813 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
814 return [
815 2 { name => 'level', type => 'int', min => 0, max => 3, help => 'a rung of the ladder, 0 the weakest' },
816 { name => 'white', type => 'enum', choices => [qw(p1 p2)], help => 'whoever is White moves first' },
817 { name => 'seat', type => 'enum', choices => [qw(p1 p2)], help => 'which seat you play' },
818 { name => 'size', type => 'int', min => 1, max => 3, help => 'squares of 2, 4 or 6 columns' },
819 { name => 'seed', type => 'str', help => 'up to thirty-two bytes, for a reproducible game' },
820 ];
821 }
822  
823 55 ▶sub run {
Subroutine run: called CC=38 SCAR=42.9
Pod documented
824 55 my ($class, @argv) = @_;
825 55 my %opt;
826  
827 55 my $ok = eval {
828 55 ▶ local $SIG{__WARN__} = sub { print STDERR "chess: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
829 55 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
830 'level=i', 'white=s', 'seat=s', 'seed=s', 'fen=s', 'moves=s',
831 'size=i', 'ascii', 'ansi!', 'pick!', 'help',
832 Game::Terminal::Kit::Save::flags());
833 };
834 55 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 4 51 55
835 51 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 12 39 51
836 39 ▶ if (@argv) { print STDERR "chess: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 38 39
837 0 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) { print STDERR "chess: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) 0 38 38
838  
839 38 ▶ my $from = defined $opt{resume} ? $opt{resume} : $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 0 38 38
840 38 my $save;
841 38 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 0 38 38
842 0 for my $new (grep { defined $opt{$_} } qw(fen moves seed white)) {
843 print STDERR "chess: --$new begins a new game, and --"
844 0 ▶ . (defined $opt{resume} ? 'resume' : 'replay') . " starts from a saved one.\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 0 0 0
845 0 return 2;
846 }
847 0 ($save, my $why) = $class->read_save($from);
848 0 ▶ unless ($save) { print STDERR "chess: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 0 0 0
849 0 my $settings = $save->settings;
850 0 ▶ defined $opt{$_} or $opt{$_} = $settings->{$_} for grep { defined $settings->{$_} } qw(level seat size);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $settings->{$_} 0 0 0
851 }
852 38 ▶ $opt{seat} = 'p1' unless defined $opt{seat};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"seat"} 35 3 38
853 38 ▶ $opt{white} = 'p1' unless defined $opt{white};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"white"} 36 2 38
854  
855 38 for my $k (qw(white seat)) {
856 75 ▶ next if $opt{$k} eq 'p1' || $opt{$k} eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{$k} eq "p1" or $opt{$k} eq "p2" 73 2 75
Condition100%3/3
$opt{$k} eq "p1" or $opt{$k} eq "p2"
l!l&&r!l&&!r
7122
MC/DC100%2/2
$opt{$k} eq "p1" or $opt{$k} eq "p2"
$opt{$k} eq "p1" $opt{$k} eq "p2"
Truth table100%3/3
A or B
A= $opt{$k} eq "p1"B= $opt{$k} eq "p2"
ABresult
1X1
011
000
857 2 print STDERR "chess: --$k takes p1 or p2, not '$opt{$k}'\n";
858 2 return 2;
859 }
860  
861 36 my @ladder = Term::Chess::Bot->ladder;
862 36 my $level;
863 36 ▶ if (defined $opt{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"level"}) 13 23 36
864 13 ▶ if ($opt{level} < 0) { print STDERR "chess: --level must not be negative\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} < 0) 1 12 13
865 12 ▶ if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]) 2 10 12
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
!ll&&!rl&&r
822
MC/DC100%2/2
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
$opt{"level"} >= scalar @ladder $opt{"level"} < $ladder[0]
Truth table100%3/3
A and B
A= $opt{"level"} >= scalar @ladderB= $opt{"level"} < $ladder[0]
ABresult
0X0
100
111
866 printf STDERR
867 "chess: --level %d is neither a rung nor a budget: a rung is 0 to %d, "
868 . "and a budget is %d nodes or more\n",
869 2 $opt{level}, $#ladder, $ladder[0];
870 2 return 2;
871 }
872 10 ▶ $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
$opt{'level'} < scalar @ladder ? : 8 2 10
873 }
874  
875 33 ▶ if (defined $opt{size} && $opt{size} !~ /\A[123]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"size"} and not $opt{"size"} =~ /\A[123]\z/u) 2 31 33
Condition100%3/3
defined $opt{"size"} and not $opt{"size"} =~ /\A[123]\z/u
!ll&&!rl&&r
2832
MC/DC100%2/2
defined $opt{"size"} and not $opt{"size"} =~ /\A[123]\z/u
defined $opt{"size"} not $opt{"size"} =~ /\A[123]\z/u
Truth table100%3/3
A and not(B)
A= defined $opt{"size"}B= not $opt{"size"} =~ /\A[123]\z/u
ABresult
0X0
100
111
876 2 print STDERR "chess: --size takes 1, 2 or 3, not '$opt{size}'\n";
877 2 return 2;
878 }
879  
880 my $terminal = $class->new(
881 size => $opt{size},
882 ascii => $opt{ascii},
883 colour => defined $opt{ansi} ? $opt{ansi} : undef,
884 picking => $opt{pick},
885 level => $level,
886 seat => $opt{seat},
887 31 ▶ (defined $opt{save} ? (save_file => $opt{save}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'ansi'} ? : 20 11 31
defined $opt{'save'} ? : 0 31 31
888 );
889  
890 31 ▶ if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"replay"}) 0 31 31
891 my $replay = Game::Terminal::Kit::Replay->new(
892 save => $save,
893 0 ▶ load => sub { Term::Chess::Game->from_text($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
894 0 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
895 0 ▶ head => [ 'chess, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 0 0 0
896 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
897 screen => $terminal->screen,
898 );
899 my ($event, $step) = Game::Terminal::Kit::Run::guard(
900 0 ▶ sub { $replay->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: not called CC=1 SCAR=6.9
901 0 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 0 0 0
902 0 $save = $save->from_step($step);
903 }
904  
905 31 my $game;
906 31 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) { } 0 31 31
907 0 $game = eval { $terminal->history->resume($save) };
908 0 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 0 0
909 0 print STDERR "chess: '" . $save->file . "' cannot be carried on: "
910 . Game::Terminal::Kit::Save::why($@) . ".\n";
911 0 return 2;
912 }
913 0 ▶ $terminal->history->file($opt{save}) if defined $opt{save};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"save"} 0 0 0
914 }
915 else {
916 31 ▶ my $seed = defined $opt{seed} ? $opt{seed} : 'chess-terminal-default-seed32';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 2 29 31
917 31 $seed = substr($seed . ('.' x 32), 0, 32);
918  
919 31 my %new = (seed => $seed, white => $opt{white});
920 31 ▶ $new{position} = $opt{fen} if defined $opt{fen};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"fen"} 4 27 31
921  
922 31 $game = eval { Term::Chess::Game->new(%new) };
923 31 ▶ if (!$game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 30 31
924 1 my $why = $@;
925 1 $why =~ s/\s+at \S+ line \d+\.?\s*\z//;
926 1 $why =~ s/\ATerm::Chess::Game: //;
927 1 print STDERR "chess: could not start a game: $why\n";
928 1 return 2;
929 }
930  
931 30 ▶ if (defined $opt{moves}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"moves"}) 2 28 30
932 2 for my $mv (grep { length } split /[\s,]+/, $opt{moves}) {
933 8 my $refusal = $game->play($mv);
934 8 ▶ next unless $refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refusal 7 1 8
935 1 print STDERR "chess: --moves refused at '$mv': ", $refusal->code, "\n";
936 1 return 2;
937 }
938 }
939 }
940  
941 29 $terminal->game($game);
942  
943 return Game::Terminal::Kit::Run::guard(
944 29 ▶ sub { $terminal->start },
Subroutine __ANON__: called CC=1 SCAR=0.0
945 29 $terminal->keyboard, $terminal->screen,
946 );
947 }
948  
949 7 1;
950  
951 __END__
↑ Summary Chess/Game.pm »