| 1 | package Game::Xiangqi::Terminal; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 6 | ▶ | use Object::Proto::Sugar -types; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 6 | ▶ | use Game::Xiangqi; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 6 | ▶ | use Game::Xiangqi::Engine ':all'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 6 | ▶ | use Game::Xiangqi::Notation; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 6 | ▶ | use Game::Xiangqi::Bot; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 6 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | use Game::Terminal::Kit::History; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use Game::Terminal::Kit::Keys; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use Game::Terminal::Kit::Run; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use Game::Terminal::Kit::Screen; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 6 | ▶ | use Game::Terminal::Kit::Steer; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | my $N = 'Game::Xiangqi::Notation'; | |||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | my %GLYPH = ( | |||||||||||||||||||||||||||||||||||||||||
| 26 | (RED | GENERAL) => "\x{5E25}", | ||||||||||||||||||||||||||||||||||||||||||
| 27 | (RED | ADVISOR) => "\x{4ED5}", | ||||||||||||||||||||||||||||||||||||||||||
| 28 | (RED | ELEPHANT) => "\x{76F8}", | ||||||||||||||||||||||||||||||||||||||||||
| 29 | (RED | CHARIOT) => "\x{4FE5}", | ||||||||||||||||||||||||||||||||||||||||||
| 30 | (RED | HORSE) => "\x{508C}", | ||||||||||||||||||||||||||||||||||||||||||
| 31 | (RED | CANNON) => "\x{70AE}", | ||||||||||||||||||||||||||||||||||||||||||
| 32 | (RED | SOLDIER) => "\x{5175}", | ||||||||||||||||||||||||||||||||||||||||||
| 33 | (BLACK | GENERAL) => "\x{5C07}", | ||||||||||||||||||||||||||||||||||||||||||
| 34 | (BLACK | ADVISOR) => "\x{58EB}", | ||||||||||||||||||||||||||||||||||||||||||
| 35 | (BLACK | ELEPHANT) => "\x{8C61}", | ||||||||||||||||||||||||||||||||||||||||||
| 36 | (BLACK | CHARIOT) => "\x{8ECA}", | ||||||||||||||||||||||||||||||||||||||||||
| 37 | (BLACK | HORSE) => "\x{99AC}", | ||||||||||||||||||||||||||||||||||||||||||
| 38 | (BLACK | CANNON) => "\x{7832}", | ||||||||||||||||||||||||||||||||||||||||||
| 39 | (BLACK | SOLDIER) => "\x{5352}", | ||||||||||||||||||||||||||||||||||||||||||
| 40 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | my %LETTER = ( | |||||||||||||||||||||||||||||||||||||||||
| 43 | GENERAL => 'K', ADVISOR => 'A', ELEPHANT => 'E', CHARIOT => 'R', | ||||||||||||||||||||||||||||||||||||||||||
| 44 | HORSE => 'H', CANNON => 'C', SOLDIER => 'P', | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | my @KIND_NAME = (undef, 'GENERAL', 'ADVISOR', 'ELEPHANT', 'CHARIOT', | |||||||||||||||||||||||||||||||||||||||||
| 47 | 'HORSE', 'CANNON', 'SOLDIER'); | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 6 | my %WORD = ( | |||||||||||||||||||||||||||||||||||||||||
| 50 | GENERAL => 'general', ADVISOR => 'advisor', ELEPHANT => 'elephant', | ||||||||||||||||||||||||||||||||||||||||||
| 51 | CHARIOT => 'chariot', HORSE => 'horse', CANNON => 'cannon', | ||||||||||||||||||||||||||||||||||||||||||
| 52 | SOLDIER => 'soldier', | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 6 | my %COLOUR = (red => '1;31', black => '1;34'); | |||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||
| 57 | 6 | ▶ | use constant { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 58 | 6 | CELL => 2, | |||||||||||||||||||||||||||||||||||||||||
| 59 | GAP => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | RANK_FIRST => 100, | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 6 | }; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 6 | has _in => (is => 'rw', init_arg => 'in', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 6 | has game => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 6 | has wxf => (is => 'rw', default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 6 | has level => (is => 'ro'); | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | 6 | has seat => (is => 'ro', isa => Str, default => 'p1'); | |||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 6 | has _fh => (is => 'rw', init_arg => 'out', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | 6 | has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0); | |||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 6 | has _colour => (is => 'rw', init_arg => 'colour', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 6 | has _keysource => (is => 'rw', isa => Any, init_arg => 'keysource', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 6 | has _pending => (is => 'rw', isa => Any, init_arg => 'pending', private => 1); | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 6 | has keyboard => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 6 | has screen => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 6 | has _picking => (is => 'rw', isa => Any, init_arg => 'picking'); | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 6 | has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind'); | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 6 | has history => (is => 'rw'); | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 77 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=16 SCAR=28.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 77 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 77 | ▶ | $self->wxf($self->wxf ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 77 | ▶ | my $out = $self->_fh || \*STDOUT; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_fh || \*STDOUT
MC/DC100%1/1
$self->_fh || \*STDOUT
$self->_fh
Truth table100%2/2
A
A= $self->_fh
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 77 | ▶ | $self->keyboard(Game::Terminal::Kit::Keys->new( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition75%3/4
$self->_in || \*STDIN
$self->_pending || []
MC/DC100%1/1
$self->_in || \*STDIN
$self->_in
MC/DC0%0/1
$self->_pending || []
$self->_pending
Truth table100%2/2
A
A= $self->_in
Truth table50%1/2
A
A= $self->_pending
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | in => $self->_in || \*STDIN, | ||||||||||||||||||||||||||||||||||||||||||
| 100 | pending => $self->_pending || [], | ||||||||||||||||||||||||||||||||||||||||||
| 101 | ($self->_keysource ? (keysource => $self->_keysource) : ()), | ||||||||||||||||||||||||||||||||||||||||||
| 102 | )) unless $self->keyboard; | ||||||||||||||||||||||||||||||||||||||||||
| 103 | $self->screen(Game::Terminal::Kit::Screen->new( | ||||||||||||||||||||||||||||||||||||||||||
| 104 | out => $out, | ||||||||||||||||||||||||||||||||||||||||||
| 105 | interactive => (defined $self->_rewind ? ($self->_rewind ? 1 : 0) | ||||||||||||||||||||||||||||||||||||||||||
| 106 | 77 | ▶ | : eval { -t $out } ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | colour => Game::Terminal::Kit::Screen::no_colour() ? 0 | ||||||||||||||||||||||||||||||||||||||||||
| 108 | : defined $self->_colour ? ($self->_colour ? 1 : 0) | ||||||||||||||||||||||||||||||||||||||||||
| 109 | : 1, | ||||||||||||||||||||||||||||||||||||||||||
| 110 | ascii => $self->_ascii ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 111 | )) unless $self->screen; | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 77 | $self->screen->utf8; | |||||||||||||||||||||||||||||||||||||||||
| 113 | $self->history(Game::Terminal::Kit::History->new( | ||||||||||||||||||||||||||||||||||||||||||
| 114 | 85 | ▶ | save => sub { $_[0]->to_text }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 9 | ▶ | load => sub { Game::Xiangqi->from_text($_[0]) }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 77 | ▶ | )) unless $self->history; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 77 | return; | |||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 13 | ▶ | sub in { my $s = shift; $s->keyboard->in(shift) if @_; $s->keyboard->in } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
in: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 36 | ▶ | sub out { my $s = shift; if (@_) { $s->screen->out(shift); $s->screen->utf8 } $s->screen->out } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
out: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 122 | 0 | ▶ | sub ascii { my $s = shift; if (@_) { $s->screen->ascii(shift() ? 1 : 0); $s->screen->utf8 } $s->screen->ascii ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ascii: called CC=4 SCAR=17.1
Pod
documented
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 5 | ▶ | sub colour { my $s = shift; $s->screen->colour(shift() ? 1 : 0) if @_; $s->screen->colour ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
colour: called CC=4 SCAR=15.2
Pod
documented
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub keysource { my $s = shift; $s->keyboard->keysource(shift) if @_; $s->keyboard->keysource } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keysource: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub pending { my $s = shift; $s->keyboard->pending(shift) if @_; $s->keyboard->pending } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pending: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | 304 | ▶ | sub _say { my ($self, @lines) = @_; $self->screen->say(@lines); return } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_say: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 27450 | ▶ | sub _cell { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_cell: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 27450 | my ($self, $piece, $spot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 131 | 27450 | ▶ | if (!$piece || $piece == EMPTY) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $piece or $piece == 0
MC/DC50%1/2
not $piece or $piece == 0
not $piece
$piece == 0
Truth table66%2/3
not(A) or B
A= not $pieceB= $piece == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 20323 | ▶ | return $self->ascii ? ($spot ? ' +' : ' .') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | : ($spot ? "\x{FF0B}" : "\x{30FB}"); | ||||||||||||||||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||||||||||||||||
| 135 | 7127 | ▶ | if ($self->ascii) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 5791 | my $l = $LETTER{ $KIND_NAME[ kind_of($piece) ] }; | |||||||||||||||||||||||||||||||||||||||||
| 137 | 5791 | ▶ | $l = lc $l if colour_of($piece) == BLACK; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 138 | 5791 | return " $l"; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | return $self->screen->paint($GLYPH{$piece}, | ||||||||||||||||||||||||||||||||||||||||||
| 141 | 1336 | ▶ | colour_of($piece) == RED ? $COLOUR{red} : $COLOUR{black}); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | 3050 | ▶ | sub _hline { my $self = shift; return $self->ascii ? '-' : "\x{2500}" } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hline: called CC=2 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | 2440 | ▶ | sub _between { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_between: called CC=12 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 2440 | my ($self, $lower) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 148 | 2440 | ▶ | my ($vert, $up, $down) = $self->ascii ? ('|', '/', "\\") | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 149 | : ("\x{2502}", "\x{2571}", "\x{2572}"); | ||||||||||||||||||||||||||||||||||||||||||
| 150 | 2440 | ▶ | my $palace_low = ($lower == 0 || $lower == 7); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$lower == 0 || $lower == 7
MC/DC100%2/2
$lower == 0 || $lower == 7
$lower == 0
$lower == 7
Truth table100%3/3
A || B
A= $lower == 0B= $lower == 7
| |||||||||||||||||||||||||||||||||||||||||||
| 151 | 2440 | ▶ | my $palace_high = ($lower == 1 || $lower == 8); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%3/3
$lower == 1 || $lower == 8
MC/DC100%2/2
$lower == 1 || $lower == 8
$lower == 1
$lower == 8
Truth table100%3/3
A || B
A= $lower == 1B= $lower == 8
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | 2440 | my $row = ''; | |||||||||||||||||||||||||||||||||||||||||
| 154 | 2440 | for my $f (0 .. 8) { | |||||||||||||||||||||||||||||||||||||||||
| 155 | 21960 | $row .= $vert . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 21960 | ▶ | next if $f == 8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 19520 | my $g = ' '; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 19520 | ▶ | if ($palace_low) { $g = $up if $f == 3; $g = $down if $f == 4 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | 19520 | ▶ | if ($palace_high) { $g = $down if $f == 3; $g = $up if $f == 4 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 19520 | $row .= $g; | |||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||
| 162 | 2440 | return $row; | |||||||||||||||||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||
| 165 | 305 | ▶ | sub _river { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_river: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 166 | 305 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 305 | ▶ | my $vert = $self->ascii ? '|' : "\x{2502}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 305 | my $row = $vert . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 169 | 305 | $row .= ' ' x (GAP + (CELL + GAP) * 7 + CELL - CELL); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 305 | $row .= $vert . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 171 | 305 | return $row; | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 3050 | ▶ | sub _marks { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_marks: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 3050 | my ($self, $r, $o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 3050 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 177 | 3050 | my @gap = ($self->_hline) x 10; | |||||||||||||||||||||||||||||||||||||||||
| 178 | 3050 | $gap[0] = $gap[9] = ' '; | |||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | 3050 | ▶ | my $take = $self->ascii ? 'x' : "\x{00D7}"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | 3050 | for my $f (0 .. 8) { | |||||||||||||||||||||||||||||||||||||||||
| 183 | 27450 | my $pt = $E->point_of($f, $r); | |||||||||||||||||||||||||||||||||||||||||
| 184 | 27450 | ▶ | next unless $o->{_take}{$pt}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 185 | 10 | $gap[$f] = $take; | |||||||||||||||||||||||||||||||||||||||||
| 186 | 10 | $gap[$f + 1] = $take; | |||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||
| 188 | 3050 | for my $pair ([ $o->{from}, '(', ')' ], [ $o->{cursor}, '[', ']' ]) { | |||||||||||||||||||||||||||||||||||||||||
| 189 | 6100 | my ($pt, $left, $right) = @$pair; | |||||||||||||||||||||||||||||||||||||||||
| 190 | 6100 | ▶ | next unless defined $pt && $E->rank_of($pt) == $r; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $pt and $E->rank_of($pt) == $r
MC/DC100%2/2
defined $pt and $E->rank_of($pt) == $r
defined $pt
$E->rank_of($pt) == $r
Truth table100%3/3
A and B
A= defined $ptB= $E->rank_of($pt) == $r
| |||||||||||||||||||||||||||||||||||||||||||
| 191 | 336 | my $f = $E->file_of($pt); | |||||||||||||||||||||||||||||||||||||||||
| 192 | 336 | $gap[$f] = $left; | |||||||||||||||||||||||||||||||||||||||||
| 193 | 336 | $gap[$f + 1] = $right; | |||||||||||||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||||||||||||
| 195 | 3050 | return @gap; | |||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 305 | ▶ | sub board_lines { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
board_lines: called CC=15 SCAR=27.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | 305 | my ($self, %o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 200 | 305 | ▶ | my $pos = $o{position} || ($self->game ? $self->game->position : Game::Xiangqi::Engine->new); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->position : 'Game::Xiangqi::Engine'->new)
MC/DC0%0/2
$o{'position'} || ($self->game ? $self->game->position : 'Game::Xiangqi::Engine'->new)
$o{'position'}
($self->game ? $self->game->position : 'Game::Xiangqi::Engine'->new)
Truth table66%2/3
A || B
A= $o{'position'}B= ($self->game ? $self->game->position : 'Game::Xiangqi::Engine'->new)
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | 305 | ▶ | my $flip = $o{flip} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 202 | 305 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | 305 | my (%spot, %take); | |||||||||||||||||||||||||||||||||||||||||
| 205 | 305 | ▶ | for my $pt (@{ $o{spots} || [] }) { | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$o{'spots'} || []
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 651 | ▶ | $pos->at($pt) ? ($take{$pt} = 1) : ($spot{$pt} = 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||||||||||
| 208 | 305 | $o{_take} = \%take; | |||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | 305 | ▶ | my @ranks = $flip ? (0 .. 9) : reverse(0 .. 9); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 305 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||
| 213 | 305 | for my $i (0 .. $#ranks) { | |||||||||||||||||||||||||||||||||||||||||
| 214 | 3050 | my $r = $ranks[$i]; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 3050 | my @gap = $self->_marks($r, \%o); | |||||||||||||||||||||||||||||||||||||||||
| 216 | 3050 | my $row = $gap[0]; | |||||||||||||||||||||||||||||||||||||||||
| 217 | 3050 | for my $f (0 .. 8) { | |||||||||||||||||||||||||||||||||||||||||
| 218 | 27450 | my $pt = $E->point_of($f, $r); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 27450 | $row .= $self->_cell($pos->at($pt), $spot{$pt}); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 27450 | $row .= $gap[$f + 1]; | |||||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||||
| 222 | 3050 | push @lines, sprintf '%d %s', $r, $row; | |||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 3050 | ▶ | next if $i == $#ranks; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 2745 | my $next = $ranks[$i + 1]; | |||||||||||||||||||||||||||||||||||||||||
| 226 | 2745 | ▶ | my $lower = $r < $next ? $r : $next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 2745 | ▶ | push @lines, ' ' . (($lower == 4) ? $self->_river : $self->_between($lower)) . ' '; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | 305 | my $legend = ' '; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 305 | for my $f (0 .. 8) { | |||||||||||||||||||||||||||||||||||||||||
| 232 | 2745 | $legend .= chr(97 + $f) . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 233 | 2745 | ▶ | $legend .= ' ' if $f != 8; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||
| 235 | 305 | push @lines, $legend . ' '; | |||||||||||||||||||||||||||||||||||||||||
| 236 | 305 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | 37 | ▶ | sub draw { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
draw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 37 | my ($self, %o) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 241 | 37 | $self->_say($self->board_lines(%o)); | |||||||||||||||||||||||||||||||||||||||||
| 242 | 37 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 14 | ▶ | sub keys_available { return $_[0]->keyboard->keys_available } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
keys_available: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | 0 | ▶ | sub raw { return $_[0]->keyboard->raw ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
raw: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 7 | ▶ | sub enter_raw { return $_[0]->keyboard->enter_raw ? $_[0] : undef } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
enter_raw: called CC=2 SCAR=7.6
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | 27 | ▶ | sub leave_raw { $_[0]->keyboard->leave_raw; return $_[0] } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
leave_raw: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | 161 | ▶ | sub read_key { return $_[0]->keyboard->read_key } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_key: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | 47 | ▶ | sub picking { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
picking: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 256 | 47 | my ($self, @set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 257 | 47 | ▶ | $self->_picking($set[0] ? 1 : 0) if @set; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 258 | 47 | ▶ | return $self->_picking ? 1 : 0 if defined $self->_picking; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | 14 | return $self->keys_available; | |||||||||||||||||||||||||||||||||||||||||
| 260 | } | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | 161 | ▶ | sub rewind { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
rewind: called CC=6 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 161 | my ($self, @set) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 264 | 161 | ▶ | $self->_rewind($set[0] ? 1 : 0) if @set; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 265 | 161 | ▶ | return $self->_rewind ? 1 : 0 if defined $self->_rewind; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | ▶ | return eval { -t $self->out } ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | 165 | ▶ | sub point_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
point_name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | 165 | my ($self, $pt) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 271 | 165 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 272 | 165 | return chr(97 + $E->file_of($pt)) . $E->rank_of($pt); | |||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | 119 | ▶ | sub piece_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece_name: called CC=4 SCAR=14.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 276 | 119 | my ($self, $piece) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 277 | 119 | ▶ | return 'nothing' unless $piece && Game::Xiangqi::Engine::kind_of($piece); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$piece and Game::Xiangqi::Engine::kind_of($piece)
MC/DC0%0/2
$piece and Game::Xiangqi::Engine::kind_of($piece)
$piece
Game::Xiangqi::Engine::kind_of($piece)
Truth table33%1/3
A and B
A= $pieceB= Game::Xiangqi::Engine::kind_of($piece)
| |||||||||||||||||||||||||||||||||||||||||||
| 278 | 119 | ▶ | my $side = Game::Xiangqi::Engine::colour_of($piece) == RED ? 'red' : 'black'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | 119 | return "$side " . $WORD{ $KIND_NAME[ Game::Xiangqi::Engine::kind_of($piece) ] }; | |||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 46 | ▶ | sub origins { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
origins: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 46 | my ($self, $pos, $flip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 46 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 285 | 46 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 286 | 46 | my @from = grep { !$seen{$_}++ } map { $E->move_from($_) } $pos->legal; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 46 | return $self->_as_drawn(\@from, $flip); | |||||||||||||||||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | 212 | ▶ | sub _as_drawn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_drawn: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 212 | my ($self, $points, $flip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 212 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 293 | 212 | ▶ | my $way = $flip ? -1 : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 294 | 212 | ▶ | return [ sort { $way * ($E->rank_of($b) <=> $E->rank_of($a)) | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$way * ($E->rank_of($b) <=> $E->rank_of($a)) || $E->file_of($a) <=> $E->file_of($b)
MC/DC0%0/2
$way * ($E->rank_of($b) <=> $E->rank_of($a)) || $E->file_of($a) <=> $E->file_of($b)
$way * ($E->rank_of($b) <=> $E->rank_of($a))
$E->file_of($a) <=> $E->file_of($b)
Truth table66%2/3
A || B
A= $way * ($E->rank_of($b) <=> $E->rank_of($a))B= $E->file_of($a) <=> $E->file_of($b)
| |||||||||||||||||||||||||||||||||||||||||||
| 295 | || $E->file_of($a) <=> $E->file_of($b) } @$points ]; | ||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 166 | ▶ | sub targets { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
targets: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 166 | my ($self, $pos, $from, $flip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 166 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 301 | 938 | my @to = map { $E->move_to($_) } | |||||||||||||||||||||||||||||||||||||||||
| 302 | 166 | grep { $E->move_from($_) == $from } $pos->legal; | |||||||||||||||||||||||||||||||||||||||||
| 303 | 166 | return $self->_as_drawn(\@to, $flip); | |||||||||||||||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | 245 | ▶ | sub step { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
step: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | 245 | my ($self, $points, $at, $key, $flip) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 308 | 245 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 309 | 245 | ▶ | return 0 if $key eq 'home'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 310 | 221 | ▶ | return $#$points if $key eq 'end'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | 202 | ▶ | return Game::Terminal::Kit::Steer::ring(scalar @$points, $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"
| |||||||||||||||||||||||||||||||||||||||||||
| 312 | if $key eq 'right' || $key eq 'tab'; | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 96 | ▶ | return Game::Terminal::Kit::Steer::ring(scalar @$points, $at, -1) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$key eq "left" or $key eq "back_tab"
MC/DC50%1/2
$key eq "left" or $key eq "back_tab"
$key eq "left"
$key eq "back_tab"
Truth table66%2/3
A or B
A= $key eq "left"B= $key eq "back_tab"
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | if $key eq 'left' || $key eq 'back_tab'; | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | my @drawn = map { | ||||||||||||||||||||||||||||||||||||||||||
| 317 | 45 | ▶ | [ $E->file_of($_), RANK_FIRST * ($flip ? $E->rank_of($_) : 9 - $E->rank_of($_)) ] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 318 | } @$points; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | 45 | my $next = Game::Terminal::Kit::Steer::nearest( | |||||||||||||||||||||||||||||||||||||||||
| 320 | \@drawn, $at, Game::Terminal::Kit::Steer::way($key)); | ||||||||||||||||||||||||||||||||||||||||||
| 321 | 45 | ▶ | return defined $next ? $next : $at; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | 51 | ▶ | sub move_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
move_line: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 325 | 51 | my ($self, $pos, $mv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 326 | 51 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 327 | 51 | my $to = $E->move_to($mv); | |||||||||||||||||||||||||||||||||||||||||
| 328 | 51 | my $prey = $pos->at($to); | |||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||
| 330 | 51 | ▶ | my $line = $self->point_name($to) . ': ' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$prey and Game::Xiangqi::Engine::kind_of($prey)
MC/DC50%1/2
$prey and Game::Xiangqi::Engine::kind_of($prey)
$prey
Game::Xiangqi::Engine::kind_of($prey)
Truth table66%2/3
A and B
A= $preyB= Game::Xiangqi::Engine::kind_of($prey)
| |||||||||||||||||||||||||||||||||||||||||||
| 331 | . ($prey && Game::Xiangqi::Engine::kind_of($prey) | ||||||||||||||||||||||||||||||||||||||||||
| 332 | ? 'takes the ' . $self->piece_name($prey) | ||||||||||||||||||||||||||||||||||||||||||
| 333 | : 'a quiet move'); | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 51 | ▶ | $line .= $pos->is_mate($mv) ? ', and it is mate' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | : $pos->is_ttc($mv) ? ', and it threatens mate' | ||||||||||||||||||||||||||||||||||||||||||
| 337 | : $pos->is_check($mv) ? ', and it checks' | ||||||||||||||||||||||||||||||||||||||||||
| 338 | : ''; | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 51 | my $mine = $pos->at($E->move_from($mv)); | |||||||||||||||||||||||||||||||||||||||||
| 341 | 51 | ▶ | $line .= ', but it loses the ' . $self->piece_name($mine) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 342 | if $pos->is_sacrifice($mv); | ||||||||||||||||||||||||||||||||||||||||||
| 343 | 51 | ▶ | $line .= ', an even exchange' if $pos->is_exchange($mv); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | 51 | return $line; | |||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | 114 | ▶ | sub piece_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
piece_line: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 348 | 114 | my ($self, $pos, $from) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 349 | 114 | my $n = scalar @{ $self->targets($pos, $from) }; | |||||||||||||||||||||||||||||||||||||||||
| 350 | 114 | ▶ | my $line = sprintf 'the %s on %s, %d %s', | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 351 | $self->piece_name($pos->at($from)), $self->point_name($from), | ||||||||||||||||||||||||||||||||||||||||||
| 352 | $n, ($n == 1 ? 'move' : 'moves'); | ||||||||||||||||||||||||||||||||||||||||||
| 353 | 114 | ▶ | $line .= ', and it is attacked' | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 354 | if $pos->attacked($from, Game::Xiangqi::Engine::other($pos->side)); | ||||||||||||||||||||||||||||||||||||||||||
| 355 | 114 | return $line; | |||||||||||||||||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | 38 | ▶ | sub pick_move { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pick_move: called CC=24 SCAR=36.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 359 | 38 | my ($self, $g, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 360 | 38 | my $pos = $g->position; | |||||||||||||||||||||||||||||||||||||||||
| 361 | 38 | ▶ | my $flip = $seat eq 'p2' ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 362 | 38 | my $from = $self->origins($pos, $flip); | |||||||||||||||||||||||||||||||||||||||||
| 363 | 38 | ▶ | return undef unless @$from; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | 38 | my ($at, $held, $to, $toat, $drawn) = (0, undef, undef, 0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||
| 367 | 38 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 368 | 161 | my %o = (flip => $flip); | |||||||||||||||||||||||||||||||||||||||||
| 369 | 161 | ▶ | if (defined $held) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 370 | 49 | $to = $self->targets($pos, $held, $flip); | |||||||||||||||||||||||||||||||||||||||||
| 371 | 49 | %o = (%o, from => $held, cursor => $to->[$toat], spots => $to); | |||||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||||
| 373 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 374 | 112 | %o = (%o, cursor => $from->[$at]); | |||||||||||||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | 161 | my @lines = $self->board_lines(%o); | |||||||||||||||||||||||||||||||||||||||||
| 378 | 161 | ▶ | push @lines, ' ' . (defined $held | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 379 | ? $self->move_line($pos, $pos->move($held, $to->[$toat])) | ||||||||||||||||||||||||||||||||||||||||||
| 380 | : $self->piece_line($pos, $from->[$at])); | ||||||||||||||||||||||||||||||||||||||||||
| 381 | 161 | ▶ | push @lines, ' ' . (defined $held | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | ? 'arrows to choose, enter or space to play, escape to pick another' | ||||||||||||||||||||||||||||||||||||||||||
| 383 | : 'arrows to choose a piece, enter or space to take it, ? for the keys'); | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | 161 | $self->screen->interactive($self->rewind); | |||||||||||||||||||||||||||||||||||||||||
| 386 | 161 | $self->screen->rewind($drawn); | |||||||||||||||||||||||||||||||||||||||||
| 387 | 161 | $self->_say(@lines); | |||||||||||||||||||||||||||||||||||||||||
| 388 | 161 | $drawn = scalar @lines; | |||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||
| 390 | 161 | my $key = $self->read_key; | |||||||||||||||||||||||||||||||||||||||||
| 391 | 161 | ▶ | return 'quit' unless defined $key; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 392 | 155 | ▶ | return 'quit' if $key eq 'q' || $key eq 'interrupt' || $key eq 'eof'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
$key eq "q" or $key eq "interrupt"
MC/DC33%1/3
$key eq "q" or $key eq "interrupt" or $key eq "eof"
$key eq "q"
$key eq "interrupt"
$key eq "eof"
Truth table50%2/4
A or B or C
A= $key eq "q"B= $key eq "interrupt"C= $key eq "eof"
| |||||||||||||||||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||||||||||||||||
| 394 | 145 | ▶ | 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 " "
| |||||||||||||||||||||||||||||||||||||||||||
| 395 | 26 | ▶ | return $N->iccs_of($pos->move($held, $to->[$toat])) if defined $held; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 396 | 15 | ($held, $toat) = ($from->[$at], 0); | |||||||||||||||||||||||||||||||||||||||||
| 397 | 15 | next; | |||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||
| 399 | 119 | ▶ | if ($key eq 'escape' || $key eq 'backspace') { | ||||||||||||||||||||||||||||||||||||||||
|
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | 1 | $held = undef; | |||||||||||||||||||||||||||||||||||||||||
| 401 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | 118 | ▶ | if ($key =~ /\A(?:up|down|left|right|home|end|tab|back_tab)\z/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | 106 | ▶ | defined $held | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 405 | ? ($toat = $self->step($to, $toat, $key, $flip)) | ||||||||||||||||||||||||||||||||||||||||||
| 406 | : ($at = $self->step($from, $at, $key, $flip)); | ||||||||||||||||||||||||||||||||||||||||||
| 407 | 106 | next; | |||||||||||||||||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||||||||||||||||
| 409 | 12 | ▶ | return 'undo' if $key eq 'u'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 410 | 6 | ▶ | return 'redo' if $key eq 'r'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 411 | 1 | ▶ | if ($key eq '?') { $self->_keys; $drawn = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 412 | 0 | ▶ | if ($key eq 'l') { $self->_legal($g); $drawn = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 413 | 0 | ▶ | if ($key eq 'f') { $self->_say($pos->to_fen); $drawn = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 414 | 0 | ▶ | if ($key eq 'g') { $self->_history($g); $drawn = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 415 | 0 | ▶ | if ($key eq 'h') { $self->_hint($g, $seat); $drawn = 0; next } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 416 | } | ||||||||||||||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | 1 | ▶ | sub _keys { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_keys: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 420 | 1 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 421 | 1 | $self->_say( | |||||||||||||||||||||||||||||||||||||||||
| 422 | 'arrows move the cursor; it only stops where a move is', | ||||||||||||||||||||||||||||||||||||||||||
| 423 | 'enter take the piece under the cursor, then play to the point', | ||||||||||||||||||||||||||||||||||||||||||
| 424 | 'escape put the piece back and choose another', | ||||||||||||||||||||||||||||||||||||||||||
| 425 | 'tab the next one, whichever you are choosing', | ||||||||||||||||||||||||||||||||||||||||||
| 426 | 'h the strongest move this engine can find', | ||||||||||||||||||||||||||||||||||||||||||
| 427 | 'l f g every move you have, the FEN, the moves so far', | ||||||||||||||||||||||||||||||||||||||||||
| 428 | 'u r take your last move back, play it again', | ||||||||||||||||||||||||||||||||||||||||||
| 429 | 'q give up and leave', | ||||||||||||||||||||||||||||||||||||||||||
| 430 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 431 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | 27 | ▶ | sub start { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
start: called CC=36 SCAR=36.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 435 | 27 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | 27 | my $g = $self->game; | |||||||||||||||||||||||||||||||||||||||||
| 438 | 27 | ▶ | if (!$g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 439 | 1 | $g = Game::Xiangqi->new( | |||||||||||||||||||||||||||||||||||||||||
| 440 | seed => substr('xiangqi-terminal-default' . ('.' x 32), 0, 32)); | ||||||||||||||||||||||||||||||||||||||||||
| 441 | 1 | $self->game($g); | |||||||||||||||||||||||||||||||||||||||||
| 442 | } | ||||||||||||||||||||||||||||||||||||||||||
| 443 | 27 | ▶ | return 2 unless $g; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 444 | 27 | my $seat = $self->seat; | |||||||||||||||||||||||||||||||||||||||||
| 445 | 27 | $self->history->start($g); | |||||||||||||||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||||||||||||||||
| 447 | 27 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 448 | 27 | ▶ | $self->picking(0) unless $pick; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 27 | ▶ | $self->_say($pick | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | ? 'xiangqi. arrows to choose a move, enter or space to play it, ? for the keys.' | ||||||||||||||||||||||||||||||||||||||||||
| 451 | : 'xiangqi. a move is ICCS, like h2e2. "help" for the rest.'); | ||||||||||||||||||||||||||||||||||||||||||
| 452 | 27 | ▶ | $self->draw(flip => ($seat eq 'p2')) unless $pick; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | 27 | ▶ | while ($self->game->status eq 'active') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 455 | 69 | $g = $self->game; | |||||||||||||||||||||||||||||||||||||||||
| 456 | 69 | my $bot = $g->bot; | |||||||||||||||||||||||||||||||||||||||||
| 457 | 69 | ▶ | if ($g->turn ne $seat) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 458 | 11 | my $mv = do { local $Game::Xiangqi::Bot::LEVEL = $self->level; | |||||||||||||||||||||||||||||||||||||||||
| 459 | 11 | $bot->choose($g, $g->turn) }; | |||||||||||||||||||||||||||||||||||||||||
| 460 | 11 | ▶ | last unless defined $mv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 461 | 11 | $self->_say($self->_describe($g, $mv, 'the bot plays')); | |||||||||||||||||||||||||||||||||||||||||
| 462 | 11 | my $refusal = $g->play($mv); | |||||||||||||||||||||||||||||||||||||||||
| 463 | 11 | ▶ | if ($refusal) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 464 | 1 | $self->_say('the bot offered a move the game refused: ' | |||||||||||||||||||||||||||||||||||||||||
| 465 | . $refusal->code); | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 1 | last; | |||||||||||||||||||||||||||||||||||||||||
| 467 | } | ||||||||||||||||||||||||||||||||||||||||||
| 468 | 10 | ▶ | $self->draw(flip => ($seat eq 'p2')) unless $pick; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | 10 | next; | |||||||||||||||||||||||||||||||||||||||||
| 470 | } | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | 58 | $self->person_turn; | |||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | 58 | ▶ | if ($pick) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 475 | 24 | my $chosen = $self->pick_move($g, $seat); | |||||||||||||||||||||||||||||||||||||||||
| 476 | 24 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | 17 | ▶ | $self->take_back, next if $chosen eq 'undo'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 478 | 11 | ▶ | $self->play_again, next if $chosen eq 'redo'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 479 | 6 | my $refusal = $g->play($chosen); | |||||||||||||||||||||||||||||||||||||||||
| 480 | 6 | ▶ | $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 481 | if $refusal; | ||||||||||||||||||||||||||||||||||||||||||
| 482 | 6 | next; | |||||||||||||||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||
| 485 | 34 | my $line = $self->keyboard->read_line($self->_prompt($g), $self->out); | |||||||||||||||||||||||||||||||||||||||||
| 486 | 34 | ▶ | last unless defined $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 487 | 34 | ▶ | next unless length $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||
| 489 | 34 | ▶ | 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"
| |||||||||||||||||||||||||||||||||||||||||||
| 490 | 17 | ▶ | $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 "?"
| |||||||||||||||||||||||||||||||||||||||||||
| 491 | 15 | ▶ | $self->_legal($g), next if $line eq 'legal'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 492 | 14 | ▶ | $self->_say($g->position->to_fen), next if $line eq 'fen'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 493 | 13 | ▶ | $self->_history($g), next if $line eq 'log'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 8 | ▶ | $self->draw(flip => ($seat eq 'p2')), next if $line eq 'board'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 495 | 7 | ▶ | $self->_hint($g, $seat), next if $line eq 'hint'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | 7 | ▶ | if ($line eq 'undo' || $line eq 'redo') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$line eq "undo" or $line eq "redo"
MC/DC100%2/2
$line eq "undo" or $line eq "redo"
$line eq "undo"
$line eq "redo"
Truth table100%3/3
A or B
A= $line eq "undo"B= $line eq "redo"
| |||||||||||||||||||||||||||||||||||||||||||
| 497 | 3 | ▶ | $self->draw(flip => ($seat eq 'p2')) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | if $line eq 'undo' ? $self->take_back : $self->play_again; | ||||||||||||||||||||||||||||||||||||||||||
| 499 | 3 | next; | |||||||||||||||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||||||||||||||||
| 502 | 4 | my $refusal = $g->play($line); | |||||||||||||||||||||||||||||||||||||||||
| 503 | 4 | ▶ | if ($refusal) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 504 | 1 | $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')'); | |||||||||||||||||||||||||||||||||||||||||
| 505 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||
| 506 | } | ||||||||||||||||||||||||||||||||||||||||||
| 507 | 3 | $self->draw(flip => ($seat eq 'p2')); | |||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||||||||||||||||
| 510 | 27 | $self->leave_raw; | |||||||||||||||||||||||||||||||||||||||||
| 511 | 27 | ▶ | $self->draw(flip => ($seat eq 'p2')) if $pick; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 512 | 27 | return $self->_verdict($self->game, $seat); | |||||||||||||||||||||||||||||||||||||||||
| 513 | } | ||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | 58 | ▶ | sub person_turn { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
person_turn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 516 | 58 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 517 | 58 | $self->history->turn($self->game); | |||||||||||||||||||||||||||||||||||||||||
| 518 | 58 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 519 | } | ||||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | 8 | ▶ | sub take_back { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
take_back: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 522 | 8 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 8 | my $game = $self->history->undo; | |||||||||||||||||||||||||||||||||||||||||
| 524 | 8 | ▶ | unless ($game) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | 3 | $self->_say('nothing to take back'); | |||||||||||||||||||||||||||||||||||||||||
| 526 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||||||||||||||||
| 528 | 5 | $self->swap($game); | |||||||||||||||||||||||||||||||||||||||||
| 529 | 5 | $self->_say('taken back'); | |||||||||||||||||||||||||||||||||||||||||
| 530 | 5 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | 6 | ▶ | sub play_again { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
play_again: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 534 | 6 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 535 | 6 | my $game = $self->history->redo; | |||||||||||||||||||||||||||||||||||||||||
| 536 | 6 | ▶ | unless ($game) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 537 | 2 | $self->_say('nothing to play again'); | |||||||||||||||||||||||||||||||||||||||||
| 538 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 539 | } | ||||||||||||||||||||||||||||||||||||||||||
| 540 | 4 | $self->swap($game); | |||||||||||||||||||||||||||||||||||||||||
| 541 | 4 | $self->_say('played again'); | |||||||||||||||||||||||||||||||||||||||||
| 542 | 4 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 543 | } | ||||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | 9 | ▶ | sub swap { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
swap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | 9 | my ($self, $game) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 547 | 9 | $self->game($game); | |||||||||||||||||||||||||||||||||||||||||
| 548 | 9 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 549 | } | ||||||||||||||||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||||||||||||||||
| 551 | 34 | ▶ | sub _prompt { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prompt: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 552 | 34 | my ($self, $g) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 553 | 34 | ▶ | return sprintf '%s to move> ', $g->position->side == RED ? 'red' : 'black'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 554 | } | ||||||||||||||||||||||||||||||||||||||||||
| 555 | |||||||||||||||||||||||||||||||||||||||||||
| 556 | 11 | ▶ | sub _describe { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_describe: called CC=4 SCAR=23.2
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | 11 | my ($self, $g, $iccs, $lead) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 558 | 11 | ▶ | return "$lead $iccs" unless $self->wxf; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 559 | 0 | my $mv = $N->move_of_iccs($iccs); | |||||||||||||||||||||||||||||||||||||||||
| 560 | 0 | ▶ | my $wxf = defined $mv ? $N->wxf_of($g->position, $mv) : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 561 | 0 | ▶ | return defined $wxf ? "$lead $iccs ($wxf)" : "$lead $iccs"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | 2 | ▶ | sub _help { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_help: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 566 | 2 | $self->_say( | |||||||||||||||||||||||||||||||||||||||||
| 567 | 'a move is ICCS: the from point then the to point, like h2e2', | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 'board draw it again', | ||||||||||||||||||||||||||||||||||||||||||
| 569 | 'legal every move you have', | ||||||||||||||||||||||||||||||||||||||||||
| 570 | 'fen the position as a FEN', | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 'log the moves so far', | ||||||||||||||||||||||||||||||||||||||||||
| 572 | 'hint the strongest move this engine can find', | ||||||||||||||||||||||||||||||||||||||||||
| 573 | "undo take your last move back, and the bot's reply with it", | ||||||||||||||||||||||||||||||||||||||||||
| 574 | 'redo play again what undo took back', | ||||||||||||||||||||||||||||||||||||||||||
| 575 | 'quit give up and leave', | ||||||||||||||||||||||||||||||||||||||||||
| 576 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 577 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | 1 | ▶ | sub _legal { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_legal: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 581 | 1 | my ($self, $g) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 582 | 1 | my @m = @{ $g->legal }; | |||||||||||||||||||||||||||||||||||||||||
| 583 | 1 | $self->_say(scalar(@m) . ' moves: ' . join ' ', @m); | |||||||||||||||||||||||||||||||||||||||||
| 584 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 585 | } | ||||||||||||||||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||||||||||||||||
| 587 | 5 | ▶ | sub _history { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_history: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 588 | 5 | my ($self, $g) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 589 | 5 | my @l = @{ $g->log }; | |||||||||||||||||||||||||||||||||||||||||
| 590 | 5 | ▶ | $self->_say(@l ? "@l" : 'no moves yet'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 591 | 5 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 592 | } | ||||||||||||||||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||||||||||||||||
| 594 | 0 | ▶ | sub _hint { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_hint: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 595 | 0 | my ($self, $g, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 596 | 0 | my $mv = $g->bot->hint($g, $seat); | |||||||||||||||||||||||||||||||||||||||||
| 597 | 0 | ▶ | $self->_say(defined $mv ? $self->_describe($g, $mv, 'try') : 'nothing to suggest'); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 598 | 0 | return $self; | |||||||||||||||||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||||||||||||||||
| 601 | 27 | ▶ | sub _verdict { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_verdict: called CC=6 SCAR=20.2
| |||||||||||||||||||||||||||||||||||||||||||
| 602 | 27 | my ($self, $g, $seat) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 603 | 27 | ▶ | return 0 if $g->status eq 'active'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 604 | 2 | my $r = $g->result; | |||||||||||||||||||||||||||||||||||||||||
| 605 | 2 | ▶ | my $why = $r->{reason} || 'over'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$r->{'reason'} || 'over'
MC/DC0%0/1
$r->{'reason'} || 'over'
$r->{'reason'}
Truth table50%1/2
A
A= $r->{'reason'}
| |||||||||||||||||||||||||||||||||||||||||||
| 606 | 2 | ▶ | $why .= " (rule $r->{rule})" if $r->{rule}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | 0 | ▶ | if (!defined $r->{winner}) { $self->_say("drawn: $why"); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 609 | 0 | ▶ | if ($r->{winner} eq $seat) { $self->_say("you won: $why"); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 610 | 2 | $self->_say("you lost: $why"); | |||||||||||||||||||||||||||||||||||||||||
| 611 | 2 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 612 | } | ||||||||||||||||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||||||||||||||||
| 614 | 11 | ▶ | sub ucci { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ucci: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 615 | 11 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 616 | 11 | my $fh = $self->in; | |||||||||||||||||||||||||||||||||||||||||
| 617 | 11 | my $pos = Game::Xiangqi::Engine->new; | |||||||||||||||||||||||||||||||||||||||||
| 618 | 11 | my @played; | |||||||||||||||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||||||||||||||||
| 620 | 11 | ▶ | while (defined(my $line = <$fh>)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 621 | 41 | $line =~ s/\A\s+|\s+\z//g; | |||||||||||||||||||||||||||||||||||||||||
| 622 | 41 | ▶ | next unless length $line; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 623 | 39 | my @w = split /\s+/, $line; | |||||||||||||||||||||||||||||||||||||||||
| 624 | 39 | my $cmd = shift @w; | |||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | 39 | ▶ | if ($cmd eq 'ucci') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 627 | 4 | $self->_say('id name Game::Xiangqi ' . $Game::Xiangqi::VERSION); | |||||||||||||||||||||||||||||||||||||||||
| 628 | 4 | $self->_say('id author Game::Xiangqi'); | |||||||||||||||||||||||||||||||||||||||||
| 629 | 4 | $self->_say('option usemillisec type check default false'); | |||||||||||||||||||||||||||||||||||||||||
| 630 | 4 | $self->_say('ucciok'); | |||||||||||||||||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||||||||||||||||
| 632 | 3 | elsif ($cmd eq 'isready') { $self->_say('readyok') } | |||||||||||||||||||||||||||||||||||||||||
| 633 | 11 | elsif ($cmd eq 'quit') { $self->_say('bye'); last } | |||||||||||||||||||||||||||||||||||||||||
| 634 | elsif ($cmd eq 'stop') { } | ||||||||||||||||||||||||||||||||||||||||||
| 635 | elsif ($cmd eq 'position') { | ||||||||||||||||||||||||||||||||||||||||||
| 636 | 8 | ($pos, @played) = $self->_ucci_position(@w); | |||||||||||||||||||||||||||||||||||||||||
| 637 | 8 | ▶ | $self->_say('info string position refused') unless $pos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 638 | 8 | ▶ | $pos ||= Game::Xiangqi::Engine->new; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$pos ||= "Game::Xiangqi::Engine"->new
MC/DC0%0/2
$pos ||= "Game::Xiangqi::Engine"->new
$pos
"Game::Xiangqi::Engine"->new
Truth table66%2/3
A ||= B
A= $posB= "Game::Xiangqi::Engine"->new
| |||||||||||||||||||||||||||||||||||||||||||
| 639 | } | ||||||||||||||||||||||||||||||||||||||||||
| 640 | elsif ($cmd eq 'go') { | ||||||||||||||||||||||||||||||||||||||||||
| 641 | 9 | ▶ | $self->_say('bestmove ' . ($self->_ucci_go($pos, @w) || 'nobestmove')); | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$self->_ucci_go($pos, @w) || 'nobestmove'
MC/DC100%1/1
$self->_ucci_go($pos, @w) || 'nobestmove'
$self->_ucci_go($pos, @w)
Truth table100%2/2
A
A= $self->_ucci_go($pos, @w)
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | } | ||||||||||||||||||||||||||||||||||||||||||
| 643 | } | ||||||||||||||||||||||||||||||||||||||||||
| 644 | 11 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 645 | } | ||||||||||||||||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||||||||||||||||
| 647 | 8 | ▶ | sub _ucci_position { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ucci_position: called CC=13 SCAR=26.5
| |||||||||||||||||||||||||||||||||||||||||||
| 648 | 8 | my ($self, @w) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 649 | 8 | my $E = 'Game::Xiangqi::Engine'; | |||||||||||||||||||||||||||||||||||||||||
| 650 | 8 | my ($pos, @moves); | |||||||||||||||||||||||||||||||||||||||||
| 651 | |||||||||||||||||||||||||||||||||||||||||||
| 652 | 8 | ▶ | if (@w && $w[0] eq 'startpos') { shift @w; $pos = $E->new } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
@w and $w[0] eq "startpos"
MC/DC50%1/2
@w and $w[0] eq "startpos"
@w
$w[0] eq "startpos"
Truth table66%2/3
A and B
A= @wB= $w[0] eq "startpos"
| |||||||||||||||||||||||||||||||||||||||||||
| 653 | ▶ | elsif (@w && $w[0] eq 'fen') { | |||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
@w and $w[0] eq "fen"
MC/DC0%0/2
@w and $w[0] eq "fen"
@w
$w[0] eq "fen"
Truth table33%1/3
A and B
A= @wB= $w[0] eq "fen"
| |||||||||||||||||||||||||||||||||||||||||||
| 654 | 4 | shift @w; | |||||||||||||||||||||||||||||||||||||||||
| 655 | 4 | my @fen; | |||||||||||||||||||||||||||||||||||||||||
| 656 | 4 | ▶ | push @fen, shift @w while @w && $w[0] ne 'moves'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@w and $w[0] ne "moves"
MC/DC50%1/2
@w and $w[0] ne "moves"
@w
$w[0] ne "moves"
Truth table66%2/3
A and B
A= @wB= $w[0] ne "moves"
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | 4 | ($pos) = $E->of_fen(join ' ', @fen); | |||||||||||||||||||||||||||||||||||||||||
| 658 | 4 | ▶ | return (undef) unless $pos; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 659 | } | ||||||||||||||||||||||||||||||||||||||||||
| 660 | 0 | else { return (undef) } | |||||||||||||||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||||||||||||||||
| 662 | 7 | ▶ | if (@w && $w[0] eq 'moves') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@w and $w[0] eq "moves"
MC/DC50%1/2
@w and $w[0] eq "moves"
@w
$w[0] eq "moves"
Truth table66%2/3
A and B
A= @wB= $w[0] eq "moves"
| |||||||||||||||||||||||||||||||||||||||||||
| 663 | 1 | shift @w; | |||||||||||||||||||||||||||||||||||||||||
| 664 | 1 | for my $iccs (@w) { | |||||||||||||||||||||||||||||||||||||||||
| 665 | 2 | my $mv = $N->move_of_iccs($iccs); | |||||||||||||||||||||||||||||||||||||||||
| 666 | 2 | ▶ | return (undef) unless defined $mv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 667 | 2 | ▶ | return (undef) unless grep { $_ == $mv } $pos->legal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 668 | 2 | $pos->do_move($mv); | |||||||||||||||||||||||||||||||||||||||||
| 669 | 2 | push @moves, $iccs; | |||||||||||||||||||||||||||||||||||||||||
| 670 | } | ||||||||||||||||||||||||||||||||||||||||||
| 671 | } | ||||||||||||||||||||||||||||||||||||||||||
| 672 | 7 | return ($pos, @moves); | |||||||||||||||||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||||||||||||||||
| 675 | 9 | ▶ | sub _ucci_go { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ucci_go: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 676 | 9 | my ($self, $pos, @w) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 677 | 9 | my %arg; | |||||||||||||||||||||||||||||||||||||||||
| 678 | 9 | ▶ | while (@w >= 2) { my $k = shift @w; $arg{$k} = shift @w } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||||||||||||||||
| 680 | 9 | my $mv; | |||||||||||||||||||||||||||||||||||||||||
| 681 | 9 | ▶ | if (defined $arg{depth}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 682 | 1 | ($mv) = $pos->search_to_depth($arg{depth}, 200_000_000, 0); | |||||||||||||||||||||||||||||||||||||||||
| 683 | } | ||||||||||||||||||||||||||||||||||||||||||
| 684 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 685 | 8 | ▶ | my $nodes = $arg{nodes} || 100_000; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg{'nodes'} || 100000
MC/DC100%1/1
$arg{'nodes'} || 100000
$arg{'nodes'}
Truth table100%2/2
A
A= $arg{'nodes'}
| |||||||||||||||||||||||||||||||||||||||||||
| 686 | 8 | ($mv) = $pos->search($nodes, 0); | |||||||||||||||||||||||||||||||||||||||||
| 687 | } | ||||||||||||||||||||||||||||||||||||||||||
| 688 | 9 | ▶ | return undef unless $mv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 689 | 8 | return $N->iccs_of($mv); | |||||||||||||||||||||||||||||||||||||||||
| 690 | } | ||||||||||||||||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||||||||||||||||
| 692 | 14 | ▶ | sub _usage { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_usage: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 693 | 14 | return <<'USAGE'; | |||||||||||||||||||||||||||||||||||||||||
| 694 | xiangqi - Chinese chess on a terminal | ||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | usage: xiangqi [options] | ||||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||
| 698 | --level N node budget for the bot, or a rung index into the ladder | ||||||||||||||||||||||||||||||||||||||||||
| 699 | --red SEAT p1 or p2; whoever is Red moves first | ||||||||||||||||||||||||||||||||||||||||||
| 700 | --seat SEAT which seat you play (default p1) | ||||||||||||||||||||||||||||||||||||||||||
| 701 | --seed STR up to thirty-two bytes, for a reproducible game | ||||||||||||||||||||||||||||||||||||||||||
| 702 | --fen FEN start from a position instead of the opening | ||||||||||||||||||||||||||||||||||||||||||
| 703 | --moves LIST replay these ICCS moves first, comma or space separated | ||||||||||||||||||||||||||||||||||||||||||
| 704 | --wxf show WXF notation beside the coordinates | ||||||||||||||||||||||||||||||||||||||||||
| 705 | --ascii Latin letters instead of the characters | ||||||||||||||||||||||||||||||||||||||||||
| 706 | --ansi colour; --no-ansi turns it off (NO_COLOR beats both) | ||||||||||||||||||||||||||||||||||||||||||
| 707 | --pick choose a move with the arrow keys; --no-pick to type it | ||||||||||||||||||||||||||||||||||||||||||
| 708 | --ucci speak UCCI on stdin and stdout, and draw nothing | ||||||||||||||||||||||||||||||||||||||||||
| 709 | --help this | ||||||||||||||||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||||||||||||||||
| 711 | exit status: | ||||||||||||||||||||||||||||||||||||||||||
| 712 | 0 you won, or the game drew, or you quit | ||||||||||||||||||||||||||||||||||||||||||
| 713 | 1 you lost | ||||||||||||||||||||||||||||||||||||||||||
| 714 | 2 a bad option | ||||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | With a terminal and Term::ReadKey the arrow keys walk the pieces that have a | ||||||||||||||||||||||||||||||||||||||||||
| 717 | move and then that piece's points, with the caption saying what each one does. | ||||||||||||||||||||||||||||||||||||||||||
| 718 | Without either, or with --no-pick, a move is typed as ICCS: the from point then | ||||||||||||||||||||||||||||||||||||||||||
| 719 | the to point, like h2e2, and "help" lists the rest. | ||||||||||||||||||||||||||||||||||||||||||
| 720 | |||||||||||||||||||||||||||||||||||||||||||
| 721 | The engine is bounded in NODES and never in seconds, so the same position | ||||||||||||||||||||||||||||||||||||||||||
| 722 | answers the same on a busy machine as on an idle one. | ||||||||||||||||||||||||||||||||||||||||||
| 723 | USAGE | ||||||||||||||||||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||||||||||||||||
| 726 | 2 | ▶ | sub options { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 727 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 728 | 2 | { name => 'level', type => 'int', min => 0, max => 3, help => 'a rung of the ladder, 0 the weakest' }, | |||||||||||||||||||||||||||||||||||||||||
| 729 | { name => 'red', type => 'enum', choices => [qw(p1 p2)], help => 'whoever is Red moves first' }, | ||||||||||||||||||||||||||||||||||||||||||
| 730 | { name => 'seat', type => 'enum', choices => [qw(p1 p2)], help => 'which seat you play' }, | ||||||||||||||||||||||||||||||||||||||||||
| 731 | { name => 'wxf', type => 'flag', help => 'show WXF notation beside the coordinates' }, | ||||||||||||||||||||||||||||||||||||||||||
| 732 | { name => 'seed', type => 'str', help => 'up to thirty-two bytes, for a reproducible game' }, | ||||||||||||||||||||||||||||||||||||||||||
| 733 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 734 | } | ||||||||||||||||||||||||||||||||||||||||||
| 735 | |||||||||||||||||||||||||||||||||||||||||||
| 736 | 36 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=21 SCAR=30.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 737 | 36 | my ($class, @argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 738 | 36 | my %opt = (seat => 'p1', red => 'p1'); | |||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | 36 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||
| 741 | 36 | ▶ | local $SIG{__WARN__} = sub { print STDERR "xiangqi: $_[0]" }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 742 | 36 | Getopt::Long::GetOptionsFromArray(\@argv, \%opt, | |||||||||||||||||||||||||||||||||||||||||
| 743 | 'level=i', 'red=s', 'seat=s', 'seed=s', 'fen=s', 'moves=s', | ||||||||||||||||||||||||||||||||||||||||||
| 744 | 'wxf', 'ascii', 'ansi!', 'pick!', 'ucci', 'help'); | ||||||||||||||||||||||||||||||||||||||||||
| 745 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 746 | 36 | ▶ | if (!$ok) { print STDERR _usage(); return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 747 | 33 | ▶ | if ($opt{help}) { print _usage(); return 0 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 748 | 23 | ▶ | if (@argv) { print STDERR "xiangqi: not an option: $argv[0]\n", _usage(); return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||
| 750 | 22 | for my $k (qw(red seat)) { | |||||||||||||||||||||||||||||||||||||||||
| 751 | 43 | ▶ | next if $opt{$k} eq 'p1' || $opt{$k} eq 'p2'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$opt{$k} eq "p1" or $opt{$k} eq "p2"
MC/DC50%1/2
$opt{$k} eq "p1" or $opt{$k} eq "p2"
$opt{$k} eq "p1"
$opt{$k} eq "p2"
Truth table66%2/3
A or B
A= $opt{$k} eq "p1"B= $opt{$k} eq "p2"
| |||||||||||||||||||||||||||||||||||||||||||
| 752 | 2 | print STDERR "xiangqi: --$k takes p1 or p2, not '$opt{$k}'\n"; | |||||||||||||||||||||||||||||||||||||||||
| 753 | 2 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 754 | } | ||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||
| 756 | 20 | my @ladder = @Game::Xiangqi::Bot::LADDER; | |||||||||||||||||||||||||||||||||||||||||
| 757 | 20 | my $level; | |||||||||||||||||||||||||||||||||||||||||
| 758 | 20 | ▶ | if (defined $opt{level}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | 7 | ▶ | if ($opt{level} < 0) { print STDERR "xiangqi: --level must not be negative\n"; return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 760 | 6 | ▶ | 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]
| |||||||||||||||||||||||||||||||||||||||||||
| 761 | printf STDERR | ||||||||||||||||||||||||||||||||||||||||||
| 762 | "xiangqi: --level %d is neither a rung nor a budget: a rung is 0 to %d, " | ||||||||||||||||||||||||||||||||||||||||||
| 763 | . "and a budget is %d nodes or more\n", | ||||||||||||||||||||||||||||||||||||||||||
| 764 | 2 | $opt{level}, $#ladder, $ladder[0]; | |||||||||||||||||||||||||||||||||||||||||
| 765 | 2 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 766 | } | ||||||||||||||||||||||||||||||||||||||||||
| 767 | 4 | ▶ | $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 768 | } | ||||||||||||||||||||||||||||||||||||||||||
| 769 | |||||||||||||||||||||||||||||||||||||||||||
| 770 | my $terminal = $class->new( | ||||||||||||||||||||||||||||||||||||||||||
| 771 | ascii => $opt{ascii}, | ||||||||||||||||||||||||||||||||||||||||||
| 772 | wxf => $opt{wxf}, | ||||||||||||||||||||||||||||||||||||||||||
| 773 | colour => defined $opt{ansi} ? $opt{ansi} : undef, | ||||||||||||||||||||||||||||||||||||||||||
| 774 | picking => $opt{pick}, | ||||||||||||||||||||||||||||||||||||||||||
| 775 | level => $level, | ||||||||||||||||||||||||||||||||||||||||||
| 776 | seat => $opt{seat}, | ||||||||||||||||||||||||||||||||||||||||||
| 777 | 17 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 778 | |||||||||||||||||||||||||||||||||||||||||||
| 779 | 17 | ▶ | return $terminal->ucci if $opt{ucci}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 780 | |||||||||||||||||||||||||||||||||||||||||||
| 781 | 16 | ▶ | my $seed = defined $opt{seed} ? $opt{seed} : 'xiangqi-terminal-default-seed32'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 782 | 16 | $seed = substr($seed . ('.' x 32), 0, 32); | |||||||||||||||||||||||||||||||||||||||||
| 783 | |||||||||||||||||||||||||||||||||||||||||||
| 784 | 16 | my %new = (seed => $seed, red => $opt{red}); | |||||||||||||||||||||||||||||||||||||||||
| 785 | 16 | ▶ | if (defined $opt{fen}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 786 | 2 | my ($pos, $err) = Game::Xiangqi::Engine->of_fen($opt{fen}); | |||||||||||||||||||||||||||||||||||||||||
| 787 | 2 | ▶ | if (!$pos) { print STDERR "xiangqi: --fen refused, code $err\n"; return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 788 | 1 | $new{position} = $pos; | |||||||||||||||||||||||||||||||||||||||||
| 789 | } | ||||||||||||||||||||||||||||||||||||||||||
| 790 | |||||||||||||||||||||||||||||||||||||||||||
| 791 | 15 | my $game = eval { Game::Xiangqi->new(%new) }; | |||||||||||||||||||||||||||||||||||||||||
| 792 | 0 | ▶ | if (!$game) { print STDERR "xiangqi: could not start a game\n"; return 2 } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 793 | |||||||||||||||||||||||||||||||||||||||||||
| 794 | 15 | ▶ | if (defined $opt{moves}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 795 | 2 | for my $mv (grep { length } split /[\s,]+/, $opt{moves}) { | |||||||||||||||||||||||||||||||||||||||||
| 796 | 4 | my $refusal = $game->play($mv); | |||||||||||||||||||||||||||||||||||||||||
| 797 | 4 | ▶ | next unless $refusal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 798 | 1 | print STDERR "xiangqi: --moves refused at '$mv': ", $refusal->code, "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 799 | 1 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 800 | } | ||||||||||||||||||||||||||||||||||||||||||
| 801 | } | ||||||||||||||||||||||||||||||||||||||||||
| 802 | |||||||||||||||||||||||||||||||||||||||||||
| 803 | 14 | $terminal->game($game); | |||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||
| 805 | return Game::Terminal::Kit::Run::guard( | ||||||||||||||||||||||||||||||||||||||||||
| 806 | 14 | ▶ | sub { $terminal->start }, $terminal->keyboard, $terminal->screen); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 807 | } | ||||||||||||||||||||||||||||||||||||||||||
| 808 | |||||||||||||||||||||||||||||||||||||||||||
| 809 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 810 | |||||||||||||||||||||||||||||||||||||||||||
| 811 | __END__ | ||||||||||||||||||||||||||||||||||||||||||