blib/lib/Game/Xiangqi/Terminal.pm

filter: Statementstmt 88.5% 435 / 491 Branchbran 75.4% 246 / 326 Conditioncond 72.1% 70 / 97 MC/DCmcdc 48.3% 29 / 60 Subroutinesub 94.5% 52 / 55 Podpod 95.8% 23 / 24 totaltotal 81.2% 855 / 1053 CC 211 SCAR 27.8
CC
211
Cov
82%
pick_move
62.7
start
30.6
read_char
15.1
CRAP 16.2
↑ Summary bin/xiangqi »
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  
14 3 our $VERSION = '0.03';
15  
16 3 my $N = 'Game::Xiangqi::Notation';
17  
18 3 my %GLYPH = (
19 (RED | GENERAL) => "\x{5E25}",
20 (RED | ADVISOR) => "\x{4ED5}",
21 (RED | ELEPHANT) => "\x{76F8}",
22 (RED | CHARIOT) => "\x{4FE5}",
23 (RED | HORSE) => "\x{508C}",
24 (RED | CANNON) => "\x{70AE}",
25 (RED | SOLDIER) => "\x{5175}",
26 (BLACK | GENERAL) => "\x{5C07}",
27 (BLACK | ADVISOR) => "\x{58EB}",
28 (BLACK | ELEPHANT) => "\x{8C61}",
29 (BLACK | CHARIOT) => "\x{8ECA}",
30 (BLACK | HORSE) => "\x{99AC}",
31 (BLACK | CANNON) => "\x{7832}",
32 (BLACK | SOLDIER) => "\x{5352}",
33 );
34  
35 3 my %LETTER = (
36 GENERAL => 'K', ADVISOR => 'A', ELEPHANT => 'E', CHARIOT => 'R',
37 HORSE => 'H', CANNON => 'C', SOLDIER => 'P',
38 );
39 3 my @KIND_NAME = (undef, 'GENERAL', 'ADVISOR', 'ELEPHANT', 'CHARIOT',
40 'HORSE', 'CANNON', 'SOLDIER');
41  
42 3 my %WORD = (
43 GENERAL => 'general', ADVISOR => 'advisor', ELEPHANT => 'elephant',
44 CHARIOT => 'chariot', HORSE => 'horse', CANNON => 'cannon',
45 SOLDIER => 'soldier',
46 );
47  
48 3 our %SEQUENCE = (
49 'A' => 'up', 'B' => 'down', 'C' => 'right', 'D' => 'left',
50 'H' => 'home', 'F' => 'end',
51 '1~' => 'home', '4~' => 'end', '5~' => 'page_up', '6~' => 'page_down',
52 '7~' => 'home', '8~' => 'end',
53 );
54  
55 3 our %CONTROL = (
56 "\r" => 'enter', "\n" => 'enter', "\t" => 'tab',
57 "\x7f" => 'backspace', "\x08" => 'backspace',
58 "\x03" => 'interrupt', "\x04" => 'eof',
59 );
60  
61 3 my %COLOUR = (red => "\e[1;31m", black => "\e[1;34m", off => "\e[0m");
62  
63 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 3 CELL => 2,
65 GAP => 1,
66 3 };
67  
68 45 ▶has in => (is => 'rw', default => sub { \*STDIN });
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 3 has game => (is => 'rw');
71  
72 3 has wxf => (is => 'rw', default => 0);
73  
74 3 has level => (is => 'ro');
75  
76 3 has seat => (is => 'ro', isa => Str, default => 'p1');
77  
78 33 ▶has _fh => (is => 'rw', init_arg => 'out', private => 1, default => sub { \*STDOUT });
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 3 has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0);
81  
82 3 has _colour => (is => 'rw', init_arg => 'colour', private => 1);
83  
84 3 has raw => (is => 'rw', isa => Any, default => 0);
85  
86 3 has keysource => (is => 'rw', isa => Any);
87  
88 60 ▶has pending => (is => 'rw', isa => ArrayRef, default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
89  
90 3 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
91  
92 3 has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind');
93  
94 60 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=17.9
Pod undocumented
95 60 my ($self) = @_;
96  
97 60 ▶ $self->wxf($self->wxf ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->wxf ? : 0 60 60
98 60 ▶ $self->_ascii($self->_ascii ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 16 44 60
99 $self->_colour(
100 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
101 : defined $self->_colour ? ($self->_colour ? 1 : 0)
102 : 1
103 );
104 60 $self->_encode_out;
105 60 return;
106 }
107  
108 61 ▶sub _encode_out {
Subroutine _encode_out: called CC=4 SCAR=13.9
109 61 my ($self) = @_;
110 61 ▶ return $self if $self->_ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ascii 16 45 61
111 45 my $fh = $self->_fh;
112 45 ▶ return $self unless $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 45 45
113 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
114 33 eval { binmode $fh, ':encoding(UTF-8)'; 1 };
115 33 return $self;
116 }
117  
118 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
119 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
120 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
121  
122 1149 ▶sub _say { my ($self, @t) = @_; my $fh = $self->_fh; print {$fh} @t, "\n" }
Subroutine _say: called CC=1 SCAR=0.0
123 25 ▶sub _out { my ($self, @t) = @_; my $fh = $self->_fh; print {$fh} @t }
Subroutine _out: called CC=1 SCAR=0.0
124  
125 7740 ▶sub _cell {
Subroutine _cell: called CC=10 SCAR=23.0
126 7740 my ($self, $piece, $spot) = @_;
127 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
128 5504 ▶ return $self->_ascii ? ($spot ? ' +' : ' .')
Branch: 3 branches
BranchTrueFalseTotal
$self->_ascii ? : 2000 3504 5504
$spot ? : 18 1982 2000
$spot ? : 6 3498 3504
129 : ($spot ? "\x{FF0B}" : "\x{30FB}");
130 }
131 2236 ▶ if ($self->_ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_ascii) 1060 1176 2236
132 1060 my $l = $LETTER{ $KIND_NAME[ kind_of($piece) ] };
133 1060 ▶ $l = lc $l if colour_of($piece) == BLACK;
Branch: 1 branch
BranchTrueFalseTotal
if colour_of($piece) == 16 530 530 1060
134 1060 return " $l";
135 }
136 1176 my $g = $GLYPH{$piece};
137 1176 ▶ return $g unless $self->_colour;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_colour 531 645 1176
138 645 ▶ my $c = colour_of($piece) == RED ? $COLOUR{red} : $COLOUR{black};
Branch: 1 branch
BranchTrueFalseTotal
colour_of($piece) == 8 ? : 321 324 645
139 645 return $c . $g . $COLOUR{off};
140 }
141  
142 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
143  
144 688 ▶sub _between {
Subroutine _between: called CC=12 SCAR=24.8
145 688 my ($self, $lower) = @_;
146 688 ▶ my ($vert, $up, $down) = $self->_ascii ? ('|', '/', "\\")
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 272 416 688
147 : ("\x{2502}", "\x{2571}", "\x{2572}");
148 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
149 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
150  
151 688 my $row = '';
152 688 for my $f (0 .. 8) {
153 6192 $row .= $vert . ' ';
154 6192 ▶ next if $f == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $f == 8 688 5504 6192
155 5504 my $g = ' ';
156 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
157 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
158 5504 $row .= $g;
159 }
160 688 return $row;
161 }
162  
163 86 ▶sub _river {
Subroutine _river: called CC=2 SCAR=6.9
164 86 my ($self) = @_;
165 86 ▶ my $vert = $self->_ascii ? '|' : "\x{2502}";
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 34 52 86
166 86 my $row = $vert . ' ';
167 86 $row .= ' ' x (GAP + (CELL + GAP) * 7 + CELL - CELL);
168 86 $row .= $vert . ' ';
169 86 return $row;
170 }
171  
172 860 ▶sub _marks {
Subroutine _marks: called CC=7 SCAR=19.5
173 860 my ($self, $r, $o) = @_;
174 860 my $E = 'Game::Xiangqi::Engine';
175 860 my @gap = ($self->_hline) x 10;
176 860 $gap[0] = $gap[9] = ' ';
177  
178 860 ▶ my $take = $self->_ascii ? 'x' : "\x{00D7}";
Branch: 1 branch
BranchTrueFalseTotal
$self->_ascii ? : 340 520 860
179  
180 860 for my $f (0 .. 8) {
181 7740 my $pt = $E->point_of($f, $r);
182 7740 ▶ next unless $o->{_take}{$pt};
Branch: 1 branch
BranchTrueFalseTotal
unless $o->{"_take"}{$pt} 7730 10 7740
183 10 $gap[$f] = $take;
184 10 $gap[$f + 1] = $take;
185 }
186 860 for my $pair ([ $o->{from}, '(', ')' ], [ $o->{cursor}, '[', ']' ]) {
187 1720 my ($pt, $left, $right) = @$pair;
188 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
189 53 my $f = $E->file_of($pt);
190 53 $gap[$f] = $left;
191 53 $gap[$f + 1] = $right;
192 }
193 860 return @gap;
194 }
195  
196 86 ▶sub board_lines {
Subroutine board_lines: called CC=15 SCAR=27.1
Pod documented
197 86 my ($self, %o) = @_;
198 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
199 86 ▶ my $flip = $o{flip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o{'flip'} ? : 1 85 86
200 86 my $E = 'Game::Xiangqi::Engine';
201  
202 86 my (%spot, %take);
203 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
204 34 ▶ $pos->at($pt) ? ($take{$pt} = 1) : ($spot{$pt} = 1);
Branch: 1 branch
BranchTrueFalseTotal
$pos->at($pt) ? : 10 24 34
205 }
206 86 $o{_take} = \%take;
207  
208 86 ▶ my @ranks = $flip ? (0 .. 9) : reverse(0 .. 9);
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 1 85 86
209 86 my @lines;
210  
211 86 for my $i (0 .. $#ranks) {
212 860 my $r = $ranks[$i];
213 860 my @gap = $self->_marks($r, \%o);
214 860 my $row = $gap[0];
215 860 for my $f (0 .. 8) {
216 7740 my $pt = $E->point_of($f, $r);
217 7740 $row .= $self->_cell($pos->at($pt), $spot{$pt});
218 7740 $row .= $gap[$f + 1];
219 }
220 860 push @lines, sprintf '%d %s', $r, $row;
221  
222 860 ▶ next if $i == $#ranks;
Branch: 1 branch
BranchTrueFalseTotal
if $i == $#ranks 86 774 860
223 774 my $next = $ranks[$i + 1];
224 774 ▶ my $lower = $r < $next ? $r : $next;
Branch: 1 branch
BranchTrueFalseTotal
$r < $next ? : 9 765 774
225 774 ▶ push @lines, ' ' . (($lower == 4) ? $self->_river : $self->_between($lower)) . ' ';
Branch: 1 branch
BranchTrueFalseTotal
$lower == 4 ? : 86 688 774
226 }
227  
228 86 my $legend = ' ';
229 86 for my $f (0 .. 8) {
230 774 $legend .= chr(97 + $f) . ' ';
231 774 ▶ $legend .= ' ' if $f != 8;
Branch: 1 branch
BranchTrueFalseTotal
if $f != 8 688 86 774
232 }
233 86 push @lines, $legend . ' ';
234 86 return @lines;
235 }
236  
237 26 ▶sub draw {
Subroutine draw: called CC=2 SCAR=6.9
Pod documented
238 26 my ($self, %o) = @_;
239 26 $self->_say($_) for $self->board_lines(%o);
240 26 return $self;
241 }
242  
243 14 ▶sub keys_available {
Subroutine keys_available: called CC=4 SCAR=18.7
Pod documented
244 14 my ($self) = @_;
245 14 ▶ return 1 if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 0 14 14
246 14 ▶ return 0 unless eval { -t $self->in };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { -t $self->in } } 14 0 14
247 0 ▶ return eval { require Term::ReadKey; 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require Term::ReadKey; 1 } } ? : 0 0 0
248 }
249  
250 39 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
251 39 my ($self, @set) = @_;
252 39 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 19 20 39
$set[0] ? : 0 19 19
253 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
254 14 return $self->keys_available;
255 }
256  
257 15 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=26.2
Pod documented
258 15 my ($self, @set) = @_;
259 15 ▶ $self->_rewind($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 15 15
$set[0] ? : 0 0 0
260 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
261 0 ▶ return eval { -t $self->_fh } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->_fh } } ? : 0 0 0
262 }
263  
264 1 ▶sub enter_raw {
Subroutine enter_raw: called CC=5 SCAR=24.2
Pod documented
265 1 my ($self) = @_;
266 1 ▶ return $self if $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
if $self->raw 0 1 1
267 1 ▶ if ($self->keysource) { $self->raw(1); return $self }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->keysource) 1 0 1
268 0 ▶ return undef unless $self->keys_available;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keys_available 0 0 0
269 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
270 0 $self->raw(1);
271 0 return $self;
272 }
273  
274 34 ▶sub leave_raw {
Subroutine leave_raw: called CC=3 SCAR=11.2
Pod documented
275 34 my ($self) = @_;
276 34 ▶ return $self unless $self->raw;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->raw 33 1 34
277 1 ▶ eval { Term::ReadKey::ReadMode(0, $self->in) } unless $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->keysource 0 1 1
278 1 $self->raw(0);
279 1 return $self;
280 }
281  
282 38 ▶sub read_char {
Subroutine read_char: called CC=6 SCAR=27.1
Pod documented
283 38 my ($self, $wait) = @_;
284 38 my $pending = $self->pending;
285 38 ▶ return shift @$pending if @$pending;
Branch: 1 branch
BranchTrueFalseTotal
if @$pending 1 37 38
286 37 ▶ return $self->keysource->($wait) if $self->keysource;
Branch: 1 branch
BranchTrueFalseTotal
if $self->keysource 37 0 37
287 0 ▶ my $char = Term::ReadKey::ReadKey($wait ? 0 : -1, $self->in);
Branch: 1 branch
BranchTrueFalseTotal
$wait ? : 0 0 0
288 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
289 0 select undef, undef, undef, 0.05;
290 0 return Term::ReadKey::ReadKey(-1, $self->in);
291 }
292  
293 25 ▶sub read_key {
Subroutine read_key: called CC=4 SCAR=13.9
Pod documented
294 25 my ($self) = @_;
295 25 my $char = $self->read_char(1);
296 25 ▶ return undef unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 25 25
297 25 ▶ return $CONTROL{$char} if $CONTROL{$char};
Branch: 1 branch
BranchTrueFalseTotal
if $Game::Xiangqi::Terminal::CONTROL{$char} 10 15 25
298 15 ▶ return $self->read_sequence if $char eq "\e";
Branch: 1 branch
BranchTrueFalseTotal
if $char eq "\e" 7 8 15
299 8 return $char;
300 }
301  
302 7 ▶sub read_sequence {
Subroutine read_sequence: called CC=8 SCAR=21.5
Pod documented
303 7 my ($self) = @_;
304 7 my $opener = $self->read_char(0);
305 7 ▶ return 'escape' unless defined $opener;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opener 0 7 7
306 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
307 1 unshift @{ $self->pending }, $opener;
308 1 return 'escape';
309 }
310 6 my $tail = '';
311 6 ▶ while (length $tail < 8) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $tail < 8) 6 0 6
312 6 my $char = $self->read_char(0);
313 6 ▶ last unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 0 6 6
314 6 $tail .= $char;
315 6 ▶ last if $char =~ /[A-Za-z~]/;
Branch: 1 branch
BranchTrueFalseTotal
if $char =~ /[A-Za-z~]/ 6 0 6
316 }
317 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
318 }
319  
320 29 ▶sub point_name {
Subroutine point_name: called CC=1 SCAR=0.0
Pod documented
321 29 my ($self, $pt) = @_;
322 29 my $E = 'Game::Xiangqi::Engine';
323 29 return chr(97 + $E->file_of($pt)) . $E->rank_of($pt);
324 }
325  
326 20 ▶sub piece_name {
Subroutine piece_name: called CC=4 SCAR=14.6
Pod documented
327 20 my ($self, $piece) = @_;
328 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
329 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
330 20 return "$side " . $WORD{ $KIND_NAME[ Game::Xiangqi::Engine::kind_of($piece) ] };
331 }
332  
333 12 ▶sub origins {
Subroutine origins: called CC=2 SCAR=6.9
Pod documented
334 12 my ($self, $pos) = @_;
335 12 my $E = 'Game::Xiangqi::Engine';
336 12 my %seen;
337 12 my @from = grep { !$seen{$_}++ } map { $E->move_from($_) } $pos->legal;
338 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
339 || $E->file_of($a) <=> $E->file_of($b) } @from ];
340 }
341  
342 28 ▶sub targets {
Subroutine targets: called CC=2 SCAR=7.0
Pod documented
343 28 my ($self, $pos, $from) = @_;
344 28 my $E = 'Game::Xiangqi::Engine';
345 43 my @to = map { $E->move_to($_) }
346 28 grep { $E->move_from($_) == $from } $pos->legal;
347 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
348 || $E->file_of($a) <=> $E->file_of($b) } @to ];
349 }
350  
351 145 ▶sub step {
Subroutine step: called CC=14 SCAR=26.4
Pod documented
352 145 my ($self, $points, $at, $key, $flip) = @_;
353 145 my $E = 'Game::Xiangqi::Engine';
354 145 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 24 121 145
355 121 ▶ return $#$points if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 19 102 121
356 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
357 64 ▶ return ($at - 1) % @$points if $key eq 'left';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" 19 45 64
358  
359 45 my $here = $points->[$at];
360 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
361 45 ▶ my $want = $key eq 'up' ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'up' ? : 22 23 45
362 45 my ($pick, $near, $off);
363 45 for my $i (0 .. $#$points) {
364 621 my $away = ($row->($points->[$i]) - $row->($here)) * $want;
365 621 ▶ next unless $away > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $away > 0 583 38 621
366 38 my $sideways = abs($E->file_of($points->[$i]) - $E->file_of($here));
367 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
368 && ($away > $near || ($away == $near && $sideways >= $off));
369 13 ($pick, $near, $off) = ($i, $away, $sideways);
370 }
371 45 ▶ return defined $pick ? $pick : $at;
Branch: 1 branch
BranchTrueFalseTotal
defined $pick ? : 8 37 45
372 }
373  
374 10 ▶sub move_line {
Subroutine move_line: called CC=8 SCAR=21.1
Pod documented
375 10 my ($self, $pos, $mv) = @_;
376 10 my $E = 'Game::Xiangqi::Engine';
377 10 my $to = $E->move_to($mv);
378 10 my $prey = $pos->at($to);
379  
380 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
381 . ($prey && Game::Xiangqi::Engine::kind_of($prey)
382 ? 'takes the ' . $self->piece_name($prey)
383 : 'a quiet move');
384  
385 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
386 : $pos->is_ttc($mv) ? ', and it threatens mate'
387 : $pos->is_check($mv) ? ', and it checks'
388 : '';
389  
390 10 my $mine = $pos->at($E->move_from($mv));
391 10 ▶ $line .= ', but it loses the ' . $self->piece_name($mine)
Branch: 1 branch
BranchTrueFalseTotal
if $pos->is_sacrifice($mv) 0 10 10
392 if $pos->is_sacrifice($mv);
393 10 ▶ $line .= ', an even exchange' if $pos->is_exchange($mv);
Branch: 1 branch
BranchTrueFalseTotal
if $pos->is_exchange($mv) 0 10 10
394 10 return $line;
395 }
396  
397 19 ▶sub piece_line {
Subroutine piece_line: called CC=3 SCAR=11.0
Pod documented
398 19 my ($self, $pos, $from) = @_;
399 19 my $n = scalar @{ $self->targets($pos, $from) };
400 19 ▶ my $line = sprintf 'the %s on %s, %d %s',
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 17 2 19
401 $self->piece_name($pos->at($from)), $self->point_name($from),
402 $n, ($n == 1 ? 'move' : 'moves');
403 19 ▶ $line .= ', and it is attacked'
Branch: 1 branch
BranchTrueFalseTotal
if $pos->attacked($from, Game::Xiangqi::Engine::other($pos->side)) 0 19 19
404 if $pos->attacked($from, Game::Xiangqi::Engine::other($pos->side));
405 19 return $line;
406 }
407  
408 10 ▶sub pick_move {
Subroutine pick_move: called CC=25 SCAR=41.4
Pod documented
409 10 my ($self, $g, $seat) = @_;
410 10 my $pos = $g->position;
411 10 ▶ my $flip = $seat eq 'p2' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$seat eq 'p2' ? : 0 10 10
412 10 my $from = $self->origins($pos);
413 10 ▶ return undef unless @$from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$from 0 10 10
414  
415 10 my ($at, $held, $to, $toat, $drawn) = (0, undef, undef, 0, 0);
416  
417 10 while (1) {
418 25 my %o = (flip => $flip);
419 25 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) { } 8 17 25
420 8 $to = $self->targets($pos, $held);
421 8 %o = (%o, from => $held, cursor => $to->[$toat], spots => $to);
422 }
423 else {
424 17 %o = (%o, cursor => $from->[$at]);
425 }
426  
427 25 my @lines = $self->board_lines(%o);
428 25 ▶ push @lines, ' ' . (defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 8 17 25
429 ? $self->move_line($pos, $pos->move($held, $to->[$toat]))
430 : $self->piece_line($pos, $from->[$at]));
431 25 ▶ push @lines, ' ' . (defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 8 17 25
432 ? 'arrows to choose, enter or space to play, escape to pick another'
433 : 'arrows to choose a piece, enter or space to take it, ? for the keys');
434  
435 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
436 25 $self->_say($_) for @lines;
437 25 $drawn = scalar @lines;
438  
439 25 my $key = $self->read_key;
440 25 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 25 25
441 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
442  
443 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
444 14 ▶ return $N->iccs_of($pos->move($held, $to->[$toat])) if defined $held;
Branch: 1 branch
BranchTrueFalseTotal
if defined $held 6 8 14
445 8 ($held, $toat) = ($from->[$at], 0);
446 8 next;
447 }
448 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
449 1 $held = undef;
450 1 next;
451 }
452 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
453 6 ▶ defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 0 6 6
454 ? ($toat = $self->step($to, $toat, $key, $flip))
455 : ($at = $self->step($from, $at, $key, $flip));
456 6 next;
457 }
458 0 ▶ if ($key eq '?') { $self->_keys; $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 0 0
459 0 ▶ if ($key eq 'l') { $self->_legal($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "l") 0 0 0
460 0 ▶ if ($key eq 'f') { $self->_say($pos->to_fen); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "f") 0 0 0
461 0 ▶ if ($key eq 'g') { $self->_history($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g") 0 0 0
462 0 ▶ if ($key eq 'h') { $self->_hint($g, $seat); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 0 0 0
463 }
464 }
465  
466 0 ▶sub _keys {
Subroutine _keys: not called CC=2 SCAR=17.9
467 0 my ($self) = @_;
468 0 $self->_say($_) for
469 'arrows move the cursor; it only stops where a move is',
470 'enter take the piece under the cursor, then play to the point',
471 'escape put the piece back and choose another',
472 'tab the next one, whichever you are choosing',
473 'h the strongest move this engine can find',
474 'l f g every move you have, the FEN, the moves so far',
475 'q give up and leave';
476 0 return $self;
477 }
478  
479 20 ▶sub start {
Subroutine start: called CC=30 SCAR=34.2
Pod documented
480 20 my ($self) = @_;
481  
482 20 my $g = $self->game;
483 20 ▶ if (!$g) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($g) 1 19 20
484 1 $g = Game::Xiangqi->new(
485 seed => substr('xiangqi-terminal-default' . ('.' x 32), 0, 32));
486 1 $self->game($g);
487 }
488 20 ▶ return 2 unless $g;
Branch: 1 branch
BranchTrueFalseTotal
unless $g 0 20 20
489 20 my $seat = $self->seat;
490 20 my $bot = $g->bot;
491  
492 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
493 20 ▶ $self->picking(0) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 19 1 20
494 20 ▶ $self->_say($pick
Branch: 1 branch
BranchTrueFalseTotal
$pick ? : 1 19 20
495 ? 'xiangqi. arrows to choose a move, enter or space to play it, ? for the keys.'
496 : 'xiangqi. a move is ICCS, like h2e2. "help" for the rest.');
497 20 ▶ $self->draw(flip => ($seat eq 'p2')) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 19 1 20
498  
499 20 my $fh = $self->in;
500 20 ▶ while ($g->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->status eq "active") 31 2 33
501 31 ▶ if ($g->turn ne $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->turn ne $seat) 4 27 31
502 4 my $mv = do { local $Game::Xiangqi::Bot::LEVEL = $self->level;
503 4 $bot->choose($g, $g->turn) };
504 4 ▶ last unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 4 4
505 4 $self->_say($self->_describe($g, $mv, 'the bot plays'));
506 4 my $refusal = $g->play($mv);
507 4 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 3 4
508 1 $self->_say('the bot offered a move the game refused: '
509 . $refusal->code);
510 1 last;
511 }
512 3 ▶ $self->draw(flip => ($seat eq 'p2')) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 2 1 3
513 3 next;
514 }
515  
516 27 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 2 25 27
517 2 my $chosen = $self->pick_move($g, $seat);
518 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
519 1 my $refusal = $g->play($chosen);
520 1 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')')
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 0 1 1
521 if $refusal;
522 1 next;
523 }
524  
525 25 $self->_out($self->_prompt($g));
526 25 my $line = <$fh>;
527 25 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 25 25
528 25 $line =~ s/\A\s+|\s+\z//g;
529 25 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 25 25
530  
531 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
532 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
533 8 ▶ $self->_legal($g), next if $line eq 'legal';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "legal" 1 7 8
534 7 ▶ $self->_say($g->position->to_fen), next if $line eq 'fen';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "fen" 1 6 7
535 6 ▶ $self->_history($g), next if $line eq 'log';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "log" 2 4 6
536 4 ▶ $self->draw(flip => ($seat eq 'p2')), next if $line eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "board" 1 3 4
537 3 ▶ $self->_hint($g, $seat), next if $line eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "hint" 0 3 3
538  
539 3 my $refusal = $g->play($line);
540 3 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 2 3
541 1 $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')');
542 1 next;
543 }
544 2 $self->draw(flip => ($seat eq 'p2'));
545 }
546  
547 20 $self->leave_raw;
548 20 ▶ $self->draw(flip => ($seat eq 'p2')) if $pick;
Branch: 1 branch
BranchTrueFalseTotal
if $pick 1 19 20
549 20 return $self->_verdict($g, $seat);
550 }
551  
552 25 ▶sub _prompt {
Subroutine _prompt: called CC=2 SCAR=7.2
553 25 my ($self, $g) = @_;
554 25 ▶ return sprintf '%s to move> ', $g->position->side == RED ? 'red' : 'black';
Branch: 1 branch
BranchTrueFalseTotal
$g->position->side == 8 ? : 25 0 25
555 }
556  
557 4 ▶sub _describe {
Subroutine _describe: called CC=4 SCAR=23.2
558 4 my ($self, $g, $iccs, $lead) = @_;
559 4 ▶ return "$lead $iccs" unless $self->wxf;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->wxf 4 0 4
560 0 my $mv = $N->move_of_iccs($iccs);
561 0 ▶ my $wxf = defined $mv ? $N->wxf_of($g->position, $mv) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 0 0 0
562 0 ▶ return defined $wxf ? "$lead $iccs ($wxf)" : "$lead $iccs";
Branch: 1 branch
BranchTrueFalseTotal
defined $wxf ? : 0 0 0
563 }
564  
565 1 ▶sub _help {
Subroutine _help: called CC=2 SCAR=6.9
566 1 my ($self) = @_;
567 1 $self->_say($_) for
568 'a move is ICCS: the from point then the to point, like h2e2',
569 'board draw it again',
570 'legal every move you have',
571 'fen the position as a FEN',
572 'log the moves so far',
573 'hint the strongest move this engine can find',
574 'quit give up and leave';
575 1 return $self;
576 }
577  
578 1 ▶sub _legal {
Subroutine _legal: called CC=1 SCAR=0.0
579 1 my ($self, $g) = @_;
580 1 my @m = @{ $g->legal };
581 1 $self->_say(scalar(@m) . ' moves: ' . join ' ', @m);
582 1 return $self;
583 }
584  
585 2 ▶sub _history {
Subroutine _history: called CC=2 SCAR=6.9
586 2 my ($self, $g) = @_;
587 2 my @l = @{ $g->log };
588 2 ▶ $self->_say(@l ? "@l" : 'no moves yet');
Branch: 1 branch
BranchTrueFalseTotal
@l ? : 1 1 2
589 2 return $self;
590 }
591  
592 0 ▶sub _hint {
Subroutine _hint: not called CC=2 SCAR=17.9
593 0 my ($self, $g, $seat) = @_;
594 0 my $mv = $g->bot->hint($g, $seat);
595 0 ▶ $self->_say(defined $mv ? $self->_describe($g, $mv, 'try') : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 0 0 0
596 0 return $self;
597 }
598  
599 20 ▶sub _verdict {
Subroutine _verdict: called CC=6 SCAR=20.2
600 20 my ($self, $g, $seat) = @_;
601 20 ▶ return 0 if $g->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
if $g->status eq "active" 18 2 20
602 2 my $r = $g->result;
603 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
604 2 ▶ $why .= " (rule $r->{rule})" if $r->{rule};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"rule"} 0 2 2
605  
606 2 ▶ if (!defined $r->{winner}) { $self->_say("drawn: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $r->{"winner"}) 0 2 2
607 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
608 2 $self->_say("you lost: $why");
609 2 return 1;
610 }
611  
612 11 ▶sub ucci {
Subroutine ucci: called CC=12 SCAR=24.9
Pod documented
613 11 my ($self) = @_;
614 11 my $fh = $self->in;
615 11 my $pos = Game::Xiangqi::Engine->new;
616 11 my @played;
617  
618 11 ▶ while (defined(my $line = <$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 41 0 41
619 41 $line =~ s/\A\s+|\s+\z//g;
620 41 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 2 39 41
621 39 my @w = split /\s+/, $line;
622 39 my $cmd = shift @w;
623  
624 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
625 4 $self->_say('id name Game::Xiangqi ' . $Game::Xiangqi::VERSION);
626 4 $self->_say('id author Game::Xiangqi');
627 4 $self->_say('option usemillisec type check default false');
628 4 $self->_say('ucciok');
629 }
630 3 elsif ($cmd eq 'isready') { $self->_say('readyok') }
631 11 elsif ($cmd eq 'quit') { $self->_say('bye'); last }
632 elsif ($cmd eq 'stop') { }
633 elsif ($cmd eq 'position') {
634 8 ($pos, @played) = $self->_ucci_position(@w);
635 8 ▶ $self->_say('info string position refused') unless $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 1 7 8
636 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
637 }
638 elsif ($cmd eq 'go') {
639 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
640 }
641 }
642 11 return 0;
643 }
644  
645 8 ▶sub _ucci_position {
Subroutine _ucci_position: called CC=13 SCAR=26.5
646 8 my ($self, @w) = @_;
647 8 my $E = 'Game::Xiangqi::Engine';
648 8 my ($pos, @moves);
649  
650 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
651 ▶ 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
652 4 shift @w;
653 4 my @fen;
654 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
655 4 ($pos) = $E->of_fen(join ' ', @fen);
656 4 ▶ return (undef) unless $pos;
Branch: 1 branch
BranchTrueFalseTotal
unless $pos 1 3 4
657 }
658 0 else { return (undef) }
659  
660 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
661 1 shift @w;
662 1 for my $iccs (@w) {
663 2 my $mv = $N->move_of_iccs($iccs);
664 2 ▶ return (undef) unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 2 2
665 2 ▶ return (undef) unless grep { $_ == $mv } $pos->legal;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $mv;} $pos->legal 0 2 2
666 2 $pos->do_move($mv);
667 2 push @moves, $iccs;
668 }
669 }
670 7 return ($pos, @moves);
671 }
672  
673 9 ▶sub _ucci_go {
Subroutine _ucci_go: called CC=5 SCAR=16.1
674 9 my ($self, $pos, @w) = @_;
675 9 my %arg;
676 9 ▶ while (@w >= 2) { my $k = shift @w; $arg{$k} = shift @w }
Branch: 1 branch
BranchTrueFalseTotal
if (@w >= 2) 10 9 19
677  
678 9 my $mv;
679 9 ▶ if (defined $arg{depth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $arg{'depth'}) { } 1 8 9
680 1 ($mv) = $pos->search_to_depth($arg{depth}, 200_000_000, 0);
681 }
682 else {
683 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
684 8 ($mv) = $pos->search($nodes, 0);
685 }
686 9 ▶ return undef unless $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless $mv 1 8 9
687 8 return $N->iccs_of($mv);
688 }
689  
690 3 1;
691  
692 __END__
↑ Summary bin/xiangqi »