blib/lib/Game/Shogi/Terminal.pm

filter: Statementstmt 93.4% 581 / 622 Branchbran 81.6% 330 / 404 Conditioncond 73.5% 75 / 102 MC/DCmcdc 53.1% 34 / 64 Subroutinesub 94.3% 83 / 88 Podpod 97.3% 36 / 37 totaltotal 86.4% 1139 / 1317 CC 252 SCAR 29.8
CC
252
Cov
87%
start
44.6
run
41.2
pick_move
39.3
CRAP 19.6
↑ Summary blib/lib/Game/Shogi/USI.pm »
1 package Game::Shogi::Terminal;
2  
3 27 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 27 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 27 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 27 ▶use Game::Shogi;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Game::Shogi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Game::Shogi::Engine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use Game::Shogi::Handicap;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Game::Shogi::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use Game::Shogi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use Game::Terminal::Kit::Replay;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 27 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 27 ▶use Game::Terminal::Kit::Save;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 27 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 27 ▶use Game::Terminal::Kit::Steer;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 27 our $VERSION = '0.02';
26  
27 27 my $E = 'Game::Shogi::Engine';
28 27 my $N = 'Game::Shogi::Notation';
29  
30 27 my %GLYPH = (
31 P => "\x{6B69}", L => "\x{9999}", N => "\x{6842}", S => "\x{9280}",
32 G => "\x{91D1}", B => "\x{89D2}", R => "\x{98DB}", K => "\x{7389}",
33 k => "\x{738B}",
34 '+P' => "\x{3068}", '+L' => "\x{674F}", '+N' => "\x{572D}", '+S' => "\x{5168}",
35 '+B' => "\x{99AC}", '+R' => "\x{9F8D}",
36 );
37  
38 27 my @KANJI_RANK = (undef, map { chr } 0x4E00, 0x4E8C, 0x4E09, 0x56DB, 0x4E94, 0x516D, 0x4E03, 0x516B, 0x4E5D);
39  
40 27 my %WORD = (
41 P => 'pawn', L => 'lance', N => 'knight', S => 'silver', G => 'gold',
42 B => 'bishop', R => 'rook', K => 'king',
43 '+P' => 'tokin', '+L' => 'promoted lance', '+N' => 'promoted knight',
44 '+S' => 'promoted silver', '+B' => 'horse', '+R' => 'dragon',
45 );
46  
47 27 my @HAND_ORDER = qw(R B G S N L P);
48  
49 27 my %SIDE = (b => 'black', w => 'white');
50  
51 27 my %SIDE_COLOUR = (b => '1;31', w => '1;34');
52  
53 27 my %HOW = (
54 mate => 'checkmate',
55 no_move => 'no legal move',
56 resign => 'resignation',
57 sennichite => 'fourfold repetition (sennichite)',
58 perpetual_check => 'perpetual check',
59 impasse_declared => 'the impasse declared',
60 cap => 'the move cap',
61 );
62  
63 27 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 27 ROW_FIRST => 100,
65 27 };
66  
67 27 has _in => (is => 'rw', init_arg => 'in', private => 1);
68  
69 27 has game => (is => 'rw');
70  
71 27 has level => (is => 'ro');
72  
73 27 has seed => (is => 'ro');
74  
75 27 has side => (is => 'ro', isa => Str, default => 'b');
76  
77 27 has kanji_ranks => (is => 'rw', default => 0);
78  
79 27 has _fh => (is => 'rw', init_arg => 'out', private => 1);
80  
81 27 has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0);
82  
83 27 has _colour => (is => 'rw', init_arg => 'colour', private => 1);
84  
85 27 has _keysource => (is => 'rw', isa => Any, init_arg => 'keysource', private => 1);
86  
87 27 has _pending => (is => 'rw', isa => Any, init_arg => 'pending', private => 1);
88  
89 27 has keyboard => (is => 'rw');
90  
91 27 has screen => (is => 'rw');
92  
93 27 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
94  
95 27 has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind');
96  
97 27 has history => (is => 'rw');
98  
99 27 has save_file => (is => 'rw');
100  
101 125 ▶sub BUILD {
Subroutine BUILD: called CC=20 SCAR=30.4
Pod undocumented
102 125 my ($self) = @_;
103 125 my $side = $self->side;
104 125 ▶ die "Game::Shogi::Terminal: the side is 'b' or 'w', not '$side'\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $side eq "b" or $side eq "w" 1 124 125
Condition100%3/3
$side eq "b" or $side eq "w"
l!l&&r!l&&!r
12311
MC/DC100%2/2
$side eq "b" or $side eq "w"
$side eq "b" $side eq "w"
Truth table100%3/3
A or B
A= $side eq "b"B= $side eq "w"
ABresult
1X1
011
000
105 unless $side eq 'b' || $side eq 'w';
106 124 ▶ $self->kanji_ranks($self->kanji_ranks ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->kanji_ranks ? : 35 89 124
107 124 ▶ my $out = $self->_fh || \*STDOUT;
Condition100%2/2
$self->_fh || \*STDOUT
l!l
10321
MC/DC100%1/1
$self->_fh || \*STDOUT
$self->_fh
Truth table100%2/2
A
A= $self->_fh
Aresult
11
00
108 124 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 124 0 124
$self->_keysource ? : 17 107 124
Condition75%3/4
$self->_in || \*STDIN
l!l
5119
$self->_pending || []
l!l
0124
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
109 in => $self->_in || \*STDIN,
110 pending => $self->_pending || [],
111 ($self->_keysource ? (keysource => $self->_keysource) : ()),
112 )) unless $self->keyboard;
113 $self->screen(Game::Terminal::Kit::Screen->new(
114 out => $out,
115 interactive => (defined $self->_rewind ? ($self->_rewind ? 1 : 0)
116 124 ▶ : eval { -t $out } ? 1 : 0),
Branch: 8 branches
BranchTrueFalseTotal
unless $self->screen 124 0 124
defined $self->_rewind ? : 102 22 124
$self->_rewind ? : 0 102 102
eval { do { -t $out } } ? : 0 22 22
Game::Terminal::Kit::Screen::no_colour() ? : 15 109 124
defined $self->_colour ? : 102 7 109
$self->_colour ? : 34 68 102
$self->_ascii ? : 58 66 124
117 colour => Game::Terminal::Kit::Screen::no_colour() ? 0
118 : defined $self->_colour ? ($self->_colour ? 1 : 0)
119 : 1,
120 ascii => $self->_ascii ? 1 : 0,
121 )) unless $self->screen;
122 124 $self->screen->utf8;
123 124 ▶ $self->history(_new_history()) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 124 0 124
124 124 ▶ $self->history->name('shogi') unless defined $self->history->name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->history->name 0 124 124
125 124 $self->history->version($VERSION);
126 124 ▶ $self->history->file($self->save_file) if defined $self->save_file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->save_file 8 116 124
127 124 return;
128 }
129  
130 124 ▶sub _new_history {
Subroutine _new_history: called CC=1 SCAR=0.0
131 return Game::Terminal::Kit::History->new(
132 name => 'shogi',
133 version => $VERSION,
134 112 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
135 9 ▶ load => sub { Game::Shogi->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
136 124 );
137 }
138  
139 1 ▶sub in { my $s = shift; $s->keyboard->in(shift) if @_; $s->keyboard->in }
Subroutine in: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 0 1
140 0 ▶sub out { my $s = shift; if (@_) { $s->screen->out(shift); $s->screen->utf8 } $s->screen->out }
Subroutine out: called CC=2 SCAR=8.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 59 59
141 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 22869 22869
shift() ? : 0 0 0
$s->screen->ascii ? : 12058 10811 22869
142 3663 ▶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 3663 3663
shift() ? : 0 0 0
$s->screen->colour ? : 734 2929 3663
143 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
144 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
145  
146 229 ▶sub _say { my ($self, @lines) = @_; $self->screen->say(@lines); return }
Subroutine _say: called CC=1 SCAR=0.0
147  
148 667 ▶sub columns {
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
149 667 my (undef, $text) = @_;
150 667 $text =~ s/\e\[[0-9;]*m//g;
151 667 my $wide = () = $text =~ /[\x{1100}-\x{115F}\x{2E80}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE30}-\x{FE4F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}]/g;
152 667 return length($text) + $wide;
153 }
154  
155 178 ▶sub _padded {
Subroutine _padded: called CC=3 SCAR=11.0
156 178 my ($self, $text, $cols) = @_;
157 178 ▶ chop $text while $self->columns($text) > $cols;
Branch: 1 branch
BranchTrueFalseTotal
$self->columns($text) > $cols and do { chop $text } 0 178 178
158 178 my $short = $cols - $self->columns($text);
159 178 ▶ return $short > 0 ? $text . (' ' x $short) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$short > 0 ? : 140 38 178
160 }
161  
162 20007 ▶sub _cell {
Subroutine _cell: called CC=11 SCAR=24.0
163 20007 my ($self, $token, $spot) = @_;
164 20007 ▶ if (!defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 13165 6842 20007
165 13165 ▶ return $self->ascii ? (' ', $spot ? '*' : '.') : (' ', $spot ? "\x{FF0B}" : "\x{30FB}");
Branch: 3 branches
BranchTrueFalseTotal
$self->ascii ? : 7351 5814 13165
$spot ? : 2289 5062 7351
$spot ? : 15 5799 5814
166 }
167 6842 ▶ my $side = $token =~ /[a-z]/ ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /[a-z]/u ? : 3412 3430 6842
168 6842 ▶ if ($self->ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ascii) 3179 3663 6842
169 3179 return (' ' x (3 - length $token), $self->screen->paint($token, $SIDE_COLOUR{$side}));
170 }
171 3663 my $key = uc $token;
172 3663 ▶ $key = 'k' if $key eq 'K' && $side eq 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "K" and $side eq "w" 102 3561 3663
Condition100%3/3
$key eq "K" and $side eq "w"
!ll&&!rl&&r
3459102102
MC/DC100%2/2
$key eq "K" and $side eq "w"
$key eq "K" $side eq "w"
Truth table100%3/3
A and B
A= $key eq "K"B= $side eq "w"
ABresult
0X0
100
111
173 3663 ▶ return ($self->colour ? ' ' : $side eq 'w' ? 'v' : ' ', $self->screen->paint($GLYPH{$key}, $SIDE_COLOUR{$side}));
Branch: 2 branches
BranchTrueFalseTotal
$self->colour ? : 734 2929 3663
$side eq 'w' ? : 1462 1467 2929
174 }
175  
176 212 ▶sub _hand_glyph {
Subroutine _hand_glyph: called CC=3 SCAR=11.0
177 212 my ($self, $side, $letter) = @_;
178 212 ▶ return $self->screen->paint($self->ascii ? ($side eq 'w' ? lc $letter : $letter) : $GLYPH{$letter}, $SIDE_COLOUR{$side});
Branch: 2 branches
BranchTrueFalseTotal
$self->ascii ? : 128 84 212
$side eq 'w' ? : 47 81 128
179 }
180  
181 497 ▶sub _hand_items {
Subroutine _hand_items: called CC=1 SCAR=0.0
182 497 my ($self, $pos, $side) = @_;
183 497 my $hand = $pos->hands->{$side};
184 497 return grep { $hand->{$_} } @HAND_ORDER;
185 }
186  
187 494 ▶sub _hand_line {
Subroutine _hand_line: called CC=10 SCAR=23.0
188 494 my ($self, $pos, $side, %o) = @_;
189 494 my $hand = $pos->hands->{$side};
190 494 my @items = $self->_hand_items($pos, $side);
191 494 my $label = sprintf ' %-6s', $SIDE{$side};
192 494 ▶ return $label . ' -' unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 318 176 494
193 176 my @gap = (' ') x (@items + 1);
194 176 my $marked = $side eq $pos->side_to_move;
195 176 for my $pair ([ $o{from}, '(', ')' ], [ $o{cursor}, '[', ']' ]) {
196 352 my ($at, $left, $right) = @$pair;
197 352 ▶ next unless $marked && defined $at && $at =~ /\A([A-Z])\*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $marked and defined $at and $at =~ /\A([A-Z])\*\z/u 302 50 352
Condition100%6/6
$marked and defined $at and $at =~ /\A([A-Z])\*\z/u
!ll&&!rl&&r
2218150
$marked and defined $at
!ll&&!rl&&r
14081131
MC/DC100%3/3
$marked and defined $at and $at =~ /\A([A-Z])\*\z/u
$marked defined $at $at =~ /\A([A-Z])\*\z/u
Truth table100%4/4
A and B and C
A= $markedB= defined $atC= $at =~ /\A([A-Z])\*\z/u
ABCresult
0XX0
10X0
1100
1111
198 50 for my $i (0 .. $#items) {
199 56 ▶ next unless $items[$i] eq $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $items[$i] eq $1 22 34 56
200 34 ($gap[$i], $gap[$i + 1]) = ($left, $right);
201 }
202 }
203 176 my $line = $label;
204 176 for my $i (0 .. $#items) {
205 212 my $n = $hand->{ $items[$i] };
206 212 ▶ $line .= $gap[$i] . $self->_hand_glyph($side, $items[$i]) . ($n > 1 ? $n : '');
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 ? : 33 179 212
207 }
208 176 return $line . $gap[-1];
209 }
210  
211 247 ▶sub _files { $_[1] ? (1 .. 9) : reverse(1 .. 9) }
Subroutine _files: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 35 212 247
212 247 ▶sub _ranks { $_[1] ? reverse(1 .. 9) : (1 .. 9) }
Subroutine _ranks: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 35 212 247
213  
214 2470 ▶sub _rank_label {
Subroutine _rank_label: called CC=3 SCAR=11.0
215 2470 my ($self, $rank) = @_;
216 2470 ▶ return $self->kanji_ranks && !$self->ascii ? $KANJI_RANK[$rank] : chr(ord('a') + $rank - 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->kanji_ranks && !$self->ascii ? : 180 2290 2470
Condition100%3/3
$self->kanji_ranks and not $self->ascii
!ll&&!rl&&r
2120170180
MC/DC100%2/2
$self->kanji_ranks and not $self->ascii
$self->kanji_ranks not $self->ascii
Truth table100%3/3
A and not(B)
A= $self->kanji_ranksB= not $self->ascii
ABresult
0X0
100
111
217 }
218  
219 247 ▶sub board_lines {
Subroutine board_lines: called CC=20 SCAR=30.0
Pod documented
220 247 my ($self, %o) = @_;
221 247 ▶ my $pos = $o{position} || ($self->game ? $self->game->board : Game::Shogi::Engine->new);
Branch: 1 branch
BranchTrueFalseTotal
$self->game ? : 129 0 129
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->board : 'Game::Shogi::Engine'->new)
l!l&&r!l&&!r
1181290
MC/DC0%0/2
$o{'position'} || ($self->game ? $self->game->board : 'Game::Shogi::Engine'->new)
$o{'position'} ($self->game ? $self->game->board : 'Game::Shogi::Engine'->new)
Truth table66%2/3
A || B
A= $o{'position'}B= ($self->game ? $self->game->board : 'Game::Shogi::Engine'->new)
ABresult
1X1
011
000
222 247 ▶ my $flip = $o{flip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o{'flip'} ? : 35 212 247
223 247 my @files = $self->_files($flip);
224 247 my (%spot, %take);
225 247 ▶ for my $name (@{ $o{spots} || [] }) {
Condition100%2/2
$o{'spots'} || []
l!l
68179
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
Aresult
11
00
226 2356 ▶ defined $pos->at($E->square($name)) ? ($take{$name} = 1) : ($spot{$name} = 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $pos->at($E->square($name)) ? : 52 2304 2356
227 }
228 247 ▶ my ($top, $bottom) = $flip ? ('b', 'w') : ('w', 'b');
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 35 212 247
229  
230 247 my @lines = ($self->_hand_line($pos, $top, %o));
231 247 my $legend = '';
232 247 ▶ $legend .= ($self->ascii ? ' ' . $_ : ' ' . $_ . ' ') for @files;
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1170 1053 2223
233 247 my $label_cols = $self->columns($self->_rank_label(1));
234 247 push @lines, $legend . ' ' . ' ' . (' ' x $label_cols);
235  
236 247 for my $rank ($self->_ranks($flip)) {
237 2223 my @names = map { $_ . chr(ord('a') + $rank - 1) } @files;
238 2223 my @gap = (' ') x 10;
239 2223 my @opens;
240 2223 for my $i (0 .. 8) {
241 20007 ▶ next unless $take{ $names[$i] };
Branch: 1 branch
BranchTrueFalseTotal
unless $take{$names[$i]} 19955 52 20007
242 52 ($gap[$i], $gap[$i + 1], $opens[$i]) = ('x', 'x', 1);
243 }
244 2223 for my $pair ([ $o{from}, '(', ')' ], [ $o{cursor}, '[', ']' ]) {
245 4446 my ($at, $left, $right) = @$pair;
246 4446 ▶ next unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 2556 1890 4446
247 1890 for my $i (0 .. 8) {
248 17010 ▶ next unless $names[$i] eq $at;
Branch: 1 branch
BranchTrueFalseTotal
unless $names[$i] eq $at 16850 160 17010
249 160 ($gap[$i], $gap[$i + 1], $opens[$i]) = ($left, $right, 1);
250 }
251 }
252 2223 my $row = '';
253 2223 for my $i (0 .. 8) {
254 20007 my ($pad, $body) = $self->_cell($pos->at($E->square($names[$i])), $spot{ $names[$i] });
255 20007 ▶ $row .= $opens[$i] && $pad =~ /\A +\z/ ? $pad . $gap[$i] . $body : $gap[$i] . $pad . $body;
Branch: 1 branch
BranchTrueFalseTotal
$opens[$i] && $pad =~ /\A +\z/u ? : 181 19826 20007
Condition100%3/3
$opens[$i] and $pad =~ /\A +\z/u
!ll&&!rl&&r
1981412181
MC/DC100%2/2
$opens[$i] and $pad =~ /\A +\z/u
$opens[$i] $pad =~ /\A +\z/u
Truth table100%3/3
A and B
A= $opens[$i]B= $pad =~ /\A +\z/u
ABresult
0X0
100
111
256 }
257 2223 push @lines, $row . $gap[9] . ' ' . $self->_rank_label($rank);
258 }
259 247 push @lines, $self->_hand_line($pos, $bottom, %o);
260 247 return @lines;
261 }
262  
263 61 ▶sub draw {
Subroutine draw: called CC=1 SCAR=0.0
Pod documented
264 61 my ($self, %o) = @_;
265 61 $self->_say($self->board_lines(flip => $self->side eq 'w', %o));
266 61 return $self;
267 }
268  
269 13 ▶sub keys_available { return $_[0]->keyboard->keys_available }
Subroutine keys_available: called CC=1 SCAR=0.0
Pod documented
270  
271 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
272  
273 5 ▶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 ? : 5 0 5
274  
275 30 ▶sub leave_raw { $_[0]->keyboard->leave_raw; return $_[0] }
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
276  
277 89 ▶sub read_key { return $_[0]->keyboard->read_key }
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
278  
279 55 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.1
Pod documented
280 55 my ($self, @set) = @_;
281 55 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 25 30 55
$set[0] ? : 0 25 25
282 55 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 42 13 55
$self->_picking ? : 5 37 42
283 13 return $self->keys_available;
284 }
285  
286 89 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=19.9
Pod documented
287 89 my ($self, @set) = @_;
288 89 ▶ $self->_rewind($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 89 89
$set[0] ? : 0 0 0
289 89 ▶ return $self->_rewind ? 1 : 0 if defined $self->_rewind;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_rewind 87 2 89
$self->_rewind ? : 0 87 87
290 2 ▶ return eval { -t $self->out } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->out } } ? : 0 2 2
291 }
292  
293 8332 ▶sub _from_of {
Subroutine _from_of: called CC=2 SCAR=6.9
294 8332 my ($usi) = @_;
295 8332 my $m = Game::Shogi::Move->of($usi);
296 8332 ▶ return $m->is_drop ? $m->drop . '*' : $m->from;
Branch: 1 branch
BranchTrueFalseTotal
$m->is_drop ? : 5112 3220 8332
297 }
298  
299 2997 ▶sub _drawn_at {
Subroutine _drawn_at: called CC=5 SCAR=16.4
300 2997 my ($self, $pos, $origin, $flip) = @_;
301 2997 ▶ if ($origin =~ /\A([A-Z])\*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin =~ /\A([A-Z])\*\z/u) 3 2994 2997
302 3 my @items = $self->_hand_items($pos, $pos->side_to_move);
303 3 my ($i) = grep { $items[$_] eq $1 } 0 .. $#items;
304 3 ▶ return [ $i // 0, ROW_FIRST * 10 ];
Condition50%1/2
$i // 0
l!l
30
MC/DC0%0/1
$i // 0
$i
Truth table50%1/2
A
A= $i
Aresult
11
00
305 }
306 2994 my ($file, $rank) = ($origin =~ /\A([1-9])([a-i])\z/);
307 2994 $rank = ord($rank) - ord('a') + 1;
308 2994 ▶ return [ $flip ? $file - 1 : 9 - $file, ROW_FIRST * ($flip ? 9 - $rank : $rank - 1) ];
Branch: 2 branches
BranchTrueFalseTotal
$flip ? : 0 2994 2994
$flip ? : 0 2994 2994
309 }
310  
311 124 ▶sub _as_drawn {
Subroutine _as_drawn: called CC=2 SCAR=7.0
312 124 my ($self, $pos, $set, $flip) = @_;
313 124 my %at = map { $_ => $self->_drawn_at($pos, $_, $flip) } @$set;
314 124 ▶ return [ sort { $at{$a}[1] <=> $at{$b}[1] || $at{$a}[0] <=> $at{$b}[0] } @$set ];
Condition66%2/3
$at{$a}[1] <=> $at{$b}[1] || $at{$a}[0] <=> $at{$b}[0]
l!l&&r!l&&!r
48827920
MC/DC0%0/2
$at{$a}[1] <=> $at{$b}[1] || $at{$a}[0] <=> $at{$b}[0]
$at{$a}[1] <=> $at{$b}[1] $at{$a}[0] <=> $at{$b}[0]
Truth table66%2/3
A || B
A= $at{$a}[1] <=> $at{$b}[1]B= $at{$a}[0] <=> $at{$b}[0]
ABresult
1X1
011
000
315 }
316  
317 32 ▶sub origins {
Subroutine origins: called CC=1 SCAR=0.0
Pod documented
318 32 my ($self, $pos, $flip) = @_;
319 32 my %seen;
320 32 my @from = grep { !$seen{$_}++ } map { _from_of($_) } $pos->legal;
321 32 return $self->_as_drawn($pos, \@from, $flip);
322 }
323  
324 92 ▶sub targets {
Subroutine targets: called CC=1 SCAR=0.0
Pod documented
325 92 my ($self, $pos, $from, $flip) = @_;
326 92 my %seen;
327 2599 my @to = grep { !$seen{$_}++ }
328 2599 map { Game::Shogi::Move->of($_)->to }
329 92 grep { _from_of($_) eq $from } $pos->legal;
330 92 return $self->_as_drawn($pos, \@to, $flip);
331 }
332  
333 60 ▶sub choices {
Subroutine choices: called CC=2 SCAR=6.9
Pod documented
334 60 my ($self, $pos, $from, $to) = @_;
335 60 ▶ return grep { _from_of($_) eq $from && Game::Shogi::Move->of($_)->to eq $to } $pos->legal;
Condition100%3/3
_from_of($_) eq $from && 'Game::Shogi::Move'->of($_)->to eq $to
!ll&&!rl&&r
674232871
MC/DC100%2/2
_from_of($_) eq $from && 'Game::Shogi::Move'->of($_)->to eq $to
_from_of($_) eq $from 'Game::Shogi::Move'->of($_)->to eq $to
Truth table100%3/3
A && B
A= _from_of($_) eq $fromB= 'Game::Shogi::Move'->of($_)->to eq $to
ABresult
0X0
100
111
336 }
337  
338 44 ▶sub step {
Subroutine step: called CC=8 SCAR=32.1
Pod documented
339 44 my ($self, $pos, $set, $at, $key, $flip) = @_;
340 44 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 0 44 44
341 44 ▶ return $#$set if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 0 44 44
342 44 ▶ return Game::Terminal::Kit::Steer::ring(scalar @$set, $at, 1)
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "right" or $key eq "tab" 44 0 44
Condition66%2/3
$key eq "right" or $key eq "tab"
l!l&&r!l&&!r
1430
MC/DC0%0/2
$key eq "right" or $key eq "tab"
$key eq "right" $key eq "tab"
Truth table66%2/3
A or B
A= $key eq "right"B= $key eq "tab"
ABresult
1X1
011
000
343 if $key eq 'right' || $key eq 'tab';
344 0 ▶ return Game::Terminal::Kit::Steer::ring(scalar @$set, $at, -1)
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "left" or $key eq "back_tab" 0 0 0
Condition0%0/3
$key eq "left" or $key eq "back_tab"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "left" or $key eq "back_tab"
$key eq "left" $key eq "back_tab"
Truth table0%0/3
A or B
A= $key eq "left"B= $key eq "back_tab"
ABresult
1X1
011
000
345 if $key eq 'left' || $key eq 'back_tab';
346 0 my @drawn = map { $self->_drawn_at($pos, $_, $flip) } @$set;
347 0 my $next = Game::Terminal::Kit::Steer::nearest(\@drawn, $at, Game::Terminal::Kit::Steer::way($key));
348 0 ▶ return defined $next ? $next : $at;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 0 0 0
349 }
350  
351 40 ▶sub piece_name {
Subroutine piece_name: called CC=3 SCAR=11.1
Pod documented
352 40 my ($self, $token) = @_;
353 40 ▶ return 'nothing' unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 40 40
354 40 ▶ return ($token =~ /[a-z]/ ? 'white ' : 'black ') . $WORD{ uc $token };
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /[a-z]/u ? : 3 37 40
355 }
356  
357 38 ▶sub piece_line {
Subroutine piece_line: called CC=5 SCAR=16.1
Pod documented
358 38 my ($self, $pos, $from) = @_;
359 38 my $n = scalar @{ $self->targets($pos, $from) };
360 38 ▶ my $count = $n . ($n == 1 ? ' square' : ' squares');
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 34 4 38
361 38 ▶ if ($from =~ /\A([A-Z])\*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /\A([A-Z])\*\z/u) 1 37 38
362 1 my $held = $pos->hand($pos->side_to_move, $1);
363 1 return sprintf 'a %s in hand (%d), %s to drop it on', $WORD{$1}, $held, $count;
364 }
365 37 my $line = sprintf 'the %s on %s, %s', $self->piece_name($pos->at($E->square($from))), $from, $count;
366 37 ▶ my $them = $pos->side_to_move eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$pos->side_to_move eq 'b' ? : 37 0 37
367 37 ▶ $line .= ', and it is attacked' if $pos->attacked($E->square($from), $them);
Branch: 1 branch
BranchTrueFalseTotal
if $pos->attacked($E->square($from), $them) 2 35 37
368 37 return $line;
369 }
370  
371 51 ▶sub move_line {
Subroutine move_line: called CC=9 SCAR=22.3
Pod documented
372 51 my ($self, $pos, $usi) = @_;
373 51 my $m = Game::Shogi::Move->of($usi);
374 51 ▶ my $prey = $m->is_drop ? undef : $pos->at($E->square($m->to));
Branch: 1 branch
BranchTrueFalseTotal
$m->is_drop ? : 32 19 51
375 51 ▶ my $line = $N->hodges($pos, $usi) . ($self->ascii ? '' : ' ' . $N->japanese($pos, $usi)) . ': '
Branch: 3 branches
BranchTrueFalseTotal
$self->ascii ? : 48 3 51
$m->is_drop ? : 32 19 51
defined $prey ? : 3 16 19
376 . ($m->is_drop ? 'a drop' : defined $prey ? 'takes the ' . $self->piece_name($prey) : 'a quiet move');
377 51 my $after = $pos->clone;
378 51 $after->do_move($usi);
379 51 my $outcome = $after->outcome;
380 51 ▶ my $them = $pos->side_to_move eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$pos->side_to_move eq 'b' ? : 51 0 51
381 51 ▶ $line .= $outcome && $outcome->how eq 'mate' ? ', and it is mate'
Branch: 2 branches
BranchTrueFalseTotal
$outcome && $outcome->how eq 'mate' ? : 0 51 51
$after->in_check($them) ? : 1 50 51
Condition33%1/3
$outcome and $outcome->how eq "mate"
!ll&&!rl&&r
5100
MC/DC0%0/2
$outcome and $outcome->how eq "mate"
$outcome $outcome->how eq "mate"
Truth table33%1/3
A and B
A= $outcomeB= $outcome->how eq "mate"
ABresult
0X0
100
111
382 : $after->in_check($them) ? ', and it checks'
383 : '';
384 51 return $line;
385 }
386  
387 24 ▶sub pick_move {
Subroutine pick_move: called CC=35 SCAR=36.7
Pod documented
388 24 my ($self, $g) = @_;
389 24 my $pos = $g->board;
390 24 ▶ my $flip = $self->side eq 'w' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->side eq 'w' ? : 0 24 24
391 24 my $from = $self->origins($pos, $flip);
392 24 ▶ return undef unless @$from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$from 0 24 24
393  
394 24 my ($at, $held, $to, $toat, $asking, $drawn) = (0, undef, [], 0, undef, 0);
395  
396 24 while (1) {
397 89 my %o = (flip => $flip, position => $pos);
398 89 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) { } 51 38 89
399 51 $to = $self->targets($pos, $held, $flip);
400 51 %o = (%o, from => $held, cursor => $to->[$toat], spots => $to);
401 }
402 else {
403 38 %o = (%o, cursor => $from->[$at]);
404 }
405  
406 89 my @lines = $self->board_lines(%o);
407 89 my ($caption, $help);
408 89 ▶ if ($asking) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asking) { } 4 85 89
elsif (defined $held) { } 47 38 85
409 4 $caption = $self->move_line($pos, "$asking+") . ', if it promotes';
410 4 $help = 'promote? y or n, escape to choose again';
411 }
412 elsif (defined $held) {
413 47 my @c = $self->choices($pos, $held, $to->[$toat]);
414 47 my ($plain) = grep { !/\+\z/ } @c;
415 47 ▶ $caption = $self->move_line($pos, $plain // $c[0]) . (@c > 1 ? ', and it may promote' : '');
Branch: 1 branch
BranchTrueFalseTotal
@c > 1 ? : 6 41 47
Condition66%2/3
$plain // $c[0]
l!l&&r!l&&!r
4610
MC/DC0%0/2
$plain // $c[0]
$plain $c[0]
Truth table66%2/3
A // B
A= $plainB= $c[0]
ABresult
1X1
011
000
416 47 $help = 'arrows to choose, enter or space to play, escape to pick another';
417 }
418 else {
419 38 $caption = $self->piece_line($pos, $from->[$at]);
420 38 $help = 'arrows to choose a piece, enter or space to take it, S to save, ? for the keys';
421 }
422 89 push @lines, $self->_padded(" $caption", 79), $self->_padded(" $help", 79);
423  
424 89 $self->screen->interactive($self->rewind);
425 89 $self->screen->rewind($drawn);
426 89 $self->_say(@lines);
427 89 $drawn = scalar @lines;
428  
429 89 my $key = $self->read_key;
430 89 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 88 89
431 88 ▶ 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" 8 80 88
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
8080
$key eq "q" or $key eq "interrupt"
l!l&&r!l&&!r
8080
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
432  
433 80 ▶ if ($asking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asking) 4 76 80
434 4 ▶ if ($key eq 'y' || $key eq 'n') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "y" or $key eq "n") 2 2 4
Condition100%3/3
$key eq "y" or $key eq "n"
l!l&&r!l&&!r
112
MC/DC100%2/2
$key eq "y" or $key eq "n"
$key eq "y" $key eq "n"
Truth table100%3/3
A or B
A= $key eq "y"B= $key eq "n"
ABresult
1X1
011
000
435 2 ▶ return $key eq 'y' ? "$asking+" : $asking;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'y' ? : 1 1 2
436 }
437 2 ▶ $asking = undef if $key eq 'escape' || $key eq 'backspace';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "escape" or $key eq "backspace" 2 0 2
Condition33%1/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
200
MC/DC0%0/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table33%1/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
438 2 next;
439 }
440 76 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 21 55 76
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
21055
MC/DC50%1/2
$key eq "enter" or $key eq " "
$key eq "enter" $key eq " "
Truth table66%2/3
A or B
A= $key eq "enter"B= $key eq " "
ABresult
1X1
011
000
441 21 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) 10 11 21
442 10 my @c = $self->choices($pos, $held, $to->[$toat]);
443 10 ▶ return $c[0] if @c == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @c == 1 6 4 10
444 4 ($asking) = grep { !/\+\z/ } @c;
445 4 next;
446 }
447 11 ($held, $toat) = ($from->[$at], 0);
448 11 next;
449 }
450 55 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 2 53 55
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
2053
MC/DC50%1/2
$key eq "escape" or $key eq "backspace"
$key eq "escape" $key eq "backspace"
Truth table66%2/3
A or B
A= $key eq "escape"B= $key eq "backspace"
ABresult
1X1
011
000
451 2 $held = undef;
452 2 next;
453 }
454 53 ▶ 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/u) 44 9 53
455 44 ▶ defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 32 12 44
456 ? ($toat = $self->step($pos, $to, $toat, $key, $flip))
457 : ($at = $self->step($pos, $from, $at, $key, $flip));
458 44 next;
459 }
460 9 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 3 6 9
461 6 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 2 4 6
462 4 ▶ return 'declare' if $key eq 'd';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "d" 1 3 4
463 3 ▶ return 'save' if $key eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "S" 1 2 3
464 0 ▶ if ($key eq '?') { $self->_keys; $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 2 2
465 0 ▶ if ($key eq 'l') { $self->_legal($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "l") 0 2 2
466 0 ▶ if ($key eq 's') { $self->_say($g->sfen); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "s") 0 2 2
467 2 ▶ if ($key eq 'g') { $self->_history($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g") 1 1 2
468 1 ▶ if ($key eq 'h') { $self->_hint($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 1 0 1
469 }
470 }
471  
472 0 ▶sub _keys {
Subroutine _keys: not called CC=1 SCAR=6.9
473 0 my ($self) = @_;
474 0 $self->_say(
475 'arrows move the cursor; it only stops where a move is, and your hand is the last row',
476 'enter take the piece under the cursor, then play to the square',
477 'y n promote or not, when the move may do either',
478 'escape put the piece back and choose another',
479 'tab the next one, whichever you are choosing',
480 'h the strongest move this engine can find',
481 'l s g every move you have, the SFEN, the moves so far',
482 'd declare the impasse (entering king), which wins when the count allows it',
483 'u r take your last move back, play it again',
484 'S save the game, to carry on with --resume or watch with --replay',
485 'q give up and leave',
486 );
487 0 return $self;
488 }
489  
490 30 ▶sub start {
Subroutine start: called CC=41 SCAR=38.0
Pod documented
491 30 my ($self) = @_;
492 30 ▶ $self->game(Game::Shogi->new) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 3 27 30
493 30 my $side = $self->side;
494 30 ▶ my $flip = $side eq 'w' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'w' ? : 1 29 30
495 30 $self->history->start($self->game);
496  
497 30 ▶ my $pick = $self->picking && $self->enter_raw ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking && $self->enter_raw ? : 5 25 30
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
2505
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
498 30 ▶ $self->picking(0) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 25 5 30
499 30 ▶ $self->_say($pick
Branch: 1 branch
BranchTrueFalseTotal
$pick ? : 5 25 30
500 ? 'shogi. arrows to choose a move, enter or space to play it, ? for the keys.'
501 : 'shogi. a move is USI, like 7g7f or P*5e. "help" for the rest.');
502 30 ▶ $self->draw unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 25 5 30
503  
504 30 ▶ while ($self->game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active") 88 2 90
505 88 my $g = $self->game;
506 88 ▶ if ($g->turn ne $side) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->turn ne $side) 19 69 88
507 19 ▶ if (Game::Shogi::Bot->declares($g, $g->turn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Game::Shogi::Bot"->declares($g, $g->turn)) 0 19 19
508 0 $self->_say('the bot declares the impasse');
509 0 $g->declare;
510 0 last;
511 }
512 19 my $mv = do {
513 19 local $Game::Shogi::Bot::LEVEL = $self->level;
514 19 Game::Shogi::Bot->choose($g, $g->turn, seed => $self->seed);
515 };
516 19 ▶ last unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 19 19
517 19 $self->_say($self->_describe($g, $mv, 'the bot plays'));
518 19 my $refusal = $g->play($mv);
519 19 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 18 19
520 1 $self->_say('the bot offered a move the game refused: ' . $refusal->code);
521 1 last;
522 }
523 18 ▶ $self->draw unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 15 3 18
524 18 next;
525 }
526  
527 69 $self->person_turn;
528  
529 69 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 12 57 69
530 12 my $chosen = $self->pick_move($g);
531 12 ▶ last if !defined $chosen || $chosen eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $chosen or $chosen eq "quit" 5 7 12
Condition66%2/3
not defined $chosen or $chosen eq "quit"
l!l&&r!l&&!r
057
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
532 7 ▶ $self->take_back, next if $chosen eq 'undo';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "undo" 2 5 7
533 5 ▶ $self->play_again, next if $chosen eq 'redo';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "redo" 1 4 5
534 4 ▶ $self->_declare($g), next if $chosen eq 'declare';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "declare" 0 4 4
535 4 ▶ $self->_ask_save, next if $chosen eq 'save';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "save" 1 3 4
536 3 my $refusal = $g->play($chosen);
537 3 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') if $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 0 3 3
538 3 next;
539 }
540  
541 57 my $line = $self->keyboard->read_line($self->_prompt($g), $self->out);
542 57 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 57 57
543 57 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 57 57
544  
545 57 ▶ last if $line eq 'quit' || $line eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" or $line eq "q" 22 35 57
Condition66%2/3
$line eq "quit" or $line eq "q"
l!l&&r!l&&!r
22035
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
546 35 ▶ $self->_help, next if $line eq 'help' || $line eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "help" or $line eq "?" 0 35 35
Condition33%1/3
$line eq "help" or $line eq "?"
l!l&&r!l&&!r
0035
MC/DC0%0/2
$line eq "help" or $line eq "?"
$line eq "help" $line eq "?"
Truth table33%1/3
A or B
A= $line eq "help"B= $line eq "?"
ABresult
1X1
011
000
547 35 ▶ $self->_legal($g), next if $line eq 'legal';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "legal" 1 34 35
548 34 ▶ $self->_say($g->sfen), next if $line eq 'sfen';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "sfen" 1 33 34
549 33 ▶ $self->_history($g), next if $line eq 'log';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "log" 2 31 33
550 31 ▶ $self->draw, next if $line eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "board" 0 31 31
551 31 ▶ $self->_hint($g), next if $line eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "hint" 1 30 31
552 30 ▶ $self->_declare($g), next if $line eq 'declare';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "declare" 1 29 30
553 29 ▶ 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 17 29
554 12 $self->_say($self->save($file));
555 12 next;
556 }
557 17 ▶ if ($line eq 'undo' || $line eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "undo" or $line eq "redo") 2 15 17
Condition66%2/3
$line eq "undo" or $line eq "redo"
l!l&&r!l&&!r
2015
MC/DC50%1/2
$line eq "undo" or $line eq "redo"
$line eq "undo" $line eq "redo"
Truth table66%2/3
A or B
A= $line eq "undo"B= $line eq "redo"
ABresult
1X1
011
000
558 2 ▶ $self->draw if $line eq 'undo' ? $self->take_back : $self->play_again;
Branch: 2 branches
BranchTrueFalseTotal
if $line eq "undo" ? $self->take_back : $self->play_again 2 0 2
$line eq 'undo' ? : 2 0 2
559 2 next;
560 }
561  
562 15 my $refusal = $g->play($line);
563 15 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 14 15
564 1 $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')');
565 1 next;
566 }
567 14 $self->draw;
568 }
569  
570 30 $self->leave_raw;
571 30 ▶ $self->draw if $pick;
Branch: 1 branch
BranchTrueFalseTotal
if $pick 5 25 30
572 30 return $self->_verdict($self->game);
573 }
574  
575 69 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
576 69 my ($self) = @_;
577 69 $self->history->turn($self->game);
578 69 return $self;
579 }
580  
581 4 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
582 4 my ($self) = @_;
583 4 my $game = $self->history->undo;
584 4 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 3 4
585 1 $self->_say('nothing to take back');
586 1 return 0;
587 }
588 3 $self->swap($game);
589 3 $self->_say('taken back');
590 3 return 1;
591 }
592  
593 1 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=7.5
Pod documented
594 1 my ($self) = @_;
595 1 my $game = $self->history->redo;
596 1 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 1 1
597 0 $self->_say('nothing to play again');
598 0 return 0;
599 }
600 1 $self->swap($game);
601 1 $self->_say('played again');
602 1 return 1;
603 }
604  
605 4 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
606 4 my ($self, $game) = @_;
607 4 $self->game($game);
608 4 return $self;
609 }
610  
611 1 ▶sub _declare {
Subroutine _declare: called CC=2 SCAR=7.0
612 1 my ($self, $g) = @_;
613 1 my $refusal = $g->declare;
614 1 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') if $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 1 0 1
615 1 return $self;
616 }
617  
618 57 ▶sub _prompt {
Subroutine _prompt: called CC=1 SCAR=0.0
619 57 my ($self, $g) = @_;
620 57 return sprintf '%s to move> ', $SIDE{ $g->turn };
621 }
622  
623 21 ▶sub _describe {
Subroutine _describe: called CC=3 SCAR=11.0
624 21 my ($self, $g, $usi, $lead) = @_;
625 21 my $b = $g->board;
626 21 my $hodges = $N->hodges($b, $usi);
627 21 ▶ return "$lead $usi" unless defined $hodges;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hodges 1 20 21
628 20 ▶ return "$lead $usi ($hodges)" if $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii 8 12 20
629 12 return "$lead $usi ($hodges, " . $N->japanese($b, $usi) . ')';
630 }
631  
632 0 ▶sub _help {
Subroutine _help: not called CC=1 SCAR=6.9
633 0 my ($self) = @_;
634 0 $self->_say(
635 'a move is USI: from then to, like 7g7f, + to promote, like 8h2b+, and a drop like P*5e',
636 'board draw it again',
637 'legal every move you have',
638 'sfen the position as SFEN',
639 'log the moves so far',
640 'hint the strongest move this engine can find',
641 'declare declare the impasse (entering king), which wins when the count allows it',
642 "undo take your last move back, and the bot's reply with it",
643 'redo play again what undo took back',
644 'save save the game, to carry on with --resume; save FILE to that file',
645 'quit give up and leave',
646 );
647 0 return $self;
648 }
649  
650 1 ▶sub _legal {
Subroutine _legal: called CC=1 SCAR=0.0
651 1 my ($self, $g) = @_;
652 1 my @m = map { $_->usi } $g->legal;
653 1 $self->_say(scalar(@m) . ' moves: ' . join ' ', @m);
654 1 return $self;
655 }
656  
657 3 ▶sub _history {
Subroutine _history: called CC=3 SCAR=11.0
658 3 my ($self, $g) = @_;
659 3 my $record = $g->record;
660 3 ▶ my @line = map { $self->ascii ? $_->{hodges} : $_->{japanese} . ' ' . $_->{hodges} } @{ $record->{moves} };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 4 2 6
661 3 ▶ $self->_say(@line ? join(' ', map { ($_ + 1) . ". $line[$_]" } 0 .. $#line) : 'no moves yet');
Branch: 1 branch
BranchTrueFalseTotal
@line ? : 3 0 3
662 3 return $self;
663 }
664  
665 2 ▶sub _hint {
Subroutine _hint: called CC=2 SCAR=7.0
666 2 my ($self, $g) = @_;
667 2 my $mv = Game::Shogi::Bot->hint($g, $self->side, seed => $self->seed);
668 2 ▶ $self->_say(defined $mv ? $self->_describe($g, $mv, 'try') : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 2 0 2
669 2 return $self;
670 }
671  
672 30 ▶sub _verdict {
Subroutine _verdict: called CC=5 SCAR=16.8
673 30 my ($self, $g) = @_;
674 30 ▶ my $r = $g->result or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $g->result or return 0 2 28 30
675 2 ▶ my $why = $HOW{ $r->how } // $r->how;
Condition33%1/3
$HOW{$r->how} // $r->how
l!l&&r!l&&!r
200
MC/DC0%0/2
$HOW{$r->how} // $r->how
$HOW{$r->how} $r->how
Truth table33%1/3
A // B
A= $HOW{$r->how}B= $r->how
ABresult
1X1
011
000
676 0 ▶ if ($r->is_draw) { $self->_say("drawn: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_draw) 0 2 2
677 2 ▶ if ($r->winner eq $self->side) { $self->_say("you won: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($r->winner eq $self->side) 1 1 2
678 1 $self->_say("you lost: $why");
679 1 return 1;
680 }
681  
682 1 ▶sub _ask_save {
Subroutine _ask_save: called CC=2 SCAR=7.0
683 1 my ($self) = @_;
684 1 my $file = Game::Terminal::Kit::Save::ask_file($self->keyboard, $self->screen,
685 default => $self->save_default,
686 head => [ $self->board_lines, '' ]);
687 1 ▶ $self->_say($self->save($file)) if defined $file;
Branch: 1 branch
BranchTrueFalseTotal
if defined $file 1 0 1
688 1 return $self;
689 }
690  
691 13 ▶sub settings {
Subroutine settings: called CC=4 SCAR=13.9
Pod documented
692 13 my ($self) = @_;
693 return {
694 13 ▶ side => $self->side,
Branch: 3 branches
BranchTrueFalseTotal
$self->kanji_ranks ? : 0 13 13
defined $self->seed ? : 12 1 13
defined $self->level ? : 12 1 13
695 'kanji-ranks' => $self->kanji_ranks ? 1 : 0,
696 (defined $self->seed ? (seed => $self->seed) : ()),
697 (defined $self->level ? (level => $self->level) : ()),
698 };
699 }
700  
701 8 ▶sub save_default {
Subroutine save_default: called CC=2 SCAR=7.1
Pod documented
702 8 my ($self) = @_;
703 8 my $file = $self->history->file;
704 8 ▶ return defined $file ? $file : Game::Terminal::Kit::Save::default_file('shogi');
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 8 0 8
705 }
706  
707 13 ▶sub save {
Subroutine save: called CC=4 SCAR=13.9
Pod documented
708 13 my ($self, $file) = @_;
709 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
710 13 my $history = $self->history;
711 13 $history->settings($self->settings);
712 13 $history->version($VERSION);
713 13 my $path = eval { $history->saved($self->game)->write_file($file) };
714 13 ▶ return 'not saved: ' . Game::Terminal::Kit::Save::why($@) unless $path;
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 13 13
715 13 $history->file($path);
716 13 return "saved to $path";
717 }
718  
719 9 ▶sub read_save {
Subroutine read_save: called CC=1 SCAR=0.0
Pod documented
720 9 my ($class, $file) = @_;
721 9 return Game::Terminal::Kit::Save->read_game($file, 'shogi');
722 }
723  
724 3 ▶sub replay_lines {
Subroutine replay_lines: called CC=4 SCAR=13.9
Pod documented
725 3 my ($self, $game) = @_;
726 3 my $was = $self->game;
727 3 $self->game($game);
728 3 my @lines = eval {
729 3 my @moves = $game->moves;
730 3 ▶ ($self->board_lines,
Branch: 2 branches
BranchTrueFalseTotal
@moves ? : 2 1 3
$game->status eq 'active' ? : 3 0 3
731 ' ' . (@moves ? scalar(@moves) . ' moves, the last ' . $moves[-1] : 'no moves yet')
732 . ($game->status eq 'active' ? '' : ', and the game is over'));
733 };
734 3 my $died = $@;
735 3 $self->game($was);
736 3 ▶ die $died unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 3 3
737 3 return @lines;
738 }
739  
740 4 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
741 4 return <<'USAGE';
742 shogi - Japanese chess on a terminal
743  
744 usage: shogi [options]
745  
746 --level N a rung of the bot's ladder (0 the weakest), or a node budget
747 --side b|w the side you play; Black moves first in an even game
748 --handicap NAME the bot gives a handicap (it plays White, and moves first)
749 --sfen SFEN start from a position instead of the opening
750 --moves LIST replay these USI moves first, comma or space separated
751 --seed STR for a reproducible game
752 --ascii SFEN letters instead of the kanji
753 --kanji-ranks the ranks as kanji numerals, as a Japanese board has them
754 --ansi colour; --no-ansi turns it off (NO_COLOR beats both)
755 --pick choose a move with the arrow keys; --no-pick to type it
756 --usi speak USI on stdin and stdout, and draw nothing
757 --save FILE where S, or "save", writes the game: FILE, else the file it
758 was resumed from, else shogi.save
759 --resume FILE carry on a saved game, with its undo and redo
760 --replay FILE watch a saved game a turn at a time; enter plays on
761 --help this
762  
763 exit status:
764 0 you won, or the game drew, or you quit
765 1 you lost
766 2 a bad option
767  
768 With a terminal and Term::ReadKey the arrow keys walk the pieces that have a
769 move, and the pieces in your hand that have a drop, and then that piece's
770 squares. Without either, or with --no-pick, a move is typed as USI, and "help"
771 lists the rest.
772  
773 Black's pieces are red and White's blue; with --no-ansi or NO_COLOR a White
774 piece is marked with a v before it instead. The engine is bounded in NODES and never in seconds, so the
775 same position answers the same on a busy machine as on an idle one.
776 USAGE
777 }
778  
779 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
780 return [
781 2 { name => 'level', type => 'int', min => 0, max => $#Game::Shogi::Bot::LADDER, help => 'a rung of the ladder, 0 the weakest' },
782 { name => 'side', type => 'enum', choices => [qw(b w)], help => 'the side you play; Black moves first' },
783 { name => 'handicap', type => 'enum', choices => [ Game::Shogi::Handicap->names ], help => 'the bot gives a handicap' },
784 { name => 'kanji-ranks', type => 'flag', help => 'the ranks as kanji numerals' },
785 { name => 'seed', type => 'str', help => 'for a reproducible game' },
786 ];
787 }
788  
789 46 ▶sub run {
Subroutine run: called CC=41 SCAR=37.2
Pod documented
790 46 my ($class, @argv) = @_;
791 46 my %opt;
792  
793 46 my $ok = eval {
794 46 ▶ local $SIG{__WARN__} = sub { print STDERR "shogi: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
795 46 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
796 'level=i', 'side=s', 'handicap=s', 'sfen=s', 'moves=s', 'seed=s',
797 'ascii', 'kanji-ranks', 'ansi!', 'pick!', 'usi', 'help',
798 Game::Terminal::Kit::Save::flags());
799 };
800 46 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 45 46
801 45 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 2 43 45
802 43 ▶ if (@argv) { print STDERR "shogi: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 42 43
803 42 ▶ if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) { print STDERR "shogi: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (my $why = Game::Terminal::Kit::Save::check_flags(\%opt)) 2 40 42
804  
805 40 ▶ my $from = defined $opt{resume} ? $opt{resume} : $opt{replay};
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 10 30 40
806 40 my $save;
807 40 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) 12 28 40
808 12 for my $new (grep { defined $opt{$_} } qw(sfen moves handicap usi)) {
809 print STDERR "shogi: --$new begins a new game, and --"
810 4 ▶ . (defined $opt{resume} ? 'resume' : 'replay') . " starts from a saved one.\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{'resume'} ? : 4 0 4
811 4 return 2;
812 }
813 8 ($save, my $why) = $class->read_save($from);
814 8 ▶ unless ($save) { print STDERR "shogi: $why.\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 2 6 8
815 6 my $settings = $save->settings;
816 defined $opt{$_} or $opt{$_} = $settings->{$_}
817 6 ▶ for grep { defined $settings->{$_} } qw(level side seed kanji-ranks);
Branch: 1 branch
BranchTrueFalseTotal
defined $opt{$_} or $opt{$_} = $settings->{$_} 1 23 24
818 }
819 34 ▶ $opt{side} = 'b' unless defined $opt{side};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt{"side"} 26 8 34
820  
821 34 ▶ if ($opt{side} ne 'b' && $opt{side} ne 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"side"} ne "b" and $opt{"side"} ne "w") 1 33 34
Condition66%2/3
$opt{"side"} ne "b" and $opt{"side"} ne "w"
!ll&&!rl&&r
3301
MC/DC50%1/2
$opt{"side"} ne "b" and $opt{"side"} ne "w"
$opt{"side"} ne "b" $opt{"side"} ne "w"
Truth table66%2/3
A and B
A= $opt{"side"} ne "b"B= $opt{"side"} ne "w"
ABresult
0X0
100
111
822 1 print STDERR "shogi: --side takes b or w, not '$opt{side}'\n";
823 1 return 2;
824 }
825  
826 33 my @ladder = @Game::Shogi::Bot::LADDER;
827 33 my $level;
828 33 ▶ if (defined $opt{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"level"}) 24 9 33
829 24 ▶ if ($opt{level} < 0) { print STDERR "shogi: --level must not be negative\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} < 0) 1 23 24
830 23 ▶ if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]) 3 20 23
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
!ll&&!rl&&r
1373
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
831 printf STDERR
832 "shogi: --level %d is neither a rung nor a budget: a rung is 0 to %d, "
833 . "and a budget is %d nodes or more\n",
834 3 $opt{level}, $#ladder, $ladder[0];
835 3 return 2;
836 }
837 20 ▶ $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
$opt{'level'} < scalar @ladder ? : 13 7 20
838 }
839  
840 29 ▶ if ($opt{usi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"usi"}) 2 27 29
841 2 require Game::Shogi::USI;
842 2 return Game::Shogi::USI->new(nodes => $level)->run;
843 }
844  
845 27 my $game;
846 27 ▶ unless ($save) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($save) 21 6 27
847 21 ▶ if (defined $opt{handicap} && defined $opt{sfen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"handicap"} and defined $opt{"sfen"}) 1 20 21
Condition100%3/3
defined $opt{"handicap"} and defined $opt{"sfen"}
!ll&&!rl&&r
1731
MC/DC100%2/2
defined $opt{"handicap"} and defined $opt{"sfen"}
defined $opt{"handicap"} defined $opt{"sfen"}
Truth table100%3/3
A and B
A= defined $opt{"handicap"}B= defined $opt{"sfen"}
ABresult
0X0
100
111
848 1 print STDERR "shogi: --handicap and --sfen both name the start; give one\n";
849 1 return 2;
850 }
851 20 ▶ if (defined $opt{handicap} && !Game::Shogi::Handicap->known($opt{handicap})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"handicap"} and not "Game::Shogi::Handicap"->known($opt{"handicap"})) 2 18 20
Condition100%3/3
defined $opt{"handicap"} and not "Game::Shogi::Handicap"->known($opt{"handicap"})
!ll&&!rl&&r
1712
MC/DC100%2/2
defined $opt{"handicap"} and not "Game::Shogi::Handicap"->known($opt{"handicap"})
defined $opt{"handicap"} not "Game::Shogi::Handicap"->known($opt{"handicap"})
Truth table100%3/3
A and not(B)
A= defined $opt{"handicap"}B= not "Game::Shogi::Handicap"->known($opt{"handicap"})
ABresult
0X0
100
111
852 2 print STDERR "shogi: '$opt{handicap}' is not a handicap; the handicaps are ",
853 join(', ', Game::Shogi::Handicap->names), "\n";
854 2 return 2;
855 }
856 18 $game = eval {
857 Game::Shogi->new(
858 (defined $opt{handicap} ? (handicap => $opt{handicap}) : ()),
859 18 ▶ (defined $opt{sfen} ? (sfen => $opt{sfen}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'handicap'} ? : 1 17 18
defined $opt{'sfen'} ? : 2 16 18
860 );
861 };
862 18 ▶ if (!$game) { print STDERR "shogi: --sfen refused: $@"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 17 18
863  
864 17 ▶ if (defined $opt{moves}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"moves"}) 3 14 17
865 3 for my $mv (grep { length } split /[\s,]+/, $opt{moves}) {
866 6 my $refusal = $game->play($mv);
867 6 ▶ next unless $refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refusal 4 2 6
868 2 print STDERR "shogi: --moves refused at '$mv': ", $refusal->message, "\n";
869 2 return 2;
870 }
871 }
872 }
873  
874 my $terminal = $class->new(
875 ascii => $opt{ascii},
876 kanji_ranks => $opt{'kanji-ranks'},
877 colour => $opt{ansi},
878 picking => $opt{pick},
879 level => $level,
880 seed => $opt{seed} // 'shogi-terminal',
881 side => $opt{side},
882 ($game ? (game => $game) : ()),
883 21 ▶ (defined $opt{save} ? (save_file => $opt{save}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$game ? : 15 6 21
defined $opt{'save'} ? : 7 14 21
Condition100%2/2
$opt{"seed"} // "shogi-terminal"
l!l
156
MC/DC100%1/1
$opt{"seed"} // "shogi-terminal"
$opt{"seed"}
Truth table100%2/2
A
A= $opt{"seed"}
Aresult
11
00
884 );
885  
886 21 ▶ if (defined $opt{replay}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"replay"}) 1 20 21
887 my $replay = Game::Terminal::Kit::Replay->new(
888 save => $save,
889 3 ▶ load => sub { Game::Shogi->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
890 3 ▶ draw => sub { $terminal->replay_lines($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
891 1 ▶ head => [ 'shogi, a replay of ' . $save->file
Branch: 1 branch
BranchTrueFalseTotal
defined $save->saved_at ? : 1 0 1
892 . (defined $save->saved_at ? ', saved ' . $save->saved_at : ''), '' ],
893 screen => $terminal->screen,
894 );
895 my ($event, $step) = Game::Terminal::Kit::Run::guard(
896 1 ▶ sub { $replay->play($terminal->keyboard, $terminal->screen) }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
897 1 ▶ return 0 unless $event eq 'chosen';
Branch: 1 branch
BranchTrueFalseTotal
unless $event eq "chosen" 1 0 1
898 0 $save = $save->from_step($step);
899 }
900 20 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) 5 15 20
901 5 my $resumed = eval { $terminal->history->resume($save) };
902 5 ▶ unless ($resumed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resumed) 0 5 5
903 0 print STDERR "shogi: '" . $save->file . "' cannot be carried on: "
904 . Game::Terminal::Kit::Save::why($@) . ".\n";
905 0 return 2;
906 }
907 5 ▶ $terminal->history->file($opt{save}) if defined $opt{save};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt{"save"} 1 4 5
908 5 $terminal->game($resumed);
909 }
910  
911 return Game::Terminal::Kit::Run::guard(
912 20 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
913 }
914  
915 27 1;
916  
917 __END__
↑ Summary blib/lib/Game/Shogi/USI.pm »