blib/lib/Game/Xiangqi/Terminal.pm

filter: Statementstmt 89.1% 502 / 563 Branchbran 76.9% 277 / 360 Conditioncond 72.8% 75 / 103 MC/DCmcdc 50.0% 32 / 64 Subroutinesub 93.4% 57 / 61 Podpod 96.0% 24 / 25 totaltotal 82.2% 967 / 1176 CC 233 SCAR 28.0
CC
233
Cov
83%
pick_move
62.7
start
30.6
run
23.3
CRAP 16.5
↑ Summary blib/lib/Game/Xiangqi.pm »
1 package Game::Xiangqi::Terminal;
2  
3 3 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use Game::Xiangqi;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Game::Xiangqi::Engine ':all';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use Game::Xiangqi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Game::Xiangqi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 our $VERSION = '0.04';
17  
18 3 my $N = 'Game::Xiangqi::Notation';
19  
20 3 my %GLYPH = (
21 (RED | GENERAL) => "\x{5E25}",
22 (RED | ADVISOR) => "\x{4ED5}",
23 (RED | ELEPHANT) => "\x{76F8}",
24 (RED | CHARIOT) => "\x{4FE5}",
25 (RED | HORSE) => "\x{508C}",
26 (RED | CANNON) => "\x{70AE}",
27 (RED | SOLDIER) => "\x{5175}",
28 (BLACK | GENERAL) => "\x{5C07}",
29 (BLACK | ADVISOR) => "\x{58EB}",
30 (BLACK | ELEPHANT) => "\x{8C61}",
31 (BLACK | CHARIOT) => "\x{8ECA}",
32 (BLACK | HORSE) => "\x{99AC}",
33 (BLACK | CANNON) => "\x{7832}",
34 (BLACK | SOLDIER) => "\x{5352}",
35 );
36  
37 3 my %LETTER = (
38 GENERAL => 'K', ADVISOR => 'A', ELEPHANT => 'E', CHARIOT => 'R',
39 HORSE => 'H', CANNON => 'C', SOLDIER => 'P',
40 );
41 3 my @KIND_NAME = (undef, 'GENERAL', 'ADVISOR', 'ELEPHANT', 'CHARIOT',
42 'HORSE', 'CANNON', 'SOLDIER');
43  
44 3 my %WORD = (
45 GENERAL => 'general', ADVISOR => 'advisor', ELEPHANT => 'elephant',
46 CHARIOT => 'chariot', HORSE => 'horse', CANNON => 'cannon',
47 SOLDIER => 'soldier',
48 );
49  
50 3 our %SEQUENCE = (
51 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
52 'H' => 'home', 'F' => 'end',
53 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
54 '7~' => 'home', '8~' => 'end',
55 );
56  
57 3 our %CONTROL = (
58 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
59 "\x7f" => 'backspace', "\x08" => 'backspace',
60 "\x03" => 'interrupt', "\x04" => 'eof',
61 );
62  
63 3 my %COLOUR = (red => "\e[1;31m", black => "\e[1;34m", off => "\e[0m");
64  
65 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
66 3 CELL => 2,
67 GAP => 1,
68 3 };
69  
70 45 ▶has in => (is => 'rw', default => sub { \*STDIN });
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 3 has game => (is => 'rw');
73  
74 3 has wxf => (is => 'rw', default => 0);
75  
76 3 has level => (is => 'ro');
77  
78 3 has seat => (is => 'ro', isa => Str, default => 'p1');
79  
80 33 ▶has _fh => (is => 'rw', init_arg => 'out', private => 1, default => sub { \*STDOUT });
Subroutine __ANON__: called CC=1 SCAR=0.0
81  
82 3 has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0);
83  
84 3 has _colour => (is => 'rw', init_arg => 'colour', private => 1);
85  
86 3 has raw => (is => 'rw', isa => Any, default => 0);
87  
88 3 has keysource => (is => 'rw', isa => Any);
89  
90 60 ▶has pending => (is => 'rw', isa => ArrayRef, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
91  
92 3 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
93  
94 3 has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind');
95  
96 60 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
97 60 my ($self) = @_;
98  
99 60 ▶ $self->wxf($self->wxf ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->wxf ? : 0 60 60
100 60 ▶ $self->_ascii($self->_ascii ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 16 44 60
101 $self->_colour(
102 60 ▶ exists $ENV{NO_COLOR} ? 0
Branch: 3 branches
BranchTrueFalseTotal
exists $ENV{'NO_COLOR'} ? : 3 57 60
defined $self->_colour ? : 31 26 57
$self->_colour ? : 4 27 31
103 : defined $self->_colour ? ($self->_colour ? 1 : 0)
104 : 1
105 );
106 60 $self->_encode_out;
107 60 return;
108 }
109  
110 61 ▶sub _encode_out {
Subroutine _encode_out: called CC=4 SCAR=13.9
111 61 my ($self) = @_;
112 61 ▶ return $self if $self->_ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ascii 16 45 61
113 45 my $fh = $self->_fh;
114 45 ▶ return $self unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 45 45
115 45 ▶ return $self if grep { /encoding|utf8/i } PerlIO::get_layers($fh);
Branch: 1 branch
BranchTrueFalseTotal
if grep {/encoding|utf8/i;} &PerlIO::get_layers($fh) 12 33 45
116 33 eval { binmode $fh, ':encoding(UTF-8)'; 1 };
117 33 return $self;
118 }
119  
120 2 ▶sub out { my $s = shift; if (@_) { $s->_fh(shift); $s->_encode_out } $s->_fh }
Subroutine out: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
121 0 ▶sub ascii { my $s = shift; if (@_) { $s->_ascii(shift() ? 1 : 0); $s->_encode_out } $s->_ascii }
Subroutine ascii: not called CC=3 SCAR=24.8
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if (@_) 0 0 0
shift() ? : 0 0 0
122 5 ▶sub colour { my $s = shift; $s->_colour(shift() ? 1 : 0) if @_; $s->_colour }
Subroutine colour: called CC=3 SCAR=13.1
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
if @_ 0 5 5
shift() ? : 0 0 0
123  
124 1149 ▶sub _say { my ($self, @t) = @_; my $fh = $self->_fh; print {$fh} @t, "\n" }
Subroutine _say: called CC=1 SCAR=0.0
125 25 ▶sub _out { my ($self, @t) = @_; my $fh = $self->_fh; print {$fh} @t }
Subroutine _out: called CC=1 SCAR=0.0
126  
127 7740 ▶sub _cell {
Subroutine _cell: called CC=10 SCAR=23.0
128 7740 my ($self, $piece, $spot) = @_;
129 7740 ▶ if (!$piece || $piece == EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $piece or $piece == 0) 5504 2236 7740
Condition66%2/3
not $piece or $piece == 0
l!l&&r!l&&!r
550402236
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
ABresult
1X1
011
000
130 5504 ▶ return $self->_ascii ? ($spot ? ' +' : ' .')
Branch: 3 branches
BranchTrueFalseTotal
$self->_ascii ? : 2000 3504 5504
$spot ? : 18 1982 2000
$spot ? : 6 3498 3504
131 : ($spot ? "\x{FF0B}" : "\x{30FB}");
132 }
133 2236 ▶ if ($self->_ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_ascii) 1060 1176 2236
134 1060 my $l = $LETTER{ $KIND_NAME[ kind_of($piece) ] };
135 1060 ▶ $l = lc $l if colour_of($piece) == BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if colour_of($piece) == 16 530 530 1060
136 1060 return " $l";
137 }
138 1176 my $g = $GLYPH{$piece};
139 1176 ▶ return $g unless $self->_colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_colour 531 645 1176
140 645 ▶ my $c = colour_of($piece) == RED ? $COLOUR{red} : $COLOUR{black};
Branch: 1 branch
BranchTrueFalseTotal
colour_of($piece) == 8 ? : 321 324 645
141 645 return $c . $g . $COLOUR{off};
142 }
143  
144 860 ▶sub _hline { my $self = shift; return $self->_ascii ? '-' : "\x{2500}" }
Subroutine _hline: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 340 520 860
145  
146 688 ▶sub _between {
Subroutine _between: called CC=12 SCAR=24.8
147 688 my ($self, $lower) = @_;
148 688 ▶ my ($vert, $up, $down) = $self->_ascii ? ('|', '/', "\\")
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 272 416 688
149 : ("\x{2502}", "\x{2571}", "\x{2572}");
150 688 ▶ my $palace_low = ($lower == 0 || $lower == 7);
Condition100%3/3
$lower == 0 || $lower == 7
l!l&&r!l&&!r
8686516
MC/DC100%2/2
$lower == 0 || $lower == 7
$lower == 0 $lower == 7
Truth table100%3/3
A || B
A= $lower == 0B= $lower == 7
ABresult
1X1
011
000
151 688 ▶ my $palace_high = ($lower == 1 || $lower == 8);
Condition100%3/3
$lower == 1 || $lower == 8
l!l&&r!l&&!r
8686516
MC/DC100%2/2
$lower == 1 || $lower == 8
$lower == 1 $lower == 8
Truth table100%3/3
A || B
A= $lower == 1B= $lower == 8
ABresult
1X1
011
000
152  
153 688 my $row = '';
154 688 for my $f (0 .. 8) {
155 6192 $row .= $vert . ' ';
156 6192 ▶ next if $f == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $f == 8 688 5504 6192
157 5504 my $g = ' ';
158 5504 ▶ if ($palace_low) { $g = $up if $f == 3; $g = $down if $f == 4 }
Branch: 3 branches
BranchTrueFalseTotal
if ($palace_low) 1376 4128 5504
if $f == 3 172 1204 1376
if $f == 4 172 1204 1376
159 5504 ▶ if ($palace_high) { $g = $down if $f == 3; $g = $up if $f == 4 }
Branch: 3 branches
BranchTrueFalseTotal
if ($palace_high) 1376 4128 5504
if $f == 3 172 1204 1376
if $f == 4 172 1204 1376
160 5504 $row .= $g;
161 }
162 688 return $row;
163 }
164  
165 86 ▶sub _river {
Subroutine _river: called CC=2 SCAR=6.9
166 86 my ($self) = @_;
167 86 ▶ my $vert = $self->_ascii ? '|' : "\x{2502}";
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 34 52 86
168 86 my $row = $vert . ' ';
169 86 $row .= ' ' x (GAP + (CELL + GAP) * 7 + CELL - CELL);
170 86 $row .= $vert . ' ';
171 86 return $row;
172 }
173  
174 860 ▶sub _marks {
Subroutine _marks: called CC=7 SCAR=19.5
175 860 my ($self, $r, $o) = @_;
176 860 my $E = 'Game::Xiangqi::Engine';
177 860 my @gap = ($self->_hline) x 10;
178 860 $gap[0] = $gap[9] = ' ';
179  
180 860 ▶ my $take = $self->_ascii ? 'x' : "\x{00D7}";
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 340 520 860
181  
182 860 for my $f (0 .. 8) {
183 7740 my $pt = $E->point_of($f, $r);
184 7740 ▶ next unless $o->{_take}{$pt};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"_take"}{$pt} 7730 10 7740
185 10 $gap[$f] = $take;
186 10 $gap[$f + 1] = $take;
187 }
188 860 for my $pair ([ $o->{from}, '(', ')' ], [ $o->{cursor}, '[', ']' ]) {
189 1720 my ($pt, $left, $right) = @$pair;
190 1720 ▶ next unless defined $pt && $E->rank_of($pt) == $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pt and $E->rank_of($pt) == $r 1667 53 1720
Condition100%3/3
defined $pt and $E->rank_of($pt) == $r
!ll&&!rl&&r
119047753
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
ABresult
0X0
100
111
191 53 my $f = $E->file_of($pt);
192 53 $gap[$f] = $left;
193 53 $gap[$f + 1] = $right;
194 }
195 860 return @gap;
196 }
197  
198 86 ▶sub board_lines {
Subroutine board_lines: called CC=15 SCAR=27.1
Pod documented
199 86 my ($self, %o) = @_;
200 86 ▶ my $pos = $o{position} || ($self->game ? $self->game->position : Game::Xiangqi::Engine->new);
Branch: 1 branch
BranchTrueFalseTotal
$self->game ? : 50 21 71
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->position : 'Game::Xiangqi::Engine'->new)
l!l&&r!l&&!r
15710
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)
ABresult
1X1
011
000
201 86 ▶ my $flip = $o{flip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o{'flip'} ? : 1 85 86
202 86 my $E = 'Game::Xiangqi::Engine';
203  
204 86 my (%spot, %take);
205 86 ▶ for my $pt (@{ $o{spots} || [] }) {
Condition100%2/2
$o{'spots'} || []
l!l
1571
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
Aresult
11
00
206 34 ▶ $pos->at($pt) ? ($take{$pt} = 1) : ($spot{$pt} = 1);
Branch: 1 branch
BranchTrueFalseTotal
$pos->at($pt) ? : 10 24 34
207 }
208 86 $o{_take} = \%take;
209  
210 86 ▶ my @ranks = $flip ? (0 .. 9) : reverse(0 .. 9);
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 1 85 86
211 86 my @lines;
212  
213 86 for my $i (0 .. $#ranks) {
214 860 my $r = $ranks[$i];
215 860 my @gap = $self->_marks($r, \%o);
216 860 my $row = $gap[0];
217 860 for my $f (0 .. 8) {
218 7740 my $pt = $E->point_of($f, $r);
219 7740 $row .= $self->_cell($pos->at($pt), $spot{$pt});
220 7740 $row .= $gap[$f + 1];
221 }
222 860 push @lines, sprintf '%d %s', $r, $row;
223  
224 860 ▶ next if $i == $#ranks;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#ranks 86 774 860
225 774 my $next = $ranks[$i + 1];
226 774 ▶ my $lower = $r < $next ? $r : $next;
Branch: 1 branch
BranchTrueFalseTotal
$r < $next ? : 9 765 774
227 774 ▶ push @lines, ' ' . (($lower == 4) ? $self->_river : $self->_between($lower)) . ' ';
Branch: 1 branch
BranchTrueFalseTotal
$lower == 4 ? : 86 688 774
228 }
229  
230 86 my $legend = ' ';
231 86 for my $f (0 .. 8) {
232 774 $legend .= chr(97 + $f) . ' ';
233 774 ▶ $legend .= ' ' if $f != 8;
Branch: 1 branch
BranchTrueFalseTotal
if $f != 8 688 86 774
234 }
235 86 push @lines, $legend . ' ';
236 86 return @lines;
237 }
238  
239 26 ▶sub draw {
Subroutine draw: called CC=2 SCAR=6.9
Pod documented
240 26 my ($self, %o) = @_;
241 26 $self->_say($_) for $self->board_lines(%o);
242 26 return $self;
243 }
244  
245 14 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
246 14 my ($self) = @_;
247 14 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 14 14
248 14 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 14 0 14
249 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
250 }
251  
252 39 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
253 39 my ($self, @set) = @_;
254 39 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 19 20 39
$set[0] ? : 0 19 19
255 39 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 25 14 39
$self->_picking ? : 1 24 25
256 14 return $self->keys_available;
257 }
258  
259 15 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=26.2
Pod documented
260 15 my ($self, @set) = @_;
261 15 ▶ $self->_rewind($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 15 15
$set[0] ? : 0 0 0
262 15 ▶ return $self->_rewind ? 1 : 0 if defined $self->_rewind;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_rewind 15 0 15
$self->_rewind ? : 0 15 15
263 0 ▶ return eval { -t $self->_fh } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->_fh } } ? : 0 0 0
264 }
265  
266 1 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=24.2
Pod documented
267 1 my ($self) = @_;
268 1 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 1 1
269 1 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 1 0 1
270 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
271 0 ▶ return undef unless eval { Term::ReadKey::ReadMode(3, $self->in); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { Term::ReadKey::ReadMode(3, $self->in); 1 } } 0 0 0
272 0 $self->raw(1);
273 0 return $self;
274 }
275  
276 34 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
277 34 my ($self) = @_;
278 34 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 33 1 34
279 1 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 1 1
280 1 $self->raw(0);
281 1 return $self;
282 }
283  
284 38 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
285 38 my ($self, $wait) = @_;
286 38 my $pending = $self->pending;
287 38 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 1 37 38
288 37 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 37 0 37
289 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
290 0 ▶ return $char if defined $char || $wait;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char or $wait 0 0 0
Condition0%0/3
defined $char or $wait
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $char or $wait
defined $char $wait
Truth table0%0/3
A or B
A= defined $charB= $wait
ABresult
1X1
011
000
291 0 select undef, undef, undef, 0.05;
292 0 return Term::ReadKey::ReadKey(-1, $self->in);
293 }
294  
295 25 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
296 25 my ($self) = @_;
297 25 my $char = $self->read_char(1);
298 25 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 25 25
299 25 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Xiangqi::Terminal::CONTROL{$char} 10 15 25
300 15 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 7 8 15
301 8 return $char;
302 }
303  
304 7 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.5
Pod documented
305 7 my ($self) = @_;
306 7 my $opener = $self->read_char(0);
307 7 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 7 7
308 7 ▶ unless ($opener eq '[' || $opener eq 'O') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opener eq "[" or $opener eq "O") 1 6 7
Condition66%2/3
$opener eq "[" or $opener eq "O"
l!l&&r!l&&!r
601
MC/DC50%1/2
$opener eq "[" or $opener eq "O"
$opener eq "[" $opener eq "O"
Truth table66%2/3
A or B
A= $opener eq "["B= $opener eq "O"
ABresult
1X1
011
000
309 1 unshift @{ $self->pending }, $opener;
310 1 return 'escape';
311 }
312 6 my $tail = '';
313 6 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 6 0 6
314 6 my $char = $self->read_char(0);
315 6 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 6 6
316 6 $tail .= $char;
317 6 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 6 0 6
318 }
319 6 ▶ return $SEQUENCE{$tail} || 'escape';
Condition50%1/2
$Game::Xiangqi::Terminal::SEQUENCE{$tail} || 'escape'
l!l
60
MC/DC0%0/1
$Game::Xiangqi::Terminal::SEQUENCE{$tail} || 'escape'
$Game::Xiangqi::Terminal::SEQUENCE{$tail}
Truth table50%1/2
A
A= $Game::Xiangqi::Terminal::SEQUENCE{$tail}
Aresult
11
00
320 }
321  
322 29 ▶sub point_name {
Subroutine point_name: called CC=1 SCAR=0.0
Pod documented
323 29 my ($self, $pt) = @_;
324 29 my $E = 'Game::Xiangqi::Engine';
325 29 return chr(97 + $E->file_of($pt)) . $E->rank_of($pt);
326 }
327  
328 20 ▶sub piece_name {
Subroutine piece_name: called CC=4 SCAR=14.6
Pod documented
329 20 my ($self, $piece) = @_;
330 20 ▶ return 'nothing' unless $piece && Game::Xiangqi::Engine::kind_of($piece);
Branch: 1 branch
BranchTrueFalseTotal
unless $piece and Game::Xiangqi::Engine::kind_of($piece) 0 20 20
Condition33%1/3
$piece and Game::Xiangqi::Engine::kind_of($piece)
!ll&&!rl&&r
0020
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)
ABresult
0X0
100
111
331 20 ▶ my $side = Game::Xiangqi::Engine::colour_of($piece) == RED ? 'red' : 'black';
Branch: 1 branch
BranchTrueFalseTotal
Game::Xiangqi::Engine::colour_of($piece) == 8 ? : 19 1 20
332 20 return "$side " . $WORD{ $KIND_NAME[ Game::Xiangqi::Engine::kind_of($piece) ] };
333 }
334  
335 12 ▶sub origins {
Subroutine origins: called CC=2 SCAR=6.9
Pod documented
336 12 my ($self, $pos) = @_;
337 12 my $E = 'Game::Xiangqi::Engine';
338 12 my %seen;
339 12 my @from = grep { !$seen{$_}++ } map { $E->move_from($_) } $pos->legal;
340 12 ▶ return [ sort { $E->rank_of($b) <=> $E->rank_of($a)
Condition66%2/3
$E->rank_of($b) <=> $E->rank_of($a) || $E->file_of($a) <=> $E->file_of($b)
l!l&&r!l&&!r
1682860
MC/DC0%0/2
$E->rank_of($b) <=> $E->rank_of($a) || $E->file_of($a) <=> $E->file_of($b)
$E->rank_of($b) <=> $E->rank_of($a) $E->file_of($a) <=> $E->file_of($b)
Truth table66%2/3
A || B
A= $E->rank_of($b) <=> $E->rank_of($a)B= $E->file_of($a) <=> $E->file_of($b)
ABresult
1X1
011
000
341 || $E->file_of($a) <=> $E->file_of($b) } @from ];
342 }
343  
344 28 ▶sub targets {
Subroutine targets: called CC=2 SCAR=7.0
Pod documented
345 28 my ($self, $pos, $from) = @_;
346 28 my $E = 'Game::Xiangqi::Engine';
347 43 my @to = map { $E->move_to($_) }
348 28 grep { $E->move_from($_) == $from } $pos->legal;
349 28 ▶ return [ sort { $E->rank_of($b) <=> $E->rank_of($a)
Condition66%2/3
$E->rank_of($b) <=> $E->rank_of($a) || $E->file_of($a) <=> $E->file_of($b)
l!l&&r!l&&!r
2160
MC/DC0%0/2
$E->rank_of($b) <=> $E->rank_of($a) || $E->file_of($a) <=> $E->file_of($b)
$E->rank_of($b) <=> $E->rank_of($a) $E->file_of($a) <=> $E->file_of($b)
Truth table66%2/3
A || B
A= $E->rank_of($b) <=> $E->rank_of($a)B= $E->file_of($a) <=> $E->file_of($b)
ABresult
1X1
011
000
350 || $E->file_of($a) <=> $E->file_of($b) } @to ];
351 }
352  
353 145 ▶sub step {
Subroutine step: called CC=14 SCAR=26.4
Pod documented
354 145 my ($self, $points, $at, $key, $flip) = @_;
355 145 my $E = 'Game::Xiangqi::Engine';
356 145 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 24 121 145
357 121 ▶ return $#$points if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 19 102 121
358 102 ▶ return ($at + 1) % @$points if $key eq 'right' || $key eq 'tab';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq "tab" 38 64 102
Condition100%3/3
$key eq "right" or $key eq "tab"
l!l&&r!l&&!r
201864
MC/DC100%2/2
$key eq "right" or $key eq "tab"
$key eq "right" $key eq "tab"
Truth table100%3/3
A or B
A= $key eq "right"B= $key eq "tab"
ABresult
1X1
011
000
359 64 ▶ return ($at - 1) % @$points if $key eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" 19 45 64
360  
361 45 my $here = $points->[$at];
362 45 ▶ my $row = sub { $flip ? $E->rank_of($_[0]) : 9 - $E->rank_of($_[0]) };
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 30 1212 1242
363 45 ▶ my $want = $key eq 'up' ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'up' ? : 22 23 45
364 45 my ($pick, $near, $off);
365 45 for my $i (0 .. $#$points) {
366 621 my $away = ($row->($points->[$i]) - $row->($here)) * $want;
367 621 ▶ next unless $away > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $away > 0 583 38 621
368 38 my $sideways = abs($E->file_of($points->[$i]) - $E->file_of($here));
369 38 ▶ next if defined $pick
Branch: 1 branch
BranchTrueFalseTotal
if defined $pick and $away > $near || $away == $near && $sideways >= $off 25 13 38
Condition100%9/9
defined $pick and $away > $near || $away == $near && $sideways >= $off
!ll&&!rl&&r
8525
$away > $near or $away == $near and $sideways >= $off
l!l&&r!l&&!r
12135
$away == $near and $sideways >= $off
!ll&&!rl&&r
2313
MC/DC100%4/4
defined $pick and $away > $near || $away == $near && $sideways >= $off
defined $pick $away > $near $away == $near $sideways >= $off
Truth table100%5/5
A and B or C and D
A= defined $pickB= $away > $nearC= $away == $nearD= $sideways >= $off
ABCDresult
0XXX0
100X0
10100
11XX1
10111
370 && ($away > $near || ($away == $near && $sideways >= $off));
371 13 ($pick, $near, $off) = ($i, $away, $sideways);
372 }
373 45 ▶ return defined $pick ? $pick : $at;
Branch: 1 branch
BranchTrueFalseTotal
defined $pick ? : 8 37 45
374 }
375  
376 10 ▶sub move_line {
Subroutine move_line: called CC=8 SCAR=21.1
Pod documented
377 10 my ($self, $pos, $mv) = @_;
378 10 my $E = 'Game::Xiangqi::Engine';
379 10 my $to = $E->move_to($mv);
380 10 my $prey = $pos->at($to);
381  
382 10 ▶ my $line = $self->point_name($to) . ': '
Branch: 1 branch
BranchTrueFalseTotal
$prey && Game::Xiangqi::Engine::kind_of($prey) ? : 1 9 10
Condition66%2/3
$prey and Game::Xiangqi::Engine::kind_of($prey)
!ll&&!rl&&r
901
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)
ABresult
0X0
100
111
383 . ($prey && Game::Xiangqi::Engine::kind_of($prey)
384 ? 'takes the ' . $self->piece_name($prey)
385 : 'a quiet move');
386  
387 10 ▶ $line .= $pos->is_mate($mv) ? ', and it is mate'
Branch: 3 branches
BranchTrueFalseTotal
$pos->is_mate($mv) ? : 1 9 10
$pos->is_ttc($mv) ? : 1 8 9
$pos->is_check($mv) ? : 0 8 8
388 : $pos->is_ttc($mv) ? ', and it threatens mate'
389 : $pos->is_check($mv) ? ', and it checks'
390 : '';
391  
392 10 my $mine = $pos->at($E->move_from($mv));
393 10 ▶ $line .= ', but it loses the ' . $self->piece_name($mine)
Branch: 1 branch
BranchTrueFalseTotal
if $pos->is_sacrifice($mv) 0 10 10
394 if $pos->is_sacrifice($mv);
395 10 ▶ $line .= ', an even exchange' if $pos->is_exchange($mv);
Branch: 1 branch
BranchTrueFalseTotal
if $pos->is_exchange($mv) 0 10 10
396 10 return $line;
397 }
398  
399 19 ▶sub piece_line {
Subroutine piece_line: called CC=3 SCAR=11.0
Pod documented
400 19 my ($self, $pos, $from) = @_;
401 19 my $n = scalar @{ $self->targets($pos, $from) };
402 19 ▶ my $line = sprintf 'the %s on %s, %d %s',
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 17 2 19
403 $self->piece_name($pos->at($from)), $self->point_name($from),
404 $n, ($n == 1 ? 'move' : 'moves');
405 19 ▶ $line .= ', and it is attacked'
Branch: 1 branch
BranchTrueFalseTotal
if $pos->attacked($from, Game::Xiangqi::Engine::other($pos->side)) 0 19 19
406 if $pos->attacked($from, Game::Xiangqi::Engine::other($pos->side));
407 19 return $line;
408 }
409  
410 10 ▶sub pick_move {
Subroutine pick_move: called CC=25 SCAR=41.4
Pod documented
411 10 my ($self, $g, $seat) = @_;
412 10 my $pos = $g->position;
413 10 ▶ my $flip = $seat eq 'p2' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p2' ? : 0 10 10
414 10 my $from = $self->origins($pos);
415 10 ▶ return undef unless @$from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$from 0 10 10
416  
417 10 my ($at, $held, $to, $toat, $drawn) = (0, undef, undef, 0, 0);
418  
419 10 while (1) {
420 25 my %o = (flip => $flip);
421 25 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) { } 8 17 25
422 8 $to = $self->targets($pos, $held);
423 8 %o = (%o, from => $held, cursor => $to->[$toat], spots => $to);
424 }
425 else {
426 17 %o = (%o, cursor => $from->[$at]);
427 }
428  
429 25 my @lines = $self->board_lines(%o);
430 25 ▶ push @lines, ' ' . (defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 8 17 25
431 ? $self->move_line($pos, $pos->move($held, $to->[$toat]))
432 : $self->piece_line($pos, $from->[$at]));
433 25 ▶ push @lines, ' ' . (defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 8 17 25
434 ? 'arrows to choose, enter or space to play, escape to pick another'
435 : 'arrows to choose a piece, enter or space to take it, ? for the keys');
436  
437 25 ▶ $self->_out(sprintf "\e[%dA\r", $drawn) if $drawn && $self->rewind;
Branch: 1 branch
BranchTrueFalseTotal
if $drawn and $self->rewind 0 25 25
Condition66%2/3
$drawn and $self->rewind
!ll&&!rl&&r
10150
MC/DC0%0/2
$drawn and $self->rewind
$drawn $self->rewind
Truth table66%2/3
A and B
A= $drawnB= $self->rewind
ABresult
0X0
100
111
438 25 $self->_say($_) for @lines;
439 25 $drawn = scalar @lines;
440  
441 25 my $key = $self->read_key;
442 25 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 25 25
443 25 ▶ return 'quit' if $key eq 'q' || $key eq 'interrupt' || $key eq 'eof';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "q" or $key eq "interrupt" or $key eq "eof" 4 21 25
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
4021
$key eq "q" or $key eq "interrupt"
l!l&&r!l&&!r
4021
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"
ABCresult
1XX1
01X1
0011
0000
444  
445 21 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 14 7 21
Condition100%3/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
1047
MC/DC100%2/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table100%3/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
446 14 ▶ return $N->iccs_of($pos->move($held, $to->[$toat])) if defined $held;
Branch: 1 branch
BranchTrueFalseTotal
if defined $held 6 8 14
447 8 ($held, $toat) = ($from->[$at], 0);
448 8 next;
449 }
450 7 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 1 6 7
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
106
MC/DC50%1/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table66%2/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
451 1 $held = undef;
452 1 next;
453 }
454 6 ▶ if ($key =~ /\A(?:up|down|left|right|home|end|tab)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /\A(?:up|down|left|right|home|end|tab)\z/) 6 0 6
455 6 ▶ defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 0 6 6
456 ? ($toat = $self->step($to, $toat, $key, $flip))
457 : ($at = $self->step($from, $at, $key, $flip));
458 6 next;
459 }
460 0 ▶ if ($key eq '?') { $self->_keys; $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
461 0 ▶ if ($key eq 'l') { $self->_legal($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "l") 0 0 0
462 0 ▶ if ($key eq 'f') { $self->_say($pos->to_fen); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "f") 0 0 0
463 0 ▶ if ($key eq 'g') { $self->_history($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g") 0 0 0
464 0 ▶ if ($key eq 'h') { $self->_hint($g, $seat); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 0 0 0
465 }
466 }
467  
468 0 ▶sub _keys {
Subroutine _keys: not called CC=2 SCAR=17.9
469 0 my ($self) = @_;
470 0 $self->_say($_) for
471 'arrows move the cursor; it only stops where a move is',
472 'enter take the piece under the cursor, then play to the point',
473 'escape put the piece back and choose another',
474 'tab the next one, whichever you are choosing',
475 'h the strongest move this engine can find',
476 'l f g every move you have, the FEN, the moves so far',
477 'q give up and leave';
478 0 return $self;
479 }
480  
481 20 ▶sub start {
Subroutine start: called CC=30 SCAR=34.2
Pod documented
482 20 my ($self) = @_;
483  
484 20 my $g = $self->game;
485 20 ▶ if (!$g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($g) 1 19 20
486 1 $g = Game::Xiangqi->new(
487 seed => substr('xiangqi-terminal-default' . ('.' x 32), 0, 32));
488 1 $self->game($g);
489 }
490 20 ▶ return 2 unless $g;
Branch: 1 branch
BranchTrueFalseTotal
unless $g 0 20 20
491 20 my $seat = $self->seat;
492 20 my $bot = $g->bot;
493  
494 20 ▶ my $pick = $self->picking && $self->enter_raw ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking && $self->enter_raw ? : 1 19 20
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
1901
MC/DC50%1/2
$self->picking and $self->enter_raw
$self->picking $self->enter_raw
Truth table66%2/3
A and B
A= $self->pickingB= $self->enter_raw
ABresult
0X0
100
111
495 20 ▶ $self->picking(0) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 19 1 20
496 20 ▶ $self->_say($pick
Branch: 1 branch
BranchTrueFalseTotal
$pick ? : 1 19 20
497 ? 'xiangqi. arrows to choose a move, enter or space to play it, ? for the keys.'
498 : 'xiangqi. a move is ICCS, like h2e2. "help" for the rest.');
499 20 ▶ $self->draw(flip => ($seat eq 'p2')) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 19 1 20
500  
501 20 my $fh = $self->in;
502 20 ▶ while ($g->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->status eq "active") 31 2 33
503 31 ▶ if ($g->turn ne $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->turn ne $seat) 4 27 31
504 4 my $mv = do { local $Game::Xiangqi::Bot::LEVEL = $self->level;
505 4 $bot->choose($g, $g->turn) };
506 4 ▶ last unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 4 4
507 4 $self->_say($self->_describe($g, $mv, 'the bot plays'));
508 4 my $refusal = $g->play($mv);
509 4 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 3 4
510 1 $self->_say('the bot offered a move the game refused: '
511 . $refusal->code);
512 1 last;
513 }
514 3 ▶ $self->draw(flip => ($seat eq 'p2')) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 2 1 3
515 3 next;
516 }
517  
518 27 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 2 25 27
519 2 my $chosen = $self->pick_move($g, $seat);
520 2 ▶ last if !defined $chosen || $chosen eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $chosen or $chosen eq "quit" 1 1 2
Condition66%2/3
not defined $chosen or $chosen eq "quit"
l!l&&r!l&&!r
011
MC/DC50%1/2
not defined $chosen or $chosen eq "quit"
not defined $chosen $chosen eq "quit"
Truth table66%2/3
not(A) or B
A= not defined $chosenB= $chosen eq "quit"
ABresult
1X1
011
000
521 1 my $refusal = $g->play($chosen);
522 1 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')')
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 0 1 1
523 if $refusal;
524 1 next;
525 }
526  
527 25 $self->_out($self->_prompt($g));
528 25 my $line = <$fh>;
529 25 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 25 25
530 25 $line =~ s/\A\s+|\s+\z//g;
531 25 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 25 25
532  
533 25 ▶ last if $line eq 'quit' || $line eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" or $line eq "q" 16 9 25
Condition66%2/3
$line eq "quit" or $line eq "q"
l!l&&r!l&&!r
1609
MC/DC50%1/2
$line eq "quit" or $line eq "q"
$line eq "quit" $line eq "q"
Truth table66%2/3
A or B
A= $line eq "quit"B= $line eq "q"
ABresult
1X1
011
000
534 9 ▶ $self->_help, next if $line eq 'help' || $line eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "help" or $line eq "?" 1 8 9
Condition66%2/3
$line eq "help" or $line eq "?"
l!l&&r!l&&!r
108
MC/DC50%1/2
$line eq "help" or $line eq "?"
$line eq "help" $line eq "?"
Truth table66%2/3
A or B
A= $line eq "help"B= $line eq "?"
ABresult
1X1
011
000
535 8 ▶ $self->_legal($g), next if $line eq 'legal';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "legal" 1 7 8
536 7 ▶ $self->_say($g->position->to_fen), next if $line eq 'fen';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "fen" 1 6 7
537 6 ▶ $self->_history($g), next if $line eq 'log';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "log" 2 4 6
538 4 ▶ $self->draw(flip => ($seat eq 'p2')), next if $line eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "board" 1 3 4
539 3 ▶ $self->_hint($g, $seat), next if $line eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "hint" 0 3 3
540  
541 3 my $refusal = $g->play($line);
542 3 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 2 3
543 1 $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')');
544 1 next;
545 }
546 2 $self->draw(flip => ($seat eq 'p2'));
547 }
548  
549 20 $self->leave_raw;
550 20 ▶ $self->draw(flip => ($seat eq 'p2')) if $pick;
Branch: 1 branch
BranchTrueFalseTotal
if $pick 1 19 20
551 20 return $self->_verdict($g, $seat);
552 }
553  
554 25 ▶sub _prompt {
Subroutine _prompt: called CC=2 SCAR=7.2
555 25 my ($self, $g) = @_;
556 25 ▶ return sprintf '%s to move> ', $g->position->side == RED ? 'red' : 'black';
Branch: 1 branch
BranchTrueFalseTotal
$g->position->side == 8 ? : 25 0 25
557 }
558  
559 4 ▶sub _describe {
Subroutine _describe: called CC=4 SCAR=23.2
560 4 my ($self, $g, $iccs, $lead) = @_;
561 4 ▶ return "$lead $iccs" unless $self->wxf;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->wxf 4 0 4
562 0 my $mv = $N->move_of_iccs($iccs);
563 0 ▶ my $wxf = defined $mv ? $N->wxf_of($g->position, $mv) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 0 0 0
564 0 ▶ return defined $wxf ? "$lead $iccs ($wxf)" : "$lead $iccs";
Branch: 1 branch
BranchTrueFalseTotal
defined $wxf ? : 0 0 0
565 }
566  
567 1 ▶sub _help {
Subroutine _help: called CC=2 SCAR=6.9
568 1 my ($self) = @_;
569 1 $self->_say($_) for
570 'a move is ICCS: the from point then the to point, like h2e2',
571 'board draw it again',
572 'legal every move you have',
573 'fen the position as a FEN',
574 'log the moves so far',
575 'hint the strongest move this engine can find',
576 'quit give up and leave';
577 1 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 2 ▶sub _history {
Subroutine _history: called CC=2 SCAR=6.9
588 2 my ($self, $g) = @_;
589 2 my @l = @{ $g->log };
590 2 ▶ $self->_say(@l ? "@l" : 'no moves yet');
Branch: 1 branch
BranchTrueFalseTotal
@l ? : 1 1 2
591 2 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
BranchTrueFalseTotal
defined $mv ? : 0 0 0
598 0 return $self;
599 }
600  
601 20 ▶sub _verdict {
Subroutine _verdict: called CC=6 SCAR=20.2
602 20 my ($self, $g, $seat) = @_;
603 20 ▶ return 0 if $g->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $g->status eq "active" 18 2 20
604 2 my $r = $g->result;
605 2 ▶ my $why = $r->{reason} || 'over';
Condition50%1/2
$r->{'reason'} || 'over'
l!l
20
MC/DC0%0/1
$r->{'reason'} || 'over'
$r->{'reason'}
Truth table50%1/2
A
A= $r->{'reason'}
Aresult
11
00
606 2 ▶ $why .= " (rule $r->{rule})" if $r->{rule};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"rule"} 0 2 2
607  
608 2 ▶ if (!defined $r->{winner}) { $self->_say("drawn: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"winner"}) 0 2 2
609 2 ▶ if ($r->{winner} eq $seat) { $self->_say("you won: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"winner"} eq $seat) 0 2 2
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
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 41 0 41
621 41 $line =~ s/\A\s+|\s+\z//g;
622 41 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 2 39 41
623 39 my @w = split /\s+/, $line;
624 39 my $cmd = shift @w;
625  
626 39 ▶ if ($cmd eq 'ucci') {
Branch: 6 branches
BranchTrueFalseTotal
if ($cmd eq 'ucci') { } 4 35 39
elsif ($cmd eq 'isready') { } 3 32 35
elsif ($cmd eq 'quit') { } 11 21 32
elsif ($cmd eq 'stop') { } 1 20 21
elsif ($cmd eq 'position') { } 8 12 20
elsif ($cmd eq 'go') { } 9 3 12
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
BranchTrueFalseTotal
unless $pos 1 7 8
638 8 ▶ $pos ||= Game::Xiangqi::Engine->new;
Condition66%2/3
$pos ||= "Game::Xiangqi::Engine"->new
l!l&&r!l&&!r
710
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
ABresult
1X1
011
000
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'
l!l
81
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)
Aresult
11
00
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
BranchTrueFalseTotal
if (@w and $w[0] eq 'startpos') { } 4 4 8
elsif (@w and $w[0] eq 'fen') { } 4 0 4
Condition66%2/3
@w and $w[0] eq "startpos"
!ll&&!rl&&r
044
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"
ABresult
0X0
100
111
653 ▶ elsif (@w && $w[0] eq 'fen') {
Condition33%1/3
@w and $w[0] eq "fen"
!ll&&!rl&&r
004
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"
ABresult
0X0
100
111
654 4 shift @w;
655 4 my @fen;
656 4 ▶ push @fen, shift @w while @w && $w[0] ne 'moves';
Branch: 1 branch
BranchTrueFalseTotal
@w and $w[0] ne "moves" and do { push @fen, shift @w } 7 4 11
Condition66%2/3
@w and $w[0] ne "moves"
!ll&&!rl&&r
407
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"
ABresult
0X0
100
111
657 4 ($pos) = $E->of_fen(join ' ', @fen);
658 4 ▶ return (undef) unless $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 1 3 4
659 }
660 0 else { return (undef) }
661  
662 7 ▶ if (@w && $w[0] eq 'moves') {
Branch: 1 branch
BranchTrueFalseTotal
if (@w and $w[0] eq "moves") 1 6 7
Condition66%2/3
@w and $w[0] eq "moves"
!ll&&!rl&&r
601
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $mv 0 2 2
667 2 ▶ return (undef) unless grep { $_ == $mv } $pos->legal;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $mv;} $pos->legal 0 2 2
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
BranchTrueFalseTotal
if (@w >= 2) 10 9 19
679  
680 9 my $mv;
681 9 ▶ if (defined $arg{depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{'depth'}) { } 1 8 9
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
l!l
62
MC/DC100%1/1
$arg{'nodes'} || 100000
$arg{'nodes'}
Truth table100%2/2
A
A= $arg{'nodes'}
Aresult
11
00
686 8 ($mv) = $pos->search($nodes, 0);
687 }
688 9 ▶ return undef unless $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless $mv 1 8 9
689 8 return $N->iccs_of($mv);
690 }
691  
692 4 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
693 4 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 26 ▶sub run {
Subroutine run: called CC=23 SCAR=31.5
Pod documented
727 26 my ($class, @argv) = @_;
728 26 my %opt = (seat => 'p1', red => 'p1');
729  
730 26 my $ok = eval {
731 26 ▶ local $SIG{__WARN__} = sub { print STDERR "xiangqi: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
732 26 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
733 'level=i', 'red=s', 'seat=s', 'seed=s', 'fen=s', 'moves=s',
734 'wxf', 'ascii', 'ansi!', 'pick!', 'ucci', 'help');
735 };
736 26 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 2 24 26
737 24 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 23 24
738 23 ▶ if (@argv) { print STDERR "xiangqi: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 22 23
739  
740 22 for my $k (qw(red seat)) {
741 43 ▶ next if $opt{$k} eq 'p1' || $opt{$k} eq 'p2';
Branch: 1 branch
BranchTrueFalseTotal
if $opt{$k} eq "p1" or $opt{$k} eq "p2" 41 2 43
Condition66%2/3
$opt{$k} eq "p1" or $opt{$k} eq "p2"
l!l&&r!l&&!r
4102
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"
ABresult
1X1
011
000
742 2 print STDERR "xiangqi: --$k takes p1 or p2, not '$opt{$k}'\n";
743 2 return 2;
744 }
745  
746 20 my @ladder = @Game::Xiangqi::Bot::LADDER;
747 20 my $level;
748 20 ▶ if (defined $opt{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"level"}) 7 13 20
749 7 ▶ if ($opt{level} < 0) { print STDERR "xiangqi: --level must not be negative\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} < 0) 1 6 7
750 6 ▶ if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]) 2 4 6
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
!ll&&!rl&&r
222
MC/DC100%2/2
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
$opt{"level"} >= scalar @ladder $opt{"level"} < $ladder[0]
Truth table100%3/3
A and B
A= $opt{"level"} >= scalar @ladderB= $opt{"level"} < $ladder[0]
ABresult
0X0
100
111
751 printf STDERR
752 "xiangqi: --level %d is neither a rung nor a budget: a rung is 0 to %d, "
753 . "and a budget is %d nodes or more\n",
754 2 $opt{level}, $#ladder, $ladder[0];
755 2 return 2;
756 }
757 4 ▶ $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
$opt{'level'} < scalar @ladder ? : 2 2 4
758 }
759  
760 my $terminal = $class->new(
761 ascii => $opt{ascii},
762 wxf => $opt{wxf},
763 colour => defined $opt{ansi} ? $opt{ansi} : undef,
764 picking => $opt{pick},
765 level => $level,
766 seat => $opt{seat},
767 17 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'ansi'} ? : 4 13 17
768  
769 17 ▶ return $terminal->ucci if $opt{ucci};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"ucci"} 1 16 17
770  
771 16 ▶ my $seed = defined $opt{seed} ? $opt{seed} : 'xiangqi-terminal-default-seed32';
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'seed'} ? : 0 16 16
772 16 $seed = substr($seed . ('.' x 32), 0, 32);
773  
774 16 my %new = (seed => $seed, red => $opt{red});
775 16 ▶ if (defined $opt{fen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"fen"}) 2 14 16
776 2 my ($pos, $err) = Game::Xiangqi::Engine->of_fen($opt{fen});
777 2 ▶ if (!$pos) { print STDERR "xiangqi: --fen refused, code $err\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($pos) 1 1 2
778 1 $new{position} = $pos;
779 }
780  
781 15 my $game = eval { Game::Xiangqi->new(%new) };
782 15 ▶ if (!$game) { print STDERR "xiangqi: could not start a game\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 15 15
783  
784 15 ▶ if (defined $opt{moves}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"moves"}) 2 13 15
785 2 for my $mv (grep { length } split /[\s,]+/, $opt{moves}) {
786 4 my $refusal = $game->play($mv);
787 4 ▶ next unless $refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refusal 3 1 4
788 1 print STDERR "xiangqi: --moves refused at '$mv': ", $refusal->code, "\n";
789 1 return 2;
790 }
791 }
792  
793 14 $terminal->game($game);
794  
795 14 local @SIG{qw(INT TERM HUP)};
796 14 for my $sig (qw(INT TERM HUP)) {
797 42 ▶ $SIG{$sig} = sub { $terminal->leave_raw; $SIG{$sig} = 'DEFAULT'; kill $sig, $$ };
Subroutine __ANON__: not called CC=1 SCAR=3.5
798 }
799  
800 14 my $status = eval { $terminal->start };
801 14 my $died = $@;
802 14 $terminal->leave_raw;
803 14 ▶ die $died if $died;
Branch: 1 branch
BranchTrueFalseTotal
if $died 0 14 14
804 14 return $status;
805 }
806  
807 3 1;
808  
809 __END__
↑ Summary blib/lib/Game/Xiangqi.pm »