Chess.pm

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