blib/lib/Game/Checkers/Rules.pm

filter: Statementstmt 100% 134 / 134 Branchbran 94.8% 74 / 78 Conditioncond 83.3% 15 / 18 MC/DCmcdc 54.5% 6 / 11 Subroutinesub 100% 14 / 14 Podpod 100% 5 / 5 totaltotal 95.3% 248 / 260 CC 53 SCAR 23.0
CC
53
Cov
97%
generate
15.0
has_move
14.0
_walk
12.0
CRAP 9.9
« blib/lib/Game/Checkers/Notation.pm ↑ Summary blib/lib/Game/Checkers/Result.pm »
1 package Game::Checkers::Rules;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 27 ▶use Game::Checkers::Squares;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 27 ▶use Game::Checkers::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 27 our $VERSION = '0.03';
10  
11 # The raw move layout is Move's, so there is one definition of it. These are the
12 # names the generator and the search read it by.
13 27 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 RM_FROM => Game::Checkers::Move::RM_FROM,
15 RM_TO => Game::Checkers::Move::RM_TO,
16 RM_PROMOTED => Game::Checkers::Move::RM_PROMOTED,
17 RM_PATH => Game::Checkers::Move::RM_PATH,
18 RM_CAPTURES => Game::Checkers::Move::RM_CAPTURES,
19 RM_CAPTURED => Game::Checkers::Move::RM_CAPTURED,
20 RM_KING => Game::Checkers::Move::RM_KING,
21 27 };
22  
23 27 my $STEP = \@Game::Checkers::Squares::STEP;
24 27 my $JUMP_OVER = \@Game::Checkers::Squares::JUMP_OVER;
25 27 my $JUMP_TO = \@Game::Checkers::Squares::JUMP_TO;
26  
27 27 my @ALL_DIRS = @Game::Checkers::Squares::DIRS;
28  
29 27 my %FORWARD = (
30 black => [Game::Checkers::Squares::SE, Game::Checkers::Squares::SW],
31 white => [Game::Checkers::Squares::NE, Game::Checkers::Squares::NW],
32 );
33  
34 1444412 ▶sub _side {
Subroutine _side: called CC=5 SCAR=19.2
35 1444412 my ($turn) = @_;
36 die "turn must be black or white, got " . (defined $turn ? "'$turn'" : 'undef')
37 1444412 ▶ unless defined $turn && $FORWARD{$turn};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $turn and $FORWARD{$turn} 0 1444412 1444412
defined $turn ? : 0 0 0
Condition33%1/3
defined $turn and $FORWARD{$turn}
!ll&&!rl&&r
001444412
MC/DC0%0/2
defined $turn and $FORWARD{$turn}
defined $turn $FORWARD{$turn}
Truth table33%1/3
A and B
A= defined $turnB= $FORWARD{$turn}
ABresult
0X0
100
111
38 1444412 ▶ return ($turn eq 'black' ? 1 : -1, $FORWARD{$turn});
Branch: 1 branch
BranchTrueFalseTotal
$turn eq 'black' ? : 740508 703904 1444412
39 }
40  
41 1442654 ▶sub generate {
Subroutine generate: called CC=15 SCAR=27.1
Pod documented
42 1442654 my ($position, $turn) = @_;
43 1442654 my ($sign, $forward) = _side($turn);
44 1442654 my (@jumps, @simple);
45 1442654 for my $from (1 .. 32) {
46 46164928 ▶ my $value = $position->[$from] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$from] or next 19652990 26511938 46164928
47 19652990 ▶ next unless ($value > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
unless ($value > 0 ? 1 : -1) == $sign 10001814 9651176 19652990
$value > 0 ? : 9783857 9869133 19652990
48 9651176 ▶ my $king = abs($value) == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 884657 8766519 9651176
49 9651176 ▶ my $dirs = $king ? \@ALL_DIRS : $forward;
Branch: 1 branch
BranchTrueFalseTotal
$king ? : 884657 8766519 9651176
50  
51 # the mover vacates its square for the whole sequence, so a king that
52 # jumps in a circle may land back on it
53 9651176 $position->[$from] = 0;
54 9651176 _walk($position, $from, $sign, $king, $dirs, [$from], [], [], \@jumps);
55 9651176 $position->[$from] = $value;
56  
57 9651176 ▶ next if @jumps;
Branch: 1 branch
BranchTrueFalseTotal
if @jumps 2096509 7554667 9651176
58 7554667 my $base = $from * 4;
59 7554667 for my $dir (@{$dirs}) {
60 16563538 ▶ my $to = $STEP->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $to = $$STEP[$base + $dir] or next 13845698 2717840 16563538
61 13845698 ▶ next if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 5256929 8588769 13845698
62 8588769 ▶ push @simple, _raw(
Branch: 2 branches
BranchTrueFalseTotal
!$king && ($sign > 0 ? $to >= 29 : $to <= 4) ? : 137748 8451021 8588769
$sign > 0 ? : 3599589 3161937 6761526
Condition100%3/3
not $king and $sign > 0 ? $to >= 29 : $to <= 4
!ll&&!rl&&r
18272436623778137748
MC/DC100%2/2
not $king and $sign > 0 ? $to >= 29 : $to <= 4
not $king $sign > 0 ? $to >= 29 : $to <= 4
Truth table100%3/3
not(A) and B
A= not $kingB= $sign > 0 ? $to >= 29 : $to <= 4
ABresult
0X0
100
111
63 [$from, $to], [], [],
64 (!$king && ($sign > 0 ? $to >= 29 : $to <= 4)) ? 1 : 0,
65 $king
66 );
67 }
68 }
69 1442654 ▶ return _sorted(@jumps ? \@jumps : \@simple);
Branch: 1 branch
BranchTrueFalseTotal
@jumps ? : 470740 971914 1442654
70 }
71  
72 10278125 ▶sub _walk {
Subroutine _walk: called CC=12 SCAR=24.8
73 10278125 my ($position, $square, $sign, $king, $dirs, $path, $captures, $values, $out) = @_;
74 10278125 my $base = $square * 4;
75 10278125 my $found = 0;
76 10278125 for my $dir (@{$dirs}) {
77 22542988 ▶ my $landing = $JUMP_TO->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $landing = $$JUMP_TO[$base + $dir] or next 15723252 6819736 22542988
78 15723252 ▶ next if $position->[$landing];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$landing] 6494177 9229075 15723252
79 9229075 my $over = $JUMP_OVER->[$base + $dir];
80 9229075 ▶ my $victim = $position->[$over] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $victim = $position->[$over] or next 3524486 5704589 9229075
81 3524486 ▶ next if ($victim > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
if ($victim > 0 ? 1 : -1) == $sign 2745266 779220 3524486
$victim > 0 ? : 1770325 1754161 3524486
82  
83 # a piece jumped may not be jumped twice in one sequence, and it stays
84 # on the board until the sequence ends, so it still blocks
85 779220 ▶ next if grep { $_ == $over } @{$captures};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $over;} @{$captures;} 108808 670412 779220
86  
87 670412 $found = 1;
88 670412 push @{$path}, $landing;
89 670412 push @{$captures}, $over;
90 670412 push @{$values}, $victim;
91  
92 670412 ▶ if (!$king && ($sign > 0 ? $landing >= 29 : $landing <= 4)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $king and $sign > 0 ? $landing >= 29 : $landing <= 4) { } 43463 626949 670412
elsif (not _walk($position, $landing, $sign, $king, $dirs, $path, $captures, $values, $out)) { } 540088 86861 626949
$sign > 0 ? : 287713 273987 561700
Condition100%3/3
not $king and $sign > 0 ? $landing >= 29 : $landing <= 4
!ll&&!rl&&r
10871251823743463
MC/DC100%2/2
not $king and $sign > 0 ? $landing >= 29 : $landing <= 4
not $king $sign > 0 ? $landing >= 29 : $landing <= 4
Truth table100%3/3
not(A) and B
A= not $kingB= $sign > 0 ? $landing >= 29 : $landing <= 4
ABresult
0X0
100
111
93 # crowning ends the turn, even with another jump available
94 43463 push @{$out}, _raw($path, $captures, $values, 1, $king);
95 } elsif (!_walk($position, $landing, $sign, $king, $dirs, $path, $captures, $values, $out)) {
96 540088 push @{$out}, _raw($path, $captures, $values, 0, $king);
97 }
98  
99 670412 pop @{$path};
100 670412 pop @{$captures};
101 670412 pop @{$values};
102 }
103 10278125 return $found;
104 }
105  
106 9172320 ▶sub _raw {
Subroutine _raw: called CC=1 SCAR=0.0
107 9172320 my ($path, $captures, $values, $promoted, $king) = @_;
108 9172320 my @raw;
109 9172320 $raw[RM_FROM] = $path->[0];
110 9172320 $raw[RM_TO] = $path->[-1];
111 9172320 $raw[RM_PROMOTED] = $promoted;
112 9172320 $raw[RM_PATH] = [@{$path}];
113 9172320 $raw[RM_CAPTURES] = [@{$captures}];
114 9172320 $raw[RM_CAPTURED] = [@{$values}];
115 9172320 $raw[RM_KING] = $king;
116 9172320 return \@raw;
117 }
118  
119 1442654 ▶sub _sorted {
Subroutine _sorted: called CC=3 SCAR=11.0
120 1442654 my ($moves) = @_;
121 return [
122 sort {
123 $a->[RM_FROM] <=> $b->[RM_FROM]
124 || $a->[RM_TO] <=> $b->[RM_TO]
125 12755772 ▶ || join('.', @{$a->[RM_PATH]}) cmp join('.', @{$b->[RM_PATH]})
Condition83%5/6
$Game::Checkers::Rules::a->[0] <=> $Game::Checkers::Rules::b->[0] || $Game::Checkers::Rules::a->[1] <=> $Game::Checkers::Rules::b->[1] || join('.', @{$Game::Checkers::Rules::a->[3];}) cmp join('.', @{$Game::Checkers::Rules::b->[3];})
l!l&&r!l&&!r
12755714580
$Game::Checkers::Rules::a->[0] <=> $Game::Checkers::Rules::b->[0] or $Game::Checkers::Rules::a->[1] <=> $Game::Checkers::Rules::b->[1]
l!l&&r!l&&!r
9910961284475358
MC/DC0%0/3
$Game::Checkers::Rules::a->[0] <=> $Game::Checkers::Rules::b->[0] || $Game::Checkers::Rules::a->[1] <=> $Game::Checkers::Rules::b->[1] || join('.', @{$Game::Checkers::Rules::a->[3];}) cmp join('.', @{$Game::Checkers::Rules::b->[3];})
$Game::Checkers::Rules::a->[0] <=> $Game::Checkers::Rules::b->[0] $Game::Checkers::Rules::a->[1] <=> $Game::Checkers::Rules::b->[1] join('.', @{$Game::Checkers::Rules::a->[3];}) cmp join('.', @{$Game::Checkers::Rules::b->[3];})
Truth table75%3/4
A or B || C
A= $Game::Checkers::Rules::a->[0] <=> $Game::Checkers::Rules::b->[0]B= $Game::Checkers::Rules::a->[1] <=> $Game::Checkers::Rules::b->[1]C= join('.', @{$Game::Checkers::Rules::a->[3];}) cmp join('.', @{$Game::Checkers::Rules::b->[3];})
ABCresult
1XX1
01X1
0011
0000
126 1442654 } @{$moves}
127 ];
128 }
129  
130 1522355 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
131 1522355 my ($position, $raw) = @_;
132 1522355 my $from = $raw->[RM_FROM];
133 1522355 my $value = $position->[$from];
134 1522355 $position->[$from] = 0;
135 1522355 $position->[$_] = 0 for @{$raw->[RM_CAPTURES]};
136 1522355 ▶ $position->[$raw->[RM_TO]] = $raw->[RM_PROMOTED]
Branch: 2 branches
BranchTrueFalseTotal
$raw->[2] ? : 70650 1451705 1522355
$value > 0 ? : 36004 34646 70650
137 ? ($value > 0 ? 2 : -2)
138 : $value;
139 1522355 return $raw;
140 }
141  
142 1480204 ▶sub unapply {
Subroutine unapply: called CC=4 SCAR=13.9
Pod documented
143 1480204 my ($position, $raw) = @_;
144 1480204 my $to = $raw->[RM_TO];
145 1480204 my $value = $position->[$to];
146 1480204 ▶ $value = $value > 0 ? 1 : -1 if $raw->[RM_PROMOTED];
Branch: 2 branches
BranchTrueFalseTotal
if $raw->[2] 68834 1411370 1480204
$value > 0 ? : 35130 33704 68834
147 1480204 $position->[$to] = 0;
148 1480204 $position->[$raw->[RM_FROM]] = $value;
149 1480204 my $captures = $raw->[RM_CAPTURES];
150 1480204 my $values = $raw->[RM_CAPTURED];
151 1480204 $position->[$captures->[$_]] = $values->[$_] for 0 .. $#{$captures};
152 1480204 return $raw;
153 }
154  
155 1758 ▶sub has_move {
Subroutine has_move: called CC=14 SCAR=26.4
Pod documented
156 1758 my ($position, $turn) = @_;
157 1758 my ($sign, $forward) = _side($turn);
158 1758 for my $from (1 .. 32) {
159 18633 ▶ my $value = $position->[$from] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$from] or next 8429 10204 18633
160 8429 ▶ next unless ($value > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
unless ($value > 0 ? 1 : -1) == $sign 4819 3610 8429
$value > 0 ? : 7129 1300 8429
161 3610 ▶ my $dirs = abs($value) == 2 ? \@ALL_DIRS : $forward;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 452 3158 3610
162 3610 my $base = $from * 4;
163 3610 for my $dir (@{$dirs}) {
164 6096 my $step = $STEP->[$base + $dir];
165 6096 ▶ return 1 if $step && !$position->[$step];
Branch: 1 branch
BranchTrueFalseTotal
if $step and not $position->[$step] 1690 4406 6096
Condition100%3/3
$step and not $position->[$step]
!ll&&!rl&&r
90435021690
MC/DC100%2/2
$step and not $position->[$step]
$step not $position->[$step]
Truth table100%3/3
A and not(B)
A= $stepB= not $position->[$step]
ABresult
0X0
100
111
166 4406 ▶ my $landing = $JUMP_TO->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $landing = $$JUMP_TO[$base + $dir] or next 3011 1395 4406
167 3011 ▶ next if $position->[$landing];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$landing] 1662 1349 3011
168 1349 ▶ my $victim = $position->[$JUMP_OVER->[$base + $dir]] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $victim = $$position[$$JUMP_OVER[$base + $dir]] or next 1349 0 1349
169 1349 ▶ return 1 if ($victim > 0 ? 1 : -1) != $sign;
Branch: 2 branches
BranchTrueFalseTotal
if ($victim > 0 ? 1 : -1) != $sign 51 1298 1349
$victim > 0 ? : 1308 41 1349
170 }
171 }
172 17 return 0;
173 }
174  
175 1470678 ▶sub progress {
Subroutine progress: called CC=3 SCAR=11.0
Pod documented
176 1470678 my ($raw) = @_;
177 1470678 ▶ return 1 if @{$raw->[RM_CAPTURES]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$raw->[4];} 509953 960725 1470678
178 960725 ▶ return $raw->[RM_KING] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$raw->[6] ? : 233467 727258 960725
179 }
180  
181 27 1;
182  
183 __END__
« blib/lib/Game/Checkers/Notation.pm ↑ Summary blib/lib/Game/Checkers/Result.pm »