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/Terminal.pm ↑ Summary blib/lib/Game/Checkers/Board.pm »
1 package Game::Checkers::Rules;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 30 ▶use Game::Checkers::Squares;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Game::Checkers::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 30 our $VERSION = '0.04';
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 30 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 30 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 30 };
22  
23 30 my $STEP = \@Game::Checkers::Squares::STEP;
24 30 my $JUMP_OVER = \@Game::Checkers::Squares::JUMP_OVER;
25 30 my $JUMP_TO = \@Game::Checkers::Squares::JUMP_TO;
26  
27 30 my @ALL_DIRS = @Game::Checkers::Squares::DIRS;
28  
29 30 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 2190307 ▶sub _side {
Subroutine _side: called CC=5 SCAR=19.2
35 2190307 my ($turn) = @_;
36 die "turn must be black or white, got " . (defined $turn ? "'$turn'" : 'undef')
37 2190307 ▶ unless defined $turn && $FORWARD{$turn};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $turn and $FORWARD{$turn} 0 2190307 2190307
defined $turn ? : 0 0 0
Condition33%1/3
defined $turn and $FORWARD{$turn}
!ll&&!rl&&r
002190307
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 2190307 ▶ return ($turn eq 'black' ? 1 : -1, $FORWARD{$turn});
Branch: 1 branch
BranchTrueFalseTotal
$turn eq 'black' ? : 1107424 1082883 2190307
39 }
40  
41 2186949 ▶sub generate {
Subroutine generate: called CC=15 SCAR=27.1
Pod documented
42 2186949 my ($position, $turn) = @_;
43 2186949 my ($sign, $forward) = _side($turn);
44 2186949 my (@jumps, @simple);
45 2186949 for my $from (1 .. 32) {
46 69982368 ▶ my $value = $position->[$from] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$from] or next 26384884 43597484 69982368
47 26384884 ▶ next unless ($value > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
unless ($value > 0 ? 1 : -1) == $sign 13395393 12989491 26384884
$value > 0 ? : 13160259 13224625 26384884
48 12989491 ▶ my $king = abs($value) == 2 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 2252683 10736808 12989491
49 12989491 ▶ my $dirs = $king ? \@ALL_DIRS : $forward;
Branch: 1 branch
BranchTrueFalseTotal
$king ? : 2252683 10736808 12989491
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 12989491 $position->[$from] = 0;
54 12989491 _walk($position, $from, $sign, $king, $dirs, [$from], [], [], \@jumps);
55 12989491 $position->[$from] = $value;
56  
57 12989491 ▶ next if @jumps;
Branch: 1 branch
BranchTrueFalseTotal
if @jumps 2669167 10320324 12989491
58 10320324 my $base = $from * 4;
59 10320324 for my $dir (@{$dirs}) {
60 24444536 ▶ my $to = $STEP->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $to = $$STEP[$base + $dir] or next 20455146 3989390 24444536
61 20455146 ▶ next if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 6647517 13807629 20455146
62 13807629 ▶ push @simple, _raw(
Branch: 2 branches
BranchTrueFalseTotal
!$king && ($sign > 0 ? $to >= 29 : $to <= 4) ? : 203458 13604171 13807629
$sign > 0 ? : 4566918 4004853 8571771
Condition100%3/3
not $king and $sign > 0 ? $to >= 29 : $to <= 4
!ll&&!rl&&r
52358588368313203458
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 2186949 ▶ return _sorted(@jumps ? \@jumps : \@simple);
Branch: 1 branch
BranchTrueFalseTotal
@jumps ? : 642635 1544314 2186949
70 }
71  
72 13831909 ▶sub _walk {
Subroutine _walk: called CC=12 SCAR=24.8
73 13831909 my ($position, $square, $sign, $king, $dirs, $path, $captures, $values, $out) = @_;
74 13831909 my $base = $square * 4;
75 13831909 my $found = 0;
76 13831909 for my $dir (@{$dirs}) {
77 32623100 ▶ my $landing = $JUMP_TO->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $landing = $$JUMP_TO[$base + $dir] or next 22312301 10310799 32623100
78 22312301 ▶ next if $position->[$landing];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$landing] 8290792 14021509 22312301
79 14021509 my $over = $JUMP_OVER->[$base + $dir];
80 14021509 ▶ my $victim = $position->[$over] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $victim = $position->[$over] or next 4710641 9310868 14021509
81 4710641 ▶ next if ($victim > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
if ($victim > 0 ? 1 : -1) == $sign 3581548 1129093 4710641
$victim > 0 ? : 2337901 2372740 4710641
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 1129093 ▶ next if grep { $_ == $over } @{$captures};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ == $over;} @{$captures;} 227070 902023 1129093
86  
87 902023 $found = 1;
88 902023 push @{$path}, $landing;
89 902023 push @{$captures}, $over;
90 902023 push @{$values}, $victim;
91  
92 902023 ▶ if (!$king && ($sign > 0 ? $landing >= 29 : $landing <= 4)) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $king and $sign > 0 ? $landing >= 29 : $landing <= 4) { } 59605 842418 902023
elsif (not _walk($position, $landing, $sign, $king, $dirs, $path, $captures, $values, $out)) { } 725653 116765 842418
$sign > 0 ? : 347621 327444 675065
Condition100%3/3
not $king and $sign > 0 ? $landing >= 29 : $landing <= 4
!ll&&!rl&&r
22695861546059605
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 59605 push @{$out}, _raw($path, $captures, $values, 1, $king);
95 } elsif (!_walk($position, $landing, $sign, $king, $dirs, $path, $captures, $values, $out)) {
96 725653 push @{$out}, _raw($path, $captures, $values, 0, $king);
97 }
98  
99 902023 pop @{$path};
100 902023 pop @{$captures};
101 902023 pop @{$values};
102 }
103 13831909 return $found;
104 }
105  
106 14592887 ▶sub _raw {
Subroutine _raw: called CC=1 SCAR=0.0
107 14592887 my ($path, $captures, $values, $promoted, $king) = @_;
108 14592887 my @raw;
109 14592887 $raw[RM_FROM] = $path->[0];
110 14592887 $raw[RM_TO] = $path->[-1];
111 14592887 $raw[RM_PROMOTED] = $promoted;
112 14592887 $raw[RM_PATH] = [@{$path}];
113 14592887 $raw[RM_CAPTURES] = [@{$captures}];
114 14592887 $raw[RM_CAPTURED] = [@{$values}];
115 14592887 $raw[RM_KING] = $king;
116 14592887 return \@raw;
117 }
118  
119 2186949 ▶sub _sorted {
Subroutine _sorted: called CC=3 SCAR=11.0
120 2186949 my ($moves) = @_;
121 return [
122 sort {
123 $a->[RM_FROM] <=> $b->[RM_FROM]
124 || $a->[RM_TO] <=> $b->[RM_TO]
125 21387845 ▶ || 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
21387777680
$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
15302766608501168
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 2186949 } @{$moves}
127 ];
128 }
129  
130 2331761 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
131 2331761 my ($position, $raw) = @_;
132 2331761 my $from = $raw->[RM_FROM];
133 2331761 my $value = $position->[$from];
134 2331761 $position->[$from] = 0;
135 2331761 $position->[$_] = 0 for @{$raw->[RM_CAPTURES]};
136 2331761 ▶ $position->[$raw->[RM_TO]] = $raw->[RM_PROMOTED]
Branch: 2 branches
BranchTrueFalseTotal
$raw->[2] ? : 101872 2229889 2331761
$value > 0 ? : 54062 47810 101872
137 ? ($value > 0 ? 2 : -2)
138 : $value;
139 2331761 return $raw;
140 }
141  
142 2241767 ▶sub unapply {
Subroutine unapply: called CC=4 SCAR=13.9
Pod documented
143 2241767 my ($position, $raw) = @_;
144 2241767 my $to = $raw->[RM_TO];
145 2241767 my $value = $position->[$to];
146 2241767 ▶ $value = $value > 0 ? 1 : -1 if $raw->[RM_PROMOTED];
Branch: 2 branches
BranchTrueFalseTotal
if $raw->[2] 97697 2144070 2241767
$value > 0 ? : 52023 45674 97697
147 2241767 $position->[$to] = 0;
148 2241767 $position->[$raw->[RM_FROM]] = $value;
149 2241767 my $captures = $raw->[RM_CAPTURES];
150 2241767 my $values = $raw->[RM_CAPTURED];
151 2241767 $position->[$captures->[$_]] = $values->[$_] for 0 .. $#{$captures};
152 2241767 return $raw;
153 }
154  
155 3358 ▶sub has_move {
Subroutine has_move: called CC=14 SCAR=26.4
Pod documented
156 3358 my ($position, $turn) = @_;
157 3358 my ($sign, $forward) = _side($turn);
158 3358 for my $from (1 .. 32) {
159 30836 ▶ my $value = $position->[$from] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$from] or next 13971 16865 30836
160 13971 ▶ next unless ($value > 0 ? 1 : -1) == $sign;
Branch: 2 branches
BranchTrueFalseTotal
unless ($value > 0 ? 1 : -1) == $sign 7809 6162 13971
$value > 0 ? : 11504 2467 13971
161 6162 ▶ my $dirs = abs($value) == 2 ? \@ALL_DIRS : $forward;
Branch: 1 branch
BranchTrueFalseTotal
abs $value == 2 ? : 1278 4884 6162
162 6162 my $base = $from * 4;
163 6162 for my $dir (@{$dirs}) {
164 10521 my $step = $STEP->[$base + $dir];
165 10521 ▶ return 1 if $step && !$position->[$step];
Branch: 1 branch
BranchTrueFalseTotal
if $step and not $position->[$step] 3267 7254 10521
Condition100%3/3
$step and not $position->[$step]
!ll&&!rl&&r
194353113267
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 7254 ▶ my $landing = $JUMP_TO->[$base + $dir] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $landing = $$JUMP_TO[$base + $dir] or next 4552 2702 7254
167 4552 ▶ next if $position->[$landing];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$landing] 2512 2040 4552
168 2040 ▶ my $victim = $position->[$JUMP_OVER->[$base + $dir]] or next;
Branch: 1 branch
BranchTrueFalseTotal
my $victim = $$position[$$JUMP_OVER[$base + $dir]] or next 2040 0 2040
169 2040 ▶ return 1 if ($victim > 0 ? 1 : -1) != $sign;
Branch: 2 branches
BranchTrueFalseTotal
if ($victim > 0 ? 1 : -1) != $sign 67 1973 2040
$victim > 0 ? : 1925 115 2040
170 }
171 }
172 24 return 0;
173 }
174  
175 2231504 ▶sub progress {
Subroutine progress: called CC=3 SCAR=11.0
Pod documented
176 2231504 my ($raw) = @_;
177 2231504 ▶ return 1 if @{$raw->[RM_CAPTURES]};
Branch: 1 branch
BranchTrueFalseTotal
if @{$raw->[4];} 690340 1541164 2231504
178 1541164 ▶ return $raw->[RM_KING] ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$raw->[6] ? : 611522 929642 1541164
179 }
180  
181 30 1;
182  
183 __END__
« blib/lib/Game/Checkers/Terminal.pm ↑ Summary blib/lib/Game/Checkers/Board.pm »