blib/lib/Game/Oware.pm

filter: Statementstmt 99.6% 268 / 269 Branchbran 86.8% 125 / 144 Conditioncond 59.7% 49 / 82 MC/DCmcdc 25.5% 11 / 43 Subroutinesub 100% 38 / 38 Podpod 94.7% 18 / 19 totaltotal 85.5% 509 / 595 CC 109 SCAR 26.7
CC
109
Cov
89%
from_text
34.2
replay
23.7
to_text
12.1
CRAP 14.4
↑ Summary blib/lib/Game/Oware/Result.pm »
1 package Game::Oware;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 20 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use Game::Oware::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 20 ▶use Game::Oware::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 20 ▶use Game::Oware::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use Game::Oware::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use Game::Oware::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use Game::Oware::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use Game::Oware::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Game::Oware::Variant ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 20 our $VERSION = '0.04';
20  
21 has variant => (
22 is => 'ro',
23 isa => Str,
24 default => 'abapa'
25 20 );
26  
27 has board => (
28 is => 'rw',
29 isa => ArrayRef
30 20 );
31  
32 20 has turn => (is => 'rw');
33  
34 has status => (
35 is => 'rw',
36 isa => Str,
37 default => 'active'
38 20 );
39  
40 20 has [qw/ winner result /] => (is => 'rw');
41  
42 has log => (
43 is => 'rw',
44 isa => ArrayRef,
45 default => []
46 20 );
47  
48 has no_capture => (
49 is => 'rw',
50 isa => Int,
51 default => 0
52 20 );
53  
54 has seen => (
55 is => 'rw',
56 isa => HashRef,
57 default => {}
58 20 );
59  
60 has _held_seed => (
61 is => 'ro',
62 init_arg => 'seed',
63 private => 1
64 20 );
65  
66 175 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
67 175 my ($self) = @_;
68  
69 175 Game::Oware::Variant::check_variant($self->variant);
70  
71 175 ▶ $self->board(Game::Oware::Board->opening) unless $self->board;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board 122 53 175
72 Carp::croak('Game::Oware: a board is fourteen cells')
73 175 ▶ unless @{ $self->board } == Game::Oware::Board->CELLS;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->board;} == "Game::Oware::Board"->CELLS 0 175 175
74  
75 175 ▶ $self->turn('p1') unless defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->turn 172 3 175
76  
77 $self->_emit('sys', 'start', {
78 variant => $self->variant,
79 first => $self->turn,
80 175 board => join(',', @{ $self->board }),
81 });
82  
83 175 $self->_remember;
84  
85 175 return $self;
86 }
87  
88 21 ▶sub seats { return qw/ p1 p2 / }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
89  
90 50 ▶sub events { return [ @{ $_[0]->log } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
91  
92 5530 ▶sub other { return Game::Oware::Board->other($_[1]) }
Subroutine other: called CC=1 SCAR=0.0
Pod documented
93  
94 97 ▶sub captured { return Game::Oware::Scoring->captured($_[0]->board) }
Subroutine captured: called CC=1 SCAR=0.0
Pod documented
95  
96 2 ▶sub score { return Game::Oware::Scoring->score($_[0]->board, $_[0]->status) }
Subroutine score: called CC=1 SCAR=0.0
Pod documented
97  
98 1 ▶sub scores {
Subroutine scores: called CC=1 SCAR=0.0
Pod documented
99 1 my ($self) = @_;
100 1 my $captured = $self->captured;
101 1 return { map { $_ => { current => $captured->{$_} } } $self->seats };
102 }
103  
104 1 ▶sub places {
Subroutine places: called CC=2 SCAR=8.1
Pod documented
105 1 my ($self) = @_;
106 1 ▶ return undef unless $self->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "finished" 1 0 1
107 0 return $self->result->places;
108 }
109  
110 23 ▶sub seed {
Subroutine seed: called CC=2 SCAR=6.9
Pod documented
111 23 my ($self) = @_;
112 23 ▶ return undef unless $self->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "finished" 13 10 23
113 10 return $self->_held_seed;
114 }
115  
116 15211 ▶sub legal {
Subroutine legal: called CC=4 SCAR=13.9
Pod documented
117 15211 my ($self, $seat) = @_;
118 15211 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 16 15195 15211
119 15195 ▶ return [] unless defined $seat && $seat eq $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq $self->turn 12 15183 15195
Condition66%2/3
defined $seat and $seat eq $self->turn
!ll&&!rl&&r
01215183
MC/DC50%1/2
defined $seat and $seat eq $self->turn
defined $seat $seat eq $self->turn
Truth table66%2/3
A and B
A= defined $seatB= $seat eq $self->turn
ABresult
0X0
100
111
120 15183 return [ Game::Oware::Rules->legal_moves($self->board, $seat, $self->variant) ];
121 }
122  
123 3478 ▶sub waiting_on {
Subroutine waiting_on: called CC=2 SCAR=6.9
Pod documented
124 3478 my ($self) = @_;
125 3478 ▶ return () unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 12 3466 3478
126 3466 return ($self->turn);
127 }
128  
129 5493 ▶sub play {
Subroutine play: called CC=10 SCAR=23.1
Pod documented
130 5493 my ($self, $seat, $house) = @_;
131  
132 5493 ▶ return Game::Oware::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 2 5491 5493
133 unless $self->status eq 'active';
134  
135 5491 ▶ return Game::Oware::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq "p1" || $seat eq "p2" and $seat eq $self->turn 2 5489 5491
Condition55%5/9
defined $seat and $seat eq "p1" || $seat eq "p2" and $seat eq $self->turn
!ll&&!rl&&r
025489
defined $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
005491
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
276527260
MC/DC25%1/4
defined $seat and $seat eq "p1" || $seat eq "p2" and $seat eq $self->turn
defined $seat $seat eq "p1" $seat eq "p2" $seat eq $self->turn
Truth table50%3/6
A and B or C and D
A= defined $seatB= $seat eq "p1"C= $seat eq "p2"D= $seat eq $self->turn
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
136 unless defined $seat && ($seat eq 'p1' || $seat eq 'p2')
137 && $seat eq $self->turn;
138  
139 5489 Game::Oware::Board->assert_house($house);
140  
141 5488 ▶ return Game::Oware::Error->throw('not_your_house')
Branch: 1 branch
BranchTrueFalseTotal
unless "Game::Oware::Board"->owner_of($house) eq $seat 2 5486 5488
142 unless Game::Oware::Board->owner_of($house) eq $seat;
143  
144 5486 ▶ return Game::Oware::Error->throw('empty_house')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->board->[$house] 1 5485 5486
145 unless $self->board->[$house];
146  
147 5485 my $legal = $self->legal($seat);
148 return Game::Oware::Error->throw('must_feed', legal => [ @$legal ])
149 5485 ▶ unless grep { $_ == $house } @$legal;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $house;} @$legal 3 5482 5485
150  
151 5482 my ($next, $move) =
152 Game::Oware::Rules->resolve($self->board, $house, $seat, $self->variant);
153  
154 5482 $self->board($next);
155  
156 5482 ▶ if ($move->taken) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->taken) { } 430 5052 5482
157 430 $self->no_capture(0);
158 430 $self->seen({});
159 }
160 else {
161 5052 $self->no_capture($self->no_capture + 1);
162 }
163  
164 $self->_emit($seat, 'sow', {
165 house => $house,
166 5482 captured => [ @{ $move->captured } ],
167 taken => $move->taken,
168 slammed => $move->slammed,
169 5482 forfeited => [ @{ $move->forfeited } ],
170 });
171  
172 5482 $self->_after($move);
173  
174 5482 return $move;
175 }
176  
177 6 ▶sub timeout {
Subroutine timeout: called CC=5 SCAR=17.8
Pod documented
178 6 my ($self, $seat) = @_;
179 6 ▶ return Game::Oware::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 6 6
180 unless $self->status eq 'active';
181 6 ▶ Carp::croak('Game::Oware: a seat is p1 or p2')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq "p1" || $seat eq "p2" 0 6 6
Condition50%3/6
defined $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
006
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
330
MC/DC0%0/3
defined $seat and $seat eq "p1" || $seat eq "p2"
defined $seat $seat eq "p1" $seat eq "p2"
Truth table50%2/4
A and B or C
A= defined $seatB= $seat eq "p1"C= $seat eq "p2"
ABCresult
0XX0
1000
11X1
1011
182 unless defined $seat && ($seat eq 'p1' || $seat eq 'p2');
183  
184 6 $self->_emit('sys', 'timeout', { p => $seat });
185 6 return $self->_finish($self->other($seat), 'timeout', 'timeout');
186 }
187  
188 4 ▶sub abandon {
Subroutine abandon: called CC=2 SCAR=7.0
Pod documented
189 4 my ($self) = @_;
190 4 ▶ return Game::Oware::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 4 4
191 unless $self->status eq 'active';
192  
193 4 $self->_emit('sys', 'abandon', {});
194 4 return $self->_finish(undef, 'abandoned', 'abandon');
195 }
196  
197 13 ▶sub resign {
Subroutine resign: called CC=5 SCAR=17.0
Pod documented
198 13 my ($self, $seat) = @_;
199 13 ▶ return Game::Oware::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 12 13
200 unless $self->status eq 'active';
201 12 ▶ Carp::croak('Game::Oware: a seat is p1 or p2')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq "p1" || $seat eq "p2" 0 12 12
Condition50%3/6
defined $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
0012
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
1020
MC/DC0%0/3
defined $seat and $seat eq "p1" || $seat eq "p2"
defined $seat $seat eq "p1" $seat eq "p2"
Truth table50%2/4
A and B or C
A= defined $seatB= $seat eq "p1"C= $seat eq "p2"
ABCresult
0XX0
1000
11X1
1011
202 unless defined $seat && ($seat eq 'p1' || $seat eq 'p2');
203  
204 12 $self->_emit($seat, 'resign', {});
205 12 return $self->_finish($self->other($seat), 'resign', 'resign');
206 }
207  
208 11 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
209 11 my ($self) = @_;
210 11 my $copy = Object::Proto::clone($self);
211 11 $copy->board([ @{ $self->board } ]);
212 11 $copy->log([ map { { %$_, payload => { %{ $_->{payload} } } } } @{ $self->log } ]);
213 11 $copy->seen({ %{ $self->seen } });
214 11 return $copy;
215 }
216  
217 485 ▶sub to_text {
Subroutine to_text: called CC=12 SCAR=24.9
Pod documented
218 485 my ($self) = @_;
219 485 my $start = $self->log->[0]{payload};
220 485 my @line;
221 485 ▶ push @line, 'variant ' . $self->variant if $self->variant ne 'abapa';
Branch: 1 branch
BranchTrueFalseTotal
if $self->variant ne "abapa" 4 481 485
222 485 ▶ push @line, 'first ' . $start->{first} if $start->{first} ne 'p1';
Branch: 1 branch
BranchTrueFalseTotal
if $start->{"first"} ne "p1" 4 481 485
223 push @line, 'board ' . $start->{board}
224 485 ▶ if $start->{board} ne join ',', @{ Game::Oware::Board->opening };
Branch: 1 branch
BranchTrueFalseTotal
if $start->{"board"} ne join(",", @{"Game::Oware::Board"->opening;}) 154 331 485
225 485 ▶ if (defined(my $seed = $self->_held_seed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $seed = $self->_held_seed)) 482 3 485
226 482 ▶ utf8::encode($seed) if utf8::is_utf8($seed);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($seed) 0 482 482
227 482 push @line, 'seed ' . unpack 'H*', $seed;
228 }
229 my $transcript = Game::Oware::Notation->render(
230 485 [ map { $_->{payload}{house} } grep { $_->{kind} eq 'sow' } @{ $self->log } ]);
231 485 ▶ push @line, $transcript if length $transcript || @line;
Branch: 1 branch
BranchTrueFalseTotal
if length $transcript or @line 485 0 485
Condition66%2/3
length $transcript or @line
l!l&&r!l&&!r
3701150
MC/DC0%0/2
length $transcript or @line
length $transcript @line
Truth table66%2/3
A or B
A= length $transcriptB= @line
ABresult
1X1
011
000
232 485 for my $event (@{ $self->log }) {
233 16139 ▶ push @line, "resign $event->{actor}" if $event->{kind} eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "resign" 4 16135 16139
234 16139 ▶ push @line, "timeout $event->{payload}{p}" if $event->{kind} eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "timeout" 3 16136 16139
235 16139 ▶ push @line, 'abandon' if $event->{kind} eq 'abandon';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "abandon" 3 16136 16139
236 }
237 485 return join "\n", @line;
238 }
239  
240 38 ▶sub from_text {
Subroutine from_text: called CC=31 SCAR=35.3
Pod documented
241 38 my ($class, $text, %options) = @_;
242 38 ▶ Carp::croak('Game::Oware: a transcript is a string')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 38 38
Condition33%1/3
defined $text and not ref $text
!ll&&!rl&&r
0038
MC/DC0%0/2
defined $text and not ref $text
defined $text not ref $text
Truth table33%1/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
243 unless defined $text && !ref $text;
244  
245 38 my (%setting, @ending, $letters);
246 38 for my $line (split /\n/, $text) {
247 79 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 79 79
248 79 $line =~ s/^\s+|\s+$//g;
249 79 my ($word, $value) = $line =~ /^(variant|first|board|seed|resign|timeout|abandon)\b\s*(.*)$/;
250 79 ▶ if (!defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 30 49 79
251 30 ▶ Carp::croak('Game::Oware: the transcript has moves after its ending') if @ending;
Branch: 1 branch
BranchTrueFalseTotal
if @ending 1 29 30
252 29 $letters .= $line;
253 29 next;
254 }
255 49 ▶ if ($word eq 'resign' || $word eq 'timeout' || $word eq 'abandon') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "resign" or $word eq "timeout" or $word eq "abandon") 10 39 49
Condition100%6/6
$word eq "resign" or $word eq "timeout" or $word eq "abandon"
l!l&&r!l&&!r
8239
$word eq "resign" or $word eq "timeout"
l!l&&r!l&&!r
6241
MC/DC100%3/3
$word eq "resign" or $word eq "timeout" or $word eq "abandon"
$word eq "resign" $word eq "timeout" $word eq "abandon"
Truth table100%4/4
A or B or C
A= $word eq "resign"B= $word eq "timeout"C= $word eq "abandon"
ABCresult
1XX1
01X1
0011
0000
256 10 ▶ Carp::croak("Game::Oware: the transcript has a second ending, '$line'") if @ending;
Branch: 1 branch
BranchTrueFalseTotal
if @ending 0 10 10
257 10 ▶ Carp::croak("Game::Oware: '$line' does not name a seat, p1 or p2")
Branch: 2 branches
BranchTrueFalseTotal
unless $word eq "abandon" ? $value eq "" : $value =~ /^p[12]$/ 1 9 10
$word eq 'abandon' ? : 2 8 10
258 unless $word eq 'abandon' ? $value eq '' : $value =~ /^p[12]$/;
259 9 @ending = ($word, $value);
260 9 next;
261 }
262 Carp::croak("Game::Oware: the transcript says $word twice")
263 39 ▶ if exists $setting{$word};
Branch: 1 branch
BranchTrueFalseTotal
if exists $setting{$word} 0 39 39
264 39 ▶ Carp::croak("Game::Oware: the transcript names its $word after its moves")
Branch: 1 branch
BranchTrueFalseTotal
if defined $letters or @ending 0 39 39
Condition33%1/3
defined $letters or @ending
l!l&&r!l&&!r
0039
MC/DC0%0/2
defined $letters or @ending
defined $letters @ending
Truth table33%1/3
A or B
A= defined $lettersB= @ending
ABresult
1X1
011
000
265 if defined $letters || @ending;
266 39 ▶ if ($word eq 'first') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq 'first') { } 2 37 39
elsif ($word eq 'board') { } 2 35 37
elsif ($word eq 'seed') { } 32 3 35
267 2 ▶ Carp::croak("Game::Oware: the first seat is p1 or p2, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^p[12]$/ 0 2 2
268 unless $value =~ /^p[12]$/;
269 2 $setting{turn} = $value;
270 }
271 elsif ($word eq 'board') {
272 2 ▶ Carp::croak("Game::Oware: a board is fourteen counts of seeds, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^\d+(?:,\d+){13}$/ 0 2 2
273 unless $value =~ /^\d+(?:,\d+){13}$/;
274 2 $setting{board} = [ map { $_ + 0 } split /,/, $value ];
275 }
276 elsif ($word eq 'seed') {
277 32 ▶ Carp::croak("Game::Oware: a seed is written in hexadecimal, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(?:[0-9a-f]{2})*$/ 1 31 32
278 unless $value =~ /^(?:[0-9a-f]{2})*$/;
279 31 $setting{seed} = pack 'H*', $value;
280 }
281 else {
282 3 ▶ Carp::croak("Game::Oware: there is no variant '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless Game::Oware::Variant::is_variant($value) 1 2 3
283 unless Game::Oware::Variant::is_variant($value);
284 2 $setting{variant} = $value;
285 }
286 }
287  
288 34 my $game = $class->new(%options, %setting);
289 34 ▶ for my $house (@{ Game::Oware::Notation->parse($letters // '', $game->turn) }) {
Condition100%2/2
$letters // ""
l!l
277
MC/DC100%1/1
$letters // ""
$letters
Truth table100%2/2
A
A= $letters
Aresult
11
00
290 1861 my $out = $game->play($game->turn, $house);
291 1861 ▶ Carp::croak('Game::Oware: the transcript does not play: ' . $out->stringify)
Branch: 1 branch
BranchTrueFalseTotal
if ref $out and $out->isa("Game::Oware::Error") 0 1861 1861
Condition33%1/3
ref $out and $out->isa("Game::Oware::Error")
!ll&&!rl&&r
018610
MC/DC0%0/2
ref $out and $out->isa("Game::Oware::Error")
ref $out $out->isa("Game::Oware::Error")
Truth table33%1/3
A and B
A= ref $outB= $out->isa("Game::Oware::Error")
ABresult
0X0
100
111
292 if ref $out && $out->isa('Game::Oware::Error');
293 }
294 33 ▶ if (@ending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ending) 8 25 33
295 8 my ($word, $seat) = @ending;
296 8 ▶ my $out = $word eq 'abandon' ? $game->abandon : $game->$word($seat);
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'abandon' ? : 2 6 8
297 8 ▶ Carp::croak("Game::Oware: the transcript ends by $word, but the moves already end the game")
Branch: 1 branch
BranchTrueFalseTotal
if ref $out and $out->isa("Game::Oware::Error") 1 7 8
Condition66%2/3
ref $out and $out->isa("Game::Oware::Error")
!ll&&!rl&&r
071
MC/DC50%1/2
ref $out and $out->isa("Game::Oware::Error")
ref $out $out->isa("Game::Oware::Error")
Truth table66%2/3
A and B
A= ref $outB= $out->isa("Game::Oware::Error")
ABresult
0X0
100
111
298 if ref $out && $out->isa('Game::Oware::Error');
299 }
300 32 return $game;
301 }
302  
303 11 ▶sub replay {
Subroutine replay: called CC=22 SCAR=31.7
Pod documented
304 11 my ($self, $log) = @_;
305  
306 11 ▶ Carp::croak('Game::Oware: a log is an arrayref of events')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $log eq "ARRAY" 0 11 11
307 unless ref $log eq 'ARRAY';
308 Carp::croak('Game::Oware: replay wants a game that has not been played')
309 11 ▶ unless @{ $self->log } == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->log;} == 1 1 10 11
310  
311 10 my @events = @$log;
312 10 my $start = shift @events;
313 Carp::croak('Game::Oware: the log does not open with a start')
314 unless $start && ($start->{actor} // '') eq 'sys'
315 10 ▶ && ($start->{kind} // '') eq 'start';
Branch: 1 branch
BranchTrueFalseTotal
unless $start and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start" 1 9 10
Condition60%6/10
$start and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start"
!ll&&!rl&&r
109
$start and ($start->{"actor"} // "") eq "sys"
!ll&&!rl&&r
019
$start->{"actor"} // ""
l!l
100
$start->{"kind"} // ""
l!l
90
MC/DC0%0/3
$start and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start"
$start $start->{"actor"} $start->{"kind"}
Truth table25%1/4
A and B and C
A= $startB= $start->{"actor"}C= $start->{"kind"}
ABCresult
0XX0
10X0
1100
1111
316 Carp::croak('Game::Oware: the log was made under a different variant')
317 9 ▶ unless ($start->{payload}{variant} // '') eq $self->variant;
Branch: 1 branch
BranchTrueFalseTotal
unless ($start->{"payload"}{"variant"} // "") eq $self->variant 1 8 9
Condition50%1/2
$start->{"payload"}{"variant"} // ""
l!l
90
MC/DC0%0/1
$start->{"payload"}{"variant"} // ""
$start->{"payload"}{"variant"}
Truth table50%1/2
A
A= $start->{"payload"}{"variant"}
Aresult
11
00
318  
319 8 my $began = join ',', @{ $self->board };
320 8 my $logged = $start->{payload}{board};
321 8 ▶ Carp::croak('Game::Oware: the log began from a different position')
Branch: 1 branch
BranchTrueFalseTotal
if defined $logged and $logged ne $began 1 7 8
Condition66%2/3
defined $logged and $logged ne $began
!ll&&!rl&&r
071
MC/DC50%1/2
defined $logged and $logged ne $began
defined $logged $logged ne $began
Truth table66%2/3
A and B
A= defined $loggedB= $logged ne $began
ABresult
0X0
100
111
322 if defined($logged) && $logged ne $began;
323  
324 7 for my $event (@events) {
325 153 ▶ my $actor = $event->{actor} // '';
Condition50%1/2
$event->{"actor"} // ""
l!l
1530
MC/DC0%0/1
$event->{"actor"} // ""
$event->{"actor"}
Truth table50%1/2
A
A= $event->{"actor"}
Aresult
11
00
326 153 ▶ my $kind = $event->{kind} // '';
Condition50%1/2
$event->{"kind"} // ""
l!l
1530
MC/DC0%0/1
$event->{"kind"} // ""
$event->{"kind"}
Truth table50%1/2
A
A= $event->{"kind"}
Aresult
11
00
327  
328 153 ▶ if ($actor eq 'sys') {
Branch: 1 branch
BranchTrueFalseTotal
if ($actor eq "sys") 6 147 153
329 6 ▶ $self->timeout($event->{payload}{p}) if $kind eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "timeout" 1 5 6
330 6 ▶ $self->abandon if $kind eq 'abandon';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "abandon" 0 6 6
331 6 next;
332 }
333  
334 my $out = $kind eq 'resign'
335 ? $self->resign($actor)
336 147 ▶ : $self->play($actor, $event->{payload}{house});
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'resign' ? : 1 146 147
337 147 ▶ Carp::croak('Game::Oware: the log does not replay: ' . $out->stringify)
Branch: 1 branch
BranchTrueFalseTotal
if ref $out and $out->isa("Game::Oware::Error") 1 146 147
Condition66%2/3
ref $out and $out->isa("Game::Oware::Error")
!ll&&!rl&&r
01461
MC/DC50%1/2
ref $out and $out->isa("Game::Oware::Error")
ref $out $out->isa("Game::Oware::Error")
Truth table66%2/3
A and B
A= ref $outB= $out->isa("Game::Oware::Error")
ABresult
0X0
100
111
338 if ref $out && $out->isa('Game::Oware::Error');
339 }
340  
341 6 my $ours = join '|', map { _signature($_) } @{ $self->log };
342 6 my $theirs = join '|', map { _signature($_) } @$log;
343 6 ▶ Carp::croak('Game::Oware: the log does not replay to itself, so it records '
Branch: 1 branch
BranchTrueFalseTotal
unless $ours eq $theirs 3 3 6
344 . 'something this game would not have produced')
345 unless $ours eq $theirs;
346  
347 3 return $self;
348 }
349  
350 5764 ▶sub _emit {
Subroutine _emit: called CC=2 SCAR=7.0
351 5764 my ($self, $actor, $kind, $payload) = @_;
352 5764 ▶ push @{ $self->log },
Condition50%1/2
$payload || {}
l!l
57640
MC/DC0%0/1
$payload || {}
$payload
Truth table50%1/2
A
A= $payload
Aresult
11
00
353 { actor => $actor, kind => $kind, payload => $payload || {} };
354 5764 return;
355 }
356  
357 5636 ▶sub _key {
Subroutine _key: called CC=1 SCAR=0.0
358 5636 my ($self) = @_;
359 5636 return join(',', @{ $self->board }) . ':' . $self->turn;
360 }
361  
362 5636 ▶sub _remember {
Subroutine _remember: called CC=1 SCAR=0.0
363 5636 my ($self) = @_;
364 5636 my $seen = $self->seen;
365 5636 return ++$seen->{ $self->_key };
366 }
367  
368 5482 ▶sub _after {
Subroutine _after: called CC=6 SCAR=17.9
369 5482 my ($self, $move) = @_;
370  
371 5482 my $target = Game::Oware::Scoring->target_reached($self->board);
372 5482 ▶ return $self->_finish($target, 'score', 'target') if $target;
Branch: 1 branch
BranchTrueFalseTotal
if $target 20 5462 5482
373  
374 5462 ▶ return $self->_finish(undef, 'draw', 'even')
Branch: 1 branch
BranchTrueFalseTotal
if "Game::Oware::Scoring"->is_draw($self->board) 1 5461 5462
375 if Game::Oware::Scoring->is_draw($self->board);
376  
377 5461 my $limit = Game::Oware::Variant::plies_without_capture($self->variant);
378 5461 ▶ if ($self->no_capture >= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->no_capture >= $limit) 1 5460 5461
379 1 $self->board(Game::Oware::Scoring->sweep_split($self->board));
380 1 $self->_emit('sys', 'cycle', { why => 'plies', plies => $self->no_capture });
381 1 return $self->_settle('cycle');
382 }
383  
384 5460 $self->turn($self->other($move->seat));
385  
386 5460 my $repeats = $self->_remember;
387 5460 ▶ if ($repeats >= Game::Oware::Variant::repetition_limit($self->variant)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($repeats >= Game::Oware::Variant::repetition_limit($self->variant)) 11 5449 5460
388 11 $self->board(Game::Oware::Scoring->sweep_split($self->board));
389 11 $self->_emit('sys', 'cycle', { why => 'repetition', times => $repeats });
390 11 return $self->_settle('cycle');
391 }
392  
393 5449 ▶ unless (@{ $self->legal($self->turn) }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->legal($self->turn);}) 9 5440 5449
394 9 my $swept = $self->turn;
395 9 $self->board(Game::Oware::Scoring->sweep_to($self->board, $swept));
396 9 $self->_emit('sys', 'sweep', { p => $swept, why => 'no_feed' });
397 9 return $self->_settle('no_feed');
398 }
399  
400 5440 return;
401 }
402  
403 21 ▶sub _settle {
Subroutine _settle: called CC=3 SCAR=11.0
404 21 my ($self, $reason) = @_;
405  
406 21 my $captured = $self->captured;
407 return $self->_finish(undef, 'draw', $reason)
408 21 ▶ if $captured->{p1} == $captured->{p2};
Branch: 1 branch
BranchTrueFalseTotal
if $captured->{"p1"} == $captured->{"p2"} 7 14 21
409  
410 14 ▶ my $winner = $captured->{p1} > $captured->{p2} ? 'p1' : 'p2';
Branch: 1 branch
BranchTrueFalseTotal
$captured->{'p1'} > $captured->{'p2'} ? : 8 6 14
411 14 return $self->_finish($winner, 'score', $reason);
412 }
413  
414 64 ▶sub _finish {
Subroutine _finish: called CC=4 SCAR=13.9
415 64 my ($self, $winner, $result, $reason) = @_;
416  
417 64 $self->status('finished');
418 64 $self->winner($winner);
419  
420 64 my $captured = $self->captured;
421 64 ▶ my $natural = $result eq 'score' || $result eq 'draw';
Condition100%3/3
$result eq 'score' || $result eq 'draw'
l!l&&r!l&&!r
34822
MC/DC100%2/2
$result eq 'score' || $result eq 'draw'
$result eq 'score' $result eq 'draw'
Truth table100%3/3
A || B
A= $result eq 'score'B= $result eq 'draw'
ABresult
1X1
011
000
422  
423 64 ▶ my %places = defined $winner
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 52 12 64
424 ? ($winner => 1, $self->other($winner) => 2)
425 : (p1 => 1, p2 => 1);
426  
427 64 ▶ my $done = Game::Oware::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$natural ? : 42 22 64
428 winner => $winner,
429 result => $result,
430 reason => $reason,
431 captured => $captured,
432 ($natural ? (score => $captured) : ()),
433 places => \%places,
434 );
435  
436 64 $self->result($done);
437 64 $self->_emit('sys', 'game_end', {
438 winner => $winner,
439 result => $result,
440 reason => $reason,
441 captured => $captured,
442 });
443  
444 64 return $done;
445 }
446  
447 292 ▶sub _signature {
Subroutine _signature: called CC=9 SCAR=22.8
448 292 my ($event) = @_;
449 292 ▶ my $payload = $event->{payload} || {};
Condition50%1/2
$event->{'payload'} || {}
l!l
2920
MC/DC0%0/1
$event->{'payload'} || {}
$event->{'payload'}
Truth table50%1/2
A
A= $event->{'payload'}
Aresult
11
00
450 292 my @pairs;
451 292 for my $key (sort keys %$payload) {
452 1406 my $value = $payload->{$key};
453 1406 ▶ $value = join '.', @$value if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 536 870 1406
454 1406 ▶ $value = join ';', map { "$_=" . ($value->{$_} // '') } sort keys %$value
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 6 1400 1406
Condition50%1/2
$value->{$_} // ""
l!l
120
MC/DC0%0/1
$value->{$_} // ""
$value->{$_}
Truth table50%1/2
A
A= $value->{$_}
Aresult
11
00
455 if ref $value eq 'HASH';
456 1406 ▶ push @pairs, $key . '=' . (defined $value ? $value : '');
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1406 0 1406
457 }
458 292 ▶ return ($event->{actor} // '') . ':' . ($event->{kind} // '')
Condition50%2/4
$event->{"actor"} // ""
l!l
2920
$event->{"kind"} // ""
l!l
2920
MC/DC0%0/1
$event->{"actor"} // ""
$event->{"actor"}
MC/DC0%0/1
$event->{"kind"} // ""
$event->{"kind"}
Truth table50%1/2
A
A= $event->{"actor"}
Aresult
11
00
Truth table50%1/2
A
A= $event->{"kind"}
Aresult
11
00
459 . ':' . join(',', @pairs);
460 }
461  
462 20 1;
463  
464 __END__
↑ Summary blib/lib/Game/Oware/Result.pm »