blib/lib/Game/Xiangqi/Terminal.pm

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