blib/lib/Game/RoyalUr/Engine.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Game/RoyalUr.pm ↑ Summary blib/lib/Game/RoyalUr/Rules.pm »
1 package Game::RoyalUr::Engine;
2  
3 15 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 15 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 0 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=2.6
10 use Scalar::Util ();
11  
12 use Game::RoyalUr::Move;
13  
14 our $VERSION = '0.03';
15  
16 require XSLoader;
17 XSLoader::load('Game::RoyalUr', $VERSION);
18  
19 use constant {
20 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 };
39  
40 use constant {
41 ONGOING => 0,
42 WON => 1,
43 DRAWN => 2,
44  
45 BY_HOME => 1,
46 BY_PLY_CAP => 2,
47 };
48  
49 use constant {
50 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 };
62  
63 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 our %EXPORT_TAGS = (all => \@EXPORT_OK);
74  
75 sub other { $_[0] == SIDE_LIGHT ? SIDE_DARK : SIDE_LIGHT }
76  
77 sub piece_of { $_[0] == SIDE_LIGHT ? LIGHT : DARK }
78  
79 has _ptr => (is => 'rw', private => 1);
80  
81 has _pieces => (is => 'ro', init_arg => 'pieces', private => 1, default => 7);
82  
83 has _position => (is => 'ro', init_arg => 'position', private => 1);
84  
85 my %ADOPTING;
86  
87 sub BUILD {
88 my ($self) = @_;
89 my $given = $self->_ptr;
90 if ($given) {
91 return if delete $ADOPTING{$given};
92 $self->_ptr(0);
93 Carp::croak("Game::RoyalUr::Engine: a board is made by new, of_string or clone");
94 }
95  
96 my $position = $self->_position;
97 if (defined $position) {
98 my ($ptr, $err) = _of_string($position);
99 Carp::croak("Game::RoyalUr::Engine: the position was refused, code $err")
100 unless defined $ptr;
101 $self->_ptr($ptr);
102 return;
103 }
104  
105 my $pieces = $self->_pieces;
106 Carp::croak("Game::RoyalUr::Engine: pieces is a whole number from 0 to 7")
107 unless defined $pieces && $pieces =~ /\A[0-7]\z/;
108 $self->_ptr(_new_board($pieces));
109 return;
110 }
111  
112 my $adopt = sub {
113 my ($class, $ptr) = @_;
114 $ADOPTING{$ptr} = 1;
115 return $class->new(_ptr => $ptr);
116 };
117  
118 sub of_string {
119 my ($class, $position) = @_;
120 my ($ptr, $err) = _of_string(defined $position ? $position : '');
121 return (undef, $err) unless defined $ptr;
122 return ($adopt->($class, $ptr), POS_OK);
123 }
124  
125 sub clone {
126 my ($self) = @_;
127 return $adopt->(ref($self), _copy_board($self->_ptr));
128 }
129  
130 sub DEMOLISH {
131 my ($self) = @_;
132 my $ptr = $self->_ptr;
133 return unless $ptr;
134 _drop_board($ptr);
135 $self->_ptr(0);
136 return;
137 }
138  
139 sub at { _at($_[0]->_ptr, $_[1]) }
140 sub put { _put($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
141 sub lift { _lift($_[0]->_ptr, $_[1]); return $_[0] }
142 sub hand { _hand($_[0]->_ptr, $_[1]) }
143 sub set_hand { _set_hand($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
144 sub home { _home($_[0]->_ptr, $_[1]) }
145 sub set_home { _set_home($_[0]->_ptr, $_[1], $_[2]); return $_[0] }
146 sub side { _side($_[0]->_ptr) }
147 sub set_side { _set_side($_[0]->_ptr, $_[1]); return $_[0] }
148 sub count { _count($_[0]->_ptr, $_[1]) }
149 sub consistent { _consistent($_[0]->_ptr, $_[1]) }
150 sub to_string { _to_string($_[0]->_ptr) }
151 sub key_hex { _key_hex($_[0]->_ptr) }
152  
153 sub cell_of { my $c = shift; _cell_of($_[0], $_[1]) }
154 sub file_of { my $c = shift; _file_of($_[0]) }
155 sub row_of { my $c = shift; _row_of($_[0]) }
156 sub is_rosette { my $c = shift; _is_rosette($_[0]) }
157  
158 sub all_cells { 0 .. CELLS - 1 }
159  
160 sub route_len { my $c = shift; _route_len($_[0]) }
161 sub route_cell { my $c = shift; _route_cell($_[0], $_[1], $_[2]) }
162 sub route_step { my $c = shift; _route_step($_[0], $_[1], $_[2]) }
163 sub route_shared { my $c = shift; _route_shared($_[0], $_[1]) }
164  
165 sub chances {
166 my ($class, $dice, $zero_rolls) = @_;
167 return map { [ _chance($dice, $zero_rolls, $_) ] } 0 .. _chance_count($dice, $zero_rolls) - 1;
168 }
169  
170 my (@SIDE_NAME, @ROUTE_NAME);
171 BEGIN {
172 @SIDE_NAME = ('light', 'dark');
173 @ROUTE_NAME = ('short', 'long');
174 }
175  
176 sub cell_name {
177 my ($class, $cell) = @_;
178 my $file = _file_of($cell);
179 return undef if $file < 0;
180 return chr(ord('a') + $file) . (_row_of($cell) + 1);
181 }
182  
183 sub rules {
184 my ($class, $rules) = @_;
185 my ($route, $dice, $zero_rolls, $safe_rosettes, $pieces) = _rules($rules);
186 return {
187 route => $ROUTE_NAME[$route],
188 dice => $dice,
189 zero_rolls => $zero_rolls,
190 safe_rosettes => $safe_rosettes,
191 pieces => $pieces,
192 };
193 }
194  
195 sub moves {
196 my ($self, $roll, $rules) = @_;
197 Carp::croak('Game::RoyalUr::Engine: a roll is a whole number from 0 to 4')
198 unless defined $roll && $roll =~ /\A[0-4]\z/;
199 my $side = $SIDE_NAME[ _side($self->_ptr) ];
200 my @moves = map {
201 my ($from_step, $to_step, $from_cell, $to_cell, $captures, $rosette, $home) = @$_;
202 Game::RoyalUr::Move->new(
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 return wantarray ? @moves : scalar @moves;
217 }
218  
219 sub count_positions { my $c = shift; _count_positions($_[0]) }
220  
221 sub ply { _ply($_[0]->_ptr) }
222 sub set_ply { _set_ply($_[0]->_ptr, $_[1]); return $_[0] }
223 sub ply_cap { _ply_cap() }
224  
225 sub apply {
226 my ($self, $move, $rules) = @_;
227 Carp::croak('Game::RoyalUr::Engine: apply takes a move')
228 unless Scalar::Util::blessed($move) && $move->isa('Game::RoyalUr::Move');
229 return _apply($self->_ptr, $rules, $move->from_step, $move->to_step,
230 $move->from_cell, $move->to_cell, $move->home);
231 }
232  
233 sub forfeit { _forfeit($_[0]->_ptr) }
234 sub unapply { _unapply($_[0]->_ptr, $_[1]); return $_[0] }
235  
236 sub status { _status($_[0]->_ptr, $_[1]) }
237 sub how { _how($_[0]->_ptr, $_[1]) }
238 sub winner { _winner($_[0]->_ptr, $_[1]) }
239  
240 sub walk { _walk($_[0]->_ptr, $_[1], $_[2]) }
241  
242 sub evaluate { _evaluate($_[0]->_ptr, $_[2], $_[3], $_[1]) }
243  
244 sub greedy { _greedy($_[0]->_ptr, $_[1], $_[2]) }
245  
246 sub search {
247 my ($self, $roll, %option) = @_;
248 Carp::croak('Game::RoyalUr::Engine: a roll is a whole number from 0 to 4')
249 unless defined $roll && $roll =~ /\A[0-4]\z/;
250 for my $key (sort keys %option) {
251 Carp::croak("Game::RoyalUr::Engine: search has no option called '$key'")
252 unless $key =~ /\A(?:rules|weights|budget|depth)\z/;
253 }
254 my $budget = defined $option{budget} ? $option{budget} : 2_000_000_000;
255 my $depth = defined $option{depth} ? $option{depth} : 0;
256 Carp::croak('Game::RoyalUr::Engine: a budget is a whole number of nodes from 0 to 2000000000')
257 unless $budget =~ /\A\d{1,10}\z/ && $budget <= 2_000_000_000;
258 Carp::croak('Game::RoyalUr::Engine: a depth is a whole number from 0 to 32')
259 unless $depth =~ /\A\d{1,2}\z/ && $depth <= 32;
260 my ($index, $nodes, $reached, $value, $stopped) =
261 _search($self->_ptr, $roll, $option{rules}, $option{weights}, $budget, $depth);
262 return { index => $index, nodes => $nodes, depth => $reached, value => $value, stopped => $stopped };
263 }
264  
265 sub abi_version { _abi_version() }
266 sub live { _live() }
267  
268 1;
269  
270 __END__
« blib/lib/Game/RoyalUr.pm ↑ Summary blib/lib/Game/RoyalUr/Rules.pm »