blib/lib/Game/RoyalUr.pm

filter: Statementstmt 99.3% 304 / 306 Branchbran 90.3% 141 / 156 Conditioncond 70.0% 42 / 60 MC/DCmcdc 54.2% 19 / 35 Subroutinesub 100% 47 / 47 Podpod 96.4% 27 / 28 totaltotal 91.7% 580 / 632 CC 101 SCAR 25.2
CC
101
Cov
93%
replay
34.7
BUILD
16.0
_why_not
15.8
CRAP 12.4
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary blib/lib/Game/RoyalUr/Result.pm »
1 package Game::RoyalUr;
2  
3 58 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 58 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 58 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 58 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 58 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 58 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 58 ▶use Game::RoyalUr::Engine qw(EMPTY SIDE_LIGHT SIDE_DARK ROUTE_SHORT ROUTE_LONG piece_of);
Subroutine __ANON__: called CC=1 SCAR=0.0
12 58 ▶use Game::RoyalUr::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 58 ▶use Game::RoyalUr::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 58 ▶use Game::RoyalUr::Dice ();
Subroutine __ANON__: called CC=1 SCAR=0.0
15 58 ▶use Game::RoyalUr::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
16 58 ▶use Game::RoyalUr::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 58 ▶use Game::RoyalUr::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 58 our $VERSION = '0.02';
20  
21 58 my $E = 'Game::RoyalUr::Engine';
22 58 my $X = 'Game::RoyalUr::Error';
23  
24 58 my (%SIDE_OF, %ROUTE_OF, %OTHER);
25 58 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 58 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
27 58 %ROUTE_OF = (short => ROUTE_SHORT, long => ROUTE_LONG);
28 58 %OTHER = (light => 'dark', dark => 'light');
29 }
30  
31 58 has _seed => (is => 'ro', init_arg => 'seed', private => 1);
32  
33 58 has _rules_given => (is => 'ro', init_arg => 'rules', private => 1);
34  
35 58 has _first_given => (is => 'ro', init_arg => 'first', private => 1);
36  
37 58 has _position_given => (is => 'ro', init_arg => 'position', private => 1);
38  
39 58 has _script => (is => 'ro', init_arg => 'script', private => 1);
40  
41 58 has _variant => (is => 'rw', private => 1);
42  
43 58 has _game => (is => 'rw', private => 1);
44  
45 58 has _first => (is => 'rw', private => 1);
46  
47 58 has _opening => (is => 'rw', private => 1);
48  
49 58 has _rolls => (is => 'rw', private => 1);
50  
51 58 has _roll => (is => 'rw', private => 1);
52  
53 58 has _faces => (is => 'rw', private => 1);
54  
55 58 has _number => (is => 'rw', private => 1);
56  
57 58 has _log => (is => 'rw', private => 1);
58  
59 58 has _choices => (is => 'rw', private => 1);
60  
61 58 has _resigned => (is => 'rw', private => 1);
62  
63 58 has _error => (is => 'rw', private => 1);
64  
65 1356 ▶sub BUILD {
Subroutine BUILD: called CC=16 SCAR=27.7
Pod undocumented
66 1356 my ($self) = @_;
67 1356 my ($seed, $script) = ($self->_seed, $self->_script);
68 1356 ▶ Carp::croak('Game::RoyalUr: a game wants a seed: some bytes that decide every throw of its dice')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seed and not ref $seed and length $seed or ref $script eq "ARRAY" 2 1354 1356
Condition88%8/9
defined $seed and not ref $seed and length $seed or ref $script eq "ARRAY"
l!l&&r!l&&!r
1264902
defined $seed and not ref $seed and length $seed
!ll&&!rl&&r
9111264
defined $seed and not ref $seed
!ll&&!rl&&r
9101265
MC/DC75%3/4
defined $seed and not ref $seed and length $seed or ref $script eq "ARRAY"
defined $seed not ref $seed length $seed ref $script eq "ARRAY"
Truth table57%4/7
A and not(B) and C or D
A= defined $seedB= not ref $seedC= length $seedD= ref $script eq "ARRAY"
ABCDresult
111X1
0XX11
10X11
11011
0XX00
10X00
11000
69 unless (defined $seed && !ref $seed && length $seed) || ref $script eq 'ARRAY';
70  
71 1354 my $variant = Game::RoyalUr::Variant->of($self->_rules_given);
72 1352 $self->_variant($variant);
73  
74 1352 my $position = $self->_position_given;
75 1352 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 118 1234 1352
76 118 my $code = Game::RoyalUr::Notation::validate_position($position);
77 118 ▶ Carp::croak("Game::RoyalUr: the position was refused, code $code") if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 1 117 118
78 }
79  
80 1351 my $first = $self->_first_given;
81 Carp::croak("Game::RoyalUr: first is 'light' or 'dark'")
82 1351 ▶ if defined $first && !exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and not exists $SIDE_OF{$first} 1 1350 1351
Condition100%3/3
defined $first and not exists $SIDE_OF{$first}
!ll&&!rl&&r
8115391
MC/DC100%2/2
defined $first and not exists $SIDE_OF{$first}
defined $first not exists $SIDE_OF{$first}
Truth table100%3/3
A and not(B)
A= defined $firstB= not exists $SIDE_OF{$first}
ABresult
0X0
100
111
83  
84 1350 my @opening;
85 1350 ▶ if (!defined $first && !defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first or defined $position) 699 651 1350
Condition100%3/3
defined $first or defined $position
l!l&&r!l&&!r
539112699
MC/DC100%2/2
defined $first or defined $position
defined $first defined $position
Truth table100%3/3
A or B
A= defined $firstB= defined $position
ABresult
1X1
011
000
86 699 ▶ Carp::croak('Game::RoyalUr: a game with no seed must be told who moves first') if $script;
Branch: 1 branch
BranchTrueFalseTotal
if $script 0 699 699
87 699 my ($side, $used, $throws) = Game::RoyalUr::Dice::opening_for($seed, $variant->dice);
88 699 $first = $side;
89 699 @opening = map { join '', @$_ } @$throws;
90 }
91  
92 1350 ▶ my $game = Game::RoyalUr::Rules->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $position ? : 117 1233 1350
defined $first ? : 1238 112 1350
93 rules => $variant->as_hash,
94 (defined $position ? (position => $position) : ()),
95 (defined $first ? (first => $first) : ()),
96 );
97 1350 my $consistent = $game->board->consistent($variant->pieces);
98 1350 ▶ undef $game unless $consistent;
Branch: 1 branch
BranchTrueFalseTotal
unless $consistent 1 1349 1350
99 1350 ▶ Carp::croak('Game::RoyalUr: the position does not hold ' . $variant->pieces . ' pieces a side')
Branch: 1 branch
BranchTrueFalseTotal
unless $consistent 1 1349 1350
100 unless $consistent;
101  
102 1349 $self->_game($game);
103 1349 $self->_first($game->side);
104 1349 $self->_opening(\@opening);
105 1349 $self->_rolls(scalar @opening);
106 1349 $self->_log([]);
107 1349 $self->_choices([]);
108 1349 $self->_settle;
109 1349 return;
110 }
111  
112 204227 ▶sub _settle {
Subroutine _settle: called CC=5 SCAR=16.1
113 204227 my ($self) = @_;
114 204227 my $game = $self->_game;
115 204227 my $variant = $self->_variant;
116 204227 $self->_roll(undef);
117 204227 $self->_faces(undef);
118 204227 $self->_number(undef);
119  
120 204227 ▶ while (!$self->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_over) 211935 1153 213088
121 211935 my $number = $self->_rolls;
122 211935 my ($roll, $faces);
123 211935 ▶ if (my $script = $self->_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $script = $self->_script) { } 130 211805 211935
124 130 my $next = $script->[ $number - @{ $self->_opening } ];
125 130 ▶ return unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 5 125 130
126 125 ($roll, $faces) = ($next->{roll}, $next->{faces});
127 }
128 else {
129 211805 my $throw = Game::RoyalUr::Dice::throw_for($self->_seed, $number, $variant->dice);
130 211805 $faces = join '', @$throw;
131 211805 $roll = Game::RoyalUr::Dice::roll_of(Game::RoyalUr::Dice::marked($throw), $variant);
132 }
133 211930 $self->_rolls($number + 1);
134  
135 211930 ▶ if ($game->moves($roll)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->moves($roll)) 203069 8861 211930
136 203069 $self->_roll($roll);
137 203069 $self->_faces($faces);
138 203069 $self->_number($number);
139 203069 return;
140 }
141  
142 8861 push @{ $self->_log }, {
143 ply => $game->ply, side => $game->side, throw => $number,
144 roll => $roll, faces => $faces, move => undef,
145 };
146 8861 $game->forfeit;
147 }
148 1153 return;
149 }
150  
151 47704 ▶sub _refuse {
Subroutine _refuse: called CC=1 SCAR=0.0
152 47704 my ($self, $code, %detail) = @_;
153 47704 $self->_error($X->of($code, %detail));
154 47704 return undef;
155 }
156  
157 71847 ▶my $cell_named = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
158 71847 my ($name) = @_;
159 71847 ▶ return -1 unless Game::RoyalUr::Notation::square_ok($name);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::RoyalUr::Notation::square_ok($name) 1 71846 71847
160 71846 return $E->cell_of(ord(substr $name, 0, 1) - ord('a'), substr($name, 1, 1) - 1);
161 58 };
162  
163 41358 ▶sub _why_not {
Subroutine _why_not: called CC=15 SCAR=27.6
164 41358 my ($self, $from, $to) = @_;
165 41358 my $game = $self->_game;
166 41358 my $variant = $self->_variant;
167 41358 my $side = $SIDE_OF{ $game->side };
168 41358 my $route = $ROUTE_OF{ $variant->route };
169 41358 my $board = $game->board;
170 41358 my $roll = $self->_roll;
171  
172 41358 my $from_step = 0;
173 41358 ▶ if ($from eq 'hand') {
Branch: 1 branch
BranchTrueFalseTotal
if ($from eq 'hand') { } 1657 39701 41358
174 1657 ▶ return 'no_piece' unless $board->hand($side) > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $board->hand($side) > 0 232 1425 1657
175 }
176 else {
177 39701 my $piece = $board->at($cell_named->($from));
178 39701 ▶ return 'no_piece' if $piece == EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 22185 17516 39701
179 17516 ▶ return 'not_your_piece' if $piece != piece_of($side);
Branch: 1 branch
BranchTrueFalseTotal
if $piece != piece_of($side) 9020 8496 17516
180 8496 $from_step = $E->route_step($route, $side, $cell_named->($from));
181 8496 ▶ return 'no_piece' unless $from_step;
Branch: 1 branch
BranchTrueFalseTotal
unless $from_step 0 8496 8496
182 }
183  
184 9921 my $length = $E->route_len($route);
185 9921 my $lands = $from_step + $roll;
186 9921 ▶ if ($to eq 'home') {
Branch: 1 branch
BranchTrueFalseTotal
if ($to eq "home") 428 9493 9921
187 428 ▶ return 'overshoot' if $lands > $length + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $lands > $length + 1 12 416 428
188 416 ▶ return 'wrong_distance' if $lands < $length + 1;
Branch: 1 branch
BranchTrueFalseTotal
if $lands < $length + 1 416 0 416
189 0 return 'bad_move';
190 }
191  
192 9493 my $cell = $cell_named->($to);
193 9493 ▶ return 'wrong_distance' unless $lands <= $length && $E->route_cell($route, $side, $lands) == $cell;
Branch: 1 branch
BranchTrueFalseTotal
unless $lands <= $length and $E->route_cell($route, $side, $lands) == $cell 9347 146 9493
Condition100%3/3
$lands <= $length and $E->route_cell($route, $side, $lands) == $cell
!ll&&!rl&&r
4408907146
MC/DC100%2/2
$lands <= $length and $E->route_cell($route, $side, $lands) == $cell
$lands <= $length $E->route_cell($route, $side, $lands) == $cell
Truth table100%3/3
A and B
A= $lands <= $lengthB= $E->route_cell($route, $side, $lands) == $cell
ABresult
0X0
100
111
194 146 my $piece = $board->at($cell);
195 146 ▶ return 'own_piece' if $piece == piece_of($side);
Branch: 1 branch
BranchTrueFalseTotal
if $piece == piece_of($side) 144 2 146
196 2 ▶ return 'safe_rosette' if $piece != EMPTY && $variant->safe_rosettes && $E->is_rosette($cell);
Branch: 1 branch
BranchTrueFalseTotal
if $piece != 0 and $variant->safe_rosettes and $E->is_rosette($cell) 2 0 2
Condition33%2/6
$piece != 0 and $variant->safe_rosettes and $E->is_rosette($cell)
!ll&&!rl&&r
002
$piece != 0 and $variant->safe_rosettes
!ll&&!rl&&r
002
MC/DC0%0/3
$piece != 0 and $variant->safe_rosettes and $E->is_rosette($cell)
$piece != 0 $variant->safe_rosettes $E->is_rosette($cell)
Truth table25%1/4
A and B and C
A= $piece != 0B= $variant->safe_rosettesC= $E->is_rosette($cell)
ABCresult
0XX0
10X0
1100
1111
197 0 return 'bad_move';
198 }
199  
200 250582 ▶sub play {
Subroutine play: called CC=8 SCAR=20.8
Pod documented
201 250582 my ($self, $what) = @_;
202 250582 $self->_error(undef);
203 250582 ▶ return $self->_refuse('game_over') if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 4 250578 250582
204 250578 ▶ return $self->_refuse('no_roll') unless defined $self->_roll;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_roll 2 250576 250578
205  
206 250576 my ($from, $to);
207 250576 ▶ if (Scalar::Util::blessed($what) && $what->isa('Game::RoyalUr::Move')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($what) and $what->isa('Game::RoyalUr::Move')) { } 123354 127222 250576
Condition66%2/3
builtin::blessed($what) and $what->isa("Game::RoyalUr::Move")
!ll&&!rl&&r
1272220123354
MC/DC50%1/2
builtin::blessed($what) and $what->isa("Game::RoyalUr::Move")
builtin::blessed($what) $what->isa("Game::RoyalUr::Move")
Truth table66%2/3
A and B
A= builtin::blessed($what)B= $what->isa("Game::RoyalUr::Move")
ABresult
0X0
100
111
208 123354 ($from, $to) = ($what->from, $what->to);
209 }
210 else {
211 127222 my ($parsed, $why) = Game::RoyalUr::Notation::parse_move($what);
212 127222 ▶ return $self->_refuse('bad_move', text => $what, why => $why) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 6340 120882 127222
213 120882 ($from, $to) = @{$parsed}{qw(from to)};
214 }
215  
216 244236 my $game = $self->_game;
217 244236 my $roll = $self->_roll;
218 244236 ▶ my ($move) = grep { $_->from eq $from && $_->to eq $to } $game->moves($roll);
Condition100%3/3
$_->from eq $from && $_->to eq $to
!ll&&!rl&&r
5119016523202878
MC/DC100%2/2
$_->from eq $from && $_->to eq $to
$_->from eq $from $_->to eq $to
Truth table100%3/3
A && B
A= $_->from eq $fromB= $_->to eq $to
ABresult
0X0
100
111
219 244236 ▶ return $self->_refuse($self->_why_not($from, $to), from => $from, to => $to, roll => $roll)
Branch: 1 branch
BranchTrueFalseTotal
unless $move 41358 202878 244236
220 unless $move;
221  
222 202878 push @{ $self->_choices }, {
223 202878 depth => $game->depth, log => scalar @{ $self->_log },
224 rolls => $self->_rolls, roll => $roll, faces => $self->_faces, number => $self->_number,
225 };
226 202878 push @{ $self->_log }, {
227 ply => $game->ply, side => $game->side, throw => $self->_number,
228 roll => $roll, faces => $self->_faces, move => $from . '-' . $to,
229 captures => $move->captures, rosette => $move->rosette, home => $move->home,
230 };
231 202878 $game->apply($move);
232 202878 $self->_settle;
233 202878 return $move;
234 }
235  
236 73922 ▶sub play_or_die {
Subroutine play_or_die: called CC=2 SCAR=6.9
Pod documented
237 73922 my ($self, $what) = @_;
238 73922 my $move = $self->play($what);
239 73922 ▶ return $move if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 73921 1 73922
240 1 my $error = $self->_error;
241 1 Carp::croak('Game::RoyalUr: ' . $error->code . ': ' . $error->message);
242 }
243  
244 646 ▶sub undo {
Subroutine undo: called CC=4 SCAR=13.9
Pod documented
245 646 my ($self) = @_;
246 646 $self->_error(undef);
247 646 ▶ if (defined $self->_resigned) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_resigned) 2 644 646
248 2 $self->_resigned(undef);
249 2 return 1;
250 }
251 644 ▶ my $choice = pop @{ $self->_choices } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $choice = pop @{$self->_choices;} or return 0 642 2 644
252 642 my $game = $self->_game;
253 642 ▶ $game->undo while $game->depth > $choice->{depth};
Branch: 1 branch
BranchTrueFalseTotal
$game->depth > $choice->{"depth"} and do { $game->undo } 679 642 1321
254 642 splice @{ $self->_log }, $choice->{log};
255 642 $self->_rolls($choice->{rolls});
256 642 $self->_roll($choice->{roll});
257 642 $self->_faces($choice->{faces});
258 642 $self->_number($choice->{number});
259 642 return 1;
260 }
261  
262 15 ▶sub resign {
Subroutine resign: called CC=4 SCAR=13.9
Pod documented
263 15 my ($self, $side) = @_;
264 15 $self->_error(undef);
265 15 ▶ return 0 if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 14 15
266 14 ▶ $side = $self->_game->side unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 7 7 14
267 14 ▶ Carp::croak("Game::RoyalUr: a side is 'light' or 'dark'") unless exists $SIDE_OF{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $SIDE_OF{$side} 1 13 14
268 13 $self->_resigned($side);
269 13 return 1;
270 }
271  
272 1331533 ▶sub is_over { defined $_[0]->_resigned || $_[0]->_game->is_over ? 1 : 0 }
Subroutine is_over: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]->_resigned || $_[0]->_game->is_over ? : 5134 1326399 1331533
Condition100%3/3
defined $_[0]->_resigned or $_[0]->_game->is_over
l!l&&r!l&&!r
2351111326399
MC/DC100%2/2
defined $_[0]->_resigned or $_[0]->_game->is_over
defined $_[0]->_resigned $_[0]->_game->is_over
Truth table100%3/3
A or B
A= defined $_[0]->_resignedB= $_[0]->_game->is_over
ABresult
1X1
011
000
273  
274 2932 ▶sub result {
Subroutine result: called CC=4 SCAR=13.9
Pod documented
275 2932 my ($self) = @_;
276 2932 ▶ return undef unless $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_over 356 2576 2932
277 2576 my $game = $self->_game;
278 2576 my $resigned = $self->_resigned;
279 return Game::RoyalUr::Result->new(
280 2576 ▶ winner => (defined $resigned ? $OTHER{$resigned} : $game->winner),
Branch: 2 branches
BranchTrueFalseTotal
defined $resigned ? : 17 2559 2576
defined $resigned ? : 17 2559 2576
281 how => (defined $resigned ? 'resign' : $game->how),
282 final => $game->position,
283 home => { light => $game->home('light'), dark => $game->home('dark') },
284 plies => $game->ply,
285 );
286 }
287  
288 41397 ▶sub error { $_[0]->_error }
Subroutine error: called CC=1 SCAR=0.0
Pod documented
289  
290 236378 ▶sub side { $_[0]->is_over ? undef : $_[0]->_game->side }
Subroutine side: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_over ? : 232 236146 236378
291  
292 378394 ▶sub roll { $_[0]->is_over ? undef : $_[0]->_roll }
Subroutine roll: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_over ? : 470 377924 378394
293  
294 46693 ▶sub throw {
Subroutine throw: called CC=3 SCAR=11.1
Pod documented
295 46693 my ($self) = @_;
296 46693 ▶ my $faces = $self->is_over ? undef : $self->_faces;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_over ? : 0 46693 46693
297 46693 ▶ return defined $faces ? [ split //, $faces ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $faces ? : 46662 31 46693
298 }
299  
300 165187 ▶sub legal {
Subroutine legal: called CC=3 SCAR=11.0
Pod documented
301 165187 my ($self) = @_;
302 165187 my $roll = $self->roll;
303 165187 ▶ return wantarray ? () : 0 unless defined $roll;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $roll 235 164952 165187
wantarray ? : 2 233 235
304 164952 return $self->_game->moves($roll);
305 }
306  
307 52440 ▶sub variant { $_[0]->_variant }
Subroutine variant: called CC=1 SCAR=0.0
Pod documented
308  
309 7600 ▶sub first { $_[0]->_first }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
310  
311 6338 ▶sub seed { $_[0]->_seed }
Subroutine seed: called CC=1 SCAR=0.0
Pod documented
312  
313 7594 ▶sub rolls { $_[0]->_rolls }
Subroutine rolls: called CC=1 SCAR=0.0
Pod documented
314  
315 6840 ▶sub opening { [ map { [ split //, $_ ] } @{ $_[0]->_opening } ] }
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
316  
317 8334 ▶sub ply { $_[0]->_game->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
318  
319 13340 ▶sub position { $_[0]->_game->position }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
320  
321 7554 ▶sub key { $_[0]->_game->key }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
322  
323 7119 ▶sub hand { $_[0]->_game->hand($_[1]) }
Subroutine hand: called CC=1 SCAR=0.0
Pod documented
324  
325 7119 ▶sub home { $_[0]->_game->home($_[1]) }
Subroutine home: called CC=1 SCAR=0.0
Pod documented
326  
327 14157 ▶sub at {
Subroutine at: called CC=5 SCAR=16.1
Pod documented
328 14157 my ($self, $name) = @_;
329 14157 my $cell = $cell_named->($name);
330 14157 ▶ Carp::croak("Game::RoyalUr: there is no square called '" . ($name // 'undef') . "'") if $cell < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cell < 0 1 14156 14157
Condition50%1/2
$name // "undef"
l!l
10
MC/DC0%0/1
$name // "undef"
$name
Truth table50%1/2
A
A= $name
Aresult
11
00
331 14156 my $piece = $self->_game->board->at($cell);
332 14156 ▶ return $piece == EMPTY ? undef : $piece == piece_of(SIDE_LIGHT) ? 'light' : 'dark';
Branch: 2 branches
BranchTrueFalseTotal
$piece == 0 ? : 10338 3818 14156
$piece == piece_of(0) ? : 2410 1408 3818
333 }
334  
335 6336 ▶sub chances {
Subroutine chances: called CC=1 SCAR=0.0
Pod documented
336 6336 my ($self) = @_;
337 6336 my $variant = $self->_variant;
338 6336 return $E->chances($variant->dice, $variant->zero_rolls);
339 }
340  
341 8168 ▶sub log { [ map { { %$_ } } @{ $_[0]->_log } ] }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
342  
343 6601 ▶sub forfeits_since {
Subroutine forfeits_since: called CC=3 SCAR=11.0
Pod documented
344 6601 my ($self, $ply) = @_;
345 6601 ▶ $ply = 0 unless defined $ply;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ply 0 6601 6601
346 6601 ▶ return map { { %$_ } } grep { !defined $_->{move} && $_->{ply} >= $ply } @{ $self->_log };
Condition100%3/3
!defined($_->{'move'}) && $_->{'ply'} >= $ply
!ll&&!rl&&r
627519115839
MC/DC100%2/2
!defined($_->{'move'}) && $_->{'ply'} >= $ply
!defined($_->{'move'}) $_->{'ply'} >= $ply
Truth table100%3/3
not(A) && B
A= !defined($_->{'move'})B= $_->{'ply'} >= $ply
ABresult
0X0
100
111
347 }
348  
349 854 ▶sub to_record {
Subroutine to_record: called CC=7 SCAR=19.5
Pod documented
350 854 my ($self) = @_;
351 854 my $variant = $self->_variant;
352 my %record = (
353 rules => (defined $variant->name ? $variant->name : $variant->as_hash),
354 first => $self->_first,
355 turns => [ map { {
356 side => $_->{side}, roll => $_->{roll}, move => $_->{move},
357 159220 ▶ (defined $_->{faces} ? (faces => $_->{faces}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'faces'} ? : 159220 0 159220
358 854 ▶ } } @{ $self->_log } ],
Branch: 1 branch
BranchTrueFalseTotal
defined $variant->name ? : 849 5 854
359 );
360 854 ▶ if (defined $self->_seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_seed) 853 1 854
361 853 $record{seed} = unpack 'H*', $self->_seed;
362 853 ▶ $record{opening} = scalar @{ $self->_opening } if @{ $self->_opening };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_opening;} 416 437 853
363 }
364 854 ▶ if (my $result = $self->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->result) 846 8 854
365 846 ▶ $record{result} = { winner => (defined $result->winner ? $result->winner : 'draw'), how => $result->how };
Branch: 1 branch
BranchTrueFalseTotal
defined $result->winner ? : 846 0 846
366 }
367 854 return Game::RoyalUr::Notation::format_record(\%record);
368 }
369  
370 498 ▶sub replay {
Subroutine replay: called CC=30 SCAR=35.5
Pod documented
371 498 my ($class, $record, %option) = @_;
372 498 ▶ my $bad = sub { return (undef, $X->of('bad_record', @_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
373  
374 498 ▶ if (!ref $record) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $record) 492 6 498
375 492 my ($parsed, $problem) = Game::RoyalUr::Notation::parse_record($record);
376 492 ▶ return $bad->(line => $problem->{line}, why => $problem->{error}) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 43 449 492
377 449 $record = $parsed;
378 }
379 455 ▶ my @turns = @{ $record->{turns} || [] };
Condition50%1/2
$record->{'turns'} || []
l!l
4550
MC/DC0%0/1
$record->{'turns'} || []
$record->{'turns'}
Truth table50%1/2
A
A= $record->{'turns'}
Aresult
11
00
380 455 ▶ my $opening = $record->{opening} || 0;
Condition100%2/2
$record->{'opening'} || 0
l!l
214241
MC/DC100%1/1
$record->{'opening'} || 0
$record->{'opening'}
Truth table100%2/2
A
A= $record->{'opening'}
Aresult
11
00
381 455 ▶ my $rules = exists $option{rules} ? $option{rules} : $record->{rules};
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'rules'} ? : 2 453 455
382  
383 455 my $game;
384 455 ▶ if (defined $record->{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $record->{'seed'}) { } 452 3 455
385 $game = $class->new(
386 seed => pack('H*', $record->{seed}),
387 rules => $rules,
388 452 ▶ ($opening ? () : (first => $record->{first})),
Branch: 1 branch
BranchTrueFalseTotal
$opening ? : 214 238 452
389 );
390 return $bad->(ply => 0, why => 'opening')
391 452 ▶ unless $game->first eq $record->{first} && @{ $game->opening } == $opening;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->first eq $record->{"first"} and @{$game->opening;} == $opening 0 452 452
Condition33%1/3
$game->first eq $record->{"first"} and @{$game->opening;} == $opening
!ll&&!rl&&r
00452
MC/DC0%0/2
$game->first eq $record->{"first"} and @{$game->opening;} == $opening
$game->first eq $record->{"first"} @{$game->opening;} == $opening
Truth table33%1/3
A and B
A= $game->first eq $record->{"first"}B= @{$game->opening;} == $opening
ABresult
0X0
100
111
392 }
393 else {
394 $game = $class->new(
395 53 script => [ map { { roll => $_->{roll}, faces => $_->{faces} } } @turns ],
396 rules => $rules,
397 first => $record->{first},
398 3 );
399 }
400  
401 455 for my $i (0 .. $#turns) {
402 82456 my $turn = $turns[$i];
403 82456 my $made = $game->_log->[$i];
404 82456 ▶ if ($made) {
Branch: 1 branch
BranchTrueFalseTotal
if ($made) 3322 79134 82456
405 3322 ▶ return $bad->(ply => $i, why => 'move') if defined $turn->{move} || defined $made->{move};
Branch: 1 branch
BranchTrueFalseTotal
if defined $turn->{"move"} or defined $made->{"move"} 1 3321 3322
Condition66%2/3
defined $turn->{"move"} or defined $made->{"move"}
l!l&&r!l&&!r
103321
MC/DC50%1/2
defined $turn->{"move"} or defined $made->{"move"}
defined $turn->{"move"} defined $made->{"move"}
Truth table66%2/3
A or B
A= defined $turn->{"move"}B= defined $made->{"move"}
ABresult
1X1
011
000
406 3321 ▶ return $bad->(ply => $i, why => 'side') unless $made->{side} eq $turn->{side};
Branch: 1 branch
BranchTrueFalseTotal
unless $made->{"side"} eq $turn->{"side"} 0 3321 3321
407 3321 ▶ return $bad->(ply => $i, why => 'roll') unless $made->{roll} == $turn->{roll};
Branch: 1 branch
BranchTrueFalseTotal
unless $made->{"roll"} == $turn->{"roll"} 0 3321 3321
408 3321 next;
409 }
410 79134 ▶ return $bad->(ply => $i, why => 'over') if $game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over 0 79134 79134
411 79134 ▶ return $bad->(ply => $i, why => 'forfeit') unless defined $turn->{move};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn->{"move"} 1 79133 79134
412 79133 ▶ return $bad->(ply => $i, why => 'side') unless $game->side eq $turn->{side};
Branch: 1 branch
BranchTrueFalseTotal
unless $game->side eq $turn->{"side"} 1 79132 79133
413 79132 ▶ return $bad->(ply => $i, why => 'roll') unless defined $game->roll && $game->roll == $turn->{roll};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $game->roll and $game->roll == $turn->{"roll"} 2 79130 79132
Condition66%2/3
defined $game->roll and $game->roll == $turn->{"roll"}
!ll&&!rl&&r
0279130
MC/DC50%1/2
defined $game->roll and $game->roll == $turn->{"roll"}
defined $game->roll $game->roll == $turn->{"roll"}
Truth table66%2/3
A and B
A= defined $game->rollB= $game->roll == $turn->{"roll"}
ABresult
0X0
100
111
414 79130 ▶ unless ($game->play($turn->{move})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->play($turn->{"move"})) 3 79127 79130
415 3 my $error = $game->error;
416 3 return (undef, $X->of($error->code, %{ $error->detail }, ply => $i));
417 }
418 }
419  
420 447 ▶ if (my $result = $record->{result}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $record->{"result"}) 442 5 447
421 442 ▶ if ($result->{how} eq 'resign') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'how'} eq 'resign') { } 5 437 442
422 5 ▶ return $bad->(ply => scalar @turns, why => 'result') if $game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over 0 5 5
423 5 $game->resign($OTHER{ $result->{winner} });
424 }
425 else {
426 437 my $ended = $game->result;
427 437 ▶ my $winner = $ended && defined $ended->winner ? $ended->winner : 'draw';
Branch: 1 branch
BranchTrueFalseTotal
$ended && defined $ended->winner ? : 437 0 437
Condition33%1/3
$ended and defined $ended->winner
!ll&&!rl&&r
00437
MC/DC0%0/2
$ended and defined $ended->winner
$ended defined $ended->winner
Truth table33%1/3
A and B
A= $endedB= defined $ended->winner
ABresult
0X0
100
111
428 return $bad->(ply => scalar @turns, why => 'result')
429 437 ▶ unless $ended && $ended->how eq $result->{how} && $winner eq $result->{winner};
Branch: 1 branch
BranchTrueFalseTotal
unless $ended and $ended->how eq $result->{"how"} and $winner eq $result->{"winner"} 0 437 437
Condition33%2/6
$ended and $ended->how eq $result->{"how"} and $winner eq $result->{"winner"}
!ll&&!rl&&r
00437
$ended and $ended->how eq $result->{"how"}
!ll&&!rl&&r
00437
MC/DC0%0/3
$ended and $ended->how eq $result->{"how"} and $winner eq $result->{"winner"}
$ended $ended->how eq $result->{"how"} $winner eq $result->{"winner"}
Truth table25%1/4
A and B and C
A= $endedB= $ended->how eq $result->{"how"}C= $winner eq $result->{"winner"}
ABCresult
0XX0
10X0
1100
1111
430 }
431 }
432 447 return ($game, undef);
433 }
434  
435 58 1;
436  
437 __END__
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary blib/lib/Game/RoyalUr/Result.pm »