Reversi.pm

filter: Statementstmt 98.4% 254 / 258 Branchbran 89.7% 149 / 166 Conditioncond 58.1% 61 / 105 MC/DCmcdc 29.8% 17 / 57 Subroutinesub 97.3% 36 / 37 Podpod 95.2% 20 / 21 totaltotal 83.3% 537 / 644 CC 126 SCAR 26.7
CC
126
Cov
88%
from_text
35.1
replay
24.7
to_text
13.1
CRAP 14.5
« Reversi/Terminal.pm ↑ Summary Reversi/Move.pm »
1 package Game::Reversi;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 14 ▶use Game::Reversi::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use Game::Reversi::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Game::Reversi::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Game::Reversi::Opening;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 14 ▶use Game::Reversi::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 14 ▶use Game::Reversi::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 14 ▶use Game::Reversi::Scoring;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use Game::Reversi::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 14 our $VERSION = '0.04';
20  
21 has variant => (
22 is => 'ro',
23 isa => Str,
24 default => 'historic'
25 14 );
26  
27 has turn => (
28 is => 'rw'
29 14 );
30  
31 has board => (
32 is => 'rw',
33 isa => ArrayRef
34 14 );
35  
36 has status => (
37 is => 'rw',
38 isa => Str,
39 default => 'active'
40 14 );
41  
42 has [qw/winner result/] => (
43 is => 'rw'
44 14 );
45  
46 has log => (
47 is => 'rw',
48 isa => ArrayRef,
49 default => []
50 14 );
51  
52 has _held_seed => (
53 is => 'ro',
54 init_arg => 'seed',
55 private => 1
56 14 );
57  
58 511 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=22.0
Pod undocumented
59 511 my ($self) = @_;
60 511 my $variant = $self->variant;
61 511 ▶ Carp::croak("Game::Reversi: there is no variant '$variant'")
Branch: 1 branch
BranchTrueFalseTotal
unless "Game::Reversi::Opening"->describes($variant) 1 510 511
62 unless Game::Reversi::Opening->describes($variant);
63  
64 510 ▶ my $given = defined $self->board ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->board ? : 18 492 510
65 510 ▶ $self->board($given ? [ @{ $self->board } ]
Branch: 1 branch
BranchTrueFalseTotal
$given ? : 18 492 510
66 : Game::Reversi::Opening->board_for($variant));
67 Carp::croak('Game::Reversi: a board is an arrayref of 64 cells')
68 510 ▶ unless @{ $self->board } == 64;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->board;} == 64 0 510 510
69  
70 510 ▶ my $first = defined $self->turn ? $self->turn : Game::Reversi::Opening->first;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->turn ? : 19 491 510
71 510 $self->turn(Game::Reversi::Rules->opening_turn($self->board, $first));
72 510 $self->log([]);
73  
74 $self->_emit('sys', 'start', {
75 variant => $variant,
76 first => $first,
77 510 ▶ ($given ? (board => join '', map { defined $_ ? $_ : '.' } @{ $self->board }) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$given ? : 18 492 510
defined $_ ? : 133 1019 1152
78 });
79  
80 510 ▶ $self->_finish unless defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->turn 1 509 510
81 510 return $self;
82 }
83  
84 16 ▶sub from_board {
Subroutine from_board: called CC=3 SCAR=12.5
Pod documented
85 16 my ($class, $board, %o) = @_;
86 16 ▶ Carp::croak('Game::Reversi: a board is an arrayref of 64 cells')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $board eq "ARRAY" and @$board == 64 0 16 16
Condition33%1/3
ref $board eq "ARRAY" and @$board == 64
!ll&&!rl&&r
0016
MC/DC0%0/2
ref $board eq "ARRAY" and @$board == 64
ref $board eq "ARRAY" @$board == 64
Truth table33%1/3
A and B
A= ref $board eq "ARRAY"B= @$board == 64
ABresult
0X0
100
111
87 unless ref $board eq 'ARRAY' && @$board == 64;
88 16 return $class->new(%o, board => $board);
89 }
90  
91 415 ▶sub seats { return ('b', 'w') }
Subroutine seats: called CC=1 SCAR=0.0
Pod documented
92 397 ▶sub events { return [ @{ $_[0]->log } ] }
Subroutine events: called CC=1 SCAR=0.0
Pod documented
93  
94 2 ▶sub places {
Subroutine places: called CC=2 SCAR=6.9
Pod documented
95 2 my ($self) = @_;
96 2 ▶ return undef unless $self->result;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result 1 1 2
97 1 return $self->result->places;
98 }
99  
100 1495 ▶sub phase {
Subroutine phase: called CC=2 SCAR=6.9
Pod documented
101 1495 my ($self) = @_;
102 1495 ▶ return Game::Reversi::Rules->in_opening($self->board) ? 'opening' : 'play';
Branch: 1 branch
BranchTrueFalseTotal
'Game::Reversi::Rules'->in_opening($self->board) ? : 175 1320 1495
103 }
104  
105 22 ▶sub seed {
Subroutine seed: called CC=2 SCAR=6.9
Pod documented
106 22 my ($self) = @_;
107 22 ▶ return undef unless $self->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "finished" 14 8 22
108 8 return $self->_held_seed;
109 }
110  
111 702 ▶sub counts { return Game::Reversi::Scoring->count($_[0]->board) }
Subroutine counts: called CC=1 SCAR=0.0
Pod documented
112  
113 9 ▶sub score {
Subroutine score: called CC=3 SCAR=11.0
Pod documented
114 9 my ($self) = @_;
115 9 ▶ return undef unless $self->result && $self->result->natural;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->result and $self->result->natural 2 7 9
Condition100%3/3
$self->result and $self->result->natural
!ll&&!rl&&r
117
MC/DC100%2/2
$self->result and $self->result->natural
$self->result $self->result->natural
Truth table100%3/3
A and B
A= $self->resultB= $self->result->natural
ABresult
0X0
100
111
116 7 return $self->result->score;
117 }
118  
119 0 ▶sub scores {
Subroutine scores: not called CC=1 SCAR=6.9
Pod documented
120 0 my ($self) = @_;
121 0 my $count = $self->counts;
122 0 return { map { $_ => { current => $count->{$_} } } $self->seats };
123 }
124  
125 22336 ▶sub legal {
Subroutine legal: called CC=5 SCAR=16.6
Pod documented
126 22336 my ($self, $colour) = @_;
127 22336 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 20 22316 22336
128 22316 ▶ return [] unless defined $colour && defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and defined $self->turn 0 22316 22316
Condition33%1/3
defined $colour and defined $self->turn
!ll&&!rl&&r
0022316
MC/DC0%0/2
defined $colour and defined $self->turn
defined $colour defined $self->turn
Truth table33%1/3
A and B
A= defined $colourB= defined $self->turn
ABresult
0X0
100
111
129 22316 ▶ return [] unless $colour eq $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour eq $self->turn 14 22302 22316
130 22302 return [ Game::Reversi::Rules->legal($self->board, $colour) ];
131 }
132  
133 229 ▶sub waiting_on {
Subroutine waiting_on: called CC=3 SCAR=11.0
Pod documented
134 229 my ($self) = @_;
135 229 ▶ return () unless $self->status eq 'active' && defined $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" and defined $self->turn 12 217 229
Condition66%2/3
$self->status eq "active" and defined $self->turn
!ll&&!rl&&r
120217
MC/DC50%1/2
$self->status eq "active" and defined $self->turn
$self->status eq "active" defined $self->turn
Truth table66%2/3
A and B
A= $self->status eq "active"B= defined $self->turn
ABresult
0X0
100
111
136 217 return ($self->turn);
137 }
138  
139 22559 ▶sub play {
Subroutine play: called CC=12 SCAR=25.4
Pod documented
140 22559 my ($self, $colour, $square) = @_;
141  
142 22559 ▶ return Game::Reversi::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 6 22553 22559
143 unless $self->status eq 'active';
144 22553 ▶ return Game::Reversi::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and defined $self->turn and $colour eq $self->turn 3 22550 22553
Condition50%3/6
defined $colour and defined $self->turn and $colour eq $self->turn
!ll&&!rl&&r
0322550
defined $colour and defined $self->turn
!ll&&!rl&&r
0022553
MC/DC33%1/3
defined $colour and defined $self->turn and $colour eq $self->turn
defined $colour defined $self->turn $colour eq $self->turn
Truth table50%2/4
A and B and C
A= defined $colourB= defined $self->turnC= $colour eq $self->turn
ABCresult
0XX0
10X0
1100
1111
145 unless defined $colour && defined $self->turn && $colour eq $self->turn;
146  
147 22550 my $move;
148 22550 ▶ if (Game::Reversi::Rules->in_opening($self->board)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Game::Reversi::Rules'->in_opening($self->board)) { } 426 22124 22550
149 426 my $error = Game::Reversi::Opening->check($self->board, $square);
150 426 ▶ return $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 4 422 426
151 422 $move = Game::Reversi::Move->place($square, $colour);
152 422 $self->board(Game::Reversi::Opening->apply($self->board, $square, $colour));
153 422 $self->_emit($colour, 'place', { square => $square });
154 }
155 else {
156 return Game::Reversi::Error->throw('square_taken',
157 22124 ▶ legal => [ map { $_->square } @{ $self->legal($colour) } ])
Branch: 1 branch
BranchTrueFalseTotal
if defined $square and $square >= 0 and $square < 64 and defined $self->board->[$square] 1 22123 22124
Condition44%4/9
defined $square and $square >= 0 and $square < 64 and defined $self->board->[$square]
!ll&&!rl&&r
0221231
defined $square and $square >= 0 and $square < 64
!ll&&!rl&&r
0022124
defined $square and $square >= 0
!ll&&!rl&&r
0022124
MC/DC25%1/4
defined $square and $square >= 0 and $square < 64 and defined $self->board->[$square]
defined $square $square >= 0 $square < 64 defined $self->board->[$square]
Truth table40%2/5
A and B and C and D
A= defined $squareB= $square >= 0C= $square < 64D= defined $self->board->[$square]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
158 if defined $square && $square >= 0 && $square < 64
159 && defined $self->board->[$square];
160  
161 22123 my @flips = Game::Reversi::Board->flips_for($self->board, $square, $colour);
162 return Game::Reversi::Error->throw('no_flip',
163 22123 ▶ legal => [ map { $_->square } @{ $self->legal($colour) } ])
Branch: 1 branch
BranchTrueFalseTotal
unless @flips 2 22121 22123
164 unless @flips;
165  
166 22121 $move = Game::Reversi::Move->play($square, $colour, @flips);
167 22121 $self->board(Game::Reversi::Board->apply($self->board, $square, $colour));
168 22121 $self->_emit($colour, 'play', { square => $square, flips => [ @flips ] });
169 }
170  
171 22543 $self->_advance($colour);
172 22543 return $move;
173 }
174  
175 71 ▶sub pass {
Subroutine pass: called CC=5 SCAR=16.4
Pod documented
176 71 my ($self, $colour) = @_;
177 71 ▶ return Game::Reversi::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 3 68 71
178 unless $self->status eq 'active';
179 68 ▶ return Game::Reversi::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and defined $self->turn and $colour eq $self->turn 2 66 68
Condition50%3/6
defined $colour and defined $self->turn and $colour eq $self->turn
!ll&&!rl&&r
0266
defined $colour and defined $self->turn
!ll&&!rl&&r
0068
MC/DC33%1/3
defined $colour and defined $self->turn and $colour eq $self->turn
defined $colour defined $self->turn $colour eq $self->turn
Truth table50%2/4
A and B and C
A= defined $colourB= defined $self->turnC= $colour eq $self->turn
ABCresult
0XX0
10X0
1100
1111
180 unless defined $colour && defined $self->turn && $colour eq $self->turn;
181 return Game::Reversi::Error->throw('has_move',
182 66 legal => [ map { $_->square } @{ $self->legal($colour) } ]);
183 }
184  
185 71 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
186 71 my ($self) = @_;
187 71 my $copy = Object::Proto::clone($self);
188 71 $copy->board([ @{ $self->board } ]);
189 71 $copy->log([ map { { %$_, payload => { %{ $_->{payload} } } } } @{ $self->log } ]);
190 71 return $copy;
191 }
192  
193 23715 ▶sub _emit {
Subroutine _emit: called CC=2 SCAR=7.0
194 23715 my ($self, $actor, $kind, $payload) = @_;
195 23715 ▶ push @{ $self->log }, { actor => $actor, kind => $kind, payload => $payload || {} };
Condition50%1/2
$payload || {}
l!l
237150
MC/DC0%0/1
$payload || {}
$payload
Truth table50%1/2
A
A= $payload
Aresult
11
00
196 23715 return;
197 }
198  
199 22543 ▶sub _advance {
Subroutine _advance: called CC=4 SCAR=13.9
200 22543 my ($self, $just_moved) = @_;
201 22543 my ($turn, $forfeited) =
202 Game::Reversi::Rules->next_turn($self->board, $just_moved);
203  
204 22543 ▶ $self->_emit('sys', 'pass', { colour => $forfeited })
Branch: 1 branch
BranchTrueFalseTotal
if defined $forfeited and defined $turn 239 22304 22543
Condition100%3/3
defined $forfeited and defined $turn
!ll&&!rl&&r
21942362239
MC/DC100%2/2
defined $forfeited and defined $turn
defined $forfeited defined $turn
Truth table100%3/3
A and B
A= defined $forfeitedB= defined $turn
ABresult
0X0
100
111
205 if defined $forfeited && defined $turn;
206  
207 22543 $self->turn($turn);
208 22543 ▶ $self->_finish unless defined $turn;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $turn 362 22181 22543
209 22543 return;
210 }
211  
212 393 ▶sub _finish {
Subroutine _finish: called CC=8 SCAR=20.8
213 393 my ($self, $winner, $result) = @_;
214 393 my $count = $self->counts;
215  
216 393 ▶ if (!defined $result) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 363 30 393
217 363 $winner = Game::Reversi::Scoring->winner($self->board);
218 363 ▶ $result = defined $winner ? 'score' : 'draw';
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 348 15 363
219 }
220  
221 393 ▶ my $natural = ($result eq 'score' || $result eq 'draw') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$result eq 'score' || $result eq 'draw' ? : 363 30 393
Condition100%3/3
$result eq "score" or $result eq "draw"
l!l&&r!l&&!r
3481530
MC/DC100%2/2
$result eq "score" or $result eq "draw"
$result eq "score" $result eq "draw"
Truth table100%3/3
A or B
A= $result eq "score"B= $result eq "draw"
ABresult
1X1
011
000
222 393 $self->winner($winner);
223 393 $self->status('finished');
224 393 $self->turn(undef);
225 $self->result(Game::Reversi::Result->new(
226 winner => $winner,
227 result => $result,
228 counts => $count,
229 score => $natural ? Game::Reversi::Scoring->score($self->board) : undef,
230 places => {
231 393 ▶ map { $_ => (!defined $winner ? 1 : $_ eq $winner ? 1 : 2) } $self->seats
Branch: 3 branches
BranchTrueFalseTotal
$natural ? : 363 30 393
!defined($winner) ? : 42 744 786
$_ eq $winner ? : 372 372 744
232 },
233 ));
234  
235 393 $self->_emit('sys', 'game_end', { winner => $winner, result => $result });
236 393 return;
237 }
238  
239 7 ▶sub timeout {
Subroutine timeout: called CC=5 SCAR=16.8
Pod documented
240 7 my ($self, $colour) = @_;
241 7 ▶ return Game::Reversi::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 6 7
242 unless $self->status eq 'active';
243 6 ▶ Carp::croak('Game::Reversi: timeout wants the seat that ran out')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and $colour eq "b" || $colour eq "w" 0 6 6
Condition50%3/6
defined $colour and $colour eq "b" || $colour eq "w"
!ll&&!rl&&r
006
$colour eq "b" or $colour eq "w"
l!l&&r!l&&!r
330
MC/DC0%0/3
defined $colour and $colour eq "b" || $colour eq "w"
defined $colour $colour eq "b" $colour eq "w"
Truth table50%2/4
A and B or C
A= defined $colourB= $colour eq "b"C= $colour eq "w"
ABCresult
0XX0
1000
11X1
1011
244 unless defined $colour && ($colour eq 'b' || $colour eq 'w');
245  
246 6 $self->_emit('sys', 'timeout', { colour => $colour });
247 6 $self->_finish(Game::Reversi::Board->other($colour), 'timeout');
248 6 return $self->result;
249 }
250  
251 7 ▶sub abandon {
Subroutine abandon: called CC=2 SCAR=6.9
Pod documented
252 7 my ($self) = @_;
253 7 ▶ return Game::Reversi::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 6 7
254 unless $self->status eq 'active';
255 6 $self->_emit('sys', 'abandon', {});
256 6 $self->_finish(undef, 'abandoned');
257 6 return $self->result;
258 }
259  
260 20 ▶sub resign {
Subroutine resign: called CC=5 SCAR=16.8
Pod documented
261 20 my ($self, $colour) = @_;
262 20 ▶ return Game::Reversi::Error->throw('game_over')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 2 18 20
263 unless $self->status eq 'active';
264 18 ▶ return Game::Reversi::Error->throw('not_your_turn')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $colour and $colour eq "b" || $colour eq "w" 0 18 18
Condition50%3/6
defined $colour and $colour eq "b" || $colour eq "w"
!ll&&!rl&&r
0018
$colour eq "b" or $colour eq "w"
l!l&&r!l&&!r
1710
MC/DC0%0/3
defined $colour and $colour eq "b" || $colour eq "w"
defined $colour $colour eq "b" $colour eq "w"
Truth table50%2/4
A and B or C
A= defined $colourB= $colour eq "b"C= $colour eq "w"
ABCresult
0XX0
1000
11X1
1011
265 unless defined $colour && ($colour eq 'b' || $colour eq 'w');
266  
267 18 $self->_emit($colour, 'resign', {});
268 18 $self->_finish(Game::Reversi::Board->other($colour), 'resign');
269 18 return $self->result;
270 }
271  
272 15 ▶sub replay {
Subroutine replay: called CC=23 SCAR=32.1
Pod documented
273 15 my ($self, $log) = @_;
274 15 ▶ Carp::croak('Game::Reversi: a log is an arrayref of events')
Branch: 1 branch
BranchTrueFalseTotal
unless ref $log eq "ARRAY" 1 14 15
275 unless ref $log eq 'ARRAY';
276 Carp::croak('Game::Reversi: replay wants a game that has not been played')
277 14 ▶ unless @{ $self->log } == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->log;} == 1 1 13 14
278  
279 13 my @events = @$log;
280 13 my $start = shift @events;
281 Carp::croak('Game::Reversi: the log does not open with a start')
282 unless $start && ($start->{actor} // '') eq 'sys'
283 13 ▶ && ($start->{kind} // '') eq 'start';
Branch: 1 branch
BranchTrueFalseTotal
unless $start and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start" 1 12 13
Condition60%6/10
$start and ($start->{"actor"} // "") eq "sys" and ($start->{"kind"} // "") eq "start"
!ll&&!rl&&r
1012
$start and ($start->{"actor"} // "") eq "sys"
!ll&&!rl&&r
0112
$start->{"actor"} // ""
l!l
130
$start->{"kind"} // ""
l!l
120
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
284 Carp::croak('Game::Reversi: the log was made under a different variant')
285 12 ▶ unless ($start->{payload}{variant} // '') eq $self->variant;
Branch: 1 branch
BranchTrueFalseTotal
unless ($start->{"payload"}{"variant"} // "") eq $self->variant 1 11 12
Condition50%1/2
$start->{"payload"}{"variant"} // ""
l!l
120
MC/DC0%0/1
$start->{"payload"}{"variant"} // ""
$start->{"payload"}{"variant"}
Truth table50%1/2
A
A= $start->{"payload"}{"variant"}
Aresult
11
00
286  
287 11 ▶ my $began = join '', map { defined $_ ? $_ : '.' } @{ $self->board };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 4 700 704
288 11 my $logged = $start->{payload}{board};
289 11 ▶ Carp::croak('Game::Reversi: the log began from a different position')
Branch: 1 branch
BranchTrueFalseTotal
if defined $logged and $logged ne $began 0 11 11
Condition33%1/3
defined $logged and $logged ne $began
!ll&&!rl&&r
1100
MC/DC0%0/2
defined $logged and $logged ne $began
defined $logged $logged ne $began
Truth table33%1/3
A and B
A= defined $loggedB= $logged ne $began
ABresult
0X0
100
111
290 if defined($logged) && $logged ne $began;
291  
292 11 for my $event (@events) {
293 359 ▶ my $kind = $event->{kind} // '';
Condition50%1/2
$event->{"kind"} // ""
l!l
3590
MC/DC0%0/1
$event->{"kind"} // ""
$event->{"kind"}
Truth table50%1/2
A
A= $event->{"kind"}
Aresult
11
00
294  
295 359 ▶ if (($event->{actor} // '') eq 'sys') {
Branch: 1 branch
BranchTrueFalseTotal
if (($event->{"actor"} // "") eq "sys") 16 343 359
Condition50%1/2
$event->{"actor"} // ""
l!l
3590
MC/DC0%0/1
$event->{"actor"} // ""
$event->{"actor"}
Truth table50%1/2
A
A= $event->{"actor"}
Aresult
11
00
296 16 ▶ $self->timeout($event->{payload}{colour}) if $kind eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "timeout" 1 15 16
297 16 ▶ $self->abandon if $kind eq 'abandon';
Branch: 1 branch
BranchTrueFalseTotal
if $kind eq "abandon" 1 15 16
298 16 next;
299 }
300  
301 my $result = $kind eq 'resign'
302 ? $self->resign($event->{actor})
303 343 ▶ : $self->play($event->{actor}, $event->{payload}{square});
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'resign' ? : 1 342 343
304 343 ▶ Carp::croak('Game::Reversi: the log does not replay: '
Branch: 1 branch
BranchTrueFalseTotal
if ref $result and $result->isa("Game::Reversi::Error") 3 340 343
Condition66%2/3
ref $result and $result->isa("Game::Reversi::Error")
!ll&&!rl&&r
03403
MC/DC50%1/2
ref $result and $result->isa("Game::Reversi::Error")
ref $result $result->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $resultB= $result->isa("Game::Reversi::Error")
ABresult
0X0
100
111
305 . $result->stringify)
306 if ref $result && $result->isa('Game::Reversi::Error');
307 }
308  
309 8 my $ours = join '|', map { _signature($_) } @{ $self->log };
310 8 my $theirs = join '|', map { _signature($_) } @$log;
311 8 ▶ Carp::croak('Game::Reversi: the log does not replay to itself, so it '
Branch: 1 branch
BranchTrueFalseTotal
unless $ours eq $theirs 2 6 8
312 . 'records something this game would not have produced')
313 unless $ours eq $theirs;
314  
315 6 return $self;
316 }
317  
318 718 ▶sub _signature {
Subroutine _signature: called CC=6 SCAR=18.4
319 718 my ($event) = @_;
320 718 ▶ my $payload = $event->{payload} || {};
Condition50%1/2
$event->{'payload'} || {}
l!l
7180
MC/DC0%0/1
$event->{'payload'} || {}
$event->{'payload'}
Truth table50%1/2
A
A= $event->{'payload'}
Aresult
11
00
321 return join ':', $event->{actor} // '?', $event->{kind} // '?',
322 join ',', map {
323 718 ▶ my $v = $payload->{$_};
Condition50%2/4
$event->{"actor"} // "?"
l!l
7180
$event->{"kind"} // "?"
l!l
7180
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
324 1358 ▶ $_ . '=' . (ref $v eq 'ARRAY' ? join('.', @$v) : defined $v ? $v : '')
Branch: 2 branches
BranchTrueFalseTotal
ref $v eq 'ARRAY' ? : 612 746 1358
defined $v ? : 744 2 746
325 } sort keys %$payload;
326 }
327  
328 260 ▶sub to_text {
Subroutine to_text: called CC=13 SCAR=25.7
Pod documented
329 260 my ($self) = @_;
330 260 my $start = $self->log->[0]{payload};
331 260 my @line;
332 260 ▶ push @line, 'variant ' . $self->variant if $self->variant ne 'historic';
Branch: 1 branch
BranchTrueFalseTotal
if $self->variant ne "historic" 82 178 260
333 260 ▶ push @line, 'first ' . $start->{first} if $start->{first} ne Game::Reversi::Opening->first;
Branch: 1 branch
BranchTrueFalseTotal
if $start->{"first"} ne "Game::Reversi::Opening"->first 4 256 260
334 260 ▶ push @line, 'board ' . $start->{board} if defined $start->{board};
Branch: 1 branch
BranchTrueFalseTotal
if defined $start->{"board"} 4 256 260
335 260 ▶ if (defined(my $seed = $self->_held_seed)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $seed = $self->_held_seed)) 253 7 260
336 253 ▶ utf8::encode($seed) if utf8::is_utf8($seed);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($seed) 0 253 253
337 253 push @line, 'seed ' . unpack 'H*', $seed;
338 }
339 my $transcript = Game::Reversi::Notation->render(
340 6047 [ map { $_->{payload}{square} }
341 260 ▶ grep { $_->{kind} eq 'place' || $_->{kind} eq 'play' } @{ $self->log } ]);
Condition100%3/3
$_->{'kind'} eq 'place' || $_->{'kind'} eq 'play'
l!l&&r!l&&!r
6255422331
MC/DC100%2/2
$_->{'kind'} eq 'place' || $_->{'kind'} eq 'play'
$_->{'kind'} eq 'place' $_->{'kind'} eq 'play'
Truth table100%3/3
A || B
A= $_->{'kind'} eq 'place'B= $_->{'kind'} eq 'play'
ABresult
1X1
011
000
342 260 ▶ push @line, $transcript if length $transcript || @line;
Branch: 1 branch
BranchTrueFalseTotal
if length $transcript or @line 260 0 260
Condition66%2/3
length $transcript or @line
l!l&&r!l&&!r
218420
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
343 260 for my $event (@{ $self->log }) {
344 6378 ▶ push @line, "resign $event->{actor}" if $event->{kind} eq 'resign';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "resign" 4 6374 6378
345 6378 ▶ push @line, "timeout $event->{payload}{colour}" if $event->{kind} eq 'timeout';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "timeout" 3 6375 6378
346 6378 ▶ push @line, 'abandon' if $event->{kind} eq 'abandon';
Branch: 1 branch
BranchTrueFalseTotal
if $event->{"kind"} eq "abandon" 3 6375 6378
347 }
348 260 return join "\n", @line;
349 }
350  
351 36 ▶sub from_text {
Subroutine from_text: called CC=31 SCAR=35.6
Pod documented
352 36 my ($class, $text, %o) = @_;
353 36 ▶ Carp::croak('Game::Reversi: a transcript is a string')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 0 36 36
Condition33%1/3
defined $text and not ref $text
!ll&&!rl&&r
0036
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
354 unless defined $text && !ref $text;
355  
356 36 my (%setting, @ending, $squares);
357 36 for my $line (split /\n/, $text) {
358 84 ▶ next unless $line =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /\S/ 0 84 84
359 84 $line =~ s/^\s+|\s+$//g;
360 84 my ($word, $value) = $line =~ /^(variant|first|board|seed|resign|timeout|abandon)\b\s*(.*)$/;
361 84 ▶ if (!defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 30 54 84
362 30 ▶ Carp::croak('Game::Reversi: the transcript has moves after its ending') if @ending;
Branch: 1 branch
BranchTrueFalseTotal
if @ending 1 29 30
363 29 ▶ $squares = defined $squares ? "$squares $line" : $line;
Branch: 1 branch
BranchTrueFalseTotal
defined $squares ? : 0 29 29
364 29 next;
365 }
366 54 ▶ 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 44 54
Condition100%6/6
$word eq "resign" or $word eq "timeout" or $word eq "abandon"
l!l&&r!l&&!r
7344
$word eq "resign" or $word eq "timeout"
l!l&&r!l&&!r
5247
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
367 10 ▶ Carp::croak("Game::Reversi: the transcript has a second ending, '$line'") if @ending;
Branch: 1 branch
BranchTrueFalseTotal
if @ending 0 10 10
368 10 ▶ Carp::croak("Game::Reversi: '$line' does not name a colour, b or w")
Branch: 2 branches
BranchTrueFalseTotal
unless $word eq "abandon" ? $value eq "" : $value =~ /^[bw]$/ 1 9 10
$word eq 'abandon' ? : 3 7 10
369 unless $word eq 'abandon' ? $value eq '' : $value =~ /^[bw]$/;
370 9 @ending = ($word, $value);
371 9 next;
372 }
373 Carp::croak("Game::Reversi: the transcript says $word twice")
374 44 ▶ if exists $setting{$word};
Branch: 1 branch
BranchTrueFalseTotal
if exists $setting{$word} 0 44 44
375 44 ▶ Carp::croak("Game::Reversi: the transcript names its $word after its moves")
Branch: 1 branch
BranchTrueFalseTotal
if defined $squares or @ending 0 44 44
Condition33%1/3
defined $squares or @ending
l!l&&r!l&&!r
0044
MC/DC0%0/2
defined $squares or @ending
defined $squares @ending
Truth table33%1/3
A or B
A= defined $squaresB= @ending
ABresult
1X1
011
000
376 if defined $squares || @ending;
377 44 ▶ if ($word eq 'first') {
Branch: 3 branches
BranchTrueFalseTotal
if ($word eq 'first') { } 2 42 44
elsif ($word eq 'board') { } 3 39 42
elsif ($word eq 'seed') { } 29 10 39
378 2 ▶ Carp::croak("Game::Reversi: the first colour is b or w, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^[bw]$/ 0 2 2
379 unless $value =~ /^[bw]$/;
380 2 $setting{turn} = $value;
381 }
382 elsif ($word eq 'board') {
383 3 ▶ Carp::croak("Game::Reversi: a board is 64 squares of b, w and ., not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^[bw.]{64}$/ 1 2 3
384 unless $value =~ /^[bw.]{64}$/;
385 2 ▶ $setting{board} = [ map { $_ eq '.' ? undef : $_ } split //, $value ];
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '.' ? : 118 10 128
386 }
387 elsif ($word eq 'seed') {
388 29 ▶ Carp::croak("Game::Reversi: a seed is written in hexadecimal, not '$value'")
Branch: 1 branch
BranchTrueFalseTotal
unless $value =~ /^(?:[0-9a-f]{2})*$/ 0 29 29
389 unless $value =~ /^(?:[0-9a-f]{2})*$/;
390 29 $setting{seed} = pack 'H*', $value;
391 }
392 else {
393 10 $setting{variant} = $value;
394 }
395 }
396  
397 33 my $self = $class->new(%o, %setting);
398 32 for my $square (@{ Game::Reversi::Notation->parse($squares) }) {
399 411 my $result = $self->play($self->turn, $square);
400 411 ▶ Carp::croak('Game::Reversi: the transcript does not fit this game: '
Branch: 1 branch
BranchTrueFalseTotal
if ref $result and $result->can("error") 0 411 411
Condition33%1/3
ref $result and $result->can("error")
!ll&&!rl&&r
04110
MC/DC0%0/2
ref $result and $result->can("error")
ref $result $result->can("error")
Truth table33%1/3
A and B
A= ref $resultB= $result->can("error")
ABresult
0X0
100
111
401 . $result->stringify) if ref $result && $result->can('error');
402 }
403 31 ▶ if (@ending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ending) 8 23 31
404 8 my ($word, $colour) = @ending;
405 8 ▶ my $result = $word eq 'abandon' ? $self->abandon : $self->$word($colour);
Branch: 1 branch
BranchTrueFalseTotal
$word eq 'abandon' ? : 2 6 8
406 8 ▶ Carp::croak("Game::Reversi: the transcript ends by $word, but the moves already end the game")
Branch: 1 branch
BranchTrueFalseTotal
if ref $result and $result->isa("Game::Reversi::Error") 1 7 8
Condition66%2/3
ref $result and $result->isa("Game::Reversi::Error")
!ll&&!rl&&r
071
MC/DC50%1/2
ref $result and $result->isa("Game::Reversi::Error")
ref $result $result->isa("Game::Reversi::Error")
Truth table66%2/3
A and B
A= ref $resultB= $result->isa("Game::Reversi::Error")
ABresult
0X0
100
111
407 if ref $result && $result->isa('Game::Reversi::Error');
408 }
409 30 return $self;
410 }
411  
412 14 1;
413  
414 __END__
« Reversi/Terminal.pm ↑ Summary Reversi/Move.pm »