blib/lib/Game/Xiangqi/Terminal.pm

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