blib/lib/Game/RoyalUr/Engine.pm

filter: Statementstmt 100% 161 / 161 Branchbran 95.4% 42 / 44 Conditioncond 77.7% 14 / 18 MC/DCmcdc 66.6% 8 / 12 Subroutinesub 100% 61 / 61 Podpod 95.8% 46 / 48 totaltotal 96.5% 332 / 344 CC 30 SCAR 11.8
CC
30
Cov
97%
search
11.0
BUILD
7.0
moves
6.0
CRAP 3.2
« blib/lib/Game/RoyalUr/Variant.pm ↑ Summary blib/lib/Game/RoyalUr/Rules.pm »
1 package Game::RoyalUr::Engine;
2  
3 73 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 73 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 73 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 73 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 73 ▶use Exporter 'import';
Subroutine __ANON__: called CC=1 SCAR=0.0
9 73 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10 73 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 73 ▶use Game::RoyalUr::Move;
Subroutine __ANON__: called CC=1 SCAR=0.0
13  
14 73 our $VERSION = '0.02';
15  
16 73 require XSLoader;
17 73 XSLoader::load('Game::RoyalUr', $VERSION);
18  
19 73 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
20 73 EMPTY => 0,
21 LIGHT => 1,
22 DARK => 2,
23  
24 SIDE_LIGHT => 0,
25 SIDE_DARK => 1,
26  
27 ROUTE_SHORT => 0,
28 ROUTE_LONG => 1,
29  
30 FILES => 8,
31 ROWS => 3,
32 CELLS => 20,
33 PIECES_MAX => 7,
34 MOVES_MAX => 7,
35 ROLL_MAX => 4,
36 WIN => 1_000_000,
37 DEPTH_MAX => 32,
38 73 };
39  
40 73 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
41 73 ONGOING => 0,
42 WON => 1,
43 DRAWN => 2,
44  
45 BY_HOME => 1,
46 BY_PLY_CAP => 2,
47 73 };
48  
49 73 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 73 POS_OK => 0,
51 POS_NULL => 1,
52 POS_ROWS => 2,
53 POS_WIDTH => 3,
54 POS_LETTER => 4,
55 POS_GAP => 5,
56 POS_X => 6,
57 POS_SIDE => 7,
58 POS_COUNT => 8,
59 POS_FIELD => 9,
60 POS_LONG => 10,
61 73 };
62  
63 73 our @EXPORT_OK = qw(
64 EMPTY LIGHT DARK
65 SIDE_LIGHT SIDE_DARK
66 ROUTE_SHORT ROUTE_LONG
67 FILES ROWS CELLS PIECES_MAX MOVES_MAX ROLL_MAX WIN DEPTH_MAX
68 POS_OK POS_NULL POS_ROWS POS_WIDTH POS_LETTER POS_GAP POS_X
69 POS_SIDE POS_COUNT POS_FIELD POS_LONG
70 ONGOING WON DRAWN BY_HOME BY_PLY_CAP
71 other piece_of
72 );
73 73 our %EXPORT_TAGS = (all => \@EXPORT_OK);
74  
75 302 ▶sub other { $_[0] == SIDE_LIGHT ? SIDE_DARK : SIDE_LIGHT }
Subroutine other: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == 0 ? : 150 152 302
76  
77 93536 ▶sub piece_of { $_[0] == SIDE_LIGHT ? LIGHT : DARK }
Subroutine piece_of: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == 0 ? : 50146 43390 93536
78  
79 73 has _ptr => (is => 'rw', private => 1);
80  
81 73 has _pieces => (is => 'ro', init_arg => 'pieces', private => 1, default => 7);
82  
83 73 has _position => (is => 'ro', init_arg => 'position', private => 1);
84  
85 73 my %ADOPTING;
86  
87 79125 ▶sub BUILD {
Subroutine BUILD: called CC=7 SCAR=19.5
Pod undocumented
88 79125 my ($self) = @_;
89 79125 my $given = $self->_ptr;
90 79125 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 66133 12992 79125
91 66133 ▶ return if delete $ADOPTING{$given};
Branch: 1 branch
BranchTrueFalseTotal
if delete $ADOPTING{$given} 66132 1 66133
92 1 $self->_ptr(0);
93 1 Carp::croak("Game::RoyalUr::Engine: a board is made by new, of_string or clone");
94 }
95  
96 12992 my $position = $self->_position;
97 12992 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 161 12831 12992
98 161 my ($ptr, $err) = _of_string($position);
99 161 ▶ Carp::croak("Game::RoyalUr::Engine: the position was refused, code $err")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 3 158 161
100 unless defined $ptr;
101 158 $self->_ptr($ptr);
102 158 return;
103 }
104  
105 12831 my $pieces = $self->_pieces;
106 12831 ▶ Carp::croak("Game::RoyalUr::Engine: pieces is a whole number from 0 to 7")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pieces and $pieces =~ /\A[0-7]\z/ 4 12827 12831
Condition66%2/3
defined $pieces and $pieces =~ /\A[0-7]\z/
!ll&&!rl&&r
0412827
MC/DC50%1/2
defined $pieces and $pieces =~ /\A[0-7]\z/
defined $pieces $pieces =~ /\A[0-7]\z/
Truth table66%2/3
A and B
A= defined $piecesB= $pieces =~ /\A[0-7]\z/
ABresult
0X0
100
111
107 unless defined $pieces && $pieces =~ /\A[0-7]\z/;
108 12827 $self->_ptr(_new_board($pieces));
109 12827 return;
110 }
111  
112 66132 ▶my $adopt = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
113 66132 my ($class, $ptr) = @_;
114 66132 $ADOPTING{$ptr} = 1;
115 66132 return $class->new(_ptr => $ptr);
116 73 };
117  
118 7852 ▶sub of_string {
Subroutine of_string: called CC=3 SCAR=11.0
Pod documented
119 7852 my ($class, $position) = @_;
120 7852 ▶ my ($ptr, $err) = _of_string(defined $position ? $position : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 7851 1 7852
121 7852 ▶ return (undef, $err) unless defined $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 91 7761 7852
122 7761 return ($adopt->($class, $ptr), POS_OK);
123 }
124  
125 58371 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
126 58371 my ($self) = @_;
127 58371 return $adopt->(ref($self), _copy_board($self->_ptr));
128 }
129  
130 79116 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=2 SCAR=7.0
Pod undocumented
131 79116 my ($self) = @_;
132 79116 my $ptr = $self->_ptr;
133 79116 ▶ return unless $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $ptr 0 79116 79116
134 79116 _drop_board($ptr);
135 79116 $self->_ptr(0);
136 79116 return;
137 }
138  
139 274337 ▶sub at { _at($_[0]->_ptr, $_[1]) }
Subroutine at: called CC=1 SCAR=0.0
Pod documented
140 92412 ▶sub put { _put($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
Subroutine put: called CC=1 SCAR=0.0
Pod documented
141 2 ▶sub lift { _lift($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine lift: called CC=1 SCAR=0.0
Pod documented
142 10792 ▶sub hand { _hand($_[0]->_ptr, $_[1]) }
Subroutine hand: called CC=1 SCAR=0.0
Pod documented
143 22611 ▶sub set_hand { _set_hand($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
Subroutine set_hand: called CC=1 SCAR=0.0
Pod documented
144 34883 ▶sub home { _home($_[0]->_ptr, $_[1]) }
Subroutine home: called CC=1 SCAR=0.0
Pod documented
145 23209 ▶sub set_home { _set_home($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
Subroutine set_home: called CC=1 SCAR=0.0
Pod documented
146 704913 ▶sub side { _side($_[0]->_ptr) }
Subroutine side: called CC=1 SCAR=0.0
Pod documented
147 12847 ▶sub set_side { _set_side($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine set_side: called CC=1 SCAR=0.0
Pod documented
148 9 ▶sub count { _count($_[0]->_ptr, $_[1]) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
149 21367 ▶sub consistent { _consistent($_[0]->_ptr, $_[1]) }
Subroutine consistent: called CC=1 SCAR=0.0
Pod documented
150 64889 ▶sub to_string { _to_string($_[0]->_ptr) }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
151 66820 ▶sub key_hex { _key_hex($_[0]->_ptr) }
Subroutine key_hex: called CC=1 SCAR=0.0
Pod documented
152  
153 72037 ▶sub cell_of { my $c = shift; _cell_of($_[0], $_[1]) }
Subroutine cell_of: called CC=1 SCAR=0.0
Pod documented
154 101 ▶sub file_of { my $c = shift; _file_of($_[0]) }
Subroutine file_of: called CC=1 SCAR=0.0
Pod documented
155 101 ▶sub row_of { my $c = shift; _row_of($_[0]) }
Subroutine row_of: called CC=1 SCAR=0.0
Pod documented
156 136 ▶sub is_rosette { my $c = shift; _is_rosette($_[0]) }
Subroutine is_rosette: called CC=1 SCAR=0.0
Pod documented
157  
158 21816 ▶sub all_cells { 0 .. CELLS - 1 }
Subroutine all_cells: called CC=1 SCAR=0.0
Pod documented
159  
160 9941 ▶sub route_len { my $c = shift; _route_len($_[0]) }
Subroutine route_len: called CC=1 SCAR=0.0
Pod documented
161 9300 ▶sub route_cell { my $c = shift; _route_cell($_[0], $_[1], $_[2]) }
Subroutine route_cell: called CC=1 SCAR=0.0
Pod documented
162 8739 ▶sub route_step { my $c = shift; _route_step($_[0], $_[1], $_[2]) }
Subroutine route_step: called CC=1 SCAR=0.0
Pod documented
163 179 ▶sub route_shared { my $c = shift; _route_shared($_[0], $_[1]) }
Subroutine route_shared: called CC=1 SCAR=0.0
Pod documented
164  
165 6349 ▶sub chances {
Subroutine chances: called CC=1 SCAR=0.0
Pod documented
166 6349 my ($class, $dice, $zero_rolls) = @_;
167 6349 return map { [ _chance($dice, $zero_rolls, $_) ] } 0 .. _chance_count($dice, $zero_rolls) - 1;
168 }
169  
170 73 my (@SIDE_NAME, @ROUTE_NAME);
171 73 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
172 73 @SIDE_NAME = ('light', 'dark');
173 73 @ROUTE_NAME = ('short', 'long');
174 }
175  
176 3292151 ▶sub cell_name {
Subroutine cell_name: called CC=2 SCAR=7.0
Pod documented
177 3292151 my ($class, $cell) = @_;
178 3292151 my $file = _file_of($cell);
179 3292151 ▶ return undef if $file < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $file < 0 0 3292151 3292151
180 3292151 return chr(ord('a') + $file) . (_row_of($cell) + 1);
181 }
182  
183 1472 ▶sub rules {
Subroutine rules: called CC=1 SCAR=0.0
Pod documented
184 1472 my ($class, $rules) = @_;
185 1472 my ($route, $dice, $zero_rolls, $safe_rosettes, $pieces) = _rules($rules);
186 return {
187 1471 route => $ROUTE_NAME[$route],
188 dice => $dice,
189 zero_rolls => $zero_rolls,
190 safe_rosettes => $safe_rosettes,
191 pieces => $pieces,
192 };
193 }
194  
195 656248 ▶sub moves {
Subroutine moves: called CC=6 SCAR=17.9
Pod documented
196 656248 my ($self, $roll, $rules) = @_;
197 656248 ▶ Carp::croak('Game::RoyalUr::Engine: a roll is a whole number from 0 to 4')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roll and $roll =~ /\A[0-4]\z/ 6 656242 656248
Condition100%3/3
defined $roll and $roll =~ /\A[0-4]\z/
!ll&&!rl&&r
15656242
MC/DC100%2/2
defined $roll and $roll =~ /\A[0-4]\z/
defined $roll $roll =~ /\A[0-4]\z/
Truth table100%3/3
A and B
A= defined $rollB= $roll =~ /\A[0-4]\z/
ABresult
0X0
100
111
198 unless defined $roll && $roll =~ /\A[0-4]\z/;
199 656242 my $side = $SIDE_NAME[ _side($self->_ptr) ];
200 my @moves = map {
201 656242 my ($from_step, $to_step, $from_cell, $to_cell, $captures, $rosette, $home) = @$_;
202 1869061 ▶ Game::RoyalUr::Move->new(
Branch: 2 branches
BranchTrueFalseTotal
$from_step == 0 ? : 334298 1534763 1869061
$home ? : 111693 1757368 1869061
203 side => $side,
204 roll => $roll + 0,
205 from => ($from_step == 0 ? 'hand' : $self->cell_name($from_cell)),
206 to => ($home ? 'home' : $self->cell_name($to_cell)),
207 from_step => $from_step,
208 to_step => $to_step,
209 from_cell => $from_cell,
210 to_cell => $to_cell,
211 captures => $captures,
212 rosette => $rosette,
213 home => $home,
214 );
215 } _moves($self->_ptr, $roll, $rules);
216 656233 ▶ return wantarray ? @moves : scalar @moves;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 444176 212057 656233
217 }
218  
219 26 ▶sub count_positions { my $c = shift; _count_positions($_[0]) }
Subroutine count_positions: called CC=1 SCAR=0.0
Pod documented
220  
221 269799 ▶sub ply { _ply($_[0]->_ptr) }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
222 13 ▶sub set_ply { _set_ply($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine set_ply: called CC=1 SCAR=0.0
Pod documented
223 1 ▶sub ply_cap { _ply_cap() }
Subroutine ply_cap: called CC=1 SCAR=0.0
Pod documented
224  
225 228928 ▶sub apply {
Subroutine apply: called CC=3 SCAR=11.0
Pod documented
226 228928 my ($self, $move, $rules) = @_;
227 228928 ▶ Carp::croak('Game::RoyalUr::Engine: apply takes a move')
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($move) and $move->isa("Game::RoyalUr::Move") 1 228927 228928
Condition66%2/3
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
!ll&&!rl&&r
10228927
MC/DC50%1/2
builtin::blessed($move) and $move->isa("Game::RoyalUr::Move")
builtin::blessed($move) $move->isa("Game::RoyalUr::Move")
Truth table66%2/3
A and B
A= builtin::blessed($move)B= $move->isa("Game::RoyalUr::Move")
ABresult
0X0
100
111
228 unless Scalar::Util::blessed($move) && $move->isa('Game::RoyalUr::Move');
229 228927 return _apply($self->_ptr, $rules, $move->from_step, $move->to_step,
230 $move->from_cell, $move->to_cell, $move->home);
231 }
232  
233 12012 ▶sub forfeit { _forfeit($_[0]->_ptr) }
Subroutine forfeit: called CC=1 SCAR=0.0
Pod documented
234 12276 ▶sub unapply { _unapply($_[0]->_ptr, $_[1]); return $_[0] }
Subroutine unapply: called CC=1 SCAR=0.0
Pod documented
235  
236 2182273 ▶sub status { _status($_[0]->_ptr, $_[1]) }
Subroutine status: called CC=1 SCAR=0.0
Pod documented
237 2566 ▶sub how { _how($_[0]->_ptr, $_[1]) }
Subroutine how: called CC=1 SCAR=0.0
Pod documented
238 2570 ▶sub winner { _winner($_[0]->_ptr, $_[1]) }
Subroutine winner: called CC=1 SCAR=0.0
Pod documented
239  
240 484 ▶sub walk { _walk($_[0]->_ptr, $_[1], $_[2]) }
Subroutine walk: called CC=1 SCAR=0.0
Pod documented
241  
242 21 ▶sub evaluate { _evaluate($_[0]->_ptr, $_[2], $_[3], $_[1]) }
Subroutine evaluate: called CC=1 SCAR=0.0
Pod documented
243  
244 1768 ▶sub greedy { _greedy($_[0]->_ptr, $_[1], $_[2]) }
Subroutine greedy: called CC=1 SCAR=0.0
Pod documented
245  
246 6036 ▶sub search {
Subroutine search: called CC=11 SCAR=24.0
Pod documented
247 6036 my ($self, $roll, %option) = @_;
248 6036 ▶ Carp::croak('Game::RoyalUr::Engine: a roll is a whole number from 0 to 4')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roll and $roll =~ /\A[0-4]\z/ 1 6035 6036
Condition66%2/3
defined $roll and $roll =~ /\A[0-4]\z/
!ll&&!rl&&r
016035
MC/DC50%1/2
defined $roll and $roll =~ /\A[0-4]\z/
defined $roll $roll =~ /\A[0-4]\z/
Truth table66%2/3
A and B
A= defined $rollB= $roll =~ /\A[0-4]\z/
ABresult
0X0
100
111
249 unless defined $roll && $roll =~ /\A[0-4]\z/;
250 6035 for my $key (sort keys %option) {
251 19969 ▶ Carp::croak("Game::RoyalUr::Engine: search has no option called '$key'")
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\A(?:rules|weights|budget|depth)\z/ 1 19968 19969
252 unless $key =~ /\A(?:rules|weights|budget|depth)\z/;
253 }
254 6034 ▶ my $budget = defined $option{budget} ? $option{budget} : 2_000_000_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $option{'budget'} ? : 4000 2034 6034
255 6034 ▶ my $depth = defined $option{depth} ? $option{depth} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $option{'depth'} ? : 6009 25 6034
256 6034 ▶ Carp::croak('Game::RoyalUr::Engine: a budget is a whole number of nodes from 0 to 2000000000')
Branch: 1 branch
BranchTrueFalseTotal
unless $budget =~ /\A\d{1,10}\z/ and $budget <= 2000000000 2 6032 6034
Condition100%3/3
$budget =~ /\A\d{1,10}\z/ and $budget <= 2000000000
!ll&&!rl&&r
116032
MC/DC100%2/2
$budget =~ /\A\d{1,10}\z/ and $budget <= 2000000000
$budget =~ /\A\d{1,10}\z/ $budget <= 2000000000
Truth table100%3/3
A and B
A= $budget =~ /\A\d{1,10}\z/B= $budget <= 2000000000
ABresult
0X0
100
111
257 unless $budget =~ /\A\d{1,10}\z/ && $budget <= 2_000_000_000;
258 6032 ▶ Carp::croak('Game::RoyalUr::Engine: a depth is a whole number from 0 to 32')
Branch: 1 branch
BranchTrueFalseTotal
unless $depth =~ /\A\d{1,2}\z/ and $depth <= 32 1 6031 6032
Condition66%2/3
$depth =~ /\A\d{1,2}\z/ and $depth <= 32
!ll&&!rl&&r
016031
MC/DC50%1/2
$depth =~ /\A\d{1,2}\z/ and $depth <= 32
$depth =~ /\A\d{1,2}\z/ $depth <= 32
Truth table66%2/3
A and B
A= $depth =~ /\A\d{1,2}\z/B= $depth <= 32
ABresult
0X0
100
111
259 unless $depth =~ /\A\d{1,2}\z/ && $depth <= 32;
260 my ($index, $nodes, $reached, $value, $stopped) =
261 6031 _search($self->_ptr, $roll, $option{rules}, $option{weights}, $budget, $depth);
262 6030 return { index => $index, nodes => $nodes, depth => $reached, value => $value, stopped => $stopped };
263 }
264  
265 1 ▶sub abi_version { _abi_version() }
Subroutine abi_version: called CC=1 SCAR=0.0
Pod documented
266 12 ▶sub live { _live() }
Subroutine live: called CC=1 SCAR=0.0
Pod documented
267  
268 73 1;
269  
270 __END__
« blib/lib/Game/RoyalUr/Variant.pm ↑ Summary blib/lib/Game/RoyalUr/Rules.pm »