blib/lib/Game/Sternhalma/Engine.pm

filter: Statementstmt 97.4% 262 / 269 Branchbran 75.6% 124 / 164 Conditioncond 51.8% 42 / 81 MC/DCmcdc 21.2% 10 / 47 Subroutinesub 95.5% 85 / 89 Podpod 98.5% 70 / 71 totaltotal 82.2% 593 / 721 CC 113 SCAR 15.6
CC
113
Cov
83%
search
16.1
BUILD
13.0
read_move
10.0
CRAP 4.7
« blib/lib/Game/Sternhalma/Layout.pm ↑ Summary blib/lib/Game/Sternhalma/Result.pm »
1 package Game::Sternhalma::Engine;
2  
3 54 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 54 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 54 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 54 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 54 ▶use Object::Proto::Sugar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 54 ▶use Game::Sternhalma::Notation ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 54 our $VERSION = '0.01';
14  
15 54 require XSLoader;
16 54 XSLoader::load('Game::Sternhalma', $VERSION);
17  
18 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 54 E => 0,
20 NE => 1,
21 NW => 2,
22 W => 3,
23 SW => 4,
24 SE => 5,
25 54 };
26  
27 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
28 54 HOLES => 121,
29 ROWS => 17,
30 POINTS => 6,
31 CENTRE => 6,
32 SEATS_MAX => 6,
33 PIECES => 10,
34 PIECES_MAX => 15,
35 PAIRS => 312,
36 STEPS_MAX => 6,
37 HOPS_MAX => 6,
38 REACH_MAX => 120,
39 PATH_MAX => 37,
40 LEGAL_MAX => 1800,
41 54 };
42  
43 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
44 54 POS_OK => 0,
45 POS_NULL => 1,
46 POS_LONG => 2,
47 POS_LETTER => 3,
48 POS_ROWS => 4,
49 POS_WIDTH => 5,
50 POS_FIELDS => 6,
51 POS_SEAT => 7,
52 POS_COUNT => 8,
53 POS_GAME => 9,
54 54 };
55  
56 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
57 54 ONGOING => 0,
58 BY_HOME => 1,
59 BY_CAP => 2,
60 BY_DEADLOCK => 3,
61 BY_RESIGN => 4,
62 PLY_CAP_PER_SEAT => 1000,
63 54 };
64  
65 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
66 54 WIN_SCORE => 100000,
67 SEARCH_AUTO => 0,
68 SEARCH_PARANOID => 1,
69 SEARCH_MAXN => 2,
70 54 };
71  
72 54 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
73 54 PLAY_OK => 0,
74 WHY_OVER => 1,
75 WHY_NOT_A_HOLE => 2,
76 WHY_NO_PIECE => 3,
77 WHY_NOT_YOURS => 4,
78 WHY_UNREACHABLE => 5,
79 WHY_HAS_MOVE => 6,
80 WHY_NOT_A_SEAT => 7,
81 WHY_SEAT_DONE => 8,
82 54 };
83  
84 54 our @EXPORT_OK = qw(
85 E NE NW W SW SE
86 HOLES ROWS POINTS CENTRE SEATS_MAX PIECES PIECES_MAX PAIRS STEPS_MAX HOPS_MAX
87 REACH_MAX PATH_MAX LEGAL_MAX
88 ONGOING BY_HOME BY_CAP BY_DEADLOCK BY_RESIGN PLY_CAP_PER_SEAT
89 PLAY_OK WHY_OVER WHY_NOT_A_HOLE WHY_NO_PIECE WHY_NOT_YOURS WHY_UNREACHABLE
90 WHY_HAS_MOVE WHY_NOT_A_SEAT WHY_SEAT_DONE
91 WIN_SCORE SEARCH_AUTO SEARCH_PARANOID SEARCH_MAXN
92 POS_OK POS_NULL POS_LONG POS_LETTER POS_ROWS POS_WIDTH POS_FIELDS POS_SEAT POS_COUNT POS_GAME
93 );
94 54 our %EXPORT_TAGS = (all => \@EXPORT_OK);
95  
96 54 my @REASON = (
97 'the position is fine',
98 'there is no position',
99 'the position is too long',
100 'the position has a character that is not a seat, a run of empty holes or a /',
101 'the position does not have seventeen rows',
102 'a row of the position is not as long as that row of the star',
103 'the seat to move, the seat count or the piece count is missing or wrong',
104 'the position has a piece of a seat the game does not have',
105 'a seat does not have exactly its pieces',
106 'the game after the ; is malformed, out of order, or does not agree with itself',
107 );
108  
109 54 has _ptr => (is => 'rw', private => 1);
110  
111 54 has _hold => (is => 'rw', private => 1);
112  
113 54 has _want_empty => (is => 'ro', init_arg => 'empty', private => 1, default => 0);
114  
115 54 has _want_position => (is => 'ro', init_arg => 'position', private => 1);
116  
117 54 has _want_seats => (is => 'ro', init_arg => 'seats', private => 1, default => 2);
118  
119 54 has _want_pieces => (is => 'ro', init_arg => 'pieces', private => 1, default => PIECES);
120  
121 54 my %ADOPTING;
122  
123 807385 ▶sub BUILD {
Subroutine BUILD: called CC=12 SCAR=25.7
Pod undocumented
124 807385 my ($self) = @_;
125 807385 my $given = $self->_ptr;
126 807385 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 800436 6949 807385
127 800436 ▶ if (delete $ADOPTING{$given}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $ADOPTING{$given}) 800435 1 800436
128 800435 $self->_hold(_hold_board($given));
129 800435 return;
130 }
131 1 $self->_ptr(0);
132 1 Carp::croak("Game::Sternhalma::Engine: a board is made by new, from_string or clone");
133 }
134  
135 6949 my $position = $self->_want_position;
136 6949 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 1 6948 6949
137 1 my ($ptr, $err) = _of_string($position);
138 1 ▶ Carp::croak("Game::Sternhalma::Engine: the position was refused: $REASON[$err]")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 1 0 1
139 unless defined $ptr;
140 0 $self->_ptr($ptr);
141 0 $self->_hold(_hold_board($ptr));
142 0 return;
143 }
144  
145 6948 my ($seats, $pieces) = ($self->_want_seats, $self->_want_pieces);
146 6948 ▶ Carp::croak("Game::Sternhalma::Engine: no game is $seats seats of $pieces pieces")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u and _layout_ok($seats, $pieces) 7 6941 6948
Condition66%8/12
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u and _layout_ok($seats, $pieces)
!ll&&!rl&&r
166941
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u
!ll&&!rl&&r
106947
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u
!ll&&!rl&&r
016947
defined $seats and defined $pieces
!ll&&!rl&&r
006948
MC/DC40%2/5
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u and _layout_ok($seats, $pieces)
defined $seats defined $pieces $seats =~ /\A[0-9]+\z/u $pieces =~ /\A[0-9]+\z/u _layout_ok($seats, $pieces)
Truth table50%3/6
A and B and C and D and E
A= defined $seatsB= defined $piecesC= $seats =~ /\A[0-9]+\z/uD= $pieces =~ /\A[0-9]+\z/uE= _layout_ok($seats, $pieces)
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
147 unless defined $seats && defined $pieces
148 && $seats =~ /\A[0-9]+\z/ && $pieces =~ /\A[0-9]+\z/
149 && _layout_ok($seats, $pieces);
150  
151 6941 ▶ $self->_ptr($self->_want_empty ? _new_empty($seats, $pieces) : _new_board($seats, $pieces));
Branch: 1 branch
BranchTrueFalseTotal
$self->_want_empty ? : 5379 1562 6941
152 6941 ▶ Carp::croak("Game::Sternhalma::Engine: the board was not built") unless $self->_ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_ptr 0 6941 6941
153 6941 $self->_hold(_hold_board($self->_ptr));
154 6941 return;
155 }
156  
157 800435 ▶my $adopt = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
158 800435 my ($class, $ptr) = @_;
159 800435 $ADOPTING{$ptr} = 1;
160 800435 return $class->new(_ptr => $ptr);
161 54 };
162  
163 71211 ▶sub from_string {
Subroutine from_string: called CC=4 SCAR=14.1
Pod documented
164 71211 my ($class, $position) = @_;
165 71211 ▶ my ($ptr, $err) = _of_string(defined $position ? $position : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 71210 1 71211
166 71211 ▶ return (undef, $err) unless defined $ptr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ptr 1966 69245 71211
167 69245 ▶ return ($adopt->(ref($class) || $class, $ptr), POS_OK);
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0692450
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
168 }
169  
170 2 ▶sub reason {
Subroutine reason: called CC=3 SCAR=13.1
Pod documented
171 2 my ($class, $code) = @_;
172 2 ▶ return defined $code && $code =~ /\A[0-9]+\z/ ? $REASON[$code] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code && $code =~ /\A[0-9]+\z/u ? : 2 0 2
Condition33%1/3
defined $code and $code =~ /\A[0-9]+\z/u
!ll&&!rl&&r
002
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
173 }
174  
175 731190 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
176 731190 my ($self) = @_;
177 731190 return $adopt->(ref($self), _copy_board($self->_ptr));
178 }
179  
180 10242080 ▶sub at {
Subroutine at: called CC=3 SCAR=11.0
Pod documented
181 10242080 my ($self, $hole) = @_;
182 10242080 ▶ return undef unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 0 10242080 10242080
183 10242080 my $seat = _at($self->_ptr, $hole);
184 10242080 ▶ return $seat < SEATS_MAX ? $seat : undef;
Branch: 1 branch
BranchTrueFalseTotal
$seat < 6 ? : 3275418 6966662 10242080
185 }
186  
187 198163 ▶sub put {
Subroutine put: called CC=4 SCAR=14.1
Pod documented
188 198163 my ($self, $hole, $seat) = @_;
189 198163 ▶ _put($self->_ptr, $hole, $seat) if _is_hole($hole) && defined $seat && $seat =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if _is_hole($hole) and defined $seat and $seat =~ /\A[0-9]+\z/u 198162 1 198163
Condition66%4/6
_is_hole($hole) and defined $seat and $seat =~ /\A[0-9]+\z/u
!ll&&!rl&&r
10198162
_is_hole($hole) and defined $seat
!ll&&!rl&&r
10198162
MC/DC33%1/3
_is_hole($hole) and defined $seat and $seat =~ /\A[0-9]+\z/u
_is_hole($hole) defined $seat $seat =~ /\A[0-9]+\z/u
Truth table50%2/4
A and B and C
A= _is_hole($hole)B= defined $seatC= $seat =~ /\A[0-9]+\z/u
ABCresult
0XX0
10X0
1100
1111
190 198163 return $self;
191 }
192  
193 135 ▶sub lift {
Subroutine lift: called CC=2 SCAR=7.1
Pod documented
194 135 my ($self, $hole) = @_;
195 135 ▶ _lift($self->_ptr, $hole) if _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
if _is_hole($hole) 135 0 135
196 135 return $self;
197 }
198  
199 3982 ▶sub set_to_move {
Subroutine set_to_move: called CC=3 SCAR=12.5
Pod documented
200 3982 my ($self, $seat) = @_;
201 3982 ▶ _set_to_move($self->_ptr, $seat) if defined $seat && $seat =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seat and $seat =~ /\A[0-9]+\z/u 3982 0 3982
Condition33%1/3
defined $seat and $seat =~ /\A[0-9]+\z/u
!ll&&!rl&&r
003982
MC/DC0%0/2
defined $seat and $seat =~ /\A[0-9]+\z/u
defined $seat $seat =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $seatB= $seat =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
202 3982 return $self;
203 }
204  
205 132290 ▶sub to_move { _to_move($_[0]->_ptr) }
Subroutine to_move: called CC=1 SCAR=0.0
Pod documented
206 1982799 ▶sub seats { _board_seats($_[0]->_ptr) }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
207 992 ▶sub pieces { _board_pieces($_[0]->_ptr) }
Subroutine pieces: called CC=1 SCAR=0.0
Pod documented
208 24 ▶sub count { _count($_[0]->_ptr, $_[1]) }
Subroutine count: called CC=1 SCAR=0.0
Pod documented
209 188307 ▶sub to_string { _to_string($_[0]->_ptr) }
Subroutine to_string: called CC=1 SCAR=0.0
Pod documented
210 911638 ▶sub key { _key_hex($_[0]->_ptr) }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
211  
212 20 ▶sub seat_point {
Subroutine seat_point: called CC=2 SCAR=7.1
Pod documented
213 20 my ($self, $seat) = @_;
214 20 my $point = _layout_point($self->seats, $seat);
215 20 ▶ return $point < 0 ? undef : $point;
Branch: 1 branch
BranchTrueFalseTotal
$point < 0 ? : 0 20 20
216 }
217  
218 69904 ▶sub steps {
Subroutine steps: called CC=2 SCAR=6.9
Pod documented
219 69904 my ($self, $hole) = @_;
220 69904 ▶ return unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 3 69901 69904
221 69901 return _steps($self->_ptr, $hole);
222 }
223  
224 1438203 ▶sub hops {
Subroutine hops: called CC=5 SCAR=16.6
Pod documented
225 1438203 my ($self, $hole, $from) = @_;
226 1438203 ▶ return unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 0 1438203 1438203
227 1438203 ▶ $from = $hole unless defined $from;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from 13502 1424701 1438203
228 1438203 ▶ return unless $from eq '-1' || _is_hole($from);
Branch: 1 branch
BranchTrueFalseTotal
unless $from eq -1 or _is_hole($from) 0 1438203 1438203
Condition66%2/3
$from eq -1 or _is_hole($from)
l!l&&r!l&&!r
114382020
MC/DC0%0/2
$from eq -1 or _is_hole($from)
$from eq -1 _is_hole($from)
Truth table66%2/3
A or B
A= $from eq -1B= _is_hole($from)
ABresult
1X1
011
000
229 1438203 return _hops($self->_ptr, $hole, $from);
230 }
231  
232 64997 ▶sub do_move {
Subroutine do_move: called CC=4 SCAR=13.9
Pod documented
233 64997 my ($self, $from, $to) = @_;
234 64997 ▶ return 0 unless _is_hole($from) && _is_hole($to);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($from) and _is_hole($to) 2 64995 64997
Condition100%3/3
_is_hole($from) and _is_hole($to)
!ll&&!rl&&r
1164995
MC/DC100%2/2
_is_hole($from) and _is_hole($to)
_is_hole($from) _is_hole($to)
Truth table100%3/3
A and B
A= _is_hole($from)B= _is_hole($to)
ABresult
0X0
100
111
235 64995 ▶ return _do_move($self->_ptr, $from, $to) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_do_move($self->_ptr, $from, $to) ? : 64992 3 64995
236 }
237  
238 59943 ▶sub undo_move {
Subroutine undo_move: called CC=4 SCAR=14.9
Pod documented
239 59943 my ($self, $from, $to) = @_;
240 59943 ▶ return 0 unless _is_hole($from) && _is_hole($to);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($from) and _is_hole($to) 0 59943 59943
Condition33%1/3
_is_hole($from) and _is_hole($to)
!ll&&!rl&&r
0059943
MC/DC0%0/2
_is_hole($from) and _is_hole($to)
_is_hole($from) _is_hole($to)
Truth table33%1/3
A and B
A= _is_hole($from)B= _is_hole($to)
ABresult
0X0
100
111
241 59943 ▶ return _undo_move($self->_ptr, $from, $to) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_undo_move($self->_ptr, $from, $to) ? : 59942 1 59943
242 }
243  
244 117360 ▶sub reach {
Subroutine reach: called CC=2 SCAR=6.9
Pod documented
245 117360 my ($self, $from) = @_;
246 117360 ▶ return unless _is_hole($from);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($from) 1 117359 117360
247 117359 return _reach($self->_ptr, $from);
248 }
249  
250 521570 ▶sub path {
Subroutine path: called CC=3 SCAR=11.0
Pod documented
251 521570 my ($self, $from, $to) = @_;
252 521570 ▶ return unless _is_hole($from) && _is_hole($to);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($from) and _is_hole($to) 1 521569 521570
Condition66%2/3
_is_hole($from) and _is_hole($to)
!ll&&!rl&&r
01521569
MC/DC50%1/2
_is_hole($from) and _is_hole($to)
_is_hole($from) _is_hole($to)
Truth table66%2/3
A and B
A= _is_hole($from)B= _is_hole($to)
ABresult
0X0
100
111
253 521569 return _path($self->_ptr, $from, $to);
254 }
255  
256 113736 ▶sub legal {
Subroutine legal: called CC=2 SCAR=6.9
Pod documented
257 113736 my ($self) = @_;
258 113736 my @flat = _legal($self->_ptr);
259 113736 my @pairs;
260 113736 ▶ push @pairs, [ splice @flat, 0, 2 ] while @flat;
Branch: 1 branch
BranchTrueFalseTotal
@flat and do { push @pairs, [splice(@flat, 0, 2)] } 5368690 113736 5482426
261 113736 return @pairs;
262 }
263  
264 1521451 ▶sub _is_seat { defined $_[1] && $_[1] =~ /\A[0-9]+\z/ && $_[1] < $_[0]->seats }
Subroutine _is_seat: called CC=3 SCAR=14.2
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] and $_[1] =~ /\A[0-9]+\z/u and $_[1] < $_[0]->seats 1521451 0 1521451
Condition33%1/3
defined $_[1] and $_[1] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
001521451
MC/DC0%0/2
defined $_[1] and $_[1] =~ /\A[0-9]+\z/u
defined $_[1] $_[1] =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $_[1]B= $_[1] =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
265  
266 10 ▶sub home_complete { $_[0]->_is_seat($_[1]) ? _home_complete($_[0]->_ptr, $_[1]) : 0 }
Subroutine home_complete: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_seat($_[1]) ? : 10 0 10
267 490419 ▶sub finished { $_[0]->_is_seat($_[1]) ? _finished($_[0]->_ptr, $_[1]) : 0 }
Subroutine finished: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_seat($_[1]) ? : 490419 0 490419
268 359 ▶sub place_of { $_[0]->_is_seat($_[1]) ? _place_of($_[0]->_ptr, $_[1]) : 0 }
Subroutine place_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_seat($_[1]) ? : 359 0 359
269 661883 ▶sub distance_left { $_[0]->_is_seat($_[1]) ? _distance_left($_[0]->_ptr, $_[1]) : undef }
Subroutine distance_left: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->_is_seat($_[1]) ? : 661883 0 661883
270 82974 ▶sub places { my ($self) = @_; map { _place_of($self->_ptr, $_) } 0 .. $self->seats - 1 }
Subroutine places: called CC=1 SCAR=0.0
Pod documented
271 96896 ▶sub can_pass { _can_pass($_[0]->_ptr) ? 1 : 0 }
Subroutine can_pass: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_can_pass($_[0]->_ptr) ? : 4 96892 96896
272 702569 ▶sub over { _game_over($_[0]->_ptr) ? 1 : 0 }
Subroutine over: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_game_over($_[0]->_ptr) ? : 1722 700847 702569
273 107992 ▶sub how { _game_how($_[0]->_ptr) }
Subroutine how: called CC=1 SCAR=0.0
Pod documented
274 86058 ▶sub ply { _ply($_[0]->_ptr) }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
275 741 ▶sub cap { _cap($_[0]->_ptr) }
Subroutine cap: called CC=1 SCAR=0.0
Pod documented
276  
277 1189 ▶sub set_cap {
Subroutine set_cap: called CC=3 SCAR=12.5
Pod documented
278 1189 my ($self, $cap) = @_;
279 1189 ▶ _set_cap($self->_ptr, $cap) if defined $cap && $cap =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cap and $cap =~ /\A[0-9]+\z/u 1189 0 1189
Condition33%1/3
defined $cap and $cap =~ /\A[0-9]+\z/u
!ll&&!rl&&r
001189
MC/DC0%0/2
defined $cap and $cap =~ /\A[0-9]+\z/u
defined $cap $cap =~ /\A[0-9]+\z/u
Truth table33%1/3
A and B
A= defined $capB= $cap =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
280 1189 return $self;
281 }
282  
283 1053664 ▶sub play {
Subroutine play: called CC=3 SCAR=12.5
Pod documented
284 1053664 my ($self, $from, $to) = @_;
285 1053664 ▶ return (WHY_NOT_A_HOLE) unless _is_hole($from) && _is_hole($to);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($from) and _is_hole($to) 0 1053664 1053664
Condition33%1/3
_is_hole($from) and _is_hole($to)
!ll&&!rl&&r
001053664
MC/DC0%0/2
_is_hole($from) and _is_hole($to)
_is_hole($from) _is_hole($to)
Truth table33%1/3
A and B
A= _is_hole($from)B= _is_hole($to)
ABresult
0X0
100
111
286 1053664 return _play($self->_ptr, $from, $to);
287 }
288  
289 66 ▶sub pass { _pass($_[0]->_ptr) }
Subroutine pass: called CC=1 SCAR=0.0
Pod documented
290  
291 705 ▶sub resign {
Subroutine resign: called CC=2 SCAR=6.9
Pod documented
292 705 my ($self, $seat) = @_;
293 705 ▶ return (WHY_NOT_A_SEAT) unless $self->_is_seat($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_seat($seat) 1 704 705
294 704 return _resign($self->_ptr, $seat);
295 }
296  
297 193643 ▶sub unplay {
Subroutine unplay: called CC=3 SCAR=11.7
Pod documented
298 193643 my ($self, $token) = @_;
299 193643 ▶ return 0 unless defined $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token 0 193643 193643
300 193643 ▶ return _unplay($self->_ptr, $token) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_unplay($self->_ptr, $token) ? : 193643 0 193643
301 }
302  
303 144 ▶sub name { Game::Sternhalma::Notation::name_of($_[1]) }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
304 121 ▶sub hole_named { Game::Sternhalma::Notation::hole_of($_[1]) }
Subroutine hole_named: called CC=1 SCAR=0.0
Pod documented
305  
306 108033 ▶sub _neighbour {
Subroutine _neighbour: called CC=1 SCAR=0.0
307 108033 my ($p, $q) = @_;
308 108033 return scalar grep { $_ == $q } __PACKAGE__->neighbours($p);
309 }
310  
311 137443 ▶sub move_text {
Subroutine move_text: called CC=4 SCAR=13.9
Pod documented
312 137443 my ($self, $from, $to) = @_;
313 137443 my @p = $self->path($from, $to);
314 137443 ▶ return undef unless @p;
Branch: 1 branch
BranchTrueFalseTotal
unless @p 4 137439 137443
315 137439 ▶ my $kind = @p == 2 && _neighbour(@p) ? 'step' : 'chain';
Branch: 1 branch
BranchTrueFalseTotal
@p == 2 && _neighbour(@p) ? : 79363 58076 137439
Condition100%3/3
@p == 2 and _neighbour(@p)
!ll&&!rl&&r
294062867079363
MC/DC100%2/2
@p == 2 and _neighbour(@p)
@p == 2 _neighbour(@p)
Truth table100%3/3
A and B
A= @p == 2B= _neighbour(@p)
ABresult
0X0
100
111
316 137439 return Game::Sternhalma::Notation::format_move({ kind => $kind, holes => \@p });
317 }
318  
319 52119 ▶sub read_move {
Subroutine read_move: called CC=10 SCAR=23.0
Pod documented
320 52119 my ($self, $text) = @_;
321 52119 ▶ my $m = Game::Sternhalma::Notation::parse_move($text)
Branch: 2 branches
BranchTrueFalseTotal
my $m = Game::Sternhalma::Notation::parse_move($text) or return undef, 'not a move: ' . (defined $text ? $text : "nothing") 52118 1 52119
defined $text ? : 1 0 1
322 or return (undef, 'not a move: ' . (defined $text ? $text : 'nothing'));
323 52118 ▶ return ($m) if $m->{kind} eq 'pass' || $m->{kind} eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"kind"} eq "pass" or $m->{"kind"} eq "resign" 193 51925 52118
Condition66%2/3
$m->{"kind"} eq "pass" or $m->{"kind"} eq "resign"
l!l&&r!l&&!r
019351925
MC/DC50%1/2
$m->{"kind"} eq "pass" or $m->{"kind"} eq "resign"
$m->{"kind"} eq "pass" $m->{"kind"} eq "resign"
Truth table66%2/3
A or B
A= $m->{"kind"} eq "pass"B= $m->{"kind"} eq "resign"
ABresult
1X1
011
000
324 51925 my @h = @{ $m->{holes} };
325 51925 my ($from, $to) = ($h[0], $h[-1]);
326 51925 my $seat = $self->at($from);
327 51925 ▶ return (undef, $self->name($from) . ' holds no piece') unless defined $seat;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat 1 51924 51925
328 51924 ▶ if ($m->{kind} eq 'chain') {
Branch: 1 branch
BranchTrueFalseTotal
if ($m->{"kind"} eq "chain") 20708 31216 51924
329 20708 for my $i (1 .. $#h) {
330 36524 ▶ next if grep { $_ == $h[$i] } $self->hops($h[ $i - 1 ], $from);
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $h[$i];} $self->hops($h[$i - 1], $from) 36523 1 36524
331 1 return (undef, "hop $i: " . $self->name($h[$i]) . ' is not a hop from ' . $self->name($h[ $i - 1 ]));
332 }
333 }
334 return (undef, $self->name($to) . ' is not where ' . $self->name($from) . ' can go')
335 51923 ▶ unless grep { $_ == $to } $self->reach($from);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $to;} $self->reach($from) 1 51922 51923
336 51922 return ({ kind => 'move', from => $from, to => $to, path => [ $self->path($from, $to) ] });
337 }
338  
339 54 my @WEIGHT = qw(distance straggler classes blocking);
340  
341 364708 ▶sub weights_default {
Subroutine weights_default: called CC=1 SCAR=0.0
Pod documented
342 364708 my @w = _weights_default();
343 364708 return { map { $WEIGHT[$_] => $w[$_] } 0 .. $#WEIGHT };
344 }
345  
346 364708 ▶my $weights = sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
347 364708 my ($given) = @_;
348 364708 my $w = __PACKAGE__->weights_default;
349 364708 ▶ if ($given) {
Branch: 1 branch
BranchTrueFalseTotal
if ($given) 362044 2664 364708
350 362044 for my $k (keys %$given) {
351 1448176 ▶ Carp::croak("Game::Sternhalma::Engine: no weight is called '$k'") unless exists $w->{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $w->{$k} 0 1448176 1448176
352 Carp::croak("Game::Sternhalma::Engine: the weight $k is a whole number")
353 1448176 ▶ unless defined $given->{$k} && $given->{$k} =~ /\A-?[0-9]{1,6}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $given->{$k} and $given->{$k} =~ /\A-?[0-9]{1,6}\z/u 0 1448176 1448176
Condition33%1/3
defined $given->{$k} and $given->{$k} =~ /\A-?[0-9]{1,6}\z/u
!ll&&!rl&&r
001448176
MC/DC0%0/2
defined $given->{$k} and $given->{$k} =~ /\A-?[0-9]{1,6}\z/u
defined $given->{$k} $given->{$k} =~ /\A-?[0-9]{1,6}\z/u
Truth table33%1/3
A and B
A= defined $given->{$k}B= $given->{$k} =~ /\A-?[0-9]{1,6}\z/u
ABresult
0X0
100
111
354 1448176 $w->{$k} = $given->{$k};
355 }
356 }
357 364708 return map { $w->{$_} } @WEIGHT;
358 54 };
359  
360 361958 ▶sub evaluate {
Subroutine evaluate: called CC=2 SCAR=7.1
Pod documented
361 361958 my ($self, $seat, %with) = @_;
362 361958 ▶ return undef unless $self->_is_seat($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_seat($seat) 0 361958 361958
363 361958 return _evaluate($self->_ptr, $seat, $weights->($with{weights}));
364 }
365  
366 2750 ▶sub search {
Subroutine search: called CC=13 SCAR=27.8
Pod documented
367 2750 my ($self, %with) = @_;
368 2750 ▶ my $budget = defined $with{budget} ? $with{budget} : 100_000;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'budget'} ? : 2750 0 2750
369 2750 ▶ my $seed = defined $with{seed} ? $with{seed} : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'seed'} ? : 2734 16 2750
370 2750 ▶ my $depth = defined $with{depth} ? $with{depth} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'depth'} ? : 2687 63 2750
371 2750 ▶ my $mode = defined $with{mode} ? $with{mode} : SEARCH_AUTO;
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'mode'} ? : 15 2735 2750
372 2750 for ([ budget => $budget ], [ seed => $seed ], [ depth => $depth ], [ mode => $mode ]) {
373 11000 ▶ Carp::croak("Game::Sternhalma::Engine: $_->[0] is a whole number") unless $_->[1] =~ /\A[0-9]{1,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_->[1] =~ /\A[0-9]{1,15}\z/u 0 11000 11000
374 }
375 2750 my $avoid = $with{avoid};
376 Carp::croak('Game::Sternhalma::Engine: avoid is a list of keys')
377 2750 ▶ if defined $avoid && (ref $avoid ne 'ARRAY' || grep { !defined || !/\A[0-9a-f]{16}\z/ } @$avoid);
Branch: 1 branch
BranchTrueFalseTotal
if defined $avoid and ref $avoid ne "ARRAY" || grep({not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid) 0 2750 2750
Condition44%4/9
defined $avoid and ref $avoid ne "ARRAY" || grep({not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid)
!ll&&!rl&&r
20225480
ref $avoid ne "ARRAY" or grep {not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid
l!l&&r!l&&!r
002548
defined $_ && /\A[0-9a-f]{16}\z/u
!ll&&!rl&&r
0043077
MC/DC0%0/3
defined $avoid and ref $avoid ne "ARRAY" || grep({not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid)
defined $avoid ref $avoid ne "ARRAY" grep {not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid
MC/DC0%0/2
defined $_ && /\A[0-9a-f]{16}\z/u
defined $_ /\A[0-9a-f]{16}\z/u
Truth table50%2/4
A and B or C
A= defined $avoidB= ref $avoid ne "ARRAY"C= grep {not defined $_ && /\A[0-9a-f]{16}\z/u;} @$avoid
ABCresult
0XX0
1000
11X1
1011
Truth table33%1/3
A && B
A= defined $_B= /\A[0-9a-f]{16}\z/u
ABresult
0X0
100
111
378 2750 my @r = _search($self->_ptr, $budget, $seed, $depth, $mode, $weights->($with{weights}), $avoid);
379 2750 ▶ return undef unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 2750 2750
380 2750 ▶ return { from => $r[0], to => $r[1], pass => ($r[0] < 0 ? 1 : 0), nodes => $r[2], depth => $r[3],
Branch: 1 branch
BranchTrueFalseTotal
$r[0] < 0 ? : 0 2750 2750
381 score => $r[4], stopped => $r[5] };
382 }
383  
384 6117 ▶sub own_key {
Subroutine own_key: called CC=2 SCAR=7.1
Pod documented
385 6117 my ($self, $seat) = @_;
386 6117 ▶ return undef unless $self->_is_seat($seat);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_seat($seat) 0 6117 6117
387 6117 return _own_key_hex($self->_ptr, $seat);
388 }
389  
390 1664 ▶sub random_forward {
Subroutine random_forward: called CC=5 SCAR=18.1
Pod documented
391 1664 my ($self, $seed) = @_;
392 1664 ▶ $seed = 1 unless defined $seed && $seed =~ /\A[0-9]{1,15}\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and $seed =~ /\A[0-9]{1,15}\z/u 0 1664 1664
Condition33%1/3
defined $seed and $seed =~ /\A[0-9]{1,15}\z/u
!ll&&!rl&&r
001664
MC/DC0%0/2
defined $seed and $seed =~ /\A[0-9]{1,15}\z/u
defined $seed $seed =~ /\A[0-9]{1,15}\z/u
Truth table33%1/3
A and B
A= defined $seedB= $seed =~ /\A[0-9]{1,15}\z/u
ABresult
0X0
100
111
393 1664 my @r = _random_forward($self->_ptr, $seed);
394 1664 ▶ return undef unless @r;
Branch: 1 branch
BranchTrueFalseTotal
unless @r 0 1664 1664
395 1664 ▶ return { from => $r[0], to => $r[1], pass => ($r[0] < 0 ? 1 : 0) };
Branch: 1 branch
BranchTrueFalseTotal
$r[0] < 0 ? : 0 1664 1664
396 }
397  
398 17702095 ▶sub _is_hole { defined $_[0] && $_[0] =~ /\A[0-9]+\z/ && $_[0] < HOLES }
Subroutine _is_hole: called CC=3 SCAR=11.1
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u and $_[0] < 121 17702091 4 17702095
Condition66%2/3
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0417702091
MC/DC50%1/2
defined $_[0] and $_[0] =~ /\A[0-9]+\z/u
defined $_[0] $_[0] =~ /\A[0-9]+\z/u
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
399  
400 2 ▶sub abi_version { _abi_version() }
Subroutine abi_version: called CC=1 SCAR=0.0
Pod documented
401 7 ▶sub live { _live() }
Subroutine live: called CC=1 SCAR=0.0
Pod documented
402 1 ▶sub tables_ok { _tables_ok() }
Subroutine tables_ok: called CC=1 SCAR=0.0
Pod documented
403 0 ▶sub holes { HOLES }
Subroutine holes: not called CC=1 SCAR=6.9
Pod documented
404  
405 54 ▶sub layout_ok {
Subroutine layout_ok: called CC=6 SCAR=22.0
Pod documented
406 54 my (undef, $seats, $pieces) = @_;
407 54 ▶ return 0 unless defined $seats && defined $pieces
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u 0 54 54
Condition33%3/9
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0054
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u
!ll&&!rl&&r
0054
defined $seats and defined $pieces
!ll&&!rl&&r
0054
MC/DC0%0/4
defined $seats and defined $pieces and $seats =~ /\A[0-9]+\z/u and $pieces =~ /\A[0-9]+\z/u
defined $seats defined $pieces $seats =~ /\A[0-9]+\z/u $pieces =~ /\A[0-9]+\z/u
Truth table20%1/5
A and B and C and D
A= defined $seatsB= defined $piecesC= $seats =~ /\A[0-9]+\z/uD= $pieces =~ /\A[0-9]+\z/u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
408 && $seats =~ /\A[0-9]+\z/ && $pieces =~ /\A[0-9]+\z/;
409 54 ▶ return _layout_ok($seats, $pieces) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
_layout_ok($seats, $pieces) ? : 20 34 54
410 }
411  
412 106 ▶sub layout_point {
Subroutine layout_point: called CC=2 SCAR=6.9
Pod documented
413 106 my (undef, $seats, $seat) = @_;
414 106 my $point = _layout_point($seats, $seat);
415 106 ▶ return $point < 0 ? undef : $point;
Branch: 1 branch
BranchTrueFalseTotal
$point < 0 ? : 4 102 106
416 }
417  
418 2777 ▶sub hole_at {
Subroutine hole_at: called CC=2 SCAR=6.9
Pod documented
419 2777 my (undef, $x, $y) = @_;
420 2777 my $hole = _hole_of($x, $y);
421 2777 ▶ return $hole < 0 ? undef : $hole;
Branch: 1 branch
BranchTrueFalseTotal
$hole < 0 ? : 422 2355 2777
422 }
423  
424 13532 ▶sub xy {
Subroutine xy: called CC=2 SCAR=7.0
Pod documented
425 13532 my (undef, $hole) = @_;
426 13532 ▶ return unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 0 13532 13532
427 13532 my ($x, $y) = (_hole_x($hole), _hole_y($hole));
428 13532 return ($x, $y, -$x - $y);
429 }
430  
431 4612 ▶sub row_of { _is_hole($_[1]) ? _row_of($_[1]) : undef }
Subroutine row_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 4612 0 4612
432 242 ▶sub col_of { _is_hole($_[1]) ? _col_of($_[1]) : undef }
Subroutine col_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 242 0 242
433 34 ▶sub row_len { _row_len($_[1]) }
Subroutine row_len: called CC=1 SCAR=0.0
Pod documented
434 123 ▶sub row_start { my $s = _row_start($_[1]); $s < 0 ? undef : $s }
Subroutine row_start: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$s < 0 ? : 0 123 123
435  
436 3047 ▶sub next_to {
Subroutine next_to: called CC=3 SCAR=11.0
Pod documented
437 3047 my (undef, $hole, $dir) = @_;
438 3047 ▶ return undef unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 1 3046 3047
439 3046 my $to = _next($hole, $dir);
440 3046 ▶ return $to < 0 ? undef : $to;
Branch: 1 branch
BranchTrueFalseTotal
$to < 0 ? : 310 2736 3046
441 }
442  
443 280542 ▶sub neighbours {
Subroutine neighbours: called CC=2 SCAR=7.0
Pod documented
444 280542 my (undef, $hole) = @_;
445 280542 ▶ return unless _is_hole($hole);
Branch: 1 branch
BranchTrueFalseTotal
unless _is_hole($hole) 0 280542 280542
446 280542 return grep { $_ >= 0 } map { _next($hole, $_) } E .. SE;
447 }
448  
449 764 ▶sub degree { _is_hole($_[1]) ? _degree($_[1]) : 0 }
Subroutine degree: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 763 1 764
450 631 ▶sub dir_opposite { my $d = _dir_opposite($_[1]); $d < 0 ? undef : $d }
Subroutine dir_opposite: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$d < 0 ? : 1 630 631
451 56358 ▶sub point_of { _is_hole($_[1]) ? _point_of($_[1]) : undef }
Subroutine point_of: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 56357 1 56358
452 617 ▶sub on_ring { _is_hole($_[1]) ? _on_ring($_[1]) : 0 }
Subroutine on_ring: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 617 0 617
453 437987 ▶sub class_of { _is_hole($_[1]) ? _class_of($_[1]) : undef }
Subroutine class_of: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
_is_hole($_[1]) ? : 437987 0 437987
454 143 ▶sub point_holes { _point_holes($_[1]) }
Subroutine point_holes: called CC=1 SCAR=0.0
Pod documented
455 31 ▶sub front { _front($_[1]) }
Subroutine front: called CC=1 SCAR=0.0
Pod documented
456  
457 1 ▶sub pairs {
Subroutine pairs: called CC=2 SCAR=6.9
Pod documented
458 1 my @flat = _pairs();
459 1 my @pairs;
460 1 ▶ push @pairs, [ splice @flat, 0, 2 ] while @flat;
Branch: 1 branch
BranchTrueFalseTotal
@flat and do { push @pairs, [splice(@flat, 0, 2)] } 312 1 313
461 1 return @pairs;
462 }
463  
464 0 ▶sub zobrist { _zobrist_hex($_[1], $_[2]) }
Subroutine zobrist: not called CC=1 SCAR=6.9
Pod documented
465 0 ▶sub zobrist_move { _zobrist_move_hex($_[1]) }
Subroutine zobrist_move: not called CC=1 SCAR=6.9
Pod documented
466 0 ▶sub zobrist_seats { _zobrist_seats_hex($_[1]) }
Subroutine zobrist_seats: not called CC=1 SCAR=6.9
Pod documented
467  
468 54 1;
469  
470 __END__
« blib/lib/Game/Sternhalma/Layout.pm ↑ Summary blib/lib/Game/Sternhalma/Result.pm »