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 47 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 47 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 47 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 47 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 47 our $VERSION = '0.02';
12  
13 47 require XSLoader;
14 47 XSLoader::load('Game::Shogi', $VERSION);
15  
16 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 47 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 47 };
29  
30 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
31 47 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 47 };
46  
47 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
48 47 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 47 };
62  
63 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
64 47 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 47 };
73  
74 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
75 47 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 47 };
88  
89 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
90 47 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 47 };
101  
102 47 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
103 47 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 47 };
110  
111 47 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 47 our %EXPORT_TAGS = (all => \@EXPORT_OK);
126  
127 47 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 47 my @LETTER = (undef, qw(P L N S G B R K));
144  
145 47 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 47 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 47 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 47 my %KIND = (P => PAWN, L => LANCE, N => KNIGHT, S => SILVER, G => GOLD, B => BISHOP, R => ROOK, K => KING);
179 47 my %SIDE = (b => BLACK, w => WHITE);
180  
181 47 has _ptr => (is => 'rw', private => 1);
182  
183 47 has _hold => (is => 'rw', private => 1);
184  
185 47 has _want_empty => (is => 'ro', init_arg => 'empty', private => 1, default => 0);
186  
187 47 has _want_sfen => (is => 'ro', init_arg => 'sfen', private => 1);
188  
189 47 has _history => (is => 'rw', private => 1);
190  
191 47 has _trail => (is => 'rw', private => 1);
192  
193 47 has cap => (is => 'ro', default => 500);
194  
195 47 my %ADOPTING;
196  
197 5923 ▶sub BUILD {
Subroutine BUILD: called CC=11 SCAR=24.0
Pod undocumented
198 5923 my ($self) = @_;
199 5923 my $cap = $self->cap;
200 5923 ▶ 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 5919 5923
Condition75%6/8
defined $cap and $cap =~ /\A[0-9]+\z/u and $cap >= 1
!ll&&!rl&&r
315919
defined $cap and $cap =~ /\A[0-9]+\z/u
!ll&&!rl&&r
035920
$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 5919 my $given = $self->_ptr;
203 5919 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 1182 4737 5919
204 1182 ▶ if (delete $ADOPTING{$given}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $ADOPTING{$given}) 1181 1 1182
205 1181 $self->_hold(_hold_board($given));
206 1181 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 4737 my $sfen = $self->_want_sfen;
213 4737 ▶ if (defined $sfen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sfen) 2123 2614 4737
214 2123 my ($ptr, $err) = _of_sfen($sfen);
215 2123 ▶ Carp::croak("Game::Shogi::Engine: the SFEN was refused: $REASON[$err]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 3 2120 2123
216 unless defined $ptr;
217 2120 $self->_ptr($ptr);
218 2120 $self->_hold(_hold_board($ptr));
219 2120 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 1181 ▶my $adopt = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
229 1181 my ($class, $ptr, $cap) = @_;
230 1181 $ADOPTING{$ptr} = 1;
231 1181 ▶ return $class->new(_ptr => $ptr, (defined $cap ? (cap => $cap) : ()));
Branch: 1 branch
BranchTrueFalseTotal
defined $cap ? : 1169 12 1181
232 47 };
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 1169 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
247 1169 my ($self) = @_;
248 1169 return $adopt->(ref($self), _copy_board($self->_ptr), $self->cap);
249 }
250  
251 79222 ▶sub _token_of {
Subroutine _token_of: called CC=6 SCAR=17.9
252 79222 my ($piece) = @_;
253 79222 ▶ return undef if $piece == EMPTY || $piece >= OFF;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 or $piece >= 64 48141 31081 79222
Condition66%2/3
$piece == 0 or $piece >= 64
l!l&&r!l&&!r
48141031081
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 31081 my ($side, $kind) = ($piece >> 4 & 1, $piece & 15);
255 31081 my $promoted = $kind > KING;
256 31081 ▶ my $letter = $LETTER[ $promoted ? _unpromoted($kind) : $kind ];
Branch: 1 branch
BranchTrueFalseTotal
$promoted ? : 599 30482 31081
257 31081 ▶ $letter = lc $letter if $side == WHITE;
Branch: 1 branch
BranchTrueFalseTotal
if $side == 1 15222 15859 31081
258 31081 ▶ return ($promoted ? '+' : '') . $letter;
Branch: 1 branch
BranchTrueFalseTotal
$promoted ? : 599 30482 31081
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 266809 ▶sub _side_of {
Subroutine _side_of: called CC=2 SCAR=7.2
274 266809 my ($side) = @_;
275 266809 ▶ return defined $side ? $SIDE{$side} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 266809 0 266809
276 }
277  
278 14735 ▶sub _hand_kind {
Subroutine _hand_kind: called CC=3 SCAR=11.0
279 14735 my ($letter) = @_;
280 14735 ▶ return undef unless defined $letter && $letter =~ /\A[PLNSGBRplnsgbr]\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $letter and $letter =~ /\A[PLNSGBRplnsgbr]\z/u 6 14729 14735
Condition100%3/3
defined $letter and $letter =~ /\A[PLNSGBRplnsgbr]\z/u
!ll&&!rl&&r
1514729
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 14729 return $KIND{ uc $letter };
282 }
283  
284 79222 ▶sub at {
Subroutine at: called CC=2 SCAR=7.1
Pod documented
285 79222 my ($self, $sq) = @_;
286 79222 ▶ return undef unless _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_sq($sq) 0 79222 79222
287 79222 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 14470 ▶sub hand {
Subroutine hand: called CC=3 SCAR=12.0
Pod documented
304 14470 my ($self, $side, $letter) = @_;
305 14470 my ($s, $k) = (_side_of($side), _hand_kind($letter));
306 14470 ▶ return 0 unless defined $s && defined $k;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and defined $k 0 14470 14470
Condition33%1/3
defined $s and defined $k
!ll&&!rl&&r
0014470
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 14470 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 1024 ▶sub hands {
Subroutine hands: called CC=4 SCAR=13.9
Pod documented
319 1024 my ($self) = @_;
320 1024 my %hands = (b => {}, w => {});
321 1024 for my $side (qw(b w)) {
322 2048 for my $letter (qw(R B G S N L P)) {
323 14336 my $n = $self->hand($side, $letter);
324 14336 ▶ $hands{$side}{$letter} = $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 863 13473 14336
325 }
326 }
327 1024 return \%hands;
328 }
329  
330 34514 ▶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 ? : 17643 16871 34514
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 30616 ▶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 2704 ▶sub sfen { _to_sfen($_[0]->_ptr) }
Subroutine sfen: called CC=1 SCAR=0.0
Pod documented
356 38198 ▶sub key { _key_hex($_[0]->_ptr) }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
357  
358 1858874 ▶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 1858865 9 1858874
Condition100%3/3
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
271858865
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 782698 ▶sub _move_string {
Subroutine _move_string: called CC=3 SCAR=11.0
361 782698 my ($m) = @_;
362 782698 my $to = __PACKAGE__->square_name(($m >> 7) & 127);
363 782698 ▶ return $LETTER[ ($m >> 16) & 15 ] . '*' . $to if ($m >> 15) & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $m >> 15 & 1 148853 633845 782698
364 633845 my $from = __PACKAGE__->square_name($m & 127);
365 633845 ▶ return $from . $to . (($m >> 14) & 1 ? '+' : '');
Branch: 1 branch
BranchTrueFalseTotal
$m >> 14 & 1 ? : 31922 601923 633845
366 }
367  
368 162800 ▶sub _move_of {
Subroutine _move_of: called CC=5 SCAR=16.1
369 162800 my ($usi) = @_;
370 162800 ▶ return undef unless defined $usi;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $usi 3 162797 162800
371 162797 ▶ if ($usi =~ /\A([PLNSGBR])\*([1-9][a-i])\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($usi =~ /\A([PLNSGBR])\*([1-9][a-i])\z/u) 23091 139706 162797
372 23091 return (1 << 15) | ($KIND{$1} << 16) | (__PACKAGE__->square($2) << 7);
373 }
374 139706 ▶ 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 139689 139706
375 139689 my ($from, $to) = (__PACKAGE__->square($1), __PACKAGE__->square($2));
376 139689 ▶ return $from | ($to << 7) | ($3 ? 1 << 14 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$3 ? : 50286 89403 139689
377 }
378  
379 213585 ▶sub attacked {
Subroutine attacked: called CC=4 SCAR=13.9
Pod documented
380 213585 my ($self, $sq, $by) = @_;
381 213585 my $s = _side_of($by);
382 213585 ▶ return 0 unless defined $s && _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and _is_sq($sq) 2 213583 213585
Condition100%3/3
defined $s and _is_sq($sq)
!ll&&!rl&&r
11213583
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 213583 ▶ return _attacked($self->_ptr, $sq, $s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_attacked($self->_ptr, $sq, $s) ? : 27510 186073 213583
384 }
385  
386 33270 ▶sub in_check {
Subroutine in_check: called CC=3 SCAR=11.0
Pod documented
387 33270 my ($self, $side) = @_;
388 33270 my $s = _side_of($side);
389 33270 ▶ return 0 unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 33270 33270
390 33270 ▶ return _in_check($self->_ptr, $s) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_in_check($self->_ptr, $s) ? : 860 32410 33270
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 33221 ▶sub do_move {
Subroutine do_move: called CC=4 SCAR=13.9
Pod documented
397 33221 my ($self, $usi) = @_;
398 33221 my $m = _move_of($usi);
399 33221 ▶ return 0 unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 12 33209 33221
400 33209 my $trail = $self->_trail_now;
401 33209 my $record = _do_move($self->_ptr, $m);
402 33209 ▶ return 0 unless defined $record;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $record 11 33198 33209
403 33198 my $history = $self->_history;
404 33198 ▶ $self->_history($history = []) unless $history;
Branch: 1 branch
BranchTrueFalseTotal
unless $history 419 32779 33198
405 33198 push @$history, $record;
406 33198 push @$trail, [ $self->key, _pack($self->_ptr), $self->in_check($self->side_to_move) ];
407 33198 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 17420 ▶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 47 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 47 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 30588 ▶sub outcome {
Subroutine outcome: called CC=3 SCAR=11.0
Pod documented
459 30588 my ($self) = @_;
460 30588 my $s = _status($self->_ptr);
461 30588 ▶ return $self->repetition // $self->_capped unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 30558 30 30588
Condition100%3/3
$self->repetition // $self->_capped
l!l&&r!l&&!r
10730541
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 33209 ▶sub _trail_now {
Subroutine _trail_now: called CC=2 SCAR=6.9
471 33209 my ($self) = @_;
472 33209 my $trail = $self->_trail;
473 33209 ▶ return $trail if $trail;
Branch: 1 branch
BranchTrueFalseTotal
if $trail 32788 421 33209
474 421 $self->_trail($trail = [ [ $self->key, _pack($self->_ptr), 0 ] ]);
475 421 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 65 ▶sub history {
Subroutine history: called CC=3 SCAR=11.0
Pod documented
486 65 my ($self) = @_;
487 65 my $trail = $self->_trail;
488 65 ▶ 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 61 65
Condition66%2/3
$trail and $trail->[-1][1] eq _pack($self->_ptr)
!ll&&!rl&&r
4061
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 61 return map { $_->[0] } @$trail[ 0 .. $#$trail - 1 ];
490 }
491  
492 300 ▶sub search {
Subroutine search: called CC=12 SCAR=25.0
Pod documented
493 300 my ($self, %opt) = @_;
494 300 ▶ my $budget = $opt{nodes} // 60_000;
Condition50%1/2
$opt{"nodes"} // 60000
l!l
3000
MC/DC0%0/1
$opt{"nodes"} // 60000
$opt{"nodes"}
Truth table50%1/2
A
A= $opt{"nodes"}
Aresult
11
00
495 300 ▶ 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 299 300
Condition66%2/3
$budget =~ /\A[0-9]+\z/u and $budget >= 1
!ll&&!rl&&r
01299
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 299 my $history = $opt{history};
498 299 ▶ unless (ref $history eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $history eq "ARRAY") 167 132 299
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 299 ▶ ($opt{seed} // 0) & 0xFFFFFFFF, $opt{flags} // 0, $history);
Condition83%5/6
$opt{"depth"} // 0
l!l
18281
$opt{"seed"} // 0
l!l
2981
$opt{"flags"} // 0
l!l
0299
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 299 ▶ my $usi = $move ? _move_string($move) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$move ? : 298 1 299
507 299 ▶ return $usi unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 261 38 299
508 38 return ($usi, { nodes => $nodes, depth => $depth, score => $score, stopped => $stopped });
509 }
510  
511 30558 ▶sub repetition {
Subroutine repetition: called CC=8 SCAR=20.8
Pod documented
512 30558 my ($self) = @_;
513 30558 my $trail = $self->_trail;
514 30558 ▶ return undef unless $trail && @$trail >= REP_TIMES;
Branch: 1 branch
BranchTrueFalseTotal
unless $trail and @$trail >= 4 991 29567 30558
Condition100%3/3
$trail and @$trail >= 4
!ll&&!rl&&r
36063129567
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 29567 ▶ return undef unless $trail->[-1][1] eq _pack($self->_ptr);
Branch: 1 branch
BranchTrueFalseTotal
unless $trail->[-1][1] eq _pack($self->_ptr) 1 29566 29567
516 2252338 my ($code, $loser) = _repetition([ map { $_->[0] } @$trail ], [ map { $_->[1] } @$trail ],
517 29566 [ map { $_->[2] } @$trail ]);
518 29566 ▶ return undef unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 29556 10 29566
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 30548 ▶sub _capped {
Subroutine _capped: called CC=3 SCAR=11.0
528 30548 my ($self) = @_;
529 30548 ▶ 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) 30541 7 30548
Condition100%3/3
$self->move_number - 1 < $self->cap or $self->in_check($self->side_to_move)
l!l&&r!l&&!r
3054017
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 40 ▶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 28 40
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 129578 ▶sub move_fault {
Subroutine move_fault: called CC=2 SCAR=6.9
Pod documented
579 129578 my ($self, $usi) = @_;
580 129578 my $m = _move_of($usi);
581 129578 ▶ return FAULT_BAD unless defined $m;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 8 129570 129578
582 129570 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 325538 ▶sub square {
Subroutine square: called CC=3 SCAR=11.0
Pod documented
601 325538 my (undef, $name) = @_;
602 325538 ▶ 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 325530 325538
Condition100%3/3
defined $name and $name =~ /\A([1-9])([a-i])\z/u
!ll&&!rl&&r
17325530
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 325530 return _sq_of($1, ord($2) - ord('a') + 1);
604 }
605  
606 1558972 ▶sub square_name {
Subroutine square_name: called CC=2 SCAR=6.9
Pod documented
607 1558972 my (undef, $sq) = @_;
608 1558972 ▶ return undef unless _is_sq($sq);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_sq($sq) 4 1558968 1558972
609 1558968 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 47 1;
683  
684 __END__
« blib/lib/Game/Shogi/Error.pm ↑ Summary blib/lib/Game/Shogi/Move.pm »