blib/lib/Game/Shogi/Engine.pm

filter: Statementstmt 100% 341 / 341 Branchbran 90.6% 174 / 192 Conditioncond 70.3% 121 / 172 MC/DCmcdc 52.5% 51 / 97 Subroutinesub 100% 89 / 89 Podpod 98.4% 64 / 65 totaltotal 87.8% 840 / 956 CC 158 SCAR 15.0
CC
158
Cov
90%
search
12.2
BUILD
11.0
sq_of
8.5
CRAP 4.5
« blib/lib/Game/Shogi/Error.pm ↑ Summary blib/lib/Game/Shogi/Move.pm »
1 package Game::Shogi::Engine;
2  
3 24 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 24 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 24 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 24 our $VERSION = '0.01';
12  
13 24 require XSLoader;
14 24 XSLoader::load('Game::Shogi', $VERSION);
15  
16 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 SQUARES => 81,
18 FILES => 9,
19 RANKS => 9,
20 BLACK => 0,
21 WHITE => 1,
22 EMPTY => 0,
23 OFF => 64,
24 SET_PIECES => 40,
25 SFEN_MAX => 256,
26 MOVE_MAX => 999999,
27 MOVES_MAX => 1024,
28 24 };
29  
30 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 24 PAWN => 1,
32 LANCE => 2,
33 KNIGHT => 3,
34 SILVER => 4,
35 GOLD => 5,
36 BISHOP => 6,
37 ROOK => 7,
38 KING => 8,
39 TOKIN => 9,
40 PRO_LANCE => 10,
41 PRO_KNIGHT => 11,
42 PRO_SILVER => 12,
43 HORSE => 13,
44 DRAGON => 14,
45 24 };
46  
47 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
48 24 POS_OK => 0,
49 POS_NULL => 1,
50 POS_LONG => 2,
51 POS_FIELDS => 3,
52 POS_RANKS => 4,
53 POS_LETTER => 5,
54 POS_WIDTH => 6,
55 POS_PROMOTE => 7,
56 POS_SIDE => 8,
57 POS_HAND => 9,
58 POS_MOVE => 10,
59 POS_KINGS => 11,
60 POS_COUNT => 12,
61 24 };
62  
63 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 24 DROP_OK => 0,
65 DROP_BAD => 1,
66 DROP_NOT_HELD => 2,
67 DROP_OCCUPIED => 3,
68 DROP_DEAD => 4,
69 DROP_TWO_PAWNS => 5,
70 DROP_IN_CHECK => 6,
71 DROP_PAWN_MATE => 7,
72 24 };
73  
74 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
75 24 FAULT_OK => 0,
76 FAULT_BAD => 1,
77 FAULT_NOT_YOURS => 2,
78 FAULT_NOT_HELD => 3,
79 FAULT_CANNOT_REACH => 4,
80 FAULT_OCCUPIED => 5,
81 FAULT_PROMOTE_NOT_ALLOWED => 6,
82 FAULT_PROMOTE_REQUIRED => 7,
83 FAULT_DEAD => 8,
84 FAULT_TWO_PAWNS => 9,
85 FAULT_IN_CHECK => 10,
86 FAULT_PAWN_MATE => 11,
87 24 };
88  
89 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
90 24 DECLARE_OK => 0,
91 DECLARE_NO_KING => 1,
92 DECLARE_FEW_PIECES => 2,
93 DECLARE_IN_CHECK => 3,
94 DECLARE_FEW_POINTS => 4,
95 DECLARE_PIECES => 10,
96 DECLARE_POINTS_BLACK => 28,
97 DECLARE_POINTS_WHITE => 27,
98 PACKED => 96,
99 REP_TIMES => 4,
100 24 };
101  
102 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
103 24 MATE_SCORE => 30000,
104 MATE_BOUND => 29000,
105 EVAL_NO_HAND_BONUS => 1,
106 EVAL_NO_KING_SAFETY => 2,
107 EVAL_NO_ENTERING => 4,
108 SEARCH_NO_MATE => 8,
109 24 };
110  
111 24 our @EXPORT_OK = qw(
112 SQUARES FILES RANKS BLACK WHITE EMPTY OFF SET_PIECES SFEN_MAX MOVE_MAX MOVES_MAX
113 PAWN LANCE KNIGHT SILVER GOLD BISHOP ROOK KING
114 TOKIN PRO_LANCE PRO_KNIGHT PRO_SILVER HORSE DRAGON
115 POS_OK POS_NULL POS_LONG POS_FIELDS POS_RANKS POS_LETTER POS_WIDTH POS_PROMOTE
116 POS_SIDE POS_HAND POS_MOVE POS_KINGS POS_COUNT
117 DROP_OK DROP_BAD DROP_NOT_HELD DROP_OCCUPIED DROP_DEAD DROP_TWO_PAWNS DROP_IN_CHECK DROP_PAWN_MATE
118 FAULT_OK FAULT_BAD FAULT_NOT_YOURS FAULT_NOT_HELD FAULT_CANNOT_REACH FAULT_OCCUPIED
119 FAULT_PROMOTE_NOT_ALLOWED FAULT_PROMOTE_REQUIRED FAULT_DEAD FAULT_TWO_PAWNS FAULT_IN_CHECK
120 FAULT_PAWN_MATE
121 DECLARE_OK DECLARE_NO_KING DECLARE_FEW_PIECES DECLARE_IN_CHECK DECLARE_FEW_POINTS
122 DECLARE_PIECES DECLARE_POINTS_BLACK DECLARE_POINTS_WHITE PACKED REP_TIMES
123 MATE_SCORE MATE_BOUND EVAL_NO_HAND_BONUS EVAL_NO_KING_SAFETY EVAL_NO_ENTERING SEARCH_NO_MATE
124 );
125 24 our %EXPORT_TAGS = (all => \@EXPORT_OK);
126  
127 24 my @REASON = (
128 'the position is fine',
129 'there is no position',
130 'the position is too long',
131 'the position is not three or four fields with single spaces between them',
132 'the board is not nine ranks',
133 'the board has a character that is not a piece, a run of 1 to 9, + or /, or two runs side by side',
134 'a rank of the board is not nine squares',
135 'a + is not before a pawn, lance, knight, silver, bishop or rook',
136 'the side to move is not b or w',
137 'the pieces in hand are not - or counted pawns, lances, knights, silvers, golds, bishops and rooks',
138 'the move number is not 1 to 999999',
139 'one side has two kings',
140 'there are more of a piece on the board and in hand than a set has',
141 );
142  
143 24 my @LETTER = (undef, qw(P L N S G B R K));
144  
145 24 my @DECLARE_REASON = (
146 'the impasse may be declared',
147 'the king is not in the enemy camp',
148 'there are fewer than ten other pieces in the enemy camp',
149 'the king is in check',
150 'there are not enough points',
151 );
152  
153 24 my @FAULT_REASON = (
154 'the move is allowed',
155 'that is not a move',
156 'there is no piece of yours there',
157 'you have none of that piece in hand',
158 'that piece cannot move there',
159 'a piece cannot be dropped onto another',
160 'that piece may not promote on that move',
161 'that piece must promote on that move',
162 'a piece cannot be dropped where it could never move',
163 'you already have an unpromoted pawn on that file',
164 'that would leave your king in check',
165 'a pawn may not be dropped to give mate',
166 );
167  
168 24 my @DROP_REASON = (
169 'the drop is allowed',
170 'that is not a square, or not a piece a hand holds',
171 'there is none of that piece in hand',
172 'the square is not empty',
173 'the piece could never move from there',
174 'there is already an unpromoted pawn of the same side on that file',
175 'it leaves the king in check',
176 'a pawn may not be dropped to give mate',
177 );
178 24 my %KIND = (P => PAWN, L => LANCE, N => KNIGHT, S => SILVER, G => GOLD, B => BISHOP, R => ROOK, K => KING);
179 24 my %SIDE = (b => BLACK, w => WHITE);
180  
181 24 has _ptr => (is => 'rw', private => 1);
182  
183 24 has _hold => (is => 'rw', private => 1);
184  
185 24 has _want_empty => (is => 'ro', init_arg => 'empty', private => 1, default => 0);
186  
187 24 has _want_sfen => (is => 'ro', init_arg => 'sfen', private => 1);
188  
189 24 has _history => (is => 'rw', private => 1);
190  
191 24 has _trail => (is => 'rw', private => 1);
192  
193 24 has cap => (is => 'ro', default => 500);
194  
195 24 my %ADOPTING;
196  
197 5841 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.0
Pod undocumented
198 5841 my ($self) = @_;
199 5841 my $cap = $self->cap;
200 5841 ▶ Carp::croak("Game::Shogi::Engine: the cap is a move count of 1 or more, not '" . ($cap // 'undef') . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cap and $cap =~ /\A[0-9]+\z/u and $cap >= 1 4 5837 5841
Condition75%6/8
defined $cap and $cap =~ /\A[0-9]+\z/u and $cap >= 1
!ll&&!rl&&r
315837
defined $cap and $cap =~ /\A[0-9]+\z/u
!ll&&!rl&&r
035838
$cap // "undef"
l!l
40
MC/DC66%2/3
defined $cap and $cap =~ /\A[0-9]+\z/u and $cap >= 1
defined $cap $cap =~ /\A[0-9]+\z/u $cap >= 1
MC/DC0%0/1
$cap // "undef"
$cap
Truth table75%3/4
A and B and C
A= defined $capB= $cap =~ /\A[0-9]+\z/uC= $cap >= 1
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $cap
Aresult
11
00
201 unless defined $cap && $cap =~ /\A[0-9]+\z/ && $cap >= 1;
202 5837 my $given = $self->_ptr;
203 5837 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 1120 4717 5837
204 1120 ▶ if (delete $ADOPTING{$given}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $ADOPTING{$given}) 1119 1 1120
205 1119 $self->_hold(_hold_board($given));
206 1119 return;
207 }
208 1 $self->_ptr(0);
209 1 Carp::croak("Game::Shogi::Engine: a board is made by new, from_sfen or clone");
210 }
211  
212 4717 my $sfen = $self->_want_sfen;
213 4717 ▶ if (defined $sfen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sfen) 2103 2614 4717
214 2103 my ($ptr, $err) = _of_sfen($sfen);
215 2103 ▶ Carp::croak("Game::Shogi::Engine: the SFEN was refused: $REASON[$err]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 3 2100 2103
216 unless defined $ptr;
217 2100 $self->_ptr($ptr);
218 2100 $self->_hold(_hold_board($ptr));
219 2100 return;
220 }
221  
222 2614 ▶ $self->_ptr($self->_want_empty ? _new_empty() : _new_board());
Branch: 1 branch
BranchTrueFalseTotal
$self->_want_empty ? : 2344 270 2614
223 2614 ▶ Carp::croak("Game::Shogi::Engine: the board was not built") unless $self->_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ptr 0 2614 2614
224 2614 $self->_hold(_hold_board($self->_ptr));
225 2614 return;
226 }
227  
228 1119 ▶my $adopt = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
229 1119 my ($class, $ptr, $cap) = @_;
230 1119 $ADOPTING{$ptr} = 1;
231 1119 ▶ return $class->new(_ptr => $ptr, (defined $cap ? (cap => $cap) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $cap ? : 1107 12 1119
232 24 };
233  
234 60 ▶sub from_sfen {
Subroutine from_sfen: called CC=4 SCAR=14.1
Pod documented
235 60 my ($class, $sfen) = @_;
236 60 ▶ my ($ptr, $err) = _of_sfen(defined $sfen ? $sfen : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $sfen ? : 59 1 60
237 60 ▶ return (undef, $err) unless defined $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 48 12 60
238 12 ▶ return ($adopt->(ref($class) || $class, $ptr), POS_OK);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
239 }
240  
241 59 ▶sub reason {
Subroutine reason: called CC=3 SCAR=13.1
Pod documented
242 59 my ($class, $code) = @_;
243 59 ▶ return defined $code && $code =~ /\A[0-9]+\z/ ? $REASON[$code] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code && $code =~ /\A[0-9]+\z/u ? : 59 0 59
Condition33%1/3
defined $code and $code =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0059
MC/DC0%0/2
defined $code and $code =~ /\A[0-9]+\z/u
defined $code $code =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
244 }
245  
246 1107 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
247 1107 my ($self) = @_;
248 1107 return $adopt->(ref($self), _copy_board($self->_ptr), $self->cap);
249 }
250  
251 74168 ▶sub _token_of {
Subroutine _token_of: called CC=6 SCAR=17.9
252 74168 my ($piece) = @_;
253 74168 ▶ return undef if $piece == EMPTY || $piece >= OFF;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 or $piece >= 64 45589 28579 74168
Condition66%2/3
$piece == 0 or $piece >= 64
l!l&&r!l&&!r
45589028579
MC/DC50%1/2
$piece == 0 or $piece >= 64
$piece == 0 $piece >= 64
Truth table66%2/3
A or B
A= $piece == 0B= $piece >= 64
ABresult
1X1
011
000
254 28579 my ($side, $kind) = ($piece >> 4 & 1, $piece & 15);
255 28579 my $promoted = $kind > KING;
256 28579 ▶ my $letter = $LETTER[ $promoted ? _unpromoted($kind) : $kind ];
Branch: 1 branch
BranchTrueFalseTotal
$promoted ? : 599 27980 28579
257 28579 ▶ $letter = lc $letter if $side == WHITE;
Branch: 1 branch
BranchTrueFalseTotal
if $side == 1 13962 14617 28579
258 28579 ▶ return ($promoted ? '+' : '') . $letter;
Branch: 1 branch
BranchTrueFalseTotal
$promoted ? : 599 27980 28579
259 }
260  
261 4860 ▶sub _piece_of {
Subroutine _piece_of: called CC=6 SCAR=17.9
262 4860 my ($token) = @_;
263 4860 ▶ return undef unless defined $token && $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/u 7 4853 4860
Condition100%3/3
defined $token and $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/u
!ll&&!rl&&r
164853
MC/DC100%2/2
defined $token and $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/u
defined $token $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/u
Truth table100%3/3
A and B
A= defined $tokenB= $token =~ /\A(\+?)([PLNSGBRKplnsgbrk])\z/u
ABresult
0X0
100
111
264 4853 my ($plus, $letter) = ($1, $2);
265 4853 my $kind = $KIND{ uc $letter };
266 4853 ▶ if ($plus) {
Branch: 1 branch
BranchTrueFalseTotal
if ($plus) 2001 2852 4853
267 2001 $kind = _promoted($kind);
268 2001 ▶ return undef unless $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless $kind 4 1997 2001
269 }
270 4849 ▶ return (($letter eq lc $letter ? WHITE : BLACK) << 4) | $kind;
Branch: 1 branch
BranchTrueFalseTotal
$letter eq lc $letter ? : 1249 3600 4849
271 }
272  
273 264403 ▶sub _side_of {
Subroutine _side_of: called CC=2 SCAR=7.2
274 264403 my ($side) = @_;
275 264403 ▶ return defined $side ? $SIDE{$side} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 264403 0 264403
276 }
277  
278 12383 ▶sub _hand_kind {
Subroutine _hand_kind: called CC=3 SCAR=11.0
279 12383 my ($letter) = @_;
280 12383 ▶ return undef unless defined $letter && $letter =~ /\A[PLNSGBRplnsgbr]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $letter and $letter =~ /\A[PLNSGBRplnsgbr]\z/u 6 12377 12383
Condition100%3/3
defined $letter and $letter =~ /\A[PLNSGBRplnsgbr]\z/u
!ll&&!rl&&r
1512377
MC/DC100%2/2
defined $letter and $letter =~ /\A[PLNSGBRplnsgbr]\z/u
defined $letter $letter =~ /\A[PLNSGBRplnsgbr]\z/u
Truth table100%3/3
A and B
A= defined $letterB= $letter =~ /\A[PLNSGBRplnsgbr]\z/u
ABresult
0X0
100
111
281 12377 return $KIND{ uc $letter };
282 }
283  
284 74168 ▶sub at {
Subroutine at: called CC=2 SCAR=7.1
Pod documented
285 74168 my ($self, $sq) = @_;
286 74168 ▶ return undef unless _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_sq($sq) 0 74168 74168
287 74168 return _token_of(_at($self->_ptr, $sq));
288 }
289  
290 2416 ▶sub put {
Subroutine put: called CC=3 SCAR=11.0
Pod documented
291 2416 my ($self, $sq, $token) = @_;
292 2416 my $piece = _piece_of($token);
293 2416 ▶ _put($self->_ptr, $sq, $piece) if _is_sq($sq) && defined $piece;
Branch: 1 branch
BranchTrueFalseTotal
if _is_sq($sq) and defined $piece 2409 7 2416
Condition100%3/3
_is_sq($sq) and defined $piece
!ll&&!rl&&r
252409
MC/DC100%2/2
_is_sq($sq) and defined $piece
_is_sq($sq) defined $piece
Truth table100%3/3
A and B
A= _is_sq($sq)B= defined $piece
ABresult
0X0
100
111
294 2416 return $self;
295 }
296  
297 102 ▶sub lift {
Subroutine lift: called CC=2 SCAR=7.1
Pod documented
298 102 my ($self, $sq) = @_;
299 102 ▶ _lift($self->_ptr, $sq) if _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
if _is_sq($sq) 102 0 102
300 102 return $self;
301 }
302  
303 12118 ▶sub hand {
Subroutine hand: called CC=3 SCAR=12.0
Pod documented
304 12118 my ($self, $side, $letter) = @_;
305 12118 my ($s, $k) = (_side_of($side), _hand_kind($letter));
306 12118 ▶ return 0 unless defined $s && defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and defined $k 0 12118 12118
Condition33%1/3
defined $s and defined $k
!ll&&!rl&&r
0012118
MC/DC0%0/2
defined $s and defined $k
defined $s defined $k
Truth table33%1/3
A and B
A= defined $sB= defined $k
ABresult
0X0
100
111
307 12118 return _hand($self->_ptr, $s, $k);
308 }
309  
310 20 ▶sub set_hand {
Subroutine set_hand: called CC=5 SCAR=16.1
Pod documented
311 20 my ($self, $side, $letter, $count) = @_;
312 20 my ($s, $k) = (_side_of($side), _hand_kind($letter));
313 20 ▶ _set_hand($self->_ptr, $s, $k, $count)
Branch: 1 branch
BranchTrueFalseTotal
if defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u 15 5 20
Condition88%8/9
defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u
!ll&&!rl&&r
3215
defined $s and defined $k and defined $count
!ll&&!rl&&r
3017
defined $s and defined $k
!ll&&!rl&&r
1217
MC/DC75%3/4
defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u
defined $s defined $k defined $count $count =~ /\A[0-9]+\z/u
Truth table80%4/5
A and B and C and D
A= defined $sB= defined $kC= defined $countD= $count =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
314 if defined $s && defined $k && defined $count && $count =~ /\A[0-9]+\z/;
315 20 return $self;
316 }
317  
318 856 ▶sub hands {
Subroutine hands: called CC=4 SCAR=13.9
Pod documented
319 856 my ($self) = @_;
320 856 my %hands = (b => {}, w => {});
321 856 for my $side (qw(b w)) {
322 1712 for my $letter (qw(R B G S N L P)) {
323 11984 my $n = $self->hand($side, $letter);
324 11984 ▶ $hands{$side}{$letter} = $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 863 11121 11984
325 }
326 }
327 856 return \%hands;
328 }
329  
330 34304 ▶sub side_to_move { _to_move($_[0]->_ptr) == WHITE ? 'w' : 'b' }
Subroutine side_to_move: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_to_move($_[0]->_ptr) == 1 ? : 17537 16767 34304
331  
332 2329 ▶sub set_side_to_move {
Subroutine set_side_to_move: called CC=2 SCAR=7.0
Pod documented
333 2329 my ($self, $side) = @_;
334 2329 my $s = _side_of($side);
335 2329 ▶ _set_to_move($self->_ptr, $s) if defined $s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 2329 0 2329
336 2329 return $self;
337 }
338  
339 30544 ▶sub move_number { _move_number($_[0]->_ptr) }
Subroutine move_number: called CC=1 SCAR=0.0
Pod documented
340  
341 6 ▶sub set_move_number {
Subroutine set_move_number: called CC=4 SCAR=13.9
Pod documented
342 6 my ($self, $n) = @_;
343 6 ▶ _set_move_number($self->_ptr, $n) if defined $n && $n =~ /\A[0-9]+\z/ && $n <= MOVE_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if defined $n and $n =~ /\A[0-9]+\z/u and $n <= 999999 3 3 6
Condition83%5/6
defined $n and $n =~ /\A[0-9]+\z/u and $n <= 999999
!ll&&!rl&&r
213
defined $n and $n =~ /\A[0-9]+\z/u
!ll&&!rl&&r
024
MC/DC66%2/3
defined $n and $n =~ /\A[0-9]+\z/u and $n <= 999999
defined $n $n =~ /\A[0-9]+\z/u $n <= 999999
Truth table75%3/4
A and B and C
A= defined $nB= $n =~ /\A[0-9]+\z/uC= $n <= 999999
ABCresult
0XX0
10X0
1100
1111
344 6 return $self;
345 }
346  
347 10 ▶sub king_square {
Subroutine king_square: called CC=3 SCAR=11.0
Pod documented
348 10 my ($self, $side) = @_;
349 10 my $s = _side_of($side);
350 10 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 9 10
351 9 my $sq = _king_sq($self->_ptr, $s);
352 9 ▶ return $sq < 0 ? undef : $sq;
Branch: 1 branch
BranchTrueFalseTotal
$sq < 0 ? : 3 6 9
353 }
354  
355 2684 ▶sub sfen { _to_sfen($_[0]->_ptr) }
Subroutine sfen: called CC=1 SCAR=0.0
Pod documented
356 38130 ▶sub key { _key_hex($_[0]->_ptr) }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
357  
358 1852292 ▶sub _is_sq { defined $_[0] && $_[0] =~ /\A[0-9]+\z/ && $_[0] < SQUARES }
Subroutine _is_sq: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u and $_[0] < 81 1852283 9 1852292
Condition100%3/3
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
271852283
MC/DC100%2/2
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u
defined $_[0] $_[0] =~ /\A[0-9]+\z/u
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
359  
360 781955 ▶sub _move_string {
Subroutine _move_string: called CC=3 SCAR=11.0
361 781955 my ($m) = @_;
362 781955 my $to = __PACKAGE__->square_name(($m >> 7) & 127);
363 781955 ▶ return $LETTER[ ($m >> 16) & 15 ] . '*' . $to if ($m >> 15) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $m >> 15 & 1 148853 633102 781955
364 633102 my $from = __PACKAGE__->square_name($m & 127);
365 633102 ▶ return $from . $to . (($m >> 14) & 1 ? '+' : '');
Branch: 1 branch
BranchTrueFalseTotal
$m >> 14 & 1 ? : 31921 601181 633102
366 }
367  
368 162696 ▶sub _move_of {
Subroutine _move_of: called CC=5 SCAR=16.1
369 162696 my ($usi) = @_;
370 162696 ▶ return undef unless defined $usi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $usi 3 162693 162696
371 162693 ▶ if ($usi =~ /\A([PLNSGBR])\*([1-9][a-i])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usi =~ /\A([PLNSGBR])\*([1-9][a-i])\z/u) 23091 139602 162693
372 23091 return (1 << 15) | ($KIND{$1} << 16) | (__PACKAGE__->square($2) << 7);
373 }
374 139602 ▶ return undef unless $usi =~ /\A([1-9][a-i])([1-9][a-i])(\+?)\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $usi =~ /\A([1-9][a-i])([1-9][a-i])(\+?)\z/u 17 139585 139602
375 139585 my ($from, $to) = (__PACKAGE__->square($1), __PACKAGE__->square($2));
376 139585 ▶ return $from | ($to << 7) | ($3 ? 1 << 14 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 50286 89299 139585
377 }
378  
379 213583 ▶sub attacked {
Subroutine attacked: called CC=4 SCAR=13.9
Pod documented
380 213583 my ($self, $sq, $by) = @_;
381 213583 my $s = _side_of($by);
382 213583 ▶ return 0 unless defined $s && _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and _is_sq($sq) 2 213581 213583
Condition100%3/3
defined $s and _is_sq($sq)
!ll&&!rl&&r
11213581
MC/DC100%2/2
defined $s and _is_sq($sq)
defined $s _is_sq($sq)
Truth table100%3/3
A and B
A= defined $sB= _is_sq($sq)
ABresult
0X0
100
111
383 213581 ▶ return _attacked($self->_ptr, $sq, $s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_attacked($self->_ptr, $sq, $s) ? : 27510 186071 213581
384 }
385  
386 33218 ▶sub in_check {
Subroutine in_check: called CC=3 SCAR=11.0
Pod documented
387 33218 my ($self, $side) = @_;
388 33218 my $s = _side_of($side);
389 33218 ▶ return 0 unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 33218 33218
390 33218 ▶ return _in_check($self->_ptr, $s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_in_check($self->_ptr, $s) ? : 860 32358 33218
391 }
392  
393 2753 ▶sub pseudo_moves { map { _move_string($_) } _pseudo_moves($_[0]->_ptr) }
Subroutine pseudo_moves: called CC=1 SCAR=0.0
Pod documented
394 100 ▶sub board_moves { map { _move_string($_) } _board_moves($_[0]->_ptr) }
Subroutine board_moves: called CC=1 SCAR=0.0
Pod documented
395  
396 33169 ▶sub do_move {
Subroutine do_move: called CC=4 SCAR=13.9
Pod documented
397 33169 my ($self, $usi) = @_;
398 33169 my $m = _move_of($usi);
399 33169 ▶ return 0 unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 12 33157 33169
400 33157 my $trail = $self->_trail_now;
401 33157 my $record = _do_move($self->_ptr, $m);
402 33157 ▶ return 0 unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 11 33146 33157
403 33146 my $history = $self->_history;
404 33146 ▶ $self->_history($history = []) unless $history;
Branch: 1 branch
BranchTrueFalseTotal
unless $history 403 32743 33146
405 33146 push @$history, $record;
406 33146 push @$trail, [ $self->key, _pack($self->_ptr), $self->in_check($self->side_to_move) ];
407 33146 return 1;
408 }
409  
410 1987 ▶sub undo_move {
Subroutine undo_move: called CC=6 SCAR=18.1
Pod documented
411 1987 my ($self) = @_;
412 1987 my $history = $self->_history;
413 1987 ▶ return 0 unless $history && @$history;
Branch: 1 branch
BranchTrueFalseTotal
unless $history and @$history 8 1979 1987
Condition100%3/3
$history and @$history
!ll&&!rl&&r
171979
MC/DC100%2/2
$history and @$history
$history @$history
Truth table100%3/3
A and B
A= $historyB= @$history
ABresult
0X0
100
111
414 1979 ▶ return 0 unless _undo_move($self->_ptr, $history->[-1]);
Branch: 1 branch
BranchTrueFalseTotal
unless _undo_move($self->_ptr, $history->[-1]) 2 1977 1979
415 1977 pop @$history;
416 1977 my $trail = $self->_trail;
417 1977 ▶ pop @$trail if $trail && @$trail > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $trail and @$trail > 1 1977 0 1977
Condition33%1/3
$trail and @$trail > 1
!ll&&!rl&&r
001977
MC/DC0%0/2
$trail and @$trail > 1
$trail @$trail > 1
Truth table33%1/3
A and B
A= $trailB= @$trail > 1
ABresult
0X0
100
111
418 1977 return 1;
419 }
420  
421 9 ▶sub drops { map { _move_string($_) } _drops($_[0]->_ptr) }
Subroutine drops: called CC=1 SCAR=0.0
Pod documented
422 17395 ▶sub legal { map { _move_string($_) } _legal($_[0]->_ptr) }
Subroutine legal: called CC=1 SCAR=0.0
Pod documented
423 7 ▶sub has_legal_move { _has_legal_move($_[0]->_ptr) ? 1 : 0 }
Subroutine has_legal_move: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_has_legal_move($_[0]->_ptr) ? : 2 5 7
424  
425 40 ▶sub drop_fault {
Subroutine drop_fault: called CC=4 SCAR=13.9
Pod documented
426 40 my ($self, $letter, $sq) = @_;
427 40 my $k = _hand_kind($letter);
428 40 ▶ return DROP_BAD unless defined $k && _is_sq($sq) && $letter eq uc $letter;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $k and _is_sq($sq) and $letter eq uc $letter 8 32 40
Condition100%6/6
defined $k and _is_sq($sq) and $letter eq uc $letter
!ll&&!rl&&r
7132
defined $k and _is_sq($sq)
!ll&&!rl&&r
4333
MC/DC100%3/3
defined $k and _is_sq($sq) and $letter eq uc $letter
defined $k _is_sq($sq) $letter eq uc $letter
Truth table100%4/4
A and B and C
A= defined $kB= _is_sq($sq)C= $letter eq uc $letter
ABCresult
0XX0
10X0
1100
1111
429 32 return _drop_fault($self->_ptr, $k, $sq);
430 }
431  
432 8 ▶sub drop_reason {
Subroutine drop_reason: called CC=3 SCAR=13.1
Pod documented
433 8 my ($class, $code) = @_;
434 8 ▶ return defined $code && $code =~ /\A[0-9]+\z/ ? $DROP_REASON[$code] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code && $code =~ /\A[0-9]+\z/u ? : 8 0 8
Condition33%1/3
defined $code and $code =~ /\A[0-9]+\z/u
!ll&&!rl&&r
008
MC/DC0%0/2
defined $code and $code =~ /\A[0-9]+\z/u
defined $code $code =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
435 }
436  
437 24 my @PERFT = qw(nodes captures drops promotions checks mates);
438  
439 13 ▶sub perft {
Subroutine perft: called CC=3 SCAR=11.0
Pod documented
440 13 my ($self, $depth) = @_;
441 13 ▶ return 0 unless defined $depth && $depth =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth and $depth =~ /\A[0-9]+\z/u 3 10 13
Condition66%2/3
defined $depth and $depth =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0310
MC/DC50%1/2
defined $depth and $depth =~ /\A[0-9]+\z/u
defined $depth $depth =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $depthB= $depth =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
442 10 return (_perft($self->_ptr, $depth))[0];
443 }
444  
445 4 ▶sub perft_counts {
Subroutine perft_counts: called CC=3 SCAR=11.8
Pod documented
446 4 my ($self, $depth) = @_;
447 4 ▶ return undef unless defined $depth && $depth =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth and $depth =~ /\A[0-9]+\z/u 0 4 4
Condition33%1/3
defined $depth and $depth =~ /\A[0-9]+\z/u
!ll&&!rl&&r
004
MC/DC0%0/2
defined $depth and $depth =~ /\A[0-9]+\z/u
defined $depth $depth =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $depthB= $depth =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
448 4 my @counts = _perft($self->_ptr, $depth);
449 4 return { map { $PERFT[$_] => $counts[$_] } 0 .. $#PERFT };
450 }
451  
452 24 my @STATUS = qw(ongoing mate no_move);
453  
454 219 ▶sub status { $STATUS[ _status($_[0]->_ptr) ] }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
455  
456 34 ▶sub _other { $_[0] eq 'b' ? 'w' : 'b' }
Subroutine _other: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0] eq 'b' ? : 17 17 34
457  
458 30516 ▶sub outcome {
Subroutine outcome: called CC=3 SCAR=11.0
Pod documented
459 30516 my ($self) = @_;
460 30516 my $s = _status($self->_ptr);
461 30516 ▶ return $self->repetition // $self->_capped unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 30486 30 30516
Condition100%3/3
$self->repetition // $self->_capped
l!l&&r!l&&!r
10730469
MC/DC100%2/2
$self->repetition // $self->_capped
$self->repetition $self->_capped
Truth table100%3/3
A // B
A= $self->repetitionB= $self->_capped
ABresult
1X1
011
000
462 30 require Game::Shogi::Result;
463 30 return Game::Shogi::Result->new(
464 how => $STATUS[$s],
465 winner => _other($self->side_to_move),
466 ply => $self->move_number - 1,
467 );
468 }
469  
470 33157 ▶sub _trail_now {
Subroutine _trail_now: called CC=2 SCAR=6.9
471 33157 my ($self) = @_;
472 33157 my $trail = $self->_trail;
473 33157 ▶ return $trail if $trail;
Branch: 1 branch
BranchTrueFalseTotal
if $trail 32752 405 33157
474 405 $self->_trail($trail = [ [ $self->key, _pack($self->_ptr), 0 ] ]);
475 405 return $trail;
476 }
477  
478 37 ▶sub packed { _pack($_[0]->_ptr) }
Subroutine packed: called CC=1 SCAR=0.0
Pod documented
479  
480 10 ▶sub evaluate {
Subroutine evaluate: called CC=3 SCAR=11.1
Pod documented
481 10 my ($self, $flags) = @_;
482 10 ▶ return _evaluate($self->_ptr, defined $flags && $flags =~ /\A[0-9]+\z/ ? $flags : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $flags && $flags =~ /\A[0-9]+\z/u ? : 3 7 10
Condition66%2/3
defined $flags and $flags =~ /\A[0-9]+\z/u
!ll&&!rl&&r
703
MC/DC50%1/2
defined $flags and $flags =~ /\A[0-9]+\z/u
defined $flags $flags =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $flagsB= $flags =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
483 }
484  
485 55 ▶sub history {
Subroutine history: called CC=3 SCAR=11.0
Pod documented
486 55 my ($self) = @_;
487 55 my $trail = $self->_trail;
488 55 ▶ return () unless $trail && $trail->[-1][1] eq _pack($self->_ptr);
Branch: 1 branch
BranchTrueFalseTotal
unless $trail and $trail->[-1][1] eq _pack($self->_ptr) 4 51 55
Condition66%2/3
$trail and $trail->[-1][1] eq _pack($self->_ptr)
!ll&&!rl&&r
4051
MC/DC50%1/2
$trail and $trail->[-1][1] eq _pack($self->_ptr)
$trail $trail->[-1][1] eq _pack($self->_ptr)
Truth table66%2/3
A and B
A= $trailB= $trail->[-1][1] eq _pack($self->_ptr)
ABresult
0X0
100
111
489 51 return map { $_->[0] } @$trail[ 0 .. $#$trail - 1 ];
490 }
491  
492 290 ▶sub search {
Subroutine search: called CC=12 SCAR=25.0
Pod documented
493 290 my ($self, %opt) = @_;
494 290 ▶ my $budget = $opt{nodes} // 60_000;
Condition50%1/2
$opt{"nodes"} // 60000
l!l
2900
MC/DC0%0/1
$opt{"nodes"} // 60000
$opt{"nodes"}
Truth table50%1/2
A
A= $opt{"nodes"}
Aresult
11
00
495 290 ▶ Carp::croak("Game::Shogi::Engine: a search needs a node budget of 1 or more, not '$budget'")
Branch: 1 branch
BranchTrueFalseTotal
unless $budget =~ /\A[0-9]+\z/u and $budget >= 1 1 289 290
Condition66%2/3
$budget =~ /\A[0-9]+\z/u and $budget >= 1
!ll&&!rl&&r
01289
MC/DC50%1/2
$budget =~ /\A[0-9]+\z/u and $budget >= 1
$budget =~ /\A[0-9]+\z/u $budget >= 1
Truth table66%2/3
A and B
A= $budget =~ /\A[0-9]+\z/uB= $budget >= 1
ABresult
0X0
100
111
496 unless $budget =~ /\A[0-9]+\z/ && $budget >= 1;
497 289 my $history = $opt{history};
498 289 ▶ unless (ref $history eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $history eq "ARRAY") 167 122 289
499 167 my $trail = $self->_trail;
500 $history = $trail && $trail->[-1][1] eq _pack($self->_ptr)
501 167 ▶ ? [ map { $_->[0] } @$trail[ 0 .. $#$trail - 1 ] ]
Branch: 1 branch
BranchTrueFalseTotal
$trail && $trail->[-1][1] eq _pack($self->_ptr) ? : 123 44 167
Condition66%2/3
$trail and $trail->[-1][1] eq _pack($self->_ptr)
!ll&&!rl&&r
440123
MC/DC50%1/2
$trail and $trail->[-1][1] eq _pack($self->_ptr)
$trail $trail->[-1][1] eq _pack($self->_ptr)
Truth table66%2/3
A and B
A= $trailB= $trail->[-1][1] eq _pack($self->_ptr)
ABresult
0X0
100
111
502 : [];
503 }
504 my ($move, $nodes, $depth, $score, $stopped) = _search($self->_ptr, $budget, $opt{depth} // 0,
505 289 ▶ ($opt{seed} // 0) & 0xFFFFFFFF, $opt{flags} // 0, $history);
Condition83%5/6
$opt{"depth"} // 0
l!l
18271
$opt{"seed"} // 0
l!l
2881
$opt{"flags"} // 0
l!l
0289
MC/DC100%1/1
$opt{"depth"} // 0
$opt{"depth"}
MC/DC100%1/1
$opt{"seed"} // 0
$opt{"seed"}
MC/DC0%0/1
$opt{"flags"} // 0
$opt{"flags"}
Truth table100%2/2
A
A= $opt{"depth"}
Aresult
11
00
Truth table100%2/2
A
A= $opt{"seed"}
Aresult
11
00
Truth table50%1/2
A
A= $opt{"flags"}
Aresult
11
00
506 289 ▶ my $usi = $move ? _move_string($move) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$move ? : 288 1 289
507 289 ▶ return $usi unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 251 38 289
508 38 return ($usi, { nodes => $nodes, depth => $depth, score => $score, stopped => $stopped });
509 }
510  
511 30486 ▶sub repetition {
Subroutine repetition: called CC=8 SCAR=20.8
Pod documented
512 30486 my ($self) = @_;
513 30486 my $trail = $self->_trail;
514 30486 ▶ return undef unless $trail && @$trail >= REP_TIMES;
Branch: 1 branch
BranchTrueFalseTotal
unless $trail and @$trail >= 4 939 29547 30486
Condition100%3/3
$trail and @$trail >= 4
!ll&&!rl&&r
34059929547
MC/DC100%2/2
$trail and @$trail >= 4
$trail @$trail >= 4
Truth table100%3/3
A and B
A= $trailB= @$trail >= 4
ABresult
0X0
100
111
515 29547 ▶ return undef unless $trail->[-1][1] eq _pack($self->_ptr);
Branch: 1 branch
BranchTrueFalseTotal
unless $trail->[-1][1] eq _pack($self->_ptr) 1 29546 29547
516 2252248 my ($code, $loser) = _repetition([ map { $_->[0] } @$trail ], [ map { $_->[1] } @$trail ],
517 29546 [ map { $_->[2] } @$trail ]);
518 29546 ▶ return undef unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 29536 10 29546
519 10 require Game::Shogi::Result;
520 10 ▶ return Game::Shogi::Result->new(
Branch: 3 branches
BranchTrueFalseTotal
$code == 2 ? : 5 5 10
$code == 2 ? : 5 5 10
$loser == 1 ? : 4 1 5
521 how => $code == 2 ? 'perpetual_check' : 'sennichite',
522 winner => $code == 2 ? ($loser == WHITE ? 'b' : 'w') : undef,
523 ply => $self->move_number - 1,
524 );
525 }
526  
527 30476 ▶sub _capped {
Subroutine _capped: called CC=3 SCAR=11.0
528 30476 my ($self) = @_;
529 30476 ▶ return undef if $self->move_number - 1 < $self->cap || $self->in_check($self->side_to_move);
Branch: 1 branch
BranchTrueFalseTotal
if $self->move_number - 1 < $self->cap or $self->in_check($self->side_to_move) 30469 7 30476
Condition100%3/3
$self->move_number - 1 < $self->cap or $self->in_check($self->side_to_move)
l!l&&r!l&&!r
3046817
MC/DC100%2/2
$self->move_number - 1 < $self->cap or $self->in_check($self->side_to_move)
$self->move_number - 1 < $self->cap $self->in_check($self->side_to_move)
Truth table100%3/3
A or B
A= $self->move_number - 1 < $self->capB= $self->in_check($self->side_to_move)
ABresult
1X1
011
000
530 7 require Game::Shogi::Result;
531 7 return Game::Shogi::Result->new(how => 'cap', winner => undef, ply => $self->move_number - 1);
532 }
533  
534 9 ▶sub impasse_points {
Subroutine impasse_points: called CC=2 SCAR=6.9
Pod documented
535 9 my ($self, $side) = @_;
536 9 my $s = _side_of($side);
537 9 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 8 9
538 8 return _impasse_points($self->_ptr, $s);
539 }
540  
541 2 ▶sub impasse_pieces {
Subroutine impasse_pieces: called CC=2 SCAR=7.0
Pod documented
542 2 my ($self, $side) = @_;
543 2 my $s = _side_of($side);
544 2 ▶ return undef unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 2 2
545 2 return _impasse_pieces($self->_ptr, $s);
546 }
547  
548 17 ▶sub declare_fault { _declare_fault($_[0]->_ptr) }
Subroutine declare_fault: called CC=1 SCAR=0.0
Pod documented
549 30 ▶sub can_declare { _declare_fault($_[0]->_ptr) == DECLARE_OK ? 1 : 0 }
Subroutine can_declare: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_declare_fault($_[0]->_ptr) == 0 ? : 12 18 30
550  
551 15 ▶sub declare_reason {
Subroutine declare_reason: called CC=3 SCAR=13.1
Pod documented
552 15 my ($class, $code) = @_;
553 15 ▶ return defined $code && $code =~ /\A[0-9]+\z/ ? $DECLARE_REASON[$code] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code && $code =~ /\A[0-9]+\z/u ? : 15 0 15
Condition33%1/3
defined $code and $code =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $code and $code =~ /\A[0-9]+\z/u
defined $code $code =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
554 }
555  
556 8 ▶sub declare {
Subroutine declare: called CC=4 SCAR=13.9
Pod documented
557 8 my ($self) = @_;
558 8 ▶ return undef unless $self->can_declare && !_status($self->_ptr) && $self->move_number - 1 < $self->cap;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_declare and not _status($self->_ptr) and $self->move_number - 1 < $self->cap 2 6 8
Condition83%5/6
$self->can_declare and not _status($self->_ptr) and $self->move_number - 1 < $self->cap
!ll&&!rl&&r
116
$self->can_declare and not _status($self->_ptr)
!ll&&!rl&&r
107
MC/DC66%2/3
$self->can_declare and not _status($self->_ptr) and $self->move_number - 1 < $self->cap
$self->can_declare not _status($self->_ptr) $self->move_number - 1 < $self->cap
Truth table75%3/4
A and not(B) and C
A= $self->can_declareB= not _status($self->_ptr)C= $self->move_number - 1 < $self->cap
ABCresult
0XX0
10X0
1100
1111
559 6 require Game::Shogi::Result;
560 6 return Game::Shogi::Result->new(
561 how => 'impasse_declared',
562 winner => $self->side_to_move,
563 ply => $self->move_number - 1,
564 );
565 }
566  
567 11 ▶sub resign {
Subroutine resign: called CC=4 SCAR=13.9
Pod documented
568 11 my ($self, $side) = @_;
569 11 ▶ return undef unless defined $side && $side eq $self->side_to_move && !_status($self->_ptr);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side and $side eq $self->side_to_move and not _status($self->_ptr) 7 4 11
Condition100%6/6
defined $side and $side eq $self->side_to_move and not _status($self->_ptr)
!ll&&!rl&&r
614
defined $side and $side eq $self->side_to_move
!ll&&!rl&&r
155
MC/DC100%3/3
defined $side and $side eq $self->side_to_move and not _status($self->_ptr)
defined $side $side eq $self->side_to_move not _status($self->_ptr)
Truth table100%4/4
A and B and not(C)
A= defined $sideB= $side eq $self->side_to_moveC= not _status($self->_ptr)
ABCresult
0XX0
10X0
1100
1111
570 4 require Game::Shogi::Result;
571 4 return Game::Shogi::Result->new(
572 how => 'resign',
573 winner => _other($side),
574 ply => $self->move_number - 1,
575 );
576 }
577  
578 129526 ▶sub move_fault {
Subroutine move_fault: called CC=2 SCAR=6.9
Pod documented
579 129526 my ($self, $usi) = @_;
580 129526 my $m = _move_of($usi);
581 129526 ▶ return FAULT_BAD unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 8 129518 129526
582 129518 return _move_fault($self->_ptr, $m);
583 }
584  
585 38 ▶sub fault_reason {
Subroutine fault_reason: called CC=3 SCAR=13.1
Pod documented
586 38 my ($class, $code) = @_;
587 38 ▶ return defined $code && $code =~ /\A[0-9]+\z/ ? $FAULT_REASON[$code] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code && $code =~ /\A[0-9]+\z/u ? : 38 0 38
Condition33%1/3
defined $code and $code =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $code and $code =~ /\A[0-9]+\z/u
defined $code $code =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $codeB= $code =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
588 }
589  
590 11 ▶sub moves_made {
Subroutine moves_made: called CC=2 SCAR=6.9
Pod documented
591 11 my $history = $_[0]->_history;
592 11 ▶ return $history ? scalar @$history : 0;
Branch: 1 branch
BranchTrueFalseTotal
$history ? : 9 2 11
593 }
594  
595 2 ▶sub abi_version { _abi_version() }
Subroutine abi_version: called CC=1 SCAR=0.0
Pod documented
596 4 ▶sub live { _live() }
Subroutine live: called CC=1 SCAR=0.0
Pod documented
597 1 ▶sub tables_ok { _tables_ok() }
Subroutine tables_ok: called CC=1 SCAR=0.0
Pod documented
598 1 ▶sub squares { SQUARES }
Subroutine squares: called CC=1 SCAR=0.0
Pod documented
599  
600 321894 ▶sub square {
Subroutine square: called CC=3 SCAR=11.0
Pod documented
601 321894 my (undef, $name) = @_;
602 321894 ▶ return undef unless defined $name && $name =~ /\A([1-9])([a-i])\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /\A([1-9])([a-i])\z/u 8 321886 321894
Condition100%3/3
defined $name and $name =~ /\A([1-9])([a-i])\z/u
!ll&&!rl&&r
17321886
MC/DC100%2/2
defined $name and $name =~ /\A([1-9])([a-i])\z/u
defined $name $name =~ /\A([1-9])([a-i])\z/u
Truth table100%3/3
A and B
A= defined $nameB= $name =~ /\A([1-9])([a-i])\z/u
ABresult
0X0
100
111
603 321886 return _sq_of($1, ord($2) - ord('a') + 1);
604 }
605  
606 1557446 ▶sub square_name {
Subroutine square_name: called CC=2 SCAR=6.9
Pod documented
607 1557446 my (undef, $sq) = @_;
608 1557446 ▶ return undef unless _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_sq($sq) 4 1557442 1557446
609 1557442 return _file_of($sq) . chr(ord('a') + _rank_of($sq) - 1);
610 }
611  
612 85 ▶sub sq_of {
Subroutine sq_of: called CC=6 SCAR=21.4
Pod documented
613 85 my (undef, $file, $rank) = @_;
614 85 ▶ return undef unless defined $file && defined $rank
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and defined $rank and $file =~ /\A[0-9]+\z/u and $rank =~ /\A[0-9]+\z/u 0 85 85
Condition33%3/9
defined $file and defined $rank and $file =~ /\A[0-9]+\z/u and $rank =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0085
defined $file and defined $rank and $file =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0085
defined $file and defined $rank
!ll&&!rl&&r
0085
MC/DC0%0/4
defined $file and defined $rank and $file =~ /\A[0-9]+\z/u and $rank =~ /\A[0-9]+\z/u
defined $file defined $rank $file =~ /\A[0-9]+\z/u $rank =~ /\A[0-9]+\z/u
Truth table20%1/5
A and B and C and D
A= defined $fileB= defined $rankC= $file =~ /\A[0-9]+\z/uD= $rank =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
615 && $file =~ /\A[0-9]+\z/ && $rank =~ /\A[0-9]+\z/;
616 85 my $sq = _sq_of($file, $rank);
617 85 ▶ return $sq < 0 ? undef : $sq;
Branch: 1 branch
BranchTrueFalseTotal
$sq < 0 ? : 4 81 85
618 }
619  
620 162 ▶sub file_of { _is_sq($_[1]) ? _file_of($_[1]) : undef }
Subroutine file_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_sq($_[1]) ? : 162 0 162
621 243 ▶sub rank_of { _is_sq($_[1]) ? _rank_of($_[1]) : undef }
Subroutine rank_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_sq($_[1]) ? : 243 0 243
622  
623 1229 ▶sub step {
Subroutine step: called CC=7 SCAR=20.0
Pod documented
624 1229 my (undef, $sq, $dfile, $drank) = @_;
625 1229 ▶ return undef unless _is_sq($sq) && defined $dfile && defined $drank
Branch: 1 branch
BranchTrueFalseTotal
unless _is_sq($sq) and defined $dfile and defined $drank and $dfile =~ /\A-?[0-9]+\z/u and $drank =~ /\A-?[0-9]+\z/u 4 1225 1229
Condition66%8/12
_is_sq($sq) and defined $dfile and defined $drank and $dfile =~ /\A-?[0-9]+\z/u and $drank =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
401225
_is_sq($sq) and defined $dfile and defined $drank and $dfile =~ /\A-?[0-9]+\z/u
!ll&&!rl&&r
401225
_is_sq($sq) and defined $dfile and defined $drank
!ll&&!rl&&r
401225
_is_sq($sq) and defined $dfile
!ll&&!rl&&r
401225
MC/DC20%1/5
_is_sq($sq) and defined $dfile and defined $drank and $dfile =~ /\A-?[0-9]+\z/u and $drank =~ /\A-?[0-9]+\z/u
_is_sq($sq) defined $dfile defined $drank $dfile =~ /\A-?[0-9]+\z/u $drank =~ /\A-?[0-9]+\z/u
Truth table33%2/6
A and B and C and D and E
A= _is_sq($sq)B= defined $dfileC= defined $drankD= $dfile =~ /\A-?[0-9]+\z/uE= $drank =~ /\A-?[0-9]+\z/u
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
626 && $dfile =~ /\A-?[0-9]+\z/ && $drank =~ /\A-?[0-9]+\z/;
627 1225 my $to = _step($sq, $dfile, $drank);
628 1225 ▶ return $to < 0 ? undef : $to;
Branch: 1 branch
BranchTrueFalseTotal
$to < 0 ? : 247 978 1225
629 }
630  
631 325 ▶sub in_zone {
Subroutine in_zone: called CC=4 SCAR=13.9
Pod documented
632 325 my (undef, $side, $sq) = @_;
633 325 my $s = _side_of($side);
634 325 ▶ return 0 unless defined $s && _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and _is_sq($sq) 1 324 325
Condition66%2/3
defined $s and _is_sq($sq)
!ll&&!rl&&r
10324
MC/DC50%1/2
defined $s and _is_sq($sq)
defined $s _is_sq($sq)
Truth table66%2/3
A and B
A= defined $sB= _is_sq($sq)
ABresult
0X0
100
111
635 324 ▶ return _in_zone($s, $sq) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_in_zone($s, $sq) ? : 108 216 324
636 }
637  
638 165 ▶sub last_distance {
Subroutine last_distance: called CC=3 SCAR=11.0
Pod documented
639 165 my (undef, $side, $sq) = @_;
640 165 my $s = _side_of($side);
641 165 ▶ return undef unless defined $s && _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and _is_sq($sq) 1 164 165
Condition66%2/3
defined $s and _is_sq($sq)
!ll&&!rl&&r
01164
MC/DC50%1/2
defined $s and _is_sq($sq)
defined $s _is_sq($sq)
Truth table66%2/3
A and B
A= defined $sB= _is_sq($sq)
ABresult
0X0
100
111
642 164 return _last_distance($s, $sq);
643 }
644  
645 16 ▶sub promoted {
Subroutine promoted: called CC=4 SCAR=13.9
Pod documented
646 16 my (undef, $token) = @_;
647 16 my $piece = _piece_of($token);
648 16 ▶ return undef unless defined $piece && ($piece & 15) <= KING && _promoted($piece & 15);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $piece and ($piece & 15) <= 8 and _promoted($piece & 15) 8 8 16
Condition100%6/6
defined $piece and ($piece & 15) <= 8 and _promoted($piece & 15)
!ll&&!rl&&r
448
defined $piece and ($piece & 15) <= 8
!ll&&!rl&&r
2212
MC/DC100%3/3
defined $piece and ($piece & 15) <= 8 and _promoted($piece & 15)
defined $piece ($piece & 15) <= 8 _promoted($piece & 15)
Truth table100%4/4
A and B and C
A= defined $pieceB= ($piece & 15) <= 8C= _promoted($piece & 15)
ABCresult
0XX0
10X0
1100
1111
649 8 return '+' . $token;
650 }
651  
652 9 ▶sub unpromoted {
Subroutine unpromoted: called CC=2 SCAR=6.9
Pod documented
653 9 my (undef, $token) = @_;
654 9 ▶ return undef unless defined _piece_of($token);
Branch: 1 branch
BranchTrueFalseTotal
unless defined _piece_of($token) 4 5 9
655 5 (my $base = $token) =~ s/\A\+//;
656 5 return $base;
657 }
658  
659 31 ▶sub set_count {
Subroutine set_count: called CC=3 SCAR=11.0
Pod documented
660 31 my (undef, $letter) = @_;
661 31 ▶ return 0 unless defined $letter && $letter =~ /\A[PLNSGBRKplnsgbrk]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $letter and $letter =~ /\A[PLNSGBRKplnsgbrk]\z/u 1 30 31
Condition66%2/3
defined $letter and $letter =~ /\A[PLNSGBRKplnsgbrk]\z/u
!ll&&!rl&&r
0130
MC/DC50%1/2
defined $letter and $letter =~ /\A[PLNSGBRKplnsgbrk]\z/u
defined $letter $letter =~ /\A[PLNSGBRKplnsgbrk]\z/u
Truth table66%2/3
A and B
A= defined $letterB= $letter =~ /\A[PLNSGBRKplnsgbrk]\z/u
ABresult
0X0
100
111
662 30 return _set_count($KIND{ uc $letter });
663 }
664  
665 2419 ▶sub zobrist {
Subroutine zobrist: called CC=4 SCAR=14.5
Pod documented
666 2419 my (undef, $side, $token, $sq) = @_;
667 2419 my ($s, $piece) = (_side_of($side), _piece_of($token));
668 2419 ▶ return _zobrist_hex(-1, 0, -1) unless defined $s && defined $piece && _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and defined $piece and _is_sq($sq) 1 2418 2419
Condition50%3/6
defined $s and defined $piece and _is_sq($sq)
!ll&&!rl&&r
012418
defined $s and defined $piece
!ll&&!rl&&r
002419
MC/DC33%1/3
defined $s and defined $piece and _is_sq($sq)
defined $s defined $piece _is_sq($sq)
Truth table50%2/4
A and B and C
A= defined $sB= defined $pieceC= _is_sq($sq)
ABCresult
0XX0
10X0
1100
1111
669 2418 return _zobrist_hex($s, $piece & 15, $sq);
670 }
671  
672 205 ▶sub zobrist_hand {
Subroutine zobrist_hand: called CC=5 SCAR=20.2
Pod documented
673 205 my (undef, $side, $letter, $count) = @_;
674 205 my ($s, $k) = (_side_of($side), _hand_kind($letter));
675 205 ▶ return _zobrist_hand_hex(-1, 0, 0) unless defined $s && defined $k
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u 0 205 205
Condition33%3/9
defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u
!ll&&!rl&&r
00205
defined $s and defined $k and defined $count
!ll&&!rl&&r
00205
defined $s and defined $k
!ll&&!rl&&r
00205
MC/DC0%0/4
defined $s and defined $k and defined $count and $count =~ /\A[0-9]+\z/u
defined $s defined $k defined $count $count =~ /\A[0-9]+\z/u
Truth table20%1/5
A and B and C and D
A= defined $sB= defined $kC= defined $countD= $count =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
676 && defined $count && $count =~ /\A[0-9]+\z/;
677 205 return _zobrist_hand_hex($s, $k, $count);
678 }
679  
680 2 ▶sub zobrist_move { _zobrist_move_hex() }
Subroutine zobrist_move: called CC=1 SCAR=0.0
Pod documented
681  
682 24 1;
683  
684 __END__
« blib/lib/Game/Shogi/Error.pm ↑ Summary blib/lib/Game/Shogi/Move.pm »