Chess.pm

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