| 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
| |||||||||||||||||||||||||||||||||||
| 97 | 135 | ▶ | Carp::croak("Term::Chess: size is 1, 2 or 3, not '$size'") | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 98 | unless $size =~ /\A[123]\z/; | ||||||||||||||||||||||||||||||||||
| 99 | 133 | $self->_size($size + 0); | |||||||||||||||||||||||||||||||||
| 100 | 133 | ▶ | $self->keyboard(Game::Terminal::Kit::Keys->new( | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 135 | 9130 | ▶ | return $s->screen->ascii ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 141 | 13540 | ▶ | return $s->screen->colour ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 172 | 6786 | ▶ | if ($self->ascii) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 173 | 4383 | my $l = $LETTER{$piece}; | |||||||||||||||||||||||||||||||||
| 174 | 4383 | ▶ | return $colour == CP_WHITE ? $l : lc $l; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||
| 176 | 2403 | ▶ | return $FILLED{$piece} if $self->colour; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 177 | 1682 | ▶ | return $colour == CP_WHITE ? $OUTLINE{$piece} : $FILLED{$piece}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 183 | 46648 | ▶ | $paint .= ';' . $PAINT{ $colour == CP_WHITE ? 'white_piece' : 'black_piece' } if defined $colour; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 194 | 6744 | elsif ($piece) { $char = $self->glyph($piece, $colour) } | |||||||||||||||||||||||||||||||||
| 195 | 881 | elsif ($spot) { $char = '+' } | |||||||||||||||||||||||||||||||||
| 196 | 11127 | ▶ | else { $char = $self->colour ? ' ' : $light ? '.' : ':' } | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$piece and $glyph_row
MC/DC100%2/2
$piece and $glyph_row
$piece
$glyph_row
Truth table100%3/3
A and B
A= $pieceB= $glyph_row
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $sq and substr($sq, 1, 1) == $rank
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
| |||||||||||||||||||||||||||||||||||
| 215 | 283 | my ($i) = grep { $files->[$_] eq substr($sq, 0, 1) } 0 .. 7; | |||||||||||||||||||||||||||||||||
| 216 | 283 | ▶ | next unless defined $i; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->position : 'Chess::Plisco::Engine::Position'->new)
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)
| |||||||||||||||||||||||||||||||||||
| 226 | 293 | ▶ | my $flip = $o{flip} ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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'} || []
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
| |||||||||||||||||||||||||||||||||||
| 232 | 903 | my ($p) = $pos->pieceAtSquare($sq); | |||||||||||||||||||||||||||||||||
| 233 | 903 | ▶ | $p || $o{_ep}{$sq} ? ($take{$sq} = 1) : ($spot{$sq} = 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$p or $o{"_ep"}{$sq}
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}
| |||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||
| 235 | 293 | ▶ | $take{$_} = 1 for keys %{ $o{_ep} || {} }; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$o{'_ep'} || {}
MC/DC100%1/1
$o{'_ep'} || {}
$o{'_ep'}
Truth table100%2/2
A
A= $o{'_ep'}
| |||||||||||||||||||||||||||||||||||
| 236 | 293 | $o{_take} = \%take; | |||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||
| 238 | 293 | ▶ | my @ranks = $flip ? (1 .. 8) : reverse(1 .. 8); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 239 | 293 | ▶ | my @files = $flip ? reverse('a' .. 'h') : ('a' .. 'h'); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 246 | 2744 | ▶ | my @gap = $on ? @marks : (' ') x 9; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 247 | 2744 | ▶ | my $row = $on ? sprintf('%d ', $rank) : ' '; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 274 | 52 | ▶ | $self->screen->say(@notice) if @notice; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 283 | 109 | ▶ | return $self->_picking ? 1 : 0 if defined $self->_picking; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 290 | 216 | ▶ | return $self->_rewind ? 1 : 0 if defined $self->_rewind; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 291 | 30 | ▶ | return eval { -t $self->out } ? 1 : 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 310 | 99 | ▶ | return ($colour == CP_WHITE ? 'white ' : 'black ') . $WORD{$piece}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 322 | 27 | ▶ | $line .= ', checkmate' if $san =~ /#\z/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 323 | 27 | ▶ | $line .= ', check' if $san =~ /\+\z/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 331 | 223 | ▶ | return [ sort { $row->($a) <=> $row->($b) || $col->($a) <=> $col->($b) } @squares ]; | ||||||||||||||||||||||||||||||||
|
Condition66%2/3
&$row($a) <=> &$row($b) || &$col($a) <=> &$col($b)
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)
| |||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 359 | 137 | ▶ | return $#$squares if $key eq 'end'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 360 | 123 | ▶ | return Game::Terminal::Kit::Steer::ring(scalar @$squares, $at, 1) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "right" or $key eq "tab"
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"
| |||||||||||||||||||||||||||||||||||
| 361 | if $key eq 'right' || $key eq 'tab'; | ||||||||||||||||||||||||||||||||||
| 362 | 48 | ▶ | return Game::Terminal::Kit::Steer::ring(scalar @$squares, $at, -1) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 368 | } @$squares; | ||||||||||||||||||||||||||||||||||
| 369 | 35 | ▶ | my $next = Game::Terminal::Kit::Steer::nearest(\@points, $at, 0, $key eq 'up' ? -1 : 1); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 370 | 35 | ▶ | return defined $next ? $next : $at; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 388 | 73 | ▶ | my ($m) = sort { ($pos->movePromote($b) || 0) <=> ($pos->movePromote($a) || 0) } @moves; | ||||||||||||||||||||||||||||||||
|
Condition50%2/4
$pos->movePromote($b) || 0
$pos->movePromote($a) || 0
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)
Truth table50%1/2
A
A= $pos->movePromote($a)
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||
| 408 | 73 | my $san = $pos->SAN($m); | |||||||||||||||||||||||||||||||||
| 409 | 73 | ▶ | $line .= $san =~ /#\z/ ? ', and it is mate' | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 410 | : $san =~ /\+\z/ ? ', and it checks' | ||||||||||||||||||||||||||||||||||
| 411 | : ''; | ||||||||||||||||||||||||||||||||||
| 412 | 73 | ▶ | $line .= ', but it loses material' if $captured && $pos->SEE($m) < 0; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$captured and $pos->SEE($m) < 0
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 430 | 47 | my $from = $self->origins($pos, $flip); | |||||||||||||||||||||||||||||||||
| 431 | 47 | ▶ | return undef unless @$from; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 462 | 160 | ▶ | return 'quit' if $key eq 'q' && !$asking; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "q" and not $asking
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
| |||||||||||||||||||||||||||||||||||
| 463 | 145 | ▶ | return 'quit' if $key eq 'interrupt' || $key eq 'eof'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key eq "interrupt" or $key eq "eof"
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"
| |||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||
| 465 | 145 | ▶ | if ($asking) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 466 | 10 | ▶ | if ($key eq 'escape' || $key eq 'backspace') { $asking = 0; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "escape" or $key eq "backspace"
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"
| |||||||||||||||||||||||||||||||||||
| 467 | 9 | ▶ | my $letter = ($key eq 'enter' || $key eq ' ') ? 'q' | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$key eq "enter" or $key eq " "
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 " "
| |||||||||||||||||||||||||||||||||||
| 468 | : $key =~ /\A[qrbn]\z/ ? $key | ||||||||||||||||||||||||||||||||||
| 469 | : undef; | ||||||||||||||||||||||||||||||||||
| 470 | 9 | ▶ | next unless defined $letter; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||
| 477 | 125 | ▶ | if ($key eq 'enter' || $key eq ' ') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "enter" or $key eq " "
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 " "
| |||||||||||||||||||||||||||||||||||
| 478 | 49 | ▶ | if (!defined $held) { ($held, $toat) = ($from->[$at], 0); next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 479 | 22 | my @moves = $self->moves_between($pos, $held, $to->[$toat]); | |||||||||||||||||||||||||||||||||
| 480 | 22 | ▶ | if (@moves > 1) { $asking = 1; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 481 | 14 | return $pos->moveCoordinateNotation($moves[0]); | |||||||||||||||||||||||||||||||||
| 482 | } | ||||||||||||||||||||||||||||||||||
| 483 | 76 | ▶ | if ($key eq 'escape' || $key eq 'backspace') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$key eq "escape" or $key eq "backspace"
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"
| |||||||||||||||||||||||||||||||||||
| 484 | 2 | $held = undef; | |||||||||||||||||||||||||||||||||
| 485 | 2 | next; | |||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||
| 487 | 74 | ▶ | if ($key =~ /\A(?:up|down|left|right|home|end|tab)\z/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 488 | 68 | ▶ | defined $held | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 494 | 4 | ▶ | if ($key eq 'l') { $self->_legal($g); $drawn = 0; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 495 | 3 | ▶ | if ($key eq 'f') { $self->_say($pos->toFEN); $drawn = 0; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 496 | 2 | ▶ | if ($key eq 'g') { $self->_history($g); $drawn = 0; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 497 | 1 | ▶ | if ($key eq 'h') { $self->_hint($g, $seat); $drawn = 0; next } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self->picking and $self->enter_raw
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
| |||||||||||||||||||||||||||||||||||
| 532 | 52 | ▶ | $self->picking(0) unless $pick; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 533 | 52 | my $notice; | |||||||||||||||||||||||||||||||||
| 534 | 52 | ▶ | $self->_say($pick | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||||||||
| 539 | 52 | ▶ | while ($self->game->status eq 'active') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 540 | 109 | my $g = $self->game; | |||||||||||||||||||||||||||||||||
| 541 | 109 | my $flip = $self->_flip; | |||||||||||||||||||||||||||||||||
| 542 | 109 | ▶ | if ($g->turn ne $seat) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 546 | 22 | $notice = $self->describe($g, $mv, 'the bot plays'); | |||||||||||||||||||||||||||||||||
| 547 | 22 | my $refusal = $g->play($mv); | |||||||||||||||||||||||||||||||||
| 548 | 22 | ▶ | if ($refusal) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 554 | 21 | next; | |||||||||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||||||||
| 557 | 87 | $self->person_turn; | |||||||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||||||||
| 559 | 87 | ▶ | if ($pick) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 560 | 21 | my $chosen = $self->pick_move($g, $seat); | |||||||||||||||||||||||||||||||||
| 561 | 21 | ▶ | last if !defined $chosen || $chosen eq 'quit'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $chosen or $chosen eq "quit"
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"
| |||||||||||||||||||||||||||||||||||
| 562 | 14 | ▶ | if ($chosen eq 'undo') { $notice = undef if $self->take_back; next } | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 563 | 9 | ▶ | if ($chosen eq 'redo') { $notice = undef if $self->play_again; next } | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 564 | 6 | my $refusal = $g->play($chosen); | |||||||||||||||||||||||||||||||||
| 565 | 6 | ▶ | $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 572 | 66 | ▶ | next unless length $line; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||||||||
| 574 | 66 | ▶ | last if $line eq 'quit' || $line eq 'q'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line eq "quit" or $line eq "q"
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"
| |||||||||||||||||||||||||||||||||||
| 575 | 33 | ▶ | $self->_help, next if $line eq 'help' || $line eq '?'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$line eq "help" or $line eq "?"
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 "?"
| |||||||||||||||||||||||||||||||||||
| 576 | 31 | ▶ | $self->_legal($g), next if $line eq 'legal'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 577 | 30 | ▶ | $self->_say($g->position->toFEN), next if $line eq 'fen'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 578 | 29 | ▶ | $self->_history($g), next if $line eq 'log'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 579 | 27 | ▶ | $self->draw(flip => $flip), next if $line eq 'board'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 580 | 26 | ▶ | $self->_hint($g, $seat), next if $line eq 'hint'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 581 | 25 | ▶ | $self->take_back, next if $line eq 'undo'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 582 | 21 | ▶ | $self->play_again, next if $line eq 'redo'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 583 | 20 | ▶ | if ($line eq 'resign') { $g->resign($seat); last } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||||||||
| 585 | 18 | my $refusal = $g->play($line); | |||||||||||||||||||||||||||||||||
| 586 | 18 | ▶ | if ($refusal) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 595 | 52 | $self->leave_raw; | |||||||||||||||||||||||||||||||||
| 596 | 52 | ▶ | if ($pick) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 597 | 7 | ▶ | $self->clear(defined $notice ? $notice : ()); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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'
$WHY{$r->{"reason"} or ""} or $r->{"reason"}
$r->{'reason'} || ''
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"}
Truth table50%1/2
A
A= $r->{'reason'}
| |||||||||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||||||||
| 693 | 11 | ▶ | if (!defined $r->{winner}) { $self->_say("drawn: $why"); return 0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 694 | 7 | ▶ | if ($r->{winner} eq $seat) { $self->_say("you won: $why"); return 0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 753 | 51 | ▶ | if ($opt{help}) { print _usage(); return 0 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 754 | 39 | ▶ | if (@argv) { print STDERR "chess: not an option: $argv[0]\n", _usage(); return 2 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||
| 756 | 38 | for my $k (qw(white seat)) { | |||||||||||||||||||||||||||||||||
| 757 | 75 | ▶ | next if $opt{$k} eq 'p1' || $opt{$k} eq 'p2'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opt{$k} eq "p1" or $opt{$k} eq "p2"
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"
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 765 | 13 | ▶ | if ($opt{level} < 0) { print STDERR "chess: --level must not be negative\n"; return 2 } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 766 | 12 | ▶ | if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
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]
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 774 | } | ||||||||||||||||||||||||||||||||||
| 775 | |||||||||||||||||||||||||||||||||||
| 776 | 33 | ▶ | if (defined $opt{size} && $opt{size} !~ /\A[123]\z/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $opt{"size"} and not $opt{"size"} =~ /\A[123]\z/u
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||||||||
| 790 | 31 | ▶ | my $seed = defined $opt{seed} ? $opt{seed} : 'chess-terminal-default-seed32'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 795 | |||||||||||||||||||||||||||||||||||
| 796 | 31 | my $game = eval { Term::Chess::Game->new(%new) }; | |||||||||||||||||||||||||||||||||
| 797 | 31 | ▶ | if (!$game) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||