blib/lib/Game/Brandubh.pm

filter: Statementstmt 99.6% 288 / 289 Branchbran 93.3% 168 / 180 Conditioncond 87.2% 48 / 55 MC/DCmcdc 75.8% 22 / 29 Subroutinesub 100% 53 / 53 Podpod 96.9% 32 / 33 totaltotal 95.6% 611 / 639 CC 114 SCAR 18.2
CC
114
Cov
96%
BUILD
15.0
play
11.0
_apply_result
9.1
CRAP 6.1
« blib/lib/Game/Brandubh/Notation.pm ↑ Summary blib/lib/Game/Brandubh/Result.pm »
1 package Game::Brandubh;
2  
3 44 ▶use 5.010;
Subroutine __ANON__: called CC=1 SCAR=0.0
4 44 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
5 44 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
6  
7 44 ▶use Carp ();
Subroutine __ANON__: called CC=1 SCAR=0.0
8 44 ▶use Object::Proto::Sugar;
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 44 ▶use Game::Brandubh::Engine qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
11 ATTACKERS DEFENDERS EMPTY ATTACKER DEFENDER KING
12 KING_HOME KING_TAKEN
13 WHY_OK WHY_NO_PIECE WHY_NOT_YOURS WHY_NO_MOVE WHY_NOT_A_LINE
14 WHY_THRONE WHY_CORNER WHY_BLOCKED
15 44 );
16 44 ▶use Game::Brandubh::Rules qw(PLAY_OK BY_CORNER outcome_name);
Subroutine __ANON__: called CC=1 SCAR=0.0
17 44 ▶use Game::Brandubh::Notation qw(
Subroutine __ANON__: called CC=1 SCAR=0.0
18 SETUP square_name square_parse move_parse move_split move_display
19 position_ok game_string game_parse
20 44 );
21 44 ▶use Game::Brandubh::Variant;
Subroutine __ANON__: called CC=1 SCAR=0.0
22 44 ▶use Game::Brandubh::Error;
Subroutine __ANON__: called CC=1 SCAR=0.0
23 44 ▶use Game::Brandubh::Result;
Subroutine __ANON__: called CC=1 SCAR=0.0
24 44 ▶use Digest::SHA ();
Subroutine __ANON__: called CC=1 SCAR=0.0
25  
26 44 our $VERSION = '0.02';
27  
28 44 my $E = 'Game::Brandubh::Engine';
29 44 my $X = 'Game::Brandubh::Error';
30  
31 44 my (@SIDE, @PIECE, %REFUSAL);
32 44 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
33 44 @SIDE = ('attackers', 'defenders');
34 44 @PIECE = ('', 'attacker', 'defender', 'king');
35 44 %REFUSAL = (
36 WHY_NO_PIECE() => 'no_piece',
37 WHY_NOT_YOURS() => 'not_your_piece',
38 WHY_NO_MOVE() => 'no_move',
39 WHY_NOT_A_LINE() => 'not_a_line',
40 WHY_THRONE() => 'throne_closed',
41 WHY_CORNER() => 'corner_closed',
42 WHY_BLOCKED() => 'path_blocked',
43 );
44 }
45  
46 44 has attackers => (is => 'ro', default => 'p1');
47  
48 44 has _seed => (is => 'ro', init_arg => 'seed', private => 1);
49  
50 44 has _variant_given => (is => 'ro', init_arg => 'variant', private => 1);
51  
52 44 has _start_given => (is => 'ro', init_arg => 'position', private => 1);
53  
54 44 has _variant => (is => 'rw', private => 1);
55  
56 44 has _start => (is => 'rw', private => 1);
57  
58 44 has _game => (is => 'rw', private => 1);
59  
60 44 has _log => (is => 'ro', default => [], private => 1);
61  
62 44 has _shown => (is => 'ro', default => [], private => 1);
63  
64 44 has _ended => (is => 'rw', private => 1);
65  
66 44 has _offer => (is => 'rw', private => 1);
67  
68 2559 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=27.1
Pod undocumented
69 2559 my ($self) = @_;
70  
71 2559 my $attackers = $self->attackers;
72 2559 ▶ Carp::croak("Game::Brandubh: attackers must be 'p1' or 'p2', not '" . ($attackers // 'undef') . "'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attackers and $attackers eq "p1" || $attackers eq "p2" 3 2556 2559
Condition75%6/8
defined $attackers and $attackers eq "p1" || $attackers eq "p2"
!ll&&!rl&&r
032556
$attackers eq "p1" or $attackers eq "p2"
l!l&&r!l&&!r
139611603
$attackers // "undef"
l!l
30
MC/DC66%2/3
defined $attackers and $attackers eq "p1" || $attackers eq "p2"
defined $attackers $attackers eq "p1" $attackers eq "p2"
MC/DC0%0/1
$attackers // "undef"
$attackers
Truth table75%3/4
A and B or C
A= defined $attackersB= $attackers eq "p1"C= $attackers eq "p2"
ABCresult
0XX0
1000
11X1
1011
Truth table50%1/2
A
A= $attackers
Aresult
11
00
73 unless defined $attackers && ($attackers eq 'p1' || $attackers eq 'p2');
74  
75 2556 my $seed = $self->_seed;
76 2556 ▶ Carp::croak('Game::Brandubh: a seed, when one is given, is exactly thirty-two bytes')
Branch: 1 branch
BranchTrueFalseTotal
if defined $seed and ref $seed || length $seed != 32 2 2554 2556
Condition83%5/6
defined $seed and ref $seed || length $seed != 32
!ll&&!rl&&r
24321222
ref $seed or length $seed != 32
l!l&&r!l&&!r
02122
MC/DC66%2/3
defined $seed and ref $seed || length $seed != 32
defined $seed ref $seed length $seed != 32
Truth table75%3/4
A and B or C
A= defined $seedB= ref $seedC= length $seed != 32
ABCresult
0XX0
1000
11X1
1011
77 if defined $seed && (ref $seed || length($seed) != 32);
78  
79 2554 my $given = $self->_variant_given;
80 2554 my $variant;
81 2554 ▶ if (!defined $given) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $given) { } 497 2057 2554
elsif (ref $given eq 'Game::Brandubh::Variant') { } 690 1367 2057
elsif (ref $given eq 'HASH') { } 1357 10 1367
elsif (not ref $given) { } 9 1 10
82 497 $variant = Game::Brandubh::Variant->named;
83 }
84 elsif (ref $given eq 'Game::Brandubh::Variant') {
85 690 $variant = $given;
86 }
87 elsif (ref $given eq 'HASH') {
88 1357 $variant = Game::Brandubh::Variant->custom(%$given);
89 }
90 elsif (!ref $given) {
91 9 $variant = Game::Brandubh::Variant->from_string($given);
92 9 ▶ Carp::croak("Game::Brandubh: '$given' is not a rule set") unless $variant;
Branch: 1 branch
BranchTrueFalseTotal
unless $variant 2 7 9
93 }
94 else {
95 1 Carp::croak('Game::Brandubh: a variant is a name, a hash of fields or a Game::Brandubh::Variant');
96 }
97 2550 $self->_variant($variant);
98  
99 2550 my $start = $self->_start_given;
100 2550 ▶ $start = SETUP unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 1722 828 2550
101 2550 ▶ Carp::croak("Game::Brandubh: '$start' is not a position") unless position_ok($start);
Branch: 1 branch
BranchTrueFalseTotal
unless position_ok($start) 1 2549 2550
102 2549 $self->_start($start);
103  
104 2549 $self->_game(Game::Brandubh::Rules->new(position => $start, variant => $variant->as_hash));
105 2549 return;
106 }
107  
108 1907 ▶sub variant { $_[0]->_variant }
Subroutine variant: called CC=1 SCAR=0.0
Pod documented
109 301 ▶sub start { $_[0]->_start }
Subroutine start: called CC=1 SCAR=0.0
Pod documented
110  
111 308730 ▶sub status {
Subroutine status: called CC=3 SCAR=11.0
Pod documented
112 308730 my ($self) = @_;
113 308730 ▶ return 'finished' if $self->_ended || $self->_game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ended or $self->_game->is_over 4152 304578 308730
Condition100%3/3
$self->_ended or $self->_game->is_over
l!l&&r!l&&!r
1024050304578
MC/DC100%2/2
$self->_ended or $self->_game->is_over
$self->_ended $self->_game->is_over
Truth table100%3/3
A or B
A= $self->_endedB= $self->_game->is_over
ABresult
1X1
011
000
114 304578 return 'active';
115 }
116  
117 61 ▶sub side_of {
Subroutine side_of: called CC=5 SCAR=16.1
Pod documented
118 61 my ($self, $seat) = @_;
119 61 ▶ return undef unless defined $seat && ($seat eq 'p1' || $seat eq 'p2');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and $seat eq "p1" || $seat eq "p2" 2 59 61
Condition100%6/6
defined $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
1159
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
31281
MC/DC100%3/3
defined $seat and $seat eq "p1" || $seat eq "p2"
defined $seat $seat eq "p1" $seat eq "p2"
Truth table100%4/4
A and B or C
A= defined $seatB= $seat eq "p1"C= $seat eq "p2"
ABCresult
0XX0
1000
11X1
1011
120 59 ▶ return $seat eq $self->attackers ? 'attackers' : 'defenders';
Branch: 1 branch
BranchTrueFalseTotal
$seat eq $self->attackers ? : 45 14 59
121 }
122  
123 8440 ▶sub seat_of {
Subroutine seat_of: called CC=5 SCAR=16.1
Pod documented
124 8440 my ($self, $side) = @_;
125 8440 ▶ return undef unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 1 8439 8440
126 8439 ▶ return $self->attackers if $side eq 'attackers';
Branch: 1 branch
BranchTrueFalseTotal
if $side eq "attackers" 3892 4547 8439
127 4547 ▶ return ($self->attackers eq 'p1' ? 'p2' : 'p1') if $side eq 'defenders';
Branch: 2 branches
BranchTrueFalseTotal
if $side eq "defenders" 4545 2 4547
$self->attackers eq 'p1' ? : 2492 2053 4545
128 2 return undef;
129 }
130  
131 8886 ▶sub side_to_move {
Subroutine side_to_move: called CC=2 SCAR=6.9
Pod documented
132 8886 my ($self) = @_;
133 8886 ▶ return undef unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 111 8775 8886
134 8775 return $SIDE[ $self->_game->side ];
135 }
136  
137 5027 ▶sub turn {
Subroutine turn: called CC=2 SCAR=6.9
Pod documented
138 5027 my ($self) = @_;
139 5027 my $side = $self->side_to_move;
140 5027 ▶ return defined $side ? $self->seat_of($side) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 4968 59 5027
141 }
142  
143 1397765 ▶sub _names_of {
Subroutine _names_of: called CC=1 SCAR=0.0
144 1397765 my ($mv) = @_;
145 1397765 my $from = $E->move_from($mv);
146 1397765 my $to = $E->move_to($mv);
147 1397765 return (square_name($E->file_of($from), $E->rank_of($from)),
148 square_name($E->file_of($to), $E->rank_of($to)));
149 }
150  
151 361698 ▶sub _square_of {
Subroutine _square_of: called CC=2 SCAR=6.9
152 361698 my ($name) = @_;
153 361698 ▶ my ($file, $rank) = square_parse($name) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = square_parse($name) or return undef 361696 2 361698
154 361696 return $E->square_of($file, $rank);
155 }
156  
157 1573904 ▶sub _about {
Subroutine _about: called CC=9 SCAR=22.0
158 1573904 my ($self, $mv) = @_;
159 1573904 my $game = $self->_game;
160 1573904 my ($flags, @squares) = $game->preview($mv);
161 1573904 my $piece = $game->at($E->move_from($mv));
162 1573904 my @captures = sort map { square_name($E->file_of($_), $E->rank_of($_)) } @squares;
163  
164 1573904 ▶ my $wins = ($flags & (KING_HOME | KING_TAKEN)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$flags & 6 ? : 14929 1558975 1573904
165 1573904 ▶ if (!$wins && $piece != ATTACKER && @squares) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $wins and $piece != 1 and @squares) 31231 1542673 1573904
Condition100%6/6
not $wins and $piece != 1 and @squares
!ll&&!rl&&r
93049061218331231
not $wins and $piece != 1
!ll&&!rl&&r
14929915561643414
MC/DC100%3/3
not $wins and $piece != 1 and @squares
not $wins $piece != 1 @squares
Truth table100%4/4
not(A) and B and C
A= not $winsB= $piece != 1C= @squares
ABCresult
0XX0
10X0
1100
1111
166 31231 my $attackers_taken = grep { $game->at($_) == ATTACKER } @squares;
167 31231 ▶ $wins = 1 if $attackers_taken == $game->count(ATTACKER);
Branch: 1 branch
BranchTrueFalseTotal
if $attackers_taken == $game->count(1) 54 31177 31231
168 }
169 return {
170 1573904 ▶ piece => $PIECE[$piece],
Branch: 3 branches
BranchTrueFalseTotal
$piece == 3 ? : 160138 1413766 1573904
$flags & 4 ? : 10297 1563607 1573904
$flags & 2 ? : 4632 1569272 1573904
171 king => $piece == KING ? 1 : 0,
172 captures => \@captures,
173 king_home => ($flags & KING_HOME) ? 1 : 0,
174 king_taken => ($flags & KING_TAKEN) ? 1 : 0,
175 wins => $wins,
176 };
177 }
178  
179 43614 ▶sub legal {
Subroutine legal: called CC=3 SCAR=11.0
Pod documented
180 43614 my ($self) = @_;
181 43614 ▶ return [] unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 14 43600 43614
182 43600 my @legal;
183 43600 for my $mv ($self->_game->moves) {
184 1396715 my ($from, $to) = _names_of($mv);
185 1396715 my $about = $self->_about($mv);
186 push @legal, {
187 move => $from . $to,
188 from => $from,
189 to => $to,
190 piece => $about->{piece},
191 captures => $about->{captures},
192 wins => $about->{wins},
193 1396715 };
194 }
195 43600 return \@legal;
196 }
197  
198 2611 ▶sub _seat_refusal {
Subroutine _seat_refusal: called CC=5 SCAR=16.1
199 2611 my ($self, $seat) = @_;
200 2611 ▶ return $X->throw('not_a_seat') unless defined $seat && !ref $seat && ($seat eq 'p1' || $seat eq 'p2');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $seat and not ref $seat and $seat eq "p1" || $seat eq "p2" 2419 192 2611
Condition88%8/9
defined $seat and not ref $seat and $seat eq "p1" || $seat eq "p2"
!ll&&!rl&&r
22417192
defined $seat and not ref $seat
!ll&&!rl&&r
202609
$seat eq "p1" or $seat eq "p2"
l!l&&r!l&&!r
99932417
MC/DC75%3/4
defined $seat and not ref $seat and $seat eq "p1" || $seat eq "p2"
defined $seat not ref $seat $seat eq "p1" $seat eq "p2"
Truth table80%4/5
A and not(B) and C or D
A= defined $seatB= not ref $seatC= $seat eq "p1"D= $seat eq "p2"
ABCDresult
0XXX0
10XX0
11000
111X1
11011
201 192 return 0;
202 }
203  
204 182079 ▶sub play {
Subroutine play: called CC=11 SCAR=24.0
Pod documented
205 182079 my ($self, $move, $seat) = @_;
206 182079 ▶ my %with = (defined $move && !ref $move ? (move => $move) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $move && !ref($move) ? : 182076 3 182079
Condition100%3/3
defined $move and not ref $move
!ll&&!rl&&r
21182076
MC/DC100%2/2
defined $move and not ref $move
defined $move not ref $move
Truth table100%3/3
A and not(B)
A= defined $moveB= not ref $move
ABresult
0X0
100
111
207  
208 182079 ▶ return $X->throw('game_over', %with) unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 18 182061 182079
209 182061 ▶ if (defined $seat) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $seat) 2416 179645 182061
210 2416 my $refused = $self->_seat_refusal($seat);
211 2416 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 2414 2 2416
212 2 ▶ return $X->throw('not_your_turn', %with) unless $seat eq $self->turn;
Branch: 1 branch
BranchTrueFalseTotal
unless $seat eq $self->turn 2 0 2
213 }
214  
215 179645 my $wire = move_parse($move);
216 179645 ▶ return $X->throw('bad_move', %with) unless defined $wire;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wire 11 179634 179645
217 179634 my ($from_name, $to_name) = move_split($wire);
218 179634 my ($from, $to) = (_square_of($from_name), _square_of($to_name));
219  
220 179634 my $game = $self->_game;
221 179634 my $why = $game->why_not($from, $to);
222 179634 ▶ return $X->throw($REFUSAL{$why} // 'bad_move', %with) unless $why == WHY_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $why == 0 2445 177189 179634
Condition50%1/2
$REFUSAL{$why} // "bad_move"
l!l
24450
MC/DC0%0/1
$REFUSAL{$why} // "bad_move"
$REFUSAL{$why}
Truth table50%1/2
A
A= $REFUSAL{$why}
Aresult
11
00
223  
224 177189 my $mv = $E->move($from, $to);
225 177189 my $about = $self->_about($mv);
226 177189 ▶ return $X->throw('bad_move', %with) unless $game->play($mv) == PLAY_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->play($mv) == 0 0 177189 177189
227  
228 177189 push @{ $self->_log }, $wire;
229 177189 push @{ $self->_shown }, move_display($wire, $about);
230 177189 $self->_offer(undef);
231 177189 return 0;
232 }
233  
234 41007 ▶sub play_or_die {
Subroutine play_or_die: called CC=3 SCAR=11.0
Pod documented
235 41007 my ($self, $move, $seat) = @_;
236 41007 my $refused = $self->play($move, $seat);
237 41007 ▶ Carp::croak('Game::Brandubh: ' . (defined $move ? "'$move'" : 'that') . ' was refused: ' . $refused->message)
Branch: 2 branches
BranchTrueFalseTotal
if $refused 1 41006 41007
defined $move ? : 1 0 1
238 if $refused;
239 41006 return $self;
240 }
241  
242 10299 ▶sub undo {
Subroutine undo: called CC=3 SCAR=11.0
Pod documented
243 10299 my ($self) = @_;
244 10299 ▶ if ($self->_ended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_ended) 3 10296 10299
245 3 $self->_ended(undef);
246 3 return 1;
247 }
248 10296 ▶ return 0 unless $self->_game->undo;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_game->undo 3 10293 10296
249 10293 pop @{ $self->_log };
250 10293 pop @{ $self->_shown };
251 10293 $self->_offer(undef);
252 10293 return 1;
253 }
254  
255 56 ▶sub resign {
Subroutine resign: called CC=4 SCAR=13.9
Pod documented
256 56 my ($self, $seat) = @_;
257 56 ▶ return $X->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 55 56
258 55 my $refused = $self->_seat_refusal($seat);
259 55 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 4 51 55
260  
261 51 ▶ my $winner = $self->side_of($seat) eq 'attackers' ? 'defenders' : 'attackers';
Branch: 1 branch
BranchTrueFalseTotal
$self->side_of($seat) eq 'attackers' ? : 41 10 51
262 51 $self->_ended(Game::Brandubh::Result->new(
263 how => 'resign',
264 winner => $winner,
265 seat => $self->seat_of($winner),
266 ply => $self->_game->ply,
267 position => $self->_game->position,
268 ));
269 51 $self->_offer(undef);
270 51 return 0;
271 }
272  
273 73 ▶sub offer_draw {
Subroutine offer_draw: called CC=4 SCAR=13.9
Pod documented
274 73 my ($self, $seat) = @_;
275 73 ▶ return $X->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 72 73
276 72 my $refused = $self->_seat_refusal($seat);
277 72 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 1 71 72
278 71 ▶ return $X->throw('offer_standing') if defined $self->_offer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_offer 2 69 71
279 69 $self->_offer($seat);
280 69 return 0;
281 }
282  
283 69 ▶sub _answer_refusal {
Subroutine _answer_refusal: called CC=5 SCAR=16.1
284 69 my ($self, $seat) = @_;
285 69 ▶ return $X->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 68 69
286 68 my $refused = $self->_seat_refusal($seat);
287 68 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 0 68 68
288 68 ▶ return $X->throw('no_offer') unless defined $self->_offer;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_offer 3 65 68
289 65 ▶ return $X->throw('own_offer') if $self->_offer eq $seat;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_offer eq $seat 2 63 65
290 63 return 0;
291 }
292  
293 25 ▶sub accept_draw {
Subroutine accept_draw: called CC=2 SCAR=6.9
Pod documented
294 25 my ($self, $seat) = @_;
295 25 my $refused = $self->_answer_refusal($seat);
296 25 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 4 21 25
297 21 $self->_ended(Game::Brandubh::Result->new(
298 how => 'agreed',
299 ply => $self->_game->ply,
300 position => $self->_game->position,
301 ));
302 21 $self->_offer(undef);
303 21 return 0;
304 }
305  
306 44 ▶sub decline_draw {
Subroutine decline_draw: called CC=2 SCAR=6.9
Pod documented
307 44 my ($self, $seat) = @_;
308 44 my $refused = $self->_answer_refusal($seat);
309 44 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 2 42 44
310 42 $self->_offer(undef);
311 42 return 0;
312 }
313  
314 2853 ▶sub draw_offered_by { $_[0]->_offer }
Subroutine draw_offered_by: called CC=1 SCAR=0.0
Pod documented
315  
316 4207 ▶sub result {
Subroutine result: called CC=7 SCAR=19.5
Pod documented
317 4207 my ($self) = @_;
318 4207 ▶ return $self->_ended if $self->_ended;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ended 121 4086 4207
319 4086 my $game = $self->_game;
320 4086 ▶ return undef unless $game->is_over;
Branch: 1 branch
BranchTrueFalseTotal
unless $game->is_over 174 3912 4086
321  
322 3912 my $outcome = $game->outcome;
323 3912 my $how = outcome_name($outcome);
324 3912 ▶ $how = 'edge' if $outcome == BY_CORNER && $self->_variant->escape eq 'edge';
Branch: 1 branch
BranchTrueFalseTotal
if $outcome == 1 and $self->_variant->escape eq "edge" 466 3446 3912
Condition100%3/3
$outcome == 1 and $self->_variant->escape eq "edge"
!ll&&!rl&&r
14372009466
MC/DC100%2/2
$outcome == 1 and $self->_variant->escape eq "edge"
$outcome == 1 $self->_variant->escape eq "edge"
Truth table100%3/3
A and B
A= $outcome == 1B= $self->_variant->escape eq "edge"
ABresult
0X0
100
111
325  
326 3912 my $side = $game->winner;
327 3912 ▶ my $winner = defined $side ? $SIDE[$side] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $side ? : 3382 530 3912
328 3912 ▶ return Game::Brandubh::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $winner ? : 3382 530 3912
329 how => $how,
330 (defined $winner ? (winner => $winner, seat => $self->seat_of($winner)) : ()),
331 ply => $game->ply,
332 position => $game->position,
333 );
334 }
335  
336 237 ▶sub winner {
Subroutine winner: called CC=2 SCAR=6.9
Pod documented
337 237 my ($self) = @_;
338 237 my $result = $self->result;
339 237 ▶ return $result ? $result->seat : undef;
Branch: 1 branch
BranchTrueFalseTotal
$result ? : 236 1 237
340 }
341  
342 16513 ▶sub position { $_[0]->_game->position }
Subroutine position: called CC=1 SCAR=0.0
Pod documented
343 6771 ▶sub signature { $_[0]->_game->key_hex }
Subroutine signature: called CC=1 SCAR=0.0
Pod documented
344 6051 ▶sub repeats { $_[0]->_game->repeats }
Subroutine repeats: called CC=1 SCAR=0.0
Pod documented
345 22560 ▶sub ply { $_[0]->_game->ply }
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
346 7250 ▶sub log { [ @{ $_[0]->_log } ] }
Subroutine log: called CC=1 SCAR=0.0
Pod documented
347 6581 ▶sub shown { [ @{ $_[0]->_shown } ] }
Subroutine shown: called CC=1 SCAR=0.0
Pod documented
348  
349 2425 ▶sub seed {
Subroutine seed: called CC=2 SCAR=6.9
Pod documented
350 2425 my ($self) = @_;
351 2425 ▶ return undef unless $self->status eq 'finished';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "finished" 2386 39 2425
352 39 return $self->_seed;
353 }
354  
355 2430 ▶sub at {
Subroutine at: called CC=2 SCAR=6.9
Pod documented
356 2430 my ($self, $name) = @_;
357 2430 my $square = _square_of($name);
358 2430 ▶ return undef unless defined $square;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $square 2 2428 2430
359 2428 return $PIECE[ $self->_game->at($square) ];
360 }
361  
362 13642 ▶sub pieces {
Subroutine pieces: called CC=3 SCAR=11.0
Pod documented
363 13642 my ($self) = @_;
364 13642 my $game = $self->_game;
365 13642 my %pieces;
366 13642 for my $square ($E->all_squares) {
367 668458 my $piece = $game->at($square);
368 668458 ▶ next if $piece == EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if $piece == 0 512471 155987 668458
369 155987 $pieces{ square_name($E->file_of($square), $E->rank_of($square)) } = $PIECE[$piece];
370 }
371 13642 return \%pieces;
372 }
373  
374 1051 ▶sub search {
Subroutine search: called CC=8 SCAR=21.6
Pod documented
375 1051 my ($self, %with) = @_;
376 1051 ▶ return undef unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 0 1051 1051
377  
378 1051 ▶ my $salt = defined $with{salt} ? $with{salt} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $with{'salt'} ? : 1051 0 1051
379 1051 ▶ Carp::croak('Game::Brandubh: a search salt is a string') if ref $salt;
Branch: 1 branch
BranchTrueFalseTotal
if ref $salt 1 1050 1051
380 1050 my $seed = unpack 'N', Digest::SHA::sha256(join '|', $salt, $self->turn, $self->_game->ply, $self->_game->key_hex);
381  
382 my $found = $self->_game->search(
383 budget => $with{budget},
384 seed => $seed || 1,
385 (defined $with{depth} ? (depth => $with{depth}) : ()),
386 1050 ▶ (defined $with{weights} ? (weights => $with{weights}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $with{'depth'} ? : 120 930 1050
defined $with{'weights'} ? : 0 1050 1050
Condition50%1/2
$seed || 1
l!l
10500
MC/DC0%0/1
$seed || 1
$seed
Truth table50%1/2
A
A= $seed
Aresult
11
00
387 );
388 1050 ▶ return undef unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 1050 1050
389 1050 my ($from, $to) = _names_of($found->{move});
390 return {
391 move => $from . $to,
392 score => $found->{score},
393 depth => $found->{depth},
394 nodes => $found->{nodes},
395 stopped => $found->{stopped},
396 1050 };
397 }
398  
399 1 ▶sub bot { require Game::Brandubh::Bot; return 'Game::Brandubh::Bot' }
Subroutine bot: called CC=1 SCAR=0.0
Pod documented
400  
401 1696 ▶sub _apply_result {
Subroutine _apply_result: called CC=9 SCAR=22.1
402 1696 my ($self, $result) = @_;
403 1696 ▶ return 0 unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1663 33 1696
404 33 ▶ return $X->throw('bad_move') unless ref $result eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $result eq "HASH" 0 33 33
405 33 ▶ my $how = $result->{how} // '';
Condition50%1/2
$result->{"how"} // ""
l!l
330
MC/DC0%0/1
$result->{"how"} // ""
$result->{"how"}
Truth table50%1/2
A
A= $result->{"how"}
Aresult
11
00
406 33 ▶ if ($how eq 'resign') {
Branch: 1 branch
BranchTrueFalseTotal
if ($how eq "resign") 22 11 33
407 22 return $self->resign($self->seat_of($result->{by}));
408 }
409 11 ▶ if ($how eq 'agreed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($how eq "agreed") 10 1 11
410 10 ▶ return $X->throw('game_over') unless $self->status eq 'active';
Branch: 1 branch
BranchTrueFalseTotal
unless $self->status eq "active" 1 9 10
411 9 my $first = $self->turn;
412 9 my $refused = $self->offer_draw($first);
413 9 ▶ return $refused if $refused;
Branch: 1 branch
BranchTrueFalseTotal
if $refused 0 9 9
414 9 ▶ return $self->accept_draw($first eq 'p1' ? 'p2' : 'p1');
Branch: 1 branch
BranchTrueFalseTotal
$first eq 'p1' ? : 5 4 9
415 }
416 1 return $X->throw('bad_move');
417 }
418  
419 1708 ▶sub _walk {
Subroutine _walk: called CC=7 SCAR=19.5
420 1708 my ($self, $moves, $result) = @_;
421 1708 for my $i (0 .. $#$moves) {
422 123105 my $refused = $self->play($moves->[$i]);
423 123105 ▶ return wantarray ? ($self, $refused, $i) : undef if $refused;
Branch: 2 branches
BranchTrueFalseTotal
if $refused 12 123093 123105
wantarray ? : 8 4 12
424 }
425 1696 my $refused = $self->_apply_result($result);
426 1696 ▶ return wantarray ? ($self, $refused, scalar @$moves) : undef if $refused;
Branch: 2 branches
BranchTrueFalseTotal
if $refused 3 1693 1696
wantarray ? : 3 0 3
427 1693 ▶ return wantarray ? ($self, 0, scalar @$moves) : $self;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 602 1091 1693
428 }
429  
430 1711 ▶sub replay {
Subroutine replay: called CC=8 SCAR=20.8
Pod documented
431 1711 my ($proto, @rest) = @_;
432  
433 1711 ▶ if (ref $proto) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $proto) 540 1171 1711
434 540 ▶ my $moves = ref $rest[0] eq 'ARRAY' ? $rest[0] : [ grep { defined } @rest ];
Branch: 1 branch
BranchTrueFalseTotal
ref $rest[0] eq 'ARRAY' ? : 540 0 540
435 540 ▶ my $fresh = (ref $proto)->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $proto->_seed ? : 8 532 540
436 attackers => $proto->attackers,
437 variant => $proto->_variant,
438 position => $proto->_start,
439 (defined $proto->_seed ? (seed => $proto->_seed) : ()),
440 );
441 540 return $fresh->_walk($moves);
442 }
443  
444 1171 ▶ Carp::croak('Game::Brandubh: replay takes name => value pairs') if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 1 1170 1171
445 1170 my %given = @rest;
446 1170 my $moves = delete $given{moves};
447 1170 my $result = delete $given{result};
448 1170 ▶ Carp::croak('Game::Brandubh: moves is an array reference') if defined $moves && ref $moves ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if defined $moves and ref $moves ne "ARRAY" 1 1169 1170
Condition100%3/3
defined $moves and ref $moves ne "ARRAY"
!ll&&!rl&&r
111681
MC/DC100%2/2
defined $moves and ref $moves ne "ARRAY"
defined $moves ref $moves ne "ARRAY"
Truth table100%3/3
A and B
A= defined $movesB= ref $moves ne "ARRAY"
ABresult
0X0
100
111
449 1169 my $fresh = $proto->new(%given);
450 1168 ▶ return $fresh->_walk($moves || [], $result);
Condition100%2/2
$moves || []
l!l
11671
MC/DC100%1/1
$moves || []
$moves
Truth table100%2/2
A
A= $moves
Aresult
11
00
451 }
452  
453 247 ▶sub as_text {
Subroutine as_text: called CC=3 SCAR=11.0
Pod documented
454 247 my ($self) = @_;
455 247 my $ended = $self->_ended;
456 247 my $result;
457 247 ▶ if ($ended) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ended) 51 196 247
458 51 ▶ $result = $ended->how eq 'resign' ? { how => 'resign', by => $ended->loser }
Branch: 1 branch
BranchTrueFalseTotal
$ended->how eq 'resign' ? : 35 16 51
459 : { how => 'agreed' };
460 }
461 247 return game_string({
462 variant => $self->_variant->as_string,
463 start => $self->_start,
464 moves => $self->_log,
465 result => $result,
466 });
467 }
468  
469 163 ▶sub from_text {
Subroutine from_text: called CC=6 SCAR=17.9
Pod documented
470 163 my ($class, $text, @rest) = @_;
471 163 ▶ Carp::croak('Game::Brandubh: from_text takes the text and then name => value pairs') if @rest % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @rest % 2 1 162 163
472 162 my $record = game_parse($text);
473 162 ▶ return wantarray ? (undef, $X->throw('bad_move'), 0) : undef unless $record;
Branch: 2 branches
BranchTrueFalseTotal
unless $record 9 153 162
wantarray ? : 5 4 9
474 153 my $variant = Game::Brandubh::Variant->from_string($record->{variant});
475 153 ▶ return wantarray ? (undef, $X->throw('bad_move'), 0) : undef unless $variant;
Branch: 2 branches
BranchTrueFalseTotal
unless $variant 4 149 153
wantarray ? : 2 2 4
476 return $class->replay(
477 @rest,
478 variant => $variant,
479 position => $record->{start},
480 moves => $record->{moves},
481 result => $record->{result},
482 149 );
483 }
484  
485 44 1;
486  
487 __END__
« blib/lib/Game/Brandubh/Notation.pm ↑ Summary blib/lib/Game/Brandubh/Result.pm »