blib/lib/Game/Shogi/Terminal.pm

filter: Statementstmt 91.8% 495 / 539 Branchbran 79.5% 272 / 342 Conditioncond 72.7% 72 / 99 MC/DCmcdc 51.6% 32 / 62 Subroutinesub 92.1% 70 / 76 Podpod 96.8% 31 / 32 totaltotal 84.5% 972 / 1150 CC 218 SCAR 29.0
CC
218
Cov
86%
start
47.7
pick_move
38.3
step
24.8
CRAP 18.2
↑ Summary blib/lib/Game/Shogi/USI.pm »
1 package Game::Shogi::Terminal;
2  
3 4 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶use Game::Shogi;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use Game::Shogi::Bot;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use Game::Shogi::Engine ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Game::Shogi::Handicap;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use Game::Shogi::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use Game::Shogi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 4 ▶use Game::Terminal::Kit::History;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶use Game::Terminal::Kit::Keys;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use Game::Terminal::Kit::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶use Game::Terminal::Kit::Screen;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use Game::Terminal::Kit::Steer;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 our $VERSION = '0.01';
24  
25 4 my $E = 'Game::Shogi::Engine';
26 4 my $N = 'Game::Shogi::Notation';
27  
28 4 my %GLYPH = (
29 P => "\x{6B69}", L => "\x{9999}", N => "\x{6842}", S => "\x{9280}",
30 G => "\x{91D1}", B => "\x{89D2}", R => "\x{98DB}", K => "\x{7389}",
31 k => "\x{738B}",
32 '+P' => "\x{3068}", '+L' => "\x{674F}", '+N' => "\x{572D}", '+S' => "\x{5168}",
33 '+B' => "\x{99AC}", '+R' => "\x{9F8D}",
34 );
35  
36 4 my @KANJI_RANK = (undef, map { chr } 0x4E00, 0x4E8C, 0x4E09, 0x56DB, 0x4E94, 0x516D, 0x4E03, 0x516B, 0x4E5D);
37  
38 4 my %WORD = (
39 P => 'pawn', L => 'lance', N => 'knight', S => 'silver', G => 'gold',
40 B => 'bishop', R => 'rook', K => 'king',
41 '+P' => 'tokin', '+L' => 'promoted lance', '+N' => 'promoted knight',
42 '+S' => 'promoted silver', '+B' => 'horse', '+R' => 'dragon',
43 );
44  
45 4 my @HAND_ORDER = qw(R B G S N L P);
46  
47 4 my %SIDE = (b => 'black', w => 'white');
48  
49 4 my %SIDE_COLOUR = (b => '1;31', w => '1;34');
50  
51 4 my %HOW = (
52 mate => 'checkmate',
53 no_move => 'no legal move',
54 resign => 'resignation',
55 sennichite => 'fourfold repetition (sennichite)',
56 perpetual_check => 'perpetual check',
57 impasse_declared => 'the impasse declared',
58 cap => 'the move cap',
59 );
60  
61 4 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
62 4 ROW_FIRST => 100,
63 4 };
64  
65 4 has _in => (is => 'rw', init_arg => 'in', private => 1);
66  
67 4 has game => (is => 'rw');
68  
69 4 has level => (is => 'ro');
70  
71 4 has seed => (is => 'ro');
72  
73 4 has side => (is => 'ro', isa => Str, default => 'b');
74  
75 4 has kanji_ranks => (is => 'rw', default => 0);
76  
77 4 has _fh => (is => 'rw', init_arg => 'out', private => 1);
78  
79 4 has _ascii => (is => 'rw', init_arg => 'ascii', private => 1, default => 0);
80  
81 4 has _colour => (is => 'rw', init_arg => 'colour', private => 1);
82  
83 4 has _keysource => (is => 'rw', isa => Any, init_arg => 'keysource', private => 1);
84  
85 4 has _pending => (is => 'rw', isa => Any, init_arg => 'pending', private => 1);
86  
87 4 has keyboard => (is => 'rw');
88  
89 4 has screen => (is => 'rw');
90  
91 4 has _picking => (is => 'rw', isa => Any, init_arg => 'picking');
92  
93 4 has _rewind => (is => 'rw', isa => Any, init_arg => 'rewind');
94  
95 4 has history => (is => 'rw');
96  
97 110 ▶sub BUILD {
Subroutine BUILD: called CC=18 SCAR=29.2
Pod undocumented
98 110 my ($self) = @_;
99 110 my $side = $self->side;
100 110 ▶ 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 109 110
Condition100%3/3
$side eq "b" or $side eq "w"
l!l&&r!l&&!r
10811
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
101 unless $side eq 'b' || $side eq 'w';
102 109 ▶ $self->kanji_ranks($self->kanji_ranks ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->kanji_ranks ? : 35 74 109
103 109 ▶ my $out = $self->_fh || \*STDOUT;
Condition100%2/2
$self->_fh || \*STDOUT
l!l
1027
MC/DC100%1/1
$self->_fh || \*STDOUT
$self->_fh
Truth table100%2/2
A
A= $self->_fh
Aresult
11
00
104 109 ▶ $self->keyboard(Game::Terminal::Kit::Keys->new(
Branch: 2 branches
BranchTrueFalseTotal
unless $self->keyboard 109 0 109
$self->_keysource ? : 16 93 109
Condition75%3/4
$self->_in || \*STDIN
l!l
5104
$self->_pending || []
l!l
0109
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
105 in => $self->_in || \*STDIN,
106 pending => $self->_pending || [],
107 ($self->_keysource ? (keysource => $self->_keysource) : ()),
108 )) unless $self->keyboard;
109 $self->screen(Game::Terminal::Kit::Screen->new(
110 out => $out,
111 interactive => (defined $self->_rewind ? ($self->_rewind ? 1 : 0)
112 109 ▶ : eval { -t $out } ? 1 : 0),
Branch: 8 branches
BranchTrueFalseTotal
unless $self->screen 109 0 109
defined $self->_rewind ? : 102 7 109
$self->_rewind ? : 0 102 102
eval { do { -t $out } } ? : 0 7 7
Game::Terminal::Kit::Screen::no_colour() ? : 1 108 109
defined $self->_colour ? : 102 6 108
$self->_colour ? : 34 68 102
$self->_ascii ? : 57 52 109
113 colour => Game::Terminal::Kit::Screen::no_colour() ? 0
114 : defined $self->_colour ? ($self->_colour ? 1 : 0)
115 : 1,
116 ascii => $self->_ascii ? 1 : 0,
117 )) unless $self->screen;
118 109 $self->screen->utf8;
119 $self->history(Game::Terminal::Kit::History->new(
120 46 ▶ save => sub { $_[0]->to_text },
Subroutine __ANON__: called CC=1 SCAR=0.0
121 2 ▶ load => sub { Game::Shogi->from_text($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
122 109 ▶ )) unless $self->history;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->history 109 0 109
123 109 return;
124 }
125  
126 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
127 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 20 20
128 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 19079 19079
shift() ? : 0 0 0
$s->screen->ascii ? : 11698 7381 19079
129 2143 ▶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 2143 2143
shift() ? : 0 0 0
$s->screen->colour ? : 734 1409 2143
130 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
131 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
132  
133 152 ▶sub _say { my ($self, @lines) = @_; $self->screen->say(@lines); return }
Subroutine _say: called CC=1 SCAR=0.0
134  
135 617 ▶sub columns {
Subroutine columns: called CC=1 SCAR=0.0
Pod documented
136 617 my (undef, $text) = @_;
137 617 $text =~ s/\e\[[0-9;]*m//g;
138 617 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;
139 617 return length($text) + $wide;
140 }
141  
142 174 ▶sub _padded {
Subroutine _padded: called CC=3 SCAR=11.4
143 174 my ($self, $text, $cols) = @_;
144 174 ▶ chop $text while $self->columns($text) > $cols;
Branch: 1 branch
BranchTrueFalseTotal
$self->columns($text) > $cols and do { chop $text } 0 174 174
145 174 my $short = $cols - $self->columns($text);
146 174 ▶ return $short > 0 ? $text . (' ' x $short) : $text;
Branch: 1 branch
BranchTrueFalseTotal
$short > 0 ? : 174 0 174
147 }
148  
149 16605 ▶sub _cell {
Subroutine _cell: called CC=11 SCAR=24.0
150 16605 my ($self, $token, $spot) = @_;
151 16605 ▶ if (!defined $token) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $token) 11443 5162 16605
152 11443 ▶ return $self->ascii ? (' ', $spot ? '*' : '.') : (' ', $spot ? "\x{FF0B}" : "\x{30FB}");
Branch: 3 branches
BranchTrueFalseTotal
$self->ascii ? : 7187 4256 11443
$spot ? : 2289 4898 7187
$spot ? : 15 4241 4256
153 }
154 5162 ▶ my $side = $token =~ /[a-z]/ ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /[a-z]/u ? : 2572 2590 5162
155 5162 ▶ if ($self->ascii) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ascii) 3019 2143 5162
156 3019 return (' ' x (3 - length $token), $self->screen->paint($token, $SIDE_COLOUR{$side}));
157 }
158 2143 my $key = uc $token;
159 2143 ▶ $key = 'k' if $key eq 'K' && $side eq 'w';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "K" and $side eq "w" 64 2079 2143
Condition100%3/3
$key eq "K" and $side eq "w"
!ll&&!rl&&r
20156464
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
160 2143 ▶ return ($self->colour ? ' ' : $side eq 'w' ? 'v' : ' ', $self->screen->paint($GLYPH{$key}, $SIDE_COLOUR{$side}));
Branch: 2 branches
BranchTrueFalseTotal
$self->colour ? : 734 1409 2143
$side eq 'w' ? : 702 707 1409
161 }
162  
163 212 ▶sub _hand_glyph {
Subroutine _hand_glyph: called CC=3 SCAR=11.0
164 212 my ($self, $side, $letter) = @_;
165 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
166 }
167  
168 413 ▶sub _hand_items {
Subroutine _hand_items: called CC=1 SCAR=0.0
169 413 my ($self, $pos, $side) = @_;
170 413 my $hand = $pos->hands->{$side};
171 413 return grep { $hand->{$_} } @HAND_ORDER;
172 }
173  
174 410 ▶sub _hand_line {
Subroutine _hand_line: called CC=10 SCAR=23.0
175 410 my ($self, $pos, $side, %o) = @_;
176 410 my $hand = $pos->hands->{$side};
177 410 my @items = $self->_hand_items($pos, $side);
178 410 my $label = sprintf ' %-6s', $SIDE{$side};
179 410 ▶ return $label . ' -' unless @items;
Branch: 1 branch
BranchTrueFalseTotal
unless @items 234 176 410
180 176 my @gap = (' ') x (@items + 1);
181 176 my $marked = $side eq $pos->side_to_move;
182 176 for my $pair ([ $o{from}, '(', ')' ], [ $o{cursor}, '[', ']' ]) {
183 352 my ($at, $left, $right) = @$pair;
184 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
185 50 for my $i (0 .. $#items) {
186 56 ▶ next unless $items[$i] eq $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $items[$i] eq $1 22 34 56
187 34 ($gap[$i], $gap[$i + 1]) = ($left, $right);
188 }
189 }
190 176 my $line = $label;
191 176 for my $i (0 .. $#items) {
192 212 my $n = $hand->{ $items[$i] };
193 212 ▶ $line .= $gap[$i] . $self->_hand_glyph($side, $items[$i]) . ($n > 1 ? $n : '');
Branch: 1 branch
BranchTrueFalseTotal
$n > 1 ? : 33 179 212
194 }
195 176 return $line . $gap[-1];
196 }
197  
198 205 ▶sub _files { $_[1] ? (1 .. 9) : reverse(1 .. 9) }
Subroutine _files: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 35 170 205
199 205 ▶sub _ranks { $_[1] ? reverse(1 .. 9) : (1 .. 9) }
Subroutine _ranks: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 35 170 205
200  
201 2050 ▶sub _rank_label {
Subroutine _rank_label: called CC=3 SCAR=11.0
202 2050 my ($self, $rank) = @_;
203 2050 ▶ return $self->kanji_ranks && !$self->ascii ? $KANJI_RANK[$rank] : chr(ord('a') + $rank - 1);
Branch: 1 branch
BranchTrueFalseTotal
$self->kanji_ranks && !$self->ascii ? : 180 1870 2050
Condition100%3/3
$self->kanji_ranks and not $self->ascii
!ll&&!rl&&r
1700170180
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
204 }
205  
206 205 ▶sub board_lines {
Subroutine board_lines: called CC=20 SCAR=30.0
Pod documented
207 205 my ($self, %o) = @_;
208 205 ▶ my $pos = $o{position} || ($self->game ? $self->game->board : Game::Shogi::Engine->new);
Branch: 1 branch
BranchTrueFalseTotal
$self->game ? : 89 0 89
Condition66%2/3
$o{'position'} || ($self->game ? $self->game->board : 'Game::Shogi::Engine'->new)
l!l&&r!l&&!r
116890
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
209 205 ▶ my $flip = $o{flip} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$o{'flip'} ? : 35 170 205
210 205 my @files = $self->_files($flip);
211 205 my (%spot, %take);
212 205 ▶ for my $name (@{ $o{spots} || [] }) {
Condition100%2/2
$o{'spots'} || []
l!l
68137
MC/DC100%1/1
$o{'spots'} || []
$o{'spots'}
Truth table100%2/2
A
A= $o{'spots'}
Aresult
11
00
213 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
214 }
215 205 ▶ my ($top, $bottom) = $flip ? ('b', 'w') : ('w', 'b');
Branch: 1 branch
BranchTrueFalseTotal
$flip ? : 35 170 205
216  
217 205 my @lines = ($self->_hand_line($pos, $top, %o));
218 205 my $legend = '';
219 205 ▶ $legend .= ($self->ascii ? ' ' . $_ : ' ' . $_ . ' ') for @files;
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 1134 711 1845
220 205 my $label_cols = $self->columns($self->_rank_label(1));
221 205 push @lines, $legend . ' ' . ' ' . (' ' x $label_cols);
222  
223 205 for my $rank ($self->_ranks($flip)) {
224 1845 my @names = map { $_ . chr(ord('a') + $rank - 1) } @files;
225 1845 my @gap = (' ') x 10;
226 1845 my @opens;
227 1845 for my $i (0 .. 8) {
228 16605 ▶ next unless $take{ $names[$i] };
Branch: 1 branch
BranchTrueFalseTotal
unless $take{$names[$i]} 16553 52 16605
229 52 ($gap[$i], $gap[$i + 1], $opens[$i]) = ('x', 'x', 1);
230 }
231 1845 for my $pair ([ $o{from}, '(', ')' ], [ $o{cursor}, '[', ']' ]) {
232 3690 my ($at, $left, $right) = @$pair;
233 3690 ▶ next unless defined $at;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $at 1818 1872 3690
234 1872 for my $i (0 .. 8) {
235 16848 ▶ next unless $names[$i] eq $at;
Branch: 1 branch
BranchTrueFalseTotal
unless $names[$i] eq $at 16690 158 16848
236 158 ($gap[$i], $gap[$i + 1], $opens[$i]) = ($left, $right, 1);
237 }
238 }
239 1845 my $row = '';
240 1845 for my $i (0 .. 8) {
241 16605 my ($pad, $body) = $self->_cell($pos->at($E->square($names[$i])), $spot{ $names[$i] });
242 16605 ▶ $row .= $opens[$i] && $pad =~ /\A +\z/ ? $pad . $gap[$i] . $body : $gap[$i] . $pad . $body;
Branch: 1 branch
BranchTrueFalseTotal
$opens[$i] && $pad =~ /\A +\z/u ? : 179 16426 16605
Condition100%3/3
$opens[$i] and $pad =~ /\A +\z/u
!ll&&!rl&&r
1641412179
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
243 }
244 1845 push @lines, $row . $gap[9] . ' ' . $self->_rank_label($rank);
245 }
246 205 push @lines, $self->_hand_line($pos, $bottom, %o);
247 205 return @lines;
248 }
249  
250 25 ▶sub draw {
Subroutine draw: called CC=1 SCAR=0.0
Pod documented
251 25 my ($self, %o) = @_;
252 25 $self->_say($self->board_lines(flip => $self->side eq 'w', %o));
253 25 return $self;
254 }
255  
256 0 ▶sub keys_available { return $_[0]->keyboard->keys_available }
Subroutine keys_available: not called CC=1 SCAR=6.9
Pod documented
257  
258 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
259  
260 4 ▶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 ? : 4 0 4
261  
262 16 ▶sub leave_raw { $_[0]->keyboard->leave_raw; return $_[0] }
Subroutine leave_raw: called CC=1 SCAR=0.0
Pod documented
263  
264 87 ▶sub read_key { return $_[0]->keyboard->read_key }
Subroutine read_key: called CC=1 SCAR=0.0
Pod documented
265  
266 28 ▶sub picking {
Subroutine picking: called CC=5 SCAR=16.8
Pod documented
267 28 my ($self, @set) = @_;
268 28 ▶ $self->_picking($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 12 16 28
$set[0] ? : 0 12 12
269 28 ▶ return $self->_picking ? 1 : 0 if defined $self->_picking;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_picking 28 0 28
$self->_picking ? : 4 24 28
270 0 return $self->keys_available;
271 }
272  
273 87 ▶sub rewind {
Subroutine rewind: called CC=6 SCAR=26.2
Pod documented
274 87 my ($self, @set) = @_;
275 87 ▶ $self->_rewind($set[0] ? 1 : 0) if @set;
Branch: 2 branches
BranchTrueFalseTotal
if @set 0 87 87
$set[0] ? : 0 0 0
276 87 ▶ return $self->_rewind ? 1 : 0 if defined $self->_rewind;
Branch: 2 branches
BranchTrueFalseTotal
if defined $self->_rewind 87 0 87
$self->_rewind ? : 0 87 87
277 0 ▶ return eval { -t $self->out } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { -t $self->out } } ? : 0 0 0
278 }
279  
280 8212 ▶sub _from_of {
Subroutine _from_of: called CC=2 SCAR=6.9
281 8212 my ($usi) = @_;
282 8212 my $m = Game::Shogi::Move->of($usi);
283 8212 ▶ return $m->is_drop ? $m->drop . '*' : $m->from;
Branch: 1 branch
BranchTrueFalseTotal
$m->is_drop ? : 5112 3100 8212
284 }
285  
286 2961 ▶sub _drawn_at {
Subroutine _drawn_at: called CC=5 SCAR=16.4
287 2961 my ($self, $pos, $origin, $flip) = @_;
288 2961 ▶ if ($origin =~ /\A([A-Z])\*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($origin =~ /\A([A-Z])\*\z/u) 3 2958 2961
289 3 my @items = $self->_hand_items($pos, $pos->side_to_move);
290 3 my ($i) = grep { $items[$_] eq $1 } 0 .. $#items;
291 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
292 }
293 2958 my ($file, $rank) = ($origin =~ /\A([1-9])([a-i])\z/);
294 2958 $rank = ord($rank) - ord('a') + 1;
295 2958 ▶ return [ $flip ? $file - 1 : 9 - $file, ROW_FIRST * ($flip ? 9 - $rank : $rank - 1) ];
Branch: 2 branches
BranchTrueFalseTotal
$flip ? : 0 2958 2958
$flip ? : 0 2958 2958
296 }
297  
298 120 ▶sub _as_drawn {
Subroutine _as_drawn: called CC=2 SCAR=7.0
299 120 my ($self, $pos, $set, $flip) = @_;
300 120 my %at = map { $_ => $self->_drawn_at($pos, $_, $flip) } @$set;
301 120 ▶ 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
46827340
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
302 }
303  
304 30 ▶sub origins {
Subroutine origins: called CC=1 SCAR=0.0
Pod documented
305 30 my ($self, $pos, $flip) = @_;
306 30 my %seen;
307 30 my @from = grep { !$seen{$_}++ } map { _from_of($_) } $pos->legal;
308 30 return $self->_as_drawn($pos, \@from, $flip);
309 }
310  
311 90 ▶sub targets {
Subroutine targets: called CC=1 SCAR=0.0
Pod documented
312 90 my ($self, $pos, $from, $flip) = @_;
313 90 my %seen;
314 2597 my @to = grep { !$seen{$_}++ }
315 2597 map { Game::Shogi::Move->of($_)->to }
316 90 grep { _from_of($_) eq $from } $pos->legal;
317 90 return $self->_as_drawn($pos, \@to, $flip);
318 }
319  
320 60 ▶sub choices {
Subroutine choices: called CC=2 SCAR=6.9
Pod documented
321 60 my ($self, $pos, $from, $to) = @_;
322 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
323 }
324  
325 44 ▶sub step {
Subroutine step: called CC=8 SCAR=32.1
Pod documented
326 44 my ($self, $pos, $set, $at, $key, $flip) = @_;
327 44 ▶ return 0 if $key eq 'home';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "home" 0 44 44
328 44 ▶ return $#$set if $key eq 'end';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "end" 0 44 44
329 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
330 if $key eq 'right' || $key eq 'tab';
331 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
332 if $key eq 'left' || $key eq 'back_tab';
333 0 my @drawn = map { $self->_drawn_at($pos, $_, $flip) } @$set;
334 0 my $next = Game::Terminal::Kit::Steer::nearest(\@drawn, $at, Game::Terminal::Kit::Steer::way($key));
335 0 ▶ return defined $next ? $next : $at;
Branch: 1 branch
BranchTrueFalseTotal
defined $next ? : 0 0 0
336 }
337  
338 38 ▶sub piece_name {
Subroutine piece_name: called CC=3 SCAR=11.1
Pod documented
339 38 my ($self, $token) = @_;
340 38 ▶ return 'nothing' unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 38 38
341 38 ▶ return ($token =~ /[a-z]/ ? 'white ' : 'black ') . $WORD{ uc $token };
Branch: 1 branch
BranchTrueFalseTotal
$token =~ /[a-z]/u ? : 3 35 38
342 }
343  
344 36 ▶sub piece_line {
Subroutine piece_line: called CC=5 SCAR=16.1
Pod documented
345 36 my ($self, $pos, $from) = @_;
346 36 my $n = scalar @{ $self->targets($pos, $from) };
347 36 ▶ my $count = $n . ($n == 1 ? ' square' : ' squares');
Branch: 1 branch
BranchTrueFalseTotal
$n == 1 ? : 32 4 36
348 36 ▶ if ($from =~ /\A([A-Z])\*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from =~ /\A([A-Z])\*\z/u) 1 35 36
349 1 my $held = $pos->hand($pos->side_to_move, $1);
350 1 return sprintf 'a %s in hand (%d), %s to drop it on', $WORD{$1}, $held, $count;
351 }
352 35 my $line = sprintf 'the %s on %s, %s', $self->piece_name($pos->at($E->square($from))), $from, $count;
353 35 ▶ my $them = $pos->side_to_move eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$pos->side_to_move eq 'b' ? : 35 0 35
354 35 ▶ $line .= ', and it is attacked' if $pos->attacked($E->square($from), $them);
Branch: 1 branch
BranchTrueFalseTotal
if $pos->attacked($E->square($from), $them) 2 33 35
355 35 return $line;
356 }
357  
358 51 ▶sub move_line {
Subroutine move_line: called CC=9 SCAR=22.3
Pod documented
359 51 my ($self, $pos, $usi) = @_;
360 51 my $m = Game::Shogi::Move->of($usi);
361 51 ▶ my $prey = $m->is_drop ? undef : $pos->at($E->square($m->to));
Branch: 1 branch
BranchTrueFalseTotal
$m->is_drop ? : 32 19 51
362 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
363 . ($m->is_drop ? 'a drop' : defined $prey ? 'takes the ' . $self->piece_name($prey) : 'a quiet move');
364 51 my $after = $pos->clone;
365 51 $after->do_move($usi);
366 51 my $outcome = $after->outcome;
367 51 ▶ my $them = $pos->side_to_move eq 'b' ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
$pos->side_to_move eq 'b' ? : 51 0 51
368 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
369 : $after->in_check($them) ? ', and it checks'
370 : '';
371 51 return $line;
372 }
373  
374 22 ▶sub pick_move {
Subroutine pick_move: called CC=34 SCAR=36.5
Pod documented
375 22 my ($self, $g) = @_;
376 22 my $pos = $g->board;
377 22 ▶ my $flip = $self->side eq 'w' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->side eq 'w' ? : 0 22 22
378 22 my $from = $self->origins($pos, $flip);
379 22 ▶ return undef unless @$from;
Branch: 1 branch
BranchTrueFalseTotal
unless @$from 0 22 22
380  
381 22 my ($at, $held, $to, $toat, $asking, $drawn) = (0, undef, [], 0, undef, 0);
382  
383 22 while (1) {
384 87 my %o = (flip => $flip, position => $pos);
385 87 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) { } 51 36 87
386 51 $to = $self->targets($pos, $held, $flip);
387 51 %o = (%o, from => $held, cursor => $to->[$toat], spots => $to);
388 }
389 else {
390 36 %o = (%o, cursor => $from->[$at]);
391 }
392  
393 87 my @lines = $self->board_lines(%o);
394 87 my ($caption, $help);
395 87 ▶ if ($asking) {
Branch: 2 branches
BranchTrueFalseTotal
if ($asking) { } 4 83 87
elsif (defined $held) { } 47 36 83
396 4 $caption = $self->move_line($pos, "$asking+") . ', if it promotes';
397 4 $help = 'promote? y or n, escape to choose again';
398 }
399 elsif (defined $held) {
400 47 my @c = $self->choices($pos, $held, $to->[$toat]);
401 47 my ($plain) = grep { !/\+\z/ } @c;
402 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
403 47 $help = 'arrows to choose, enter or space to play, escape to pick another';
404 }
405 else {
406 36 $caption = $self->piece_line($pos, $from->[$at]);
407 36 $help = 'arrows to choose a piece, enter or space to take it, ? for the keys';
408 }
409 87 push @lines, $self->_padded(" $caption", 79), $self->_padded(" $help", 79);
410  
411 87 $self->screen->interactive($self->rewind);
412 87 $self->screen->rewind($drawn);
413 87 $self->_say(@lines);
414 87 $drawn = scalar @lines;
415  
416 87 my $key = $self->read_key;
417 87 ▶ return 'quit' unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 1 86 87
418 86 ▶ 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" 7 79 86
Condition66%4/6
$key eq "q" or $key eq "interrupt" or $key eq "eof"
l!l&&r!l&&!r
7079
$key eq "q" or $key eq "interrupt"
l!l&&r!l&&!r
7079
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
419  
420 79 ▶ if ($asking) {
Branch: 1 branch
BranchTrueFalseTotal
if ($asking) 4 75 79
421 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
422 2 ▶ return $key eq 'y' ? "$asking+" : $asking;
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'y' ? : 1 1 2
423 }
424 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
425 2 next;
426 }
427 75 ▶ if ($key eq 'enter' || $key eq ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "enter" or $key eq " ") 21 54 75
Condition66%2/3
$key eq "enter" or $key eq " "
l!l&&r!l&&!r
21054
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
428 21 ▶ if (defined $held) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $held) 10 11 21
429 10 my @c = $self->choices($pos, $held, $to->[$toat]);
430 10 ▶ return $c[0] if @c == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @c == 1 6 4 10
431 4 ($asking) = grep { !/\+\z/ } @c;
432 4 next;
433 }
434 11 ($held, $toat) = ($from->[$at], 0);
435 11 next;
436 }
437 54 ▶ if ($key eq 'escape' || $key eq 'backspace') {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "escape" or $key eq "backspace") 2 52 54
Condition66%2/3
$key eq "escape" or $key eq "backspace"
l!l&&r!l&&!r
2052
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
438 2 $held = undef;
439 2 next;
440 }
441 52 ▶ 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 8 52
442 44 ▶ defined $held
Branch: 1 branch
BranchTrueFalseTotal
defined $held ? : 32 12 44
443 ? ($toat = $self->step($pos, $to, $toat, $key, $flip))
444 : ($at = $self->step($pos, $from, $at, $key, $flip));
445 44 next;
446 }
447 8 ▶ return 'undo' if $key eq 'u';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "u" 3 5 8
448 5 ▶ return 'redo' if $key eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "r" 2 3 5
449 3 ▶ return 'declare' if $key eq 'd';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "d" 1 2 3
450 0 ▶ if ($key eq '?') { $self->_keys; $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "?") 0 2 2
451 0 ▶ if ($key eq 'l') { $self->_legal($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "l") 0 2 2
452 0 ▶ if ($key eq 's') { $self->_say($g->sfen); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "s") 0 2 2
453 2 ▶ if ($key eq 'g') { $self->_history($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "g") 1 1 2
454 1 ▶ if ($key eq 'h') { $self->_hint($g); $drawn = 0; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq "h") 1 0 1
455 }
456 }
457  
458 0 ▶sub _keys {
Subroutine _keys: not called CC=1 SCAR=6.9
459 0 my ($self) = @_;
460 0 $self->_say(
461 'arrows move the cursor; it only stops where a move is, and your hand is the last row',
462 'enter take the piece under the cursor, then play to the square',
463 'y n promote or not, when the move may do either',
464 'escape put the piece back and choose another',
465 'tab the next one, whichever you are choosing',
466 'h the strongest move this engine can find',
467 'l s g every move you have, the SFEN, the moves so far',
468 'd declare the impasse (entering king), which wins when the count allows it',
469 'u r take your last move back, play it again',
470 'q give up and leave',
471 );
472 0 return $self;
473 }
474  
475 16 ▶sub start {
Subroutine start: called CC=39 SCAR=38.6
Pod documented
476 16 my ($self) = @_;
477 16 ▶ $self->game(Game::Shogi->new) unless $self->game;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->game 2 14 16
478 16 my $side = $self->side;
479 16 ▶ my $flip = $side eq 'w' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$side eq 'w' ? : 1 15 16
480 16 $self->history->start($self->game);
481  
482 16 ▶ my $pick = $self->picking && $self->enter_raw ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->picking && $self->enter_raw ? : 4 12 16
Condition66%2/3
$self->picking and $self->enter_raw
!ll&&!rl&&r
1204
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
483 16 ▶ $self->picking(0) unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 12 4 16
484 16 ▶ $self->_say($pick
Branch: 1 branch
BranchTrueFalseTotal
$pick ? : 4 12 16
485 ? 'shogi. arrows to choose a move, enter or space to play it, ? for the keys.'
486 : 'shogi. a move is USI, like 7g7f or P*5e. "help" for the rest.');
487 16 ▶ $self->draw unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 12 4 16
488  
489 16 ▶ while ($self->game->status eq 'active') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->game->status eq "active") 39 2 41
490 39 my $g = $self->game;
491 39 ▶ if ($g->turn ne $side) {
Branch: 1 branch
BranchTrueFalseTotal
if ($g->turn ne $side) 9 30 39
492 9 ▶ if (Game::Shogi::Bot->declares($g, $g->turn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Game::Shogi::Bot"->declares($g, $g->turn)) 0 9 9
493 0 $self->_say('the bot declares the impasse');
494 0 $g->declare;
495 0 last;
496 }
497 9 my $mv = do {
498 9 local $Game::Shogi::Bot::LEVEL = $self->level;
499 9 Game::Shogi::Bot->choose($g, $g->turn, seed => $self->seed);
500 };
501 9 ▶ last unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 0 9 9
502 9 $self->_say($self->_describe($g, $mv, 'the bot plays'));
503 9 my $refusal = $g->play($mv);
504 9 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 8 9
505 1 $self->_say('the bot offered a move the game refused: ' . $refusal->code);
506 1 last;
507 }
508 8 ▶ $self->draw unless $pick;
Branch: 1 branch
BranchTrueFalseTotal
unless $pick 5 3 8
509 8 next;
510 }
511  
512 30 $self->person_turn;
513  
514 30 ▶ if ($pick) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pick) 10 20 30
515 10 my $chosen = $self->pick_move($g);
516 10 ▶ last if !defined $chosen || $chosen eq 'quit';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $chosen or $chosen eq "quit" 4 6 10
Condition66%2/3
not defined $chosen or $chosen eq "quit"
l!l&&r!l&&!r
046
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
517 6 ▶ $self->take_back, next if $chosen eq 'undo';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "undo" 2 4 6
518 4 ▶ $self->play_again, next if $chosen eq 'redo';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "redo" 1 3 4
519 3 ▶ $self->_declare($g), next if $chosen eq 'declare';
Branch: 1 branch
BranchTrueFalseTotal
if $chosen eq "declare" 0 3 3
520 3 my $refusal = $g->play($chosen);
521 3 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') if $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 0 3 3
522 3 next;
523 }
524  
525 20 my $line = $self->keyboard->read_line($self->_prompt($g), $self->out);
526 20 ▶ last unless defined $line;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line 0 20 20
527 20 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 20 20
528  
529 20 ▶ last if $line eq 'quit' || $line eq 'q';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "quit" or $line eq "q" 9 11 20
Condition66%2/3
$line eq "quit" or $line eq "q"
l!l&&r!l&&!r
9011
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
530 11 ▶ $self->_help, next if $line eq 'help' || $line eq '?';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "help" or $line eq "?" 0 11 11
Condition33%1/3
$line eq "help" or $line eq "?"
l!l&&r!l&&!r
0011
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
531 11 ▶ $self->_legal($g), next if $line eq 'legal';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "legal" 1 10 11
532 10 ▶ $self->_say($g->sfen), next if $line eq 'sfen';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "sfen" 1 9 10
533 9 ▶ $self->_history($g), next if $line eq 'log';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "log" 2 7 9
534 7 ▶ $self->draw, next if $line eq 'board';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "board" 0 7 7
535 7 ▶ $self->_hint($g), next if $line eq 'hint';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "hint" 1 6 7
536 6 ▶ $self->_declare($g), next if $line eq 'declare';
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "declare" 1 5 6
537 5 ▶ if ($line eq 'undo' || $line eq 'redo') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "undo" or $line eq "redo") 0 5 5
Condition33%1/3
$line eq "undo" or $line eq "redo"
l!l&&r!l&&!r
005
MC/DC0%0/2
$line eq "undo" or $line eq "redo"
$line eq "undo" $line eq "redo"
Truth table33%1/3
A or B
A= $line eq "undo"B= $line eq "redo"
ABresult
1X1
011
000
538 0 ▶ $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 0 0 0
$line eq 'undo' ? : 0 0 0
539 0 next;
540 }
541  
542 5 my $refusal = $g->play($line);
543 5 ▶ if ($refusal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($refusal) 1 4 5
544 1 $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')');
545 1 next;
546 }
547 4 $self->draw;
548 }
549  
550 16 $self->leave_raw;
551 16 ▶ $self->draw if $pick;
Branch: 1 branch
BranchTrueFalseTotal
if $pick 4 12 16
552 16 return $self->_verdict($self->game);
553 }
554  
555 30 ▶sub person_turn {
Subroutine person_turn: called CC=1 SCAR=0.0
Pod documented
556 30 my ($self) = @_;
557 30 $self->history->turn($self->game);
558 30 return $self;
559 }
560  
561 2 ▶sub take_back {
Subroutine take_back: called CC=2 SCAR=6.9
Pod documented
562 2 my ($self) = @_;
563 2 my $game = $self->history->undo;
564 2 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 1 2
565 1 $self->_say('nothing to take back');
566 1 return 0;
567 }
568 1 $self->swap($game);
569 1 $self->_say('taken back');
570 1 return 1;
571 }
572  
573 1 ▶sub play_again {
Subroutine play_again: called CC=2 SCAR=7.5
Pod documented
574 1 my ($self) = @_;
575 1 my $game = $self->history->redo;
576 1 ▶ unless ($game) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 0 1 1
577 0 $self->_say('nothing to play again');
578 0 return 0;
579 }
580 1 $self->swap($game);
581 1 $self->_say('played again');
582 1 return 1;
583 }
584  
585 2 ▶sub swap {
Subroutine swap: called CC=1 SCAR=0.0
Pod documented
586 2 my ($self, $game) = @_;
587 2 $self->game($game);
588 2 return $self;
589 }
590  
591 1 ▶sub _declare {
Subroutine _declare: called CC=2 SCAR=7.0
592 1 my ($self, $g) = @_;
593 1 my $refusal = $g->declare;
594 1 ▶ $self->_say('no: ' . $refusal->message . ' (' . $refusal->code . ')') if $refusal;
Branch: 1 branch
BranchTrueFalseTotal
if $refusal 1 0 1
595 1 return $self;
596 }
597  
598 20 ▶sub _prompt {
Subroutine _prompt: called CC=1 SCAR=0.0
599 20 my ($self, $g) = @_;
600 20 return sprintf '%s to move> ', $SIDE{ $g->turn };
601 }
602  
603 11 ▶sub _describe {
Subroutine _describe: called CC=3 SCAR=11.0
604 11 my ($self, $g, $usi, $lead) = @_;
605 11 my $b = $g->board;
606 11 my $hodges = $N->hodges($b, $usi);
607 11 ▶ return "$lead $usi" unless defined $hodges;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hodges 1 10 11
608 10 ▶ return "$lead $usi ($hodges)" if $self->ascii;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ascii 8 2 10
609 2 return "$lead $usi ($hodges, " . $N->japanese($b, $usi) . ')';
610 }
611  
612 0 ▶sub _help {
Subroutine _help: not called CC=1 SCAR=6.9
613 0 my ($self) = @_;
614 0 $self->_say(
615 'a move is USI: from then to, like 7g7f, + to promote, like 8h2b+, and a drop like P*5e',
616 'board draw it again',
617 'legal every move you have',
618 'sfen the position as SFEN',
619 'log the moves so far',
620 'hint the strongest move this engine can find',
621 'declare declare the impasse (entering king), which wins when the count allows it',
622 "undo take your last move back, and the bot's reply with it",
623 'redo play again what undo took back',
624 'quit give up and leave',
625 );
626 0 return $self;
627 }
628  
629 1 ▶sub _legal {
Subroutine _legal: called CC=1 SCAR=0.0
630 1 my ($self, $g) = @_;
631 1 my @m = map { $_->usi } $g->legal;
632 1 $self->_say(scalar(@m) . ' moves: ' . join ' ', @m);
633 1 return $self;
634 }
635  
636 3 ▶sub _history {
Subroutine _history: called CC=3 SCAR=11.0
637 3 my ($self, $g) = @_;
638 3 my $record = $g->record;
639 3 ▶ my @line = map { $self->ascii ? $_->{hodges} : $_->{japanese} . ' ' . $_->{hodges} } @{ $record->{moves} };
Branch: 1 branch
BranchTrueFalseTotal
$self->ascii ? : 4 2 6
640 3 ▶ $self->_say(@line ? join(' ', map { ($_ + 1) . ". $line[$_]" } 0 .. $#line) : 'no moves yet');
Branch: 1 branch
BranchTrueFalseTotal
@line ? : 3 0 3
641 3 return $self;
642 }
643  
644 2 ▶sub _hint {
Subroutine _hint: called CC=2 SCAR=7.0
645 2 my ($self, $g) = @_;
646 2 my $mv = Game::Shogi::Bot->hint($g, $self->side, seed => $self->seed);
647 2 ▶ $self->_say(defined $mv ? $self->_describe($g, $mv, 'try') : 'nothing to suggest');
Branch: 1 branch
BranchTrueFalseTotal
defined $mv ? : 2 0 2
648 2 return $self;
649 }
650  
651 16 ▶sub _verdict {
Subroutine _verdict: called CC=5 SCAR=16.8
652 16 my ($self, $g) = @_;
653 16 ▶ my $r = $g->result or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $g->result or return 0 2 14 16
654 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
655 0 ▶ if ($r->is_draw) { $self->_say("drawn: $why"); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($r->is_draw) 0 2 2
656 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
657 1 $self->_say("you lost: $why");
658 1 return 1;
659 }
660  
661 4 ▶sub _usage {
Subroutine _usage: called CC=1 SCAR=0.0
662 4 return <<'USAGE';
663 shogi - Japanese chess on a terminal
664  
665 usage: shogi [options]
666  
667 --level N a rung of the bot's ladder (0 the weakest), or a node budget
668 --side b|w the side you play; Black moves first in an even game
669 --handicap NAME the bot gives a handicap (it plays White, and moves first)
670 --sfen SFEN start from a position instead of the opening
671 --moves LIST replay these USI moves first, comma or space separated
672 --seed STR for a reproducible game
673 --ascii SFEN letters instead of the kanji
674 --kanji-ranks the ranks as kanji numerals, as a Japanese board has them
675 --ansi colour; --no-ansi turns it off (NO_COLOR beats both)
676 --pick choose a move with the arrow keys; --no-pick to type it
677 --usi speak USI on stdin and stdout, and draw nothing
678 --help this
679  
680 exit status:
681 0 you won, or the game drew, or you quit
682 1 you lost
683 2 a bad option
684  
685 With a terminal and Term::ReadKey the arrow keys walk the pieces that have a
686 move, and the pieces in your hand that have a drop, and then that piece's
687 squares. Without either, or with --no-pick, a move is typed as USI, and "help"
688 lists the rest.
689  
690 Black's pieces are red and White's blue; with --no-ansi or NO_COLOR a White
691 piece is marked with a v before it instead. The engine is bounded in NODES and never in seconds, so the
692 same position answers the same on a busy machine as on an idle one.
693 USAGE
694 }
695  
696 2 ▶sub options {
Subroutine options: called CC=1 SCAR=0.0
Pod documented
697 return [
698 2 { name => 'level', type => 'int', min => 0, max => $#Game::Shogi::Bot::LADDER, help => 'a rung of the ladder, 0 the weakest' },
699 { name => 'side', type => 'enum', choices => [qw(b w)], help => 'the side you play; Black moves first' },
700 { name => 'handicap', type => 'enum', choices => [ Game::Shogi::Handicap->names ], help => 'the bot gives a handicap' },
701 { name => 'kanji-ranks', type => 'flag', help => 'the ranks as kanji numerals' },
702 { name => 'seed', type => 'str', help => 'for a reproducible game' },
703 ];
704 }
705  
706 24 ▶sub run {
Subroutine run: called CC=23 SCAR=31.4
Pod documented
707 24 my ($class, @argv) = @_;
708 24 my %opt = (side => 'b');
709  
710 24 my $ok = eval {
711 24 ▶ local $SIG{__WARN__} = sub { print STDERR "shogi: $_[0]" };
Subroutine __ANON__: called CC=1 SCAR=0.0
712 24 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
713 'level=i', 'side=s', 'handicap=s', 'sfen=s', 'moves=s', 'seed=s',
714 'ascii', 'kanji-ranks', 'ansi!', 'pick!', 'usi', 'help');
715 };
716 24 ▶ if (!$ok) { print STDERR _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 23 24
717 23 ▶ if ($opt{help}) { print _usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 2 21 23
718 21 ▶ if (@argv) { print STDERR "shogi: not an option: $argv[0]\n", _usage(); return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if (@argv) 1 20 21
719  
720 20 ▶ if ($opt{side} ne 'b' && $opt{side} ne 'w') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"side"} ne "b" and $opt{"side"} ne "w") 1 19 20
Condition66%2/3
$opt{"side"} ne "b" and $opt{"side"} ne "w"
!ll&&!rl&&r
1901
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
721 1 print STDERR "shogi: --side takes b or w, not '$opt{side}'\n";
722 1 return 2;
723 }
724  
725 19 my @ladder = @Game::Shogi::Bot::LADDER;
726 19 my $level;
727 19 ▶ if (defined $opt{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"level"}) 10 9 19
728 10 ▶ if ($opt{level} < 0) { print STDERR "shogi: --level must not be negative\n"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} < 0) 1 9 10
729 9 ▶ if ($opt{level} >= scalar @ladder && $opt{level} < $ladder[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]) 3 6 9
Condition100%3/3
$opt{"level"} >= scalar @ladder and $opt{"level"} < $ladder[0]
!ll&&!rl&&r
513
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
730 printf STDERR
731 "shogi: --level %d is neither a rung nor a budget: a rung is 0 to %d, "
732 . "and a budget is %d nodes or more\n",
733 3 $opt{level}, $#ladder, $ladder[0];
734 3 return 2;
735 }
736 6 ▶ $level = $opt{level} < scalar @ladder ? $ladder[ $opt{level} ] : $opt{level};
Branch: 1 branch
BranchTrueFalseTotal
$opt{'level'} < scalar @ladder ? : 5 1 6
737 }
738  
739 15 ▶ if ($opt{usi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"usi"}) 2 13 15
740 2 require Game::Shogi::USI;
741 2 return Game::Shogi::USI->new(nodes => $level)->run;
742 }
743  
744 13 ▶ if (defined $opt{handicap} && defined $opt{sfen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"handicap"} and defined $opt{"sfen"}) 1 12 13
Condition100%3/3
defined $opt{"handicap"} and defined $opt{"sfen"}
!ll&&!rl&&r
931
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
745 1 print STDERR "shogi: --handicap and --sfen both name the start; give one\n";
746 1 return 2;
747 }
748 12 ▶ 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 10 12
Condition100%3/3
defined $opt{"handicap"} and not "Game::Shogi::Handicap"->known($opt{"handicap"})
!ll&&!rl&&r
912
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
749 2 print STDERR "shogi: '$opt{handicap}' is not a handicap; the handicaps are ",
750 join(', ', Game::Shogi::Handicap->names), "\n";
751 2 return 2;
752 }
753 10 my $game = eval {
754 Game::Shogi->new(
755 (defined $opt{handicap} ? (handicap => $opt{handicap}) : ()),
756 10 ▶ (defined $opt{sfen} ? (sfen => $opt{sfen}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $opt{'handicap'} ? : 1 9 10
defined $opt{'sfen'} ? : 2 8 10
757 );
758 };
759 10 ▶ if (!$game) { print STDERR "shogi: --sfen refused: $@"; return 2 }
Branch: 1 branch
BranchTrueFalseTotal
unless ($game) 1 9 10
760  
761 9 ▶ if (defined $opt{moves}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opt{"moves"}) 3 6 9
762 3 for my $mv (grep { length } split /[\s,]+/, $opt{moves}) {
763 6 my $refusal = $game->play($mv);
764 6 ▶ next unless $refusal;
Branch: 1 branch
BranchTrueFalseTotal
unless $refusal 4 2 6
765 2 print STDERR "shogi: --moves refused at '$mv': ", $refusal->message, "\n";
766 2 return 2;
767 }
768 }
769  
770 my $terminal = $class->new(
771 ascii => $opt{ascii},
772 kanji_ranks => $opt{'kanji-ranks'},
773 colour => $opt{ansi},
774 picking => $opt{pick},
775 level => $level,
776 seed => $opt{seed} // 'shogi-terminal',
777 side => $opt{side},
778 7 ▶ game => $game,
Condition100%2/2
$opt{"seed"} // "shogi-terminal"
l!l
16
MC/DC100%1/1
$opt{"seed"} // "shogi-terminal"
$opt{"seed"}
Truth table100%2/2
A
A= $opt{"seed"}
Aresult
11
00
779 );
780  
781 return Game::Terminal::Kit::Run::guard(
782 7 ▶ sub { $terminal->start }, $terminal->keyboard, $terminal->screen);
Subroutine __ANON__: called CC=1 SCAR=0.0
783 }
784  
785 4 1;
786  
787 __END__
↑ Summary blib/lib/Game/Shogi/USI.pm »