blib/lib/Game/RoyalUr.pm

filter: Statementstmt 99.3% 312 / 314 Branchbran 91.3% 159 / 174 Conditioncond 69.2% 45 / 65 MC/DCmcdc 55.2% 21 / 38 Subroutinesub 100% 49 / 49 Podpod 96.6% 29 / 30 totaltotal 91.7% 615 / 670 CC 112 SCAR 25.6
CC
112
Cov
93%
replay
36.7
BUILD
16.0
_why_not
15.8
CRAP 12.9
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary blib/lib/Game/RoyalUr/Result.pm »
1 package Game::RoyalUr;
2  
3 81 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 81 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 81 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 81 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 81 ▶use Scalar::Util ();
Subroutine __ANON__: called CC=1 SCAR=0.0
9 81 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 81 ▶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 81 ▶use Game::RoyalUr::Rules;
Subroutine __ANON__: called CC=1 SCAR=0.0
13 81 ▶use Game::RoyalUr::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
14 81 ▶use Game::RoyalUr::Dice ();
Subroutine __ANON__: called CC=1 SCAR=0.0
15 81 ▶use Game::RoyalUr::Notation ();
Subroutine __ANON__: called CC=1 SCAR=0.0
16 81 ▶use Game::RoyalUr::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
17 81 ▶use Game::RoyalUr::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
18  
19 81 our $VERSION = '0.04';
20  
21 81 my $E = 'Game::RoyalUr::Engine';
22 81 my $X = 'Game::RoyalUr::Error';
23  
24 81 my (%SIDE_OF, %ROUTE_OF, %OTHER);
25 81 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
26 81 %SIDE_OF = (light => SIDE_LIGHT, dark => SIDE_DARK);
27 81 %ROUTE_OF = (short => ROUTE_SHORT, long => ROUTE_LONG);
28 81 %OTHER = (light => 'dark', dark => 'light');
29 }
30  
31 81 has _seed => (is => 'ro', init_arg => 'seed', private => 1);
32  
33 81 has _rules_given => (is => 'ro', init_arg => 'rules', private => 1);
34  
35 81 has _first_given => (is => 'ro', init_arg => 'first', private => 1);
36  
37 81 has _position_given => (is => 'ro', init_arg => 'position', private => 1);
38  
39 81 has _script => (is => 'ro', init_arg => 'script', private => 1);
40  
41 81 has _variant => (is => 'rw', private => 1);
42  
43 81 has _game => (is => 'rw', private => 1);
44  
45 81 has _first => (is => 'rw', private => 1);
46  
47 81 has _opening => (is => 'rw', private => 1);
48  
49 81 has _rolls => (is => 'rw', private => 1);
50  
51 81 has _roll => (is => 'rw', private => 1);
52  
53 81 has _faces => (is => 'rw', private => 1);
54  
55 81 has _number => (is => 'rw', private => 1);
56  
57 81 has _log => (is => 'rw', private => 1);
58  
59 81 has _choices => (is => 'rw', private => 1);
60  
61 81 has _resigned => (is => 'rw', private => 1);
62  
63 81 has _error => (is => 'rw', private => 1);
64  
65 1559 ▶sub BUILD {
Subroutine BUILD: called CC=16 SCAR=27.7
Pod undocumented
66 1559 my ($self) = @_;
67 1559 my ($seed, $script) = ($self->_seed, $self->_script);
68 1559 ▶ 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 1557 1559
Condition88%8/9
defined $seed and not ref $seed and length $seed or ref $script eq "ARRAY"
l!l&&r!l&&!r
1464932
defined $seed and not ref $seed and length $seed
!ll&&!rl&&r
9411464
defined $seed and not ref $seed
!ll&&!rl&&r
9401465
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 1557 my $variant = Game::RoyalUr::Variant->of($self->_rules_given);
72 1555 $self->_variant($variant);
73  
74 1555 my $position = $self->_position_given;
75 1555 ▶ if (defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $position) 135 1420 1555
76 135 my $code = Game::RoyalUr::Notation::validate_position($position);
77 135 ▶ Carp::croak("Game::RoyalUr: the position was refused, code $code") if $code;
Branch: 1 branch
BranchTrueFalseTotal
if $code 1 134 135
78 }
79  
80 1554 my $first = $self->_first_given;
81 Carp::croak("Game::RoyalUr: first is 'light' or 'dark'")
82 1554 ▶ if defined $first && !exists $SIDE_OF{$first};
Branch: 1 branch
BranchTrueFalseTotal
if defined $first and not exists $SIDE_OF{$first} 1 1553 1554
Condition100%3/3
defined $first and not exists $SIDE_OF{$first}
!ll&&!rl&&r
9066471
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 1553 my @opening;
85 1553 ▶ if (!defined $first && !defined $position) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $first or defined $position) 791 762 1553
Condition100%3/3
defined $first or defined $position
l!l&&r!l&&!r
647115791
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 791 ▶ Carp::croak('Game::RoyalUr: a game with no seed must be told who moves first') if $script;
Branch: 1 branch
BranchTrueFalseTotal
if $script 0 791 791
87 791 my ($side, $used, $throws) = Game::RoyalUr::Dice::opening_for($seed, $variant->dice);
88 791 $first = $side;
89 791 @opening = map { join '', @$_ } @$throws;
90 }
91  
92 1553 ▶ my $game = Game::RoyalUr::Rules->new(
Branch: 2 branches
BranchTrueFalseTotal
defined $position ? : 134 1419 1553
defined $first ? : 1438 115 1553
93 rules => $variant->as_hash,
94 (defined $position ? (position => $position) : ()),
95 (defined $first ? (first => $first) : ()),
96 );
97 1553 my $consistent = $game->board->consistent($variant->pieces);
98 1553 ▶ undef $game unless $consistent;
Branch: 1 branch
BranchTrueFalseTotal
unless $consistent 1 1552 1553
99 1553 ▶ Carp::croak('Game::RoyalUr: the position does not hold ' . $variant->pieces . ' pieces a side')
Branch: 1 branch
BranchTrueFalseTotal
unless $consistent 1 1552 1553
100 unless $consistent;
101  
102 1552 $self->_game($game);
103 1552 $self->_first($game->side);
104 1552 $self->_opening(\@opening);
105 1552 $self->_rolls(scalar @opening);
106 1552 $self->_log([]);
107 1552 $self->_choices([]);
108 1552 $self->_settle;
109 1552 return;
110 }
111  
112 210021 ▶sub _settle {
Subroutine _settle: called CC=5 SCAR=16.1
113 210021 my ($self) = @_;
114 210021 my $game = $self->_game;
115 210021 my $variant = $self->_variant;
116 210021 $self->_roll(undef);
117 210021 $self->_faces(undef);
118 210021 $self->_number(undef);
119  
120 210021 ▶ while (!$self->is_over) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_over) 217938 1170 219108
121 217938 my $number = $self->_rolls;
122 217938 my ($roll, $faces);
123 217938 ▶ if (my $script = $self->_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $script = $self->_script) { } 133 217805 217938
124 133 my $next = $script->[ $number - @{ $self->_opening } ];
125 133 ▶ return unless $next;
Branch: 1 branch
BranchTrueFalseTotal
unless $next 6 127 133
126 127 ($roll, $faces) = ($next->{roll}, $next->{faces});
127 }
128 else {
129 217805 my $throw = Game::RoyalUr::Dice::throw_for($self->_seed, $number, $variant->dice);
130 217805 $faces = join '', @$throw;
131 217805 $roll = Game::RoyalUr::Dice::roll_of(Game::RoyalUr::Dice::marked($throw), $variant);
132 }
133 217932 $self->_rolls($number + 1);
134  
135 217932 ▶ if ($game->moves($roll)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($game->moves($roll)) 208845 9087 217932
136 208845 $self->_roll($roll);
137 208845 $self->_faces($faces);
138 208845 $self->_number($number);
139 208845 return;
140 }
141  
142 9087 push @{ $self->_log }, {
143 ply => $game->ply, side => $game->side, throw => $number,
144 roll => $roll, faces => $faces, move => undef,
145 };
146 9087 $game->forfeit;
147 }
148 1170 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 79587 ▶my $cell_named = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
158 79587 my ($name) = @_;
159 79587 ▶ return -1 unless Game::RoyalUr::Notation::square_ok($name);
Branch: 1 branch
BranchTrueFalseTotal
unless Game::RoyalUr::Notation::square_ok($name) 1 79586 79587
160 79586 return $E->cell_of(ord(substr $name, 0, 1) - ord('a'), substr($name, 1, 1) - 1);
161 81 };
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 256173 ▶sub play {
Subroutine play: called CC=8 SCAR=20.8
Pod documented
201 256173 my ($self, $what) = @_;
202 256173 $self->_error(undef);
203 256173 ▶ return $self->_refuse('game_over') if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 4 256169 256173
204 256169 ▶ return $self->_refuse('no_roll') unless defined $self->_roll;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_roll 2 256167 256169
205  
206 256167 my ($from, $to);
207 256167 ▶ if (Scalar::Util::blessed($what) && $what->isa('Game::RoyalUr::Move')) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($what) and $what->isa('Game::RoyalUr::Move')) { } 124447 131720 256167
Condition66%2/3
builtin::blessed($what) and $what->isa("Game::RoyalUr::Move")
!ll&&!rl&&r
1317200124447
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 124447 ($from, $to) = ($what->from, $what->to);
209 }
210 else {
211 131720 my ($parsed, $why) = Game::RoyalUr::Notation::parse_move($what);
212 131720 ▶ return $self->_refuse('bad_move', text => $what, why => $why) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 6340 125380 131720
213 125380 ($from, $to) = @{$parsed}{qw(from to)};
214 }
215  
216 249827 my $game = $self->_game;
217 249827 my $roll = $self->_roll;
218 249827 ▶ my ($move) = grep { $_->from eq $from && $_->to eq $to } $game->moves($roll);
Condition100%3/3
$_->from eq $from && $_->to eq $to
!ll&&!rl&&r
5192996523208469
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 249827 ▶ return $self->_refuse($self->_why_not($from, $to), from => $from, to => $to, roll => $roll)
Branch: 1 branch
BranchTrueFalseTotal
unless $move 41358 208469 249827
220 unless $move;
221  
222 208469 push @{ $self->_choices }, {
223 208469 depth => $game->depth, log => scalar @{ $self->_log },
224 rolls => $self->_rolls, roll => $roll, faces => $self->_faces, number => $self->_number,
225 };
226 208469 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 208469 $game->apply($move);
232 208469 $self->_settle;
233 208469 return $move;
234 }
235  
236 75005 ▶sub play_or_die {
Subroutine play_or_die: called CC=2 SCAR=6.9
Pod documented
237 75005 my ($self, $what) = @_;
238 75005 my $move = $self->play($what);
239 75005 ▶ return $move if $move;
Branch: 1 branch
BranchTrueFalseTotal
if $move 75004 1 75005
240 1 my $error = $self->_error;
241 1 Carp::croak('Game::RoyalUr: ' . $error->code . ': ' . $error->message);
242 }
243  
244 642 ▶sub undo {
Subroutine undo: called CC=4 SCAR=13.9
Pod documented
245 642 my ($self) = @_;
246 642 $self->_error(undef);
247 642 ▶ if (defined $self->_resigned) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_resigned) 2 640 642
248 2 $self->_resigned(undef);
249 2 return 1;
250 }
251 640 ▶ my $choice = pop @{ $self->_choices } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $choice = pop @{$self->_choices;} or return 0 639 1 640
252 639 my $game = $self->_game;
253 639 ▶ $game->undo while $game->depth > $choice->{depth};
Branch: 1 branch
BranchTrueFalseTotal
$game->depth > $choice->{"depth"} and do { $game->undo } 672 639 1311
254 639 splice @{ $self->_log }, $choice->{log};
255 639 $self->_rolls($choice->{rolls});
256 639 $self->_roll($choice->{roll});
257 639 $self->_faces($choice->{faces});
258 639 $self->_number($choice->{number});
259 639 return 1;
260 }
261  
262 20 ▶sub resign {
Subroutine resign: called CC=4 SCAR=13.9
Pod documented
263 20 my ($self, $side) = @_;
264 20 $self->_error(undef);
265 20 ▶ return 0 if $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_over 1 19 20
266 19 ▶ $side = $self->_game->side unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 7 12 19
267 19 ▶ 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 18 19
268 18 $self->_resigned($side);
269 18 return 1;
270 }
271  
272 1368854 ▶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 ? : 5267 1363587 1368854
Condition100%3/3
defined $_[0]->_resigned or $_[0]->_game->is_over
l!l&&r!l&&!r
5952081363587
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 4309 ▶sub result {
Subroutine result: called CC=4 SCAR=13.9
Pod documented
275 4309 my ($self) = @_;
276 4309 ▶ return undef unless $self->is_over;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_over 1690 2619 4309
277 2619 my $game = $self->_game;
278 2619 my $resigned = $self->_resigned;
279 return Game::RoyalUr::Result->new(
280 2619 ▶ winner => (defined $resigned ? $OTHER{$resigned} : $game->winner),
Branch: 2 branches
BranchTrueFalseTotal
defined $resigned ? : 30 2589 2619
defined $resigned ? : 30 2589 2619
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 241696 ▶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 ? : 240 241456 241696
291  
292 390363 ▶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 ? : 492 389871 390363
293  
294 47470 ▶sub throw {
Subroutine throw: called CC=3 SCAR=11.0
Pod documented
295 47470 my ($self) = @_;
296 47470 ▶ my $faces = $self->is_over ? undef : $self->_faces;
Branch: 1 branch
BranchTrueFalseTotal
$self->is_over ? : 14 47456 47470
297 47470 ▶ return defined $faces ? [ split //, $faces ] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $faces ? : 47423 47 47470
298 }
299  
300 166686 ▶sub legal {
Subroutine legal: called CC=3 SCAR=11.0
Pod documented
301 166686 my ($self) = @_;
302 166686 my $roll = $self->roll;
303 166686 ▶ return wantarray ? () : 0 unless defined $roll;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $roll 243 166443 166686
wantarray ? : 10 233 243
304 166443 return $self->_game->moves($roll);
305 }
306  
307 53404 ▶sub variant { $_[0]->_variant }
Subroutine variant: called CC=1 SCAR=0.0
Pod documented
308  
309 7747 ▶sub first { $_[0]->_first }
Subroutine first: called CC=1 SCAR=0.0
Pod documented
310  
311 6352 ▶sub seed { $_[0]->_seed }
Subroutine seed: called CC=1 SCAR=0.0
Pod documented
312  
313 7794 ▶sub rolls { $_[0]->_rolls }
Subroutine rolls: called CC=1 SCAR=0.0
Pod documented
314  
315 7021 ▶sub opening { [ map { [ split //, $_ ] } @{ $_[0]->_opening } ] }
Subroutine opening: called CC=1 SCAR=0.0
Pod documented
316  
317 8621 ▶sub ply { $_[0]->_game->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
318  
319 13938 ▶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 7893 ▶sub hand { $_[0]->_game->hand($_[1]) }
Subroutine hand: called CC=1 SCAR=0.0
Pod documented
324  
325 7893 ▶sub home { $_[0]->_game->home($_[1]) }
Subroutine home: called CC=1 SCAR=0.0
Pod documented
326  
327 21897 ▶sub at {
Subroutine at: called CC=5 SCAR=16.1
Pod documented
328 21897 my ($self, $name) = @_;
329 21897 my $cell = $cell_named->($name);
330 21897 ▶ Carp::croak("Game::RoyalUr: there is no square called '" . ($name // 'undef') . "'") if $cell < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cell < 0 1 21896 21897
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 21896 my $piece = $self->_game->board->at($cell);
332 21896 ▶ return $piece == EMPTY ? undef : $piece == piece_of(SIDE_LIGHT) ? 'light' : 'dark';
Branch: 2 branches
BranchTrueFalseTotal
$piece == 0 ? : 16382 5514 21896
$piece == piece_of(0) ? : 3687 1827 5514
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 8820 ▶sub log { [ map { { %$_ } } @{ $_[0]->_log } ] }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
342  
343 6882 ▶sub forfeits_since {
Subroutine forfeits_since: called CC=3 SCAR=11.0
Pod documented
344 6882 my ($self, $ply) = @_;
345 6882 ▶ $ply = 0 unless defined $ply;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ply 0 6882 6882
346 6882 ▶ return map { { %$_ } } grep { !defined $_->{move} && $_->{ply} >= $ply } @{ $self->_log };
Condition100%3/3
!defined($_->{'move'}) && $_->{'ply'} >= $ply
!ll&&!rl&&r
664170115841
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 1825 ▶sub to_record {
Subroutine to_record: called CC=8 SCAR=20.8
Pod documented
350 1825 my ($self) = @_;
351 1825 my $variant = $self->_variant;
352 my %record = (
353 rules => (defined $variant->name ? $variant->name : $variant->as_hash),
354 first => $self->_first,
355 (defined $self->_position_given ? (position => $self->_position_given) : ()),
356 turns => [ map { {
357 side => $_->{side}, roll => $_->{roll}, move => $_->{move},
358 197537 ▶ (defined $_->{faces} ? (faces => $_->{faces}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $_->{'faces'} ? : 197536 1 197537
359 1825 ▶ } } @{ $self->_log } ],
Branch: 2 branches
BranchTrueFalseTotal
defined $variant->name ? : 1813 12 1825
defined $self->_position_given ? : 64 1761 1825
360 );
361 1825 ▶ if (defined $self->_seed) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_seed) 1773 52 1825
362 1773 $record{seed} = unpack 'H*', $self->_seed;
363 1773 ▶ $record{opening} = scalar @{ $self->_opening } if @{ $self->_opening };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->_opening;} 762 1011 1773
364 }
365 1825 ▶ if (my $result = $self->result) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $self->result) 864 961 1825
366 864 ▶ $record{result} = { winner => (defined $result->winner ? $result->winner : 'draw'), how => $result->how };
Branch: 1 branch
BranchTrueFalseTotal
defined $result->winner ? : 864 0 864
367 }
368 1825 return Game::RoyalUr::Notation::format_record(\%record);
369 }
370  
371 653 ▶sub replay {
Subroutine replay: called CC=32 SCAR=36.0
Pod documented
372 653 my ($class, $record, %option) = @_;
373 653 ▶ my $bad = sub { return (undef, $X->of('bad_record', @_)) };
Subroutine __ANON__: called CC=1 SCAR=0.0
374  
375 653 ▶ if (!ref $record) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $record) 639 14 653
376 639 my ($parsed, $problem) = Game::RoyalUr::Notation::parse_record($record);
377 639 ▶ return $bad->(line => $problem->{line}, why => $problem->{error}) unless $parsed;
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed 50 589 639
378 589 $record = $parsed;
379 }
380 603 ▶ my @turns = @{ $record->{turns} || [] };
Condition50%1/2
$record->{'turns'} || []
l!l
6030
MC/DC0%0/1
$record->{'turns'} || []
$record->{'turns'}
Truth table50%1/2
A
A= $record->{'turns'}
Aresult
11
00
381 603 ▶ my $opening = $record->{opening} || 0;
Condition100%2/2
$record->{'opening'} || 0
l!l
292311
MC/DC100%1/1
$record->{'opening'} || 0
$record->{'opening'}
Truth table100%2/2
A
A= $record->{'opening'}
Aresult
11
00
382 603 ▶ my $rules = exists $option{rules} ? $option{rules} : $record->{rules};
Branch: 1 branch
BranchTrueFalseTotal
exists $option{'rules'} ? : 2 601 603
383  
384 603 my $game;
385 603 ▶ if (defined $record->{seed}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $record->{'seed'}) { } 599 4 603
386 $game = $class->new(
387 seed => pack('H*', $record->{seed}),
388 rules => $rules,
389 (defined $record->{position} ? (position => $record->{position}) : ()),
390 599 ▶ ($opening ? () : (first => $record->{first})),
Branch: 2 branches
BranchTrueFalseTotal
defined $record->{'position'} ? : 12 587 599
$opening ? : 292 307 599
391 );
392 return $bad->(ply => 0, why => 'opening')
393 599 ▶ unless $game->first eq $record->{first} && @{ $game->opening } == $opening;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->first eq $record->{"first"} and @{$game->opening;} == $opening 0 599 599
Condition33%1/3
$game->first eq $record->{"first"} and @{$game->opening;} == $opening
!ll&&!rl&&r
00599
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
394 }
395 else {
396 $game = $class->new(
397 53 ▶ script => [ map { { roll => $_->{roll}, faces => $_->{faces} } } @turns ],
Branch: 1 branch
BranchTrueFalseTotal
defined $record->{'position'} ? : 1 3 4
398 rules => $rules,
399 (defined $record->{position} ? (position => $record->{position}) : ()),
400 first => $record->{first},
401 4 );
402 }
403  
404 603 for my $i (0 .. $#turns) {
405 86999 my $turn = $turns[$i];
406 86999 my $made = $game->_log->[$i];
407 86999 ▶ if ($made) {
Branch: 1 branch
BranchTrueFalseTotal
if ($made) 3460 83539 86999
408 3460 ▶ 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 3459 3460
Condition66%2/3
defined $turn->{"move"} or defined $made->{"move"}
l!l&&r!l&&!r
103459
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
409 3459 ▶ return $bad->(ply => $i, why => 'side') unless $made->{side} eq $turn->{side};
Branch: 1 branch
BranchTrueFalseTotal
unless $made->{"side"} eq $turn->{"side"} 0 3459 3459
410 3459 ▶ return $bad->(ply => $i, why => 'roll') unless $made->{roll} == $turn->{roll};
Branch: 1 branch
BranchTrueFalseTotal
unless $made->{"roll"} == $turn->{"roll"} 0 3459 3459
411 3459 next;
412 }
413 83539 ▶ return $bad->(ply => $i, why => 'over') if $game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over 0 83539 83539
414 83539 ▶ return $bad->(ply => $i, why => 'forfeit') unless defined $turn->{move};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn->{"move"} 1 83538 83539
415 83538 ▶ return $bad->(ply => $i, why => 'side') unless $game->side eq $turn->{side};
Branch: 1 branch
BranchTrueFalseTotal
unless $game->side eq $turn->{"side"} 1 83537 83538
416 83537 ▶ 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 83535 83537
Condition66%2/3
defined $game->roll and $game->roll == $turn->{"roll"}
!ll&&!rl&&r
0283535
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
417 83535 ▶ unless ($game->play($turn->{move})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($game->play($turn->{"move"})) 3 83532 83535
418 3 my $error = $game->error;
419 3 return (undef, $X->of($error->code, %{ $error->detail }, ply => $i));
420 }
421 }
422  
423 595 ▶ if (my $result = $record->{result}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $result = $record->{"result"}) 447 148 595
424 447 ▶ if ($result->{how} eq 'resign') {
Branch: 1 branch
BranchTrueFalseTotal
if ($result->{'how'} eq 'resign') { } 7 440 447
425 7 ▶ return $bad->(ply => scalar @turns, why => 'result') if $game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $game->is_over 0 7 7
426 7 $game->resign($OTHER{ $result->{winner} });
427 }
428 else {
429 440 my $ended = $game->result;
430 440 ▶ my $winner = $ended && defined $ended->winner ? $ended->winner : 'draw';
Branch: 1 branch
BranchTrueFalseTotal
$ended && defined $ended->winner ? : 440 0 440
Condition33%1/3
$ended and defined $ended->winner
!ll&&!rl&&r
00440
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
431 return $bad->(ply => scalar @turns, why => 'result')
432 440 ▶ 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 440 440
Condition33%2/6
$ended and $ended->how eq $result->{"how"} and $winner eq $result->{"winner"}
!ll&&!rl&&r
00440
$ended and $ended->how eq $result->{"how"}
!ll&&!rl&&r
00440
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
433 }
434 }
435 595 return ($game, undef);
436 }
437  
438 972 ▶sub to_text { $_[0]->to_record }
Subroutine to_text: called CC=1 SCAR=0.0
Pod documented
439  
440 145 ▶sub from_text {
Subroutine from_text: called CC=9 SCAR=22.4
Pod documented
441 145 my ($class, $text) = @_;
442 145 ▶ Carp::croak('Game::RoyalUr: from_text wants the text to_text wrote, and was given '
Branch: 2 branches
BranchTrueFalseTotal
if not defined $text or ref $text 2 143 145
defined $text ? : 1 1 2
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
11143
MC/DC100%2/2
not defined $text or ref $text
not defined $text ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
443 . (defined $text ? 'a reference' : 'nothing'))
444 if !defined $text || ref $text;
445 143 my ($game, $error) = $class->replay($text);
446 143 ▶ return $game if $game;
Branch: 1 branch
BranchTrueFalseTotal
if $game 137 6 143
447 6 my $detail = $error->detail;
448 6 ▶ my $where = $detail->{line} ? " at line $detail->{line}" : defined $detail->{line} ? '' : ' at turn ' . (($detail->{ply} // 0) + 1);
Branch: 2 branches
BranchTrueFalseTotal
$detail->{'line'} ? : 4 2 6
defined $detail->{'line'} ? : 2 0 2
Condition0%0/2
$detail->{"ply"} // 0
l!l
00
MC/DC0%0/1
$detail->{"ply"} // 0
$detail->{"ply"}
Truth table0%0/2
A
A= $detail->{"ply"}
Aresult
11
00
449 Carp::croak('Game::RoyalUr: the text was refused' . $where . ': ' . $error->message
450 6 ▶ . (defined $detail->{why} ? " ($detail->{why})" : ''));
Branch: 1 branch
BranchTrueFalseTotal
defined $detail->{'why'} ? : 6 0 6
451 }
452  
453 81 1;
454  
455 __END__
« blib/lib/Game/RoyalUr/Terminal.pm ↑ Summary blib/lib/Game/RoyalUr/Result.pm »