blib/lib/Game/Merrills.pm

filter: Statementstmt 99.6% 281 / 282 Branchbran 95.9% 117 / 122 Conditioncond 96.9% 32 / 33 MC/DCmcdc 95.2% 20 / 21 Subroutinesub 100% 48 / 48 Podpod 95.6% 22 / 23 totaltotal 98.3% 520 / 529 CC 76 SCAR 17.7
CC
76
Cov
98%
_diagnose
15.0
_parse
12.0
_play
10.0
CRAP 5.9
« blib/lib/Game/Merrills/Rules.pm ↑ Summary blib/lib/Game/Merrills/Board.pm »
1 package Game::Merrills;
2  
3 80 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 80 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 80 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 80 our $VERSION = '0.02';
8  
9 80 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 80 ▶use Object::Proto::Sugar -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 80 ▶use Game::Merrills::Points;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 80 ▶use Game::Merrills::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 80 ▶use Game::Merrills::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 80 ▶use Game::Merrills::Notation;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 80 ▶use Game::Merrills::Rules;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 80 ▶use Game::Merrills::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 80 ▶use Game::Merrills::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 80 ▶use constant NO_MILL_PLIES => 120;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 80 our $NO_MILL_LIMIT = NO_MILL_PLIES;
22  
23 80 my %OTHER = (white => 'black', black => 'white');
24 80 my $OPENING = '........................ w 9 9 0 0';
25  
26 has [qw/position turn/] => (
27 is => 'rw',
28 isa => Str
29 80 );
30  
31 has flying => (
32 is => 'rw',
33 isa => Bool,
34 default => 1
35 80 );
36  
37 has [qw/result draw_offered_by _legal _position/] => (
38 is => 'rw'
39 80 );
40  
41 has [qw/history _undo/] => (
42 is => 'rw',
43 isa => ArrayRef,
44 40248 ▶ default => sub { [] }
Subroutine __ANON__: called CC=1 SCAR=0.0
45 80 );
46  
47 has [qw/no_mill _base_ply/] => (
48 is => 'rw',
49 isa => Int,
50 default => 0
51 80 );
52  
53 has repetition => (
54 is => 'rw',
55 isa => HashRef,
56 20124 ▶ default => sub { {} }
Subroutine __ANON__: called CC=1 SCAR=0.0
57 80 );
58  
59 20124 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
60 20124 my ($self) = @_;
61 20124 ▶ my $start = Game::Merrills::Notation::parse_position(
Branch: 1 branch
BranchTrueFalseTotal
defined $self->position ? : 17924 2200 20124
62 defined $self->position ? $self->position : $OPENING
63 );
64 20122 $self->position(Game::Merrills::Notation::format_position($start));
65 20122 $self->_position([ @{ $start->{cells} }, @{ $start->{hand} }{qw/white black/} ]);
66 20122 $self->turn($start->{turn});
67 20122 $self->no_mill($start->{no_mill});
68 20122 $self->_base_ply($start->{ply});
69 20122 ▶ $self->flying($self->flying ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$self->flying ? : 15153 4969 20122
70 20122 ▶ $self->repetition->{ $self->_key }++ if $self->_counting;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_counting 159 19963 20122
71 20122 $self->_check_end;
72 20122 return $self;
73 }
74  
75 80913 ▶sub status {
Subroutine status: called CC=2 SCAR=6.9
Pod documented
76 80913 ▶ return $_[0]->result ? 'finished' : 'active';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->result ? : 558 80355 80913
77 }
78  
79 125071 ▶sub ply {
Subroutine ply: called CC=1 SCAR=0.0
Pod documented
80 125071 return $_[0]->_base_ply + scalar @{ $_[0]->history };
81 }
82  
83 5038 ▶sub board {
Subroutine board: called CC=1 SCAR=0.0
Pod documented
84 5038 return Game::Merrills::Rules::board($_[0]->_position);
85 }
86  
87 191735 ▶sub in_hand {
Subroutine in_hand: called CC=2 SCAR=6.9
Pod documented
88 191735 my ($self, $side) = @_;
89 191735 ▶ return $self->_position->[
Branch: 1 branch
BranchTrueFalseTotal
$self->_side($side) eq 'white' ? : 95887 95848 191735
90 $self->_side($side) eq 'white'
91 ? Game::Merrills::Rules::HAND_WHITE
92 : Game::Merrills::Rules::HAND_BLACK
93 ];
94 }
95  
96 187016 ▶sub on_board {
Subroutine on_board: called CC=1 SCAR=0.0
Pod documented
97 187016 my ($self, $side) = @_;
98 187016 my $value = $Game::Merrills::Board::VALUE{ $self->_side($side) };
99 187016 return scalar grep { $_ == $value } @{ $self->_position }[ 0 .. 23 ];
100 }
101  
102 185612 ▶sub men {
Subroutine men: called CC=1 SCAR=0.0
Pod documented
103 185612 my ($self, $side) = @_;
104 185612 return $self->on_board($side) + $self->in_hand($side);
105 }
106  
107 42631 ▶sub phase {
Subroutine phase: called CC=1 SCAR=0.0
Pod documented
108 42631 return $_[0]->phase_of($_[0]->turn);
109 }
110  
111 42634 ▶sub phase_of {
Subroutine phase_of: called CC=1 SCAR=0.0
Pod documented
112 42634 my ($self, $side) = @_;
113 42634 return Game::Merrills::Rules::phase($self->_position, $self->_side($side), $self->flying);
114 }
115  
116 191358 ▶sub legal_moves {
Subroutine legal_moves: called CC=3 SCAR=11.0
Pod documented
117 191358 my ($self) = @_;
118 191358 my $legal = $self->_legal;
119 191358 ▶ return $legal if $legal;
Branch: 1 branch
BranchTrueFalseTotal
if $legal 97730 93628 191358
120 93628 $legal = [];
121 93628 ▶ unless ($self->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->result) 93628 0 93628
122 93628 my $turn = $self->turn;
123 $legal = [
124 1369337 map { Game::Merrills::Move->from_raw($_, $turn) }
125 93628 @{ Game::Merrills::Rules::generate($self->_position, $turn, $self->flying) }
126 ];
127 }
128 93628 $self->_legal($legal);
129 93628 return $legal;
130 }
131  
132 2 ▶sub legal_moves_for {
Subroutine legal_moves_for: called CC=2 SCAR=7.0
Pod documented
133 2 my ($self, $point) = @_;
134 2 Game::Merrills::Points::_check($point);
135 return [
136 2 ▶ grep { (defined $_->from ? $_->from : $_->to) == $point } @{ $self->legal_moves }
Branch: 1 branch
BranchTrueFalseTotal
defined $_->from ? : 16 0 16
137 ];
138 }
139  
140 89070 ▶sub move {
Subroutine move: called CC=7 SCAR=19.5
Pod documented
141 89070 my ($self, $move) = @_;
142 89070 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 4 89066 89070
143  
144 89066 ▶ my $want = $self->_parse($move)
Branch: 1 branch
BranchTrueFalseTotal
my $want = $self->_parse($move) or return $self->_error("not_a_move") 89050 16 89066
145 or return $self->_error('not_a_move');
146  
147 89050 for my $legal (@{ $self->legal_moves }) {
148 789130 ▶ next unless $legal->to == $want->{to};
Branch: 1 branch
BranchTrueFalseTotal
unless $legal->to == $want->{"to"} 671117 118013 789130
149 118013 ▶ next unless _same($legal->from, $want->{from});
Branch: 1 branch
BranchTrueFalseTotal
unless _same($legal->from, $want->{"from"}) 25283 92730 118013
150 92730 ▶ next unless _same($legal->remove, $want->{remove});
Branch: 1 branch
BranchTrueFalseTotal
unless _same($legal->remove, $want->{"remove"}) 20592 72138 92730
151 72138 return $self->_play($legal);
152 }
153 16912 return $self->_diagnose($want);
154 }
155  
156 3601 ▶sub undo {
Subroutine undo: called CC=5 SCAR=16.1
Pod documented
157 3601 my ($self) = @_;
158 3601 ▶ my $record = pop @{ $self->_undo }
Branch: 1 branch
BranchTrueFalseTotal
my $record = pop @{$self->_undo;} or return $self->_error("nothing_to_undo") 3559 42 3601
159 or return $self->_error('nothing_to_undo');
160 3559 my $move = pop @{ $self->history };
161  
162 3559 ▶ if ($record->{counted}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($record->{"counted"}) 2839 720 3559
163 2839 my $key = $self->_key;
164 2839 ▶ delete $self->repetition->{$key} unless --$self->repetition->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless --$self->repetition->{$key} 2828 11 2839
165 }
166 3559 ▶ $self->repetition($record->{repetition}) if $record->{repetition};
Branch: 1 branch
BranchTrueFalseTotal
if $record->{"repetition"} 1061 2498 3559
167  
168 3559 $self->turn($OTHER{ $self->turn });
169 3559 Game::Merrills::Rules::unapply($self->_position, $self->turn, $record->{raw});
170 3559 $self->no_mill($record->{no_mill});
171 3559 $self->draw_offered_by($record->{draw_offered_by});
172 3559 $self->result(undef);
173 3559 $self->_legal(undef);
174 3559 return $move;
175 }
176  
177 19 ▶sub resign {
Subroutine resign: called CC=2 SCAR=6.9
Pod documented
178 19 my ($self, $side) = @_;
179 19 $side = $self->_side($side);
180 18 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 17 18
181 17 return $self->_finish($OTHER{$side}, 'resign');
182 }
183  
184 3 ▶sub timeout {
Subroutine timeout: called CC=2 SCAR=6.9
Pod documented
185 3 my ($self, $side) = @_;
186 3 $side = $self->_side($side);
187 3 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 2 3
188 2 return $self->_finish($OTHER{$side}, 'timeout');
189 }
190  
191 12 ▶sub offer_draw {
Subroutine offer_draw: called CC=2 SCAR=6.9
Pod documented
192 12 my ($self, $side) = @_;
193 12 $side = $self->_side($side);
194 12 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 11 12
195 11 $self->draw_offered_by($side);
196 11 return $side;
197 }
198  
199 10 ▶sub accept_draw {
Subroutine accept_draw: called CC=4 SCAR=13.9
Pod documented
200 10 my ($self, $side) = @_;
201 10 $side = $self->_side($side);
202 10 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 9 10
203 9 my $offered = $self->draw_offered_by;
204 9 ▶ return $self->_error('no_offer') unless $offered && $offered ne $side;
Branch: 1 branch
BranchTrueFalseTotal
unless $offered and $offered ne $side 5 4 9
Condition100%3/3
$offered and $offered ne $side
!ll&&!rl&&r
414
MC/DC100%2/2
$offered and $offered ne $side
$offered $offered ne $side
Truth table100%3/3
A and B
A= $offeredB= $offered ne $side
ABresult
0X0
100
111
205 4 return $self->_finish(undef, 'agreement');
206 }
207  
208 6 ▶sub decline_draw {
Subroutine decline_draw: called CC=4 SCAR=13.9
Pod documented
209 6 my ($self, $side) = @_;
210 6 $side = $self->_side($side);
211 6 ▶ return $self->_error('game_over') if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 1 5 6
212 5 my $offered = $self->draw_offered_by;
213 5 ▶ return $self->_error('no_offer') unless $offered && $offered ne $side;
Branch: 1 branch
BranchTrueFalseTotal
unless $offered and $offered ne $side 2 3 5
Condition100%3/3
$offered and $offered ne $side
!ll&&!rl&&r
113
MC/DC100%2/2
$offered and $offered ne $side
$offered $offered ne $side
Truth table100%3/3
A and B
A= $offeredB= $offered ne $side
ABresult
0X0
100
111
214 3 $self->draw_offered_by(undef);
215 3 return $side;
216 }
217  
218 24244 ▶sub to_position {
Subroutine to_position: called CC=1 SCAR=0.0
Pod documented
219 24244 my ($self) = @_;
220 24244 my $position = $self->_position;
221 return Game::Merrills::Notation::format_position({
222 24244 cells => [ @{$position}[ 0 .. 23 ] ],
223 turn => $self->turn,
224 hand => {
225 white => $position->[Game::Merrills::Rules::HAND_WHITE],
226 black => $position->[Game::Merrills::Rules::HAND_BLACK],
227 },
228 no_mill => $self->no_mill,
229 ply => $self->ply,
230 });
231 }
232  
233 3 ▶sub from_position {
Subroutine from_position: called CC=1 SCAR=0.0
Pod documented
234 3 my ($class, $position, %option) = @_;
235 3 return $class->new(%option, position => $position);
236 }
237  
238 43 ▶sub to_text {
Subroutine to_text: called CC=2 SCAR=6.9
Pod documented
239 43 my ($self) = @_;
240 return Game::Merrills::Notation::format_record(
241 43 ▶ [ map { $_->notation } @{ $self->history } ],
Branch: 1 branch
BranchTrueFalseTotal
$self->position eq $OPENING ? : 41 2 43
242 $self->position eq $OPENING ? () : (position => $self->position)
243 );
244 }
245  
246 18 ▶sub from_text {
Subroutine from_text: called CC=4 SCAR=13.9
Pod documented
247 18 my ($class, $text, %option) = @_;
248 18 my $moves = Game::Merrills::Notation::parse_record($text);
249 16 my $position = Game::Merrills::Notation::record_position($text);
250 16 ▶ my $self = $class->new(%option, defined $position ? (position => $position) : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $position ? : 4 12 16
251 16 my $number = 0;
252 16 for my $move (@{$moves}) {
253 399 $number++;
254 399 my $played = $self->move($move);
255 399 ▶ next unless ref $played eq 'Game::Merrills::Error';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $played eq "Game::Merrills::Error" 395 4 399
256 4 die "illegal record: move $number, '"
257 . Game::Merrills::Notation::format_move($move) . "': " . $played->message . "\n";
258 }
259 12 return $self;
260 }
261  
262 17714 ▶sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod documented
263 17714 my ($self) = @_;
264 17714 my $clone = ref($self)->new(position => $self->position, flying => $self->flying);
265 17714 $clone->_position([ @{ $self->_position } ]);
266 17714 $clone->turn($self->turn);
267 17714 $clone->history([ @{ $self->history } ]);
268 $clone->_undo([
269 map {
270 536604 ▶ { %{$_}, $_->{repetition} ? (repetition => { %{ $_->{repetition} } }) : () }
Branch: 1 branch
BranchTrueFalseTotal
$_->{'repetition'} ? : 309869 226735 536604
271 17714 } @{ $self->_undo }
272 ]);
273 17714 $clone->no_mill($self->no_mill);
274 17714 $clone->repetition({ %{ $self->repetition } });
275 17714 $clone->draw_offered_by($self->draw_offered_by);
276 17714 $clone->result($self->result);
277 17714 $clone->_legal(undef);
278 17714 return $clone;
279 }
280  
281 211020 ▶sub _same {
Subroutine _same: called CC=3 SCAR=11.0
282 211020 my ($one, $two) = @_;
283 211020 ▶ return defined $one ? (defined $two && $one == $two) : !defined $two;
Branch: 1 branch
BranchTrueFalseTotal
defined $one ? : 99990 111030 211020
Condition100%3/3
defined $two and $one == $two
!ll&&!rl&&r
20824060057308
MC/DC100%2/2
defined $two and $one == $two
defined $two $one == $two
Truth table100%3/3
A and B
A= defined $twoB= $one == $two
ABresult
0X0
100
111
284 }
285  
286 421435 ▶sub _side {
Subroutine _side: called CC=3 SCAR=11.0
287 421435 my ($self, $side) = @_;
288 421435 ▶ $side = $self->turn unless defined $side;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $side 19 421416 421435
289 421435 ▶ die "side must be white or black, got '$side'" unless $OTHER{$side};
Branch: 1 branch
BranchTrueFalseTotal
unless $OTHER{$side} 1 421434 421435
290 421434 return $side;
291 }
292  
293 40824 ▶sub _key {
Subroutine _key: called CC=1 SCAR=0.0
294 40824 my ($self) = @_;
295 40824 return join '', @{ $self->_position }[ 0 .. 23 ], $self->turn;
296 }
297  
298 92260 ▶sub _counting {
Subroutine _counting: called CC=2 SCAR=6.9
299 92260 my $position = $_[0]->_position;
300 92260 ▶ return !$position->[Game::Merrills::Rules::HAND_WHITE]
Condition100%3/3
$position->[24] || $position->[25]
l!l&&r!l&&!r
52381189437985
MC/DC100%2/2
$position->[24] || $position->[25]
$position->[24] $position->[25]
Truth table100%3/3
A || B
A= $position->[24]B= $position->[25]
ABresult
1X1
011
000
301 && !$position->[Game::Merrills::Rules::HAND_BLACK];
302 }
303  
304 16986 ▶sub _error {
Subroutine _error: called CC=1 SCAR=0.0
305 16986 my ($self, $flag, %extra) = @_;
306 16986 return Game::Merrills::Error->throw(
307 $flag,
308 legal => $self->legal_moves,
309 %extra
310 );
311 }
312  
313 89066 ▶sub _parse {
Subroutine _parse: called CC=12 SCAR=24.9
314 89066 my ($self, $move) = @_;
315 89066 ▶ if (ref $move eq 'Game::Merrills::Move') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $move eq "Game::Merrills::Move") 70655 18411 89066
316 70655 $move = { from => $move->from, to => $move->to, remove => $move->remove };
317 }
318 89066 ▶ return Game::Merrills::Notation::parse_move($move) unless ref $move;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move 242 88824 89066
319 88824 ▶ return undef unless ref $move eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $move eq "HASH" 1 88823 88824
320  
321 88823 my %want;
322 88823 for my $part (qw/from to remove/) {
323 266465 my $point = $move->{$part};
324 266465 ▶ next unless defined $point;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $point 114134 152331 266465
325 152331 ▶ return undef if ref $point;
Branch: 1 branch
BranchTrueFalseTotal
if ref $point 1 152330 152331
326 152330 ▶ $point = Game::Merrills::Points::point($point) if $point =~ m/^[a-g][1-7]$/i;
Branch: 1 branch
BranchTrueFalseTotal
if $point =~ /^[a-g][1-7]$/i 5 152325 152330
327 152330 ▶ return undef unless defined $point && $point =~ m/^[0-9]+$/ && $point < 24;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $point and $point =~ /^[0-9]+$/ and $point < 24 3 152327 152330
Condition83%5/6
defined $point and $point =~ /^[0-9]+$/ and $point < 24
!ll&&!rl&&r
21152327
defined $point and $point =~ /^[0-9]+$/
!ll&&!rl&&r
02152328
MC/DC66%2/3
defined $point and $point =~ /^[0-9]+$/ and $point < 24
defined $point $point =~ /^[0-9]+$/ $point < 24
Truth table75%3/4
A and B and C
A= defined $pointB= $point =~ /^[0-9]+$/C= $point < 24
ABCresult
0XX0
10X0
1100
1111
328 152327 $want{$part} = $point + 0;
329 }
330 88819 ▶ return undef unless defined $want{to};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $want{"to"} 1 88818 88819
331 88818 return \%want;
332 }
333  
334 16912 ▶sub _diagnose {
Subroutine _diagnose: called CC=15 SCAR=27.1
335 16912 my ($self, $want) = @_;
336 16912 my ($from, $to, $remove) = @{$want}{qw/from to remove/};
337 16912 my $position = $self->_position;
338 16912 my $side = $self->turn;
339 16912 my $value = $Game::Merrills::Board::VALUE{$side};
340 16912 my $phase = $self->phase;
341  
342 16912 ▶ if (defined $from) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) { } 11826 5086 16912
343 11826 ▶ return $self->_error('men_in_hand') if $phase eq 'placing';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "placing" 3732 8094 11826
344 8094 ▶ return $self->_error('not_your_man') unless $position->[$from] == $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$from] == $value 6073 2021 8094
345 2021 ▶ return $self->_error('occupied') if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 1107 914 2021
346 914 ▶ return $self->_error('not_adjacent')
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "moving" and not Game::Merrills::Points::is_adjacent($from, $to) 858 56 914
Condition100%3/3
$phase eq "moving" and not Game::Merrills::Points::is_adjacent($from, $to)
!ll&&!rl&&r
1244858
MC/DC100%2/2
$phase eq "moving" and not Game::Merrills::Points::is_adjacent($from, $to)
$phase eq "moving" not Game::Merrills::Points::is_adjacent($from, $to)
Truth table100%3/3
A and not(B)
A= $phase eq "moving"B= not Game::Merrills::Points::is_adjacent($from, $to)
ABresult
0X0
100
111
347 if $phase eq 'moving' && !Game::Merrills::Points::is_adjacent($from, $to);
348 }
349 else {
350 5086 ▶ return $self->_error('no_men_in_hand') unless $phase eq 'placing';
Branch: 1 branch
BranchTrueFalseTotal
unless $phase eq "placing" 3958 1128 5086
351 1128 ▶ return $self->_error('occupied') if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 699 429 1128
352 }
353  
354 485 my $closes = Game::Merrills::Rules::closes($position, $side, $from, $to);
355 485 ▶ unless (defined $remove) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $remove) 58 427 485
356 58 ▶ return $self->_error('not_legal') unless $closes;
Branch: 1 branch
BranchTrueFalseTotal
unless $closes 0 58 58
357 return $self->_error('must_remove', legal => [
358 58 ▶ grep { $_->to == $to && _same($_->from, $from) } @{ $self->legal_moves }
Condition100%3/3
$_->to == $to && _same($_->from, $from)
!ll&&!rl&&r
75427250
MC/DC100%2/2
$_->to == $to && _same($_->from, $from)
$_->to == $to _same($_->from, $from)
Truth table100%3/3
A && B
A= $_->to == $toB= _same($_->from, $from)
ABresult
0X0
100
111
359 ]);
360 }
361 427 ▶ return $self->_error('nothing_to_remove') unless $closes;
Branch: 1 branch
BranchTrueFalseTotal
unless $closes 409 18 427
362 18 ▶ return $self->_error('no_man_there') unless $position->[$remove] == -$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$remove] == -$value 12 6 18
363 return $self->_error('man_in_mill')
364 6 ▶ unless grep { $_ == $remove } Game::Merrills::Rules::removable($position, $side);
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ == $remove;} Game::Merrills::Rules::removable($position, $side) 6 0 6
365 0 return $self->_error('not_legal');
366 }
367  
368 72138 ▶sub _play {
Subroutine _play: called CC=10 SCAR=23.0
369 72138 my ($self, $move) = @_;
370 72138 my $raw = $move->to_raw;
371 72138 my $mover = $self->turn;
372 72138 my $position = $self->_position;
373  
374 72138 my %record = (
375 raw => $raw,
376 no_mill => $self->no_mill,
377 draw_offered_by => $self->draw_offered_by,
378 );
379 72138 push @{ $self->history }, $move;
380  
381 72138 Game::Merrills::Rules::apply($position, $mover, $raw);
382 72138 $self->_legal(undef);
383  
384 72138 my $offered = $self->draw_offered_by;
385 72138 ▶ $self->draw_offered_by(undef) if $offered && $offered ne $mover;
Branch: 1 branch
BranchTrueFalseTotal
if $offered and $offered ne $mover 3 72135 72138
Condition100%3/3
$offered and $offered ne $mover
!ll&&!rl&&r
7213323
MC/DC100%2/2
$offered and $offered ne $mover
$offered $offered ne $mover
Truth table100%3/3
A and B
A= $offeredB= $offered ne $mover
ABresult
0X0
100
111
386  
387 72138 ▶ if ($move->is_placement || $move->is_capture) {
Branch: 1 branch
BranchTrueFalseTotal
if ($move->is_placement or $move->is_capture) { } 41777 30361 72138
Condition100%3/3
$move->is_placement or $move->is_capture
l!l&&r!l&&!r
36190558730361
MC/DC100%2/2
$move->is_placement or $move->is_capture
$move->is_placement $move->is_capture
Truth table100%3/3
A or B
A= $move->is_placementB= $move->is_capture
ABresult
1X1
011
000
388 41777 $record{repetition} = $self->repetition;
389 41777 $self->repetition({});
390 41777 $self->no_mill(0);
391 }
392 else {
393 30361 $self->no_mill($self->no_mill + 1);
394 }
395 72138 $self->turn($OTHER{$mover});
396  
397 72138 my $count = 0;
398 72138 ▶ if ($self->_counting) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_counting) 37826 34312 72138
399 37826 $count = ++$self->repetition->{ $self->_key };
400 37826 $record{counted} = 1;
401 }
402 72138 push @{ $self->_undo }, \%record;
403  
404 72138 $self->_check_end;
405 72138 ▶ return $move if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 306 71832 72138
406  
407 71832 ▶ if ($count >= 3) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count >= 3) { } 9 71823 71832
elsif ($Game::Merrills::NO_MILL_LIMIT and $self->no_mill >= $Game::Merrills::NO_MILL_LIMIT) { } 3 71827 71830
408 9 $self->_finish(undef, 'repetition');
409 }
410 ▶ elsif ($NO_MILL_LIMIT && $self->no_mill >= $NO_MILL_LIMIT) {
Condition100%3/3
$Game::Merrills::NO_MILL_LIMIT and $self->no_mill >= $Game::Merrills::NO_MILL_LIMIT
!ll&&!rl&&r
7718133
MC/DC100%2/2
$Game::Merrills::NO_MILL_LIMIT and $self->no_mill >= $Game::Merrills::NO_MILL_LIMIT
$Game::Merrills::NO_MILL_LIMIT $self->no_mill >= $Game::Merrills::NO_MILL_LIMIT
Truth table100%3/3
A and B
A= $Game::Merrills::NO_MILL_LIMITB= $self->no_mill >= $Game::Merrills::NO_MILL_LIMIT
ABresult
0X0
100
111
411 3 $self->_finish(undef, 'no_mill');
412 }
413 71832 return $move;
414 }
415  
416 92260 ▶sub _check_end {
Subroutine _check_end: called CC=5 SCAR=16.1
417 92260 my ($self) = @_;
418 92260 ▶ return if $self->result;
Branch: 1 branch
BranchTrueFalseTotal
if $self->result 0 92260 92260
419 92260 my $turn = $self->turn;
420 92260 ▶ return $self->_finish($OTHER{$turn}, 'few') if $self->men($turn) < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->men($turn) < 3 298 91962 92260
421 91962 ▶ return $self->_finish($turn, 'few') if $self->men($OTHER{$turn}) < 3;
Branch: 1 branch
BranchTrueFalseTotal
if $self->men($OTHER{$turn}) < 3 1 91961 91962
422 91961 ▶ return if Game::Merrills::Rules::has_move($self->_position, $turn, $self->flying);
Branch: 1 branch
BranchTrueFalseTotal
if Game::Merrills::Rules::has_move($self->_position, $turn, $self->flying) 91950 11 91961
423 11 return $self->_finish($OTHER{$turn}, 'blocked');
424 }
425  
426 345 ▶sub _finish {
Subroutine _finish: called CC=1 SCAR=0.0
427 345 my ($self, $winner, $reason) = @_;
428 345 $self->result(Game::Merrills::Result->new(
429 winner => $winner,
430 reason => $reason
431 ));
432 345 $self->_legal([]);
433 345 return $self->result;
434 }
435  
436 80 1;
437  
438 __END__
« blib/lib/Game/Merrills/Rules.pm ↑ Summary blib/lib/Game/Merrills/Board.pm »