blib/lib/Game/Xiangqi/Engine.pm

filter: Statementstmt 93.9% 155 / 165 Branchbran 78.9% 30 / 38 Conditioncond 80.0% 8 / 10 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 95.8% 70 / 73 Podpod 4.6% 3 / 65 totaltotal 75.5% 269 / 356 CC 29 SCAR 9.7
CC
29
Cov
91%
perft_divide
20.0
search_to_depth
5.2
BUILD
5.0
CRAP 2.6
« blib/lib/Game/Xiangqi/Bot.pm ↑ Summary blib/arch/Game/Xiangqi/Install/Files.pm »
1 package Game::Xiangqi::Engine;
2  
3 24 ▶use 5.010;
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.03';
12  
13 24 require XSLoader;
14 24 XSLoader::load('Game::Xiangqi', $VERSION);
15  
16 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 EMPTY => 0,
18 GENERAL => 1,
19 ADVISOR => 2,
20 ELEPHANT => 3,
21 CHARIOT => 4,
22 HORSE => 5,
23 CANNON => 6,
24 SOLDIER => 7,
25 RED => 8,
26 BLACK => 16,
27 BORDER => 32,
28  
29 FILES => 9,
30 RANKS => 10,
31 POINTS => 90,
32 24 };
33  
34 24 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
35 24 FEN_OK => 0,
36 FEN_NULL => 1,
37 FEN_ROWS => 2,
38 FEN_WIDTH => 3,
39 FEN_LETTER => 4,
40 FEN_SIDE => 5,
41 FEN_LONG => 6,
42  
43 ONGOING => 0,
44 BY_CHECKMATE => 1,
45 BY_STALEMATE => 2,
46  
47 J_ONGOING => 0,
48 J_PERPETUAL_CHECK => 1,
49 J_PERPETUAL_CHASE => 2,
50 J_MUTUAL => 3,
51 J_NO_VIOLATION => 4,
52 J_EFFECTIVE => 5,
53 J_PROGRESS => 6,
54 J_MOVES => 7,
55  
56 BEH_NONE => 0,
57 BEH_CHECK => 1,
58 BEH_CHASE => 2,
59 BEH_TTC => 3,
60 BEH_BLOCK => 4,
61 BEH_EXCHANGE => 5,
62 BEH_SACRIFICE => 6,
63 BEH_IDLE => 7,
64 24 };
65  
66 24 our @EXPORT_OK = qw(
67 EMPTY GENERAL ADVISOR ELEPHANT CHARIOT HORSE CANNON SOLDIER
68 RED BLACK BORDER FILES RANKS POINTS
69 FEN_OK FEN_NULL FEN_ROWS FEN_WIDTH FEN_LETTER FEN_SIDE FEN_LONG
70 ONGOING BY_CHECKMATE BY_STALEMATE
71 J_ONGOING J_PERPETUAL_CHECK J_PERPETUAL_CHASE J_MUTUAL J_NO_VIOLATION
72 J_EFFECTIVE J_PROGRESS J_MOVES
73 BEH_NONE BEH_CHECK BEH_CHASE BEH_TTC BEH_BLOCK BEH_EXCHANGE BEH_SACRIFICE BEH_IDLE
74 kind_of colour_of other
75 );
76 24 our %EXPORT_TAGS = (all => \@EXPORT_OK);
77  
78 1427 ▶sub kind_of { $_[0] & 7 }
Subroutine kind_of: called CC=1 SCAR=0.0
Pod undocumented
79 2070 ▶sub colour_of { $_[0] & (RED | BLACK) }
Subroutine colour_of: called CC=1 SCAR=0.0
Pod undocumented
80 260 ▶sub other { $_[0] == RED ? BLACK : RED }
Subroutine other: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == 8 ? : 140 120 260
81  
82 24 has _ptr => (is => 'rw', private => 1);
83  
84 24 has _empty => (is => 'ro', init_arg => 'empty', private => 1, default => 0);
85  
86 24 has _fen => (is => 'ro', init_arg => 'fen', private => 1);
87  
88 628 ▶sub BUILD {
Subroutine BUILD: called CC=5 SCAR=16.1
Pod undocumented
89 628 my ($self) = @_;
90 628 ▶ return if $self->_ptr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ptr 244 384 628
91  
92 384 my $fen = $self->_fen;
93 384 ▶ if (defined $fen) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fen) 48 336 384
94 48 my ($ptr, $err) = _of_fen($fen);
95 48 ▶ Carp::croak("Game::Xiangqi::Engine: the FEN was refused, code $err")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 0 48 48
96 unless defined $ptr;
97 48 $self->_ptr($ptr);
98 48 return;
99 }
100  
101 336 ▶ $self->_ptr($self->_empty ? _new_empty() : _new_board());
Branch: 1 branch
BranchTrueFalseTotal
$self->_empty ? : 152 184 336
102 336 return;
103 }
104  
105 21 ▶sub of_fen {
Subroutine of_fen: called CC=3 SCAR=11.0
Pod undocumented
106 21 my ($class, $fen) = @_;
107 21 ▶ my ($ptr, $err) = _of_fen(defined $fen ? $fen : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $fen ? : 21 0 21
108 21 ▶ return (undef, $err) unless defined $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 13 8 21
109 8 return ($class->new(_ptr => $ptr), FEN_OK);
110 }
111  
112 236 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
113 236 my ($self) = @_;
114 236 return ref($self)->new(_ptr => _copy_board($self->_ptr));
115 }
116  
117 628 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod undocumented
118 628 my ($self) = @_;
119 628 my $ptr = $self->_ptr;
120 628 ▶ return unless $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr 0 628 628
121 628 _drop_board($ptr);
122 628 $self->_ptr(0);
123 628 return;
124 }
125  
126 25196 ▶sub at { _at($_[0]->_ptr, $_[1]) }
Subroutine at: called CC=1 SCAR=0.0
Pod undocumented
127 563 ▶sub put { _put($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
Subroutine put: called CC=1 SCAR=0.0
Pod undocumented
128 2 ▶sub lift { _lift($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine lift: called CC=1 SCAR=0.0
Pod undocumented
129 824 ▶sub side { _side($_[0]->_ptr) }
Subroutine side: called CC=1 SCAR=0.0
Pod undocumented
130 66 ▶sub set_side { _set_side($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine set_side: called CC=1 SCAR=0.0
Pod undocumented
131 6 ▶sub count { _count($_[0]->_ptr, $_[1]) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
132 446 ▶sub find { _find($_[0]->_ptr, $_[1]) }
Subroutine find: called CC=1 SCAR=0.0
Pod undocumented
133 39 ▶sub to_fen { _to_fen($_[0]->_ptr) }
Subroutine to_fen: called CC=1 SCAR=0.0
Pod undocumented
134 266 ▶sub key_hex { _key_hex($_[0]->_ptr) }
Subroutine key_hex: called CC=1 SCAR=0.0
Pod undocumented
135  
136 460 ▶sub move { _move_make($_[1], $_[2]) }
Subroutine move: called CC=1 SCAR=0.0
Pod undocumented
137 7436 ▶sub move_from { _move_from($_[1]) }
Subroutine move_from: called CC=1 SCAR=0.0
Pod undocumented
138 4319 ▶sub move_to { _move_to($_[1]) }
Subroutine move_to: called CC=1 SCAR=0.0
Pod undocumented
139  
140 27 ▶sub play {
Subroutine play: called CC=3 SCAR=11.0
Pod undocumented
141 27 my ($self, $iccs) = @_;
142 27 require Game::Xiangqi::Notation;
143 27 my $mv = Game::Xiangqi::Notation->move_of_iccs($iccs);
144 27 ▶ return 'bad_move' unless defined $mv;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mv 2 25 27
145 25 ▶ return 'not_legal' unless grep { $_ == $mv } $self->legal;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $mv;} $self->legal 5 20 25
146 20 my (undef, $undo) = $self->do_move($mv);
147 20 return 0;
148 }
149  
150 85351 ▶sub do_move { _do_move($_[0]->_ptr, $_[1]) }
Subroutine do_move: called CC=1 SCAR=0.0
Pod undocumented
151 84217 ▶sub undo_move { _undo_move($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine undo_move: called CC=1 SCAR=0.0
Pod undocumented
152  
153 17415 ▶sub point_of { my $c = shift; _point_of($_[0], $_[1]) }
Subroutine point_of: called CC=1 SCAR=0.0
Pod undocumented
154 4195 ▶sub file_of { my $c = shift; _file_of($_[0]) }
Subroutine file_of: called CC=1 SCAR=0.0
Pod undocumented
155 8462 ▶sub rank_of { my $c = shift; _rank_of($_[0]) }
Subroutine rank_of: called CC=1 SCAR=0.0
Pod undocumented
156 3244 ▶sub on_board { my $c = shift; _on_board($_[0]) }
Subroutine on_board: called CC=1 SCAR=0.0
Pod undocumented
157 282 ▶sub in_palace { my $c = shift; _in_palace($_[0], $_[1]) }
Subroutine in_palace: called CC=1 SCAR=0.0
Pod undocumented
158 326 ▶sub crossed_river { my $c = shift; _crossed_river($_[0], $_[1]) }
Subroutine crossed_river: called CC=1 SCAR=0.0
Pod undocumented
159 3 ▶sub stride { _stride() }
Subroutine stride: called CC=1 SCAR=0.0
Pod undocumented
160  
161 6 ▶sub zobrist_hex { my $c = shift; _zobrist_hex($_[0], $_[1]) }
Subroutine zobrist_hex: called CC=1 SCAR=0.0
Pod undocumented
162 1 ▶sub abi_version { _abi_version() }
Subroutine abi_version: called CC=1 SCAR=0.0
Pod undocumented
163  
164 58 ▶sub moves {
Subroutine moves: called CC=2 SCAR=6.9
Pod undocumented
165 58 my @m = _gen_moves($_[0]->_ptr);
166 58 ▶ return wantarray ? @m : scalar @m;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 56 2 58
167 }
168  
169 6531 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod undocumented
170 6531 my @m = _gen_legal($_[0]->_ptr);
171 6531 ▶ return wantarray ? @m : scalar @m;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6519 12 6531
172 }
173 318 ▶sub attacked { _attacked($_[0]->_ptr, $_[1], $_[2]) }
Subroutine attacked: called CC=1 SCAR=0.0
Pod undocumented
174 255 ▶sub in_check { _in_check($_[0]->_ptr, $_[1]) }
Subroutine in_check: called CC=1 SCAR=0.0
Pod undocumented
175 10 ▶sub generals_face { _generals_face($_[0]->_ptr) }
Subroutine generals_face: called CC=1 SCAR=0.0
Pod undocumented
176  
177 5 ▶sub horse_leg { _horse_leg($_[0]->_ptr, $_[1], $_[2]) }
Subroutine horse_leg: called CC=1 SCAR=0.0
Pod undocumented
178 2 ▶sub elephant_eye { _elephant_eye($_[0]->_ptr, $_[1], $_[2]) }
Subroutine elephant_eye: called CC=1 SCAR=0.0
Pod undocumented
179 7 ▶sub cannon_screens { _cannon_screens($_[0]->_ptr, $_[1], $_[2]) }
Subroutine cannon_screens: called CC=1 SCAR=0.0
Pod undocumented
180 3 ▶sub soldier_may { _soldier_may($_[0]->_ptr, $_[1], $_[2]) }
Subroutine soldier_may: called CC=1 SCAR=0.0
Pod undocumented
181  
182 54 ▶sub perft { _perft($_[0]->_ptr, $_[1]) }
Subroutine perft: called CC=1 SCAR=0.0
Pod documented
183  
184 81875 ▶sub outcome { _outcome($_[0]->_ptr) }
Subroutine outcome: called CC=1 SCAR=0.0
Pod undocumented
185  
186 0 ▶sub winner { (_outcome($_[0]->_ptr))[0] }
Subroutine winner: not called CC=1 SCAR=6.9
Pod undocumented
187 0 ▶sub reason { (_outcome($_[0]->_ptr))[1] }
Subroutine reason: not called CC=1 SCAR=6.9
Pod undocumented
188 90 ▶sub is_over { (_outcome($_[0]->_ptr))[0] != 0 }
Subroutine is_over: called CC=1 SCAR=0.0
Pod undocumented
189  
190 108 ▶sub mate_in { _mate_in($_[0]->_ptr, $_[1]) }
Subroutine mate_in: called CC=1 SCAR=0.0
Pod undocumented
191  
192 106 ▶sub is_check { _is_check($_[0]->_ptr, $_[1]) }
Subroutine is_check: called CC=1 SCAR=0.0
Pod undocumented
193 57 ▶sub is_mate { _is_mate($_[0]->_ptr, $_[1]) }
Subroutine is_mate: called CC=1 SCAR=0.0
Pod undocumented
194 105 ▶sub is_ttc { _is_ttc($_[0]->_ptr, $_[1]) }
Subroutine is_ttc: called CC=1 SCAR=0.0
Pod undocumented
195 138 ▶sub is_exchange { _is_exchange($_[0]->_ptr, $_[1]) }
Subroutine is_exchange: called CC=1 SCAR=0.0
Pod undocumented
196 85 ▶sub is_block { _is_block($_[0]->_ptr, $_[1]) }
Subroutine is_block: called CC=1 SCAR=0.0
Pod undocumented
197 89 ▶sub is_sacrifice { _is_sacrifice($_[0]->_ptr, $_[1]) }
Subroutine is_sacrifice: called CC=1 SCAR=0.0
Pod undocumented
198 143 ▶sub is_idle { _is_idle($_[0]->_ptr, $_[1]) }
Subroutine is_idle: called CC=1 SCAR=0.0
Pod undocumented
199  
200 96 ▶sub is_chase {
Subroutine is_chase: called CC=2 SCAR=6.9
Pod undocumented
201 96 my @r = _is_chase($_[0]->_ptr, $_[1]);
202 96 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 93 96
203 }
204  
205 97 ▶sub protected_at {
Subroutine protected_at: called CC=2 SCAR=6.9
Pod undocumented
206 97 my @r = _protected_at($_[0]->_ptr, $_[1]);
207 97 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 90 97
208 }
209  
210 4 ▶sub value_of { my $c = shift; _value_of($_[0]) }
Subroutine value_of: called CC=1 SCAR=0.0
Pod undocumented
211  
212 51 ▶sub evaluate { _evaluate($_[0]->_ptr) }
Subroutine evaluate: called CC=1 SCAR=0.0
Pod undocumented
213  
214 521 ▶sub search {
Subroutine search: called CC=4 SCAR=13.9
Pod documented
215 521 my ($self, $budget, $seed) = @_;
216 521 ▶ my @r = _search($self->_ptr, $budget || 1, $seed || 0);
Condition100%4/4
$budget || 1
l!l
48734
$seed || 0
l!l
49526
MC/DC100%1/1
$budget || 1
$budget
MC/DC100%1/1
$seed || 0
$seed
Truth table100%2/2
A
A= $budget
Aresult
11
00
Truth table100%2/2
A
A= $seed
Aresult
11
00
217 521 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 428 93 521
218 }
219  
220 6 ▶sub search_to_depth {
Subroutine search_to_depth: called CC=5 SCAR=16.4
Pod undocumented
221 6 my ($self, $max_depth, $budget, $seed) = @_;
222 6 ▶ my @r = _search_to_depth($self->_ptr, $max_depth || 1, $budget || 1, $seed || 0);
Condition66%4/6
$max_depth || 1
l!l
60
$budget || 1
l!l
60
$seed || 0
l!l
15
MC/DC0%0/1
$max_depth || 1
$max_depth
MC/DC0%0/1
$budget || 1
$budget
MC/DC100%1/1
$seed || 0
$seed
Truth table50%1/2
A
A= $max_depth
Aresult
11
00
Truth table50%1/2
A
A= $budget
Aresult
11
00
Truth table100%2/2
A
A= $seed
Aresult
11
00
223 6 ▶ return wantarray ? @r : $r[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 1 6
224 }
225  
226 283 ▶sub judge {
Subroutine judge: called CC=1 SCAR=0.0
Pod undocumented
227 283 my ($self, $moves) = @_;
228 283 return { _judge($self->_ptr, $moves) };
229 }
230  
231 6 ▶sub rule_count { _rule_count() }
Subroutine rule_count: called CC=1 SCAR=0.0
Pod undocumented
232 176 ▶sub rule_at {
Subroutine rule_at: called CC=2 SCAR=7.0
Pod undocumented
233 176 my $c = shift;
234 176 my @r = _rule_at($_[0]);
235 176 ▶ return unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 176 176
236 176 my %h;
237 176 @h{qw(number chaser victim protectedness chasers victims verdict text)} = @r;
238 176 return \%h;
239 }
240  
241 2 ▶sub rules_implemented {
Subroutine rules_implemented: called CC=2 SCAR=6.9
Pod undocumented
242 2 my ($class) = @_;
243 2 my %n;
244 2 for my $i (0 .. _rule_count() - 1) {
245 44 my $r = $class->rule_at($i);
246 44 $n{ $r->{number} } = 1;
247 }
248 2 return sort { $a <=> $b } keys %n;
249 }
250  
251 0 ▶sub perft_divide {
Subroutine perft_divide: not called CC=4 SCAR=30.0
Pod undocumented
252 0 my ($self, $depth) = @_;
253 0 my %out;
254 0 ▶ return %out if $depth < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 1 0 0 0
255 0 for my $mv ($self->legal) {
256 0 my (undef, $u) = $self->do_move($mv);
257 0 ▶ $out{$mv} = $depth == 1 ? 1 : ($self->perft($depth - 1))[0];
Branch: 1 branch
BranchTrueFalseTotal
$depth == 1 ? : 0 0 0
258 0 $self->undo_move($u);
259 }
260 0 return %out;
261 }
262  
263 185 ▶sub all_points {
Subroutine all_points: called CC=3 SCAR=11.0
Pod undocumented
264 185 my @p;
265 185 for my $r (0 .. RANKS - 1) {
266 1850 for my $f (0 .. FILES - 1) {
267 16650 push @p, _point_of($f, $r);
268 }
269 }
270 185 return @p;
271 }
272  
273 24 1;
274  
275 __END__
« blib/lib/Game/Xiangqi/Bot.pm ↑ Summary blib/arch/Game/Xiangqi/Install/Files.pm »