blib/lib/Game/Merrills/Rules.pm

filter: Statementstmt 100% 136 / 136 Branchbran 95.9% 71 / 74 Conditioncond 100% 21 / 21 MC/DCmcdc 100% 12 / 12 Subroutinesub 100% 22 / 22 Podpod 100% 9 / 9 totaltotal 98.9% 271 / 274 CC 53 SCAR 18.5
CC
53
Cov
99%
generate
14.0
has_move
8.0
_closes
7.0
CRAP 6.4
« blib/lib/Game/Merrills/Result.pm ↑ Summary blib/lib/Game/Merrills.pm »
1 package Game::Merrills::Rules;
2  
3 83 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 83 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 83 ▶use Game::Merrills::Points;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 83 ▶use Game::Merrills::Board;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 83 ▶use Game::Merrills::Move;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 83 our $VERSION = '0.02';
11  
12 83 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
13 83 HAND_WHITE => 24,
14 HAND_BLACK => 25,
15 83 };
16  
17 83 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 83 RM_FROM => Game::Merrills::Move::RM_FROM,
19 RM_TO => Game::Merrills::Move::RM_TO,
20 RM_REMOVE => Game::Merrills::Move::RM_REMOVE,
21 RM_CLOSES => Game::Merrills::Move::RM_CLOSES,
22 RM_FLEW => Game::Merrills::Move::RM_FLEW,
23 83 };
24  
25 7707743 ▶sub _value {
Subroutine _value: called CC=4 SCAR=13.9
26 7707743 my ($side) = @_;
27 die 'side must be white or black, got ' . (defined $side ? "'$side'" : 'undef')
28 7707743 ▶ unless defined $side && exists $Game::Merrills::Board::VALUE{$side};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $side and exists $Game::Merrills::Board::VALUE{$side} 7 7707736 7707743
defined $side ? : 5 2 7
Condition100%3/3
defined $side and exists $Game::Merrills::Board::VALUE{$side}
!ll&&!rl&&r
257707736
MC/DC100%2/2
defined $side and exists $Game::Merrills::Board::VALUE{$side}
defined $side exists $Game::Merrills::Board::VALUE{$side}
Truth table100%3/3
A and B
A= defined $sideB= exists $Game::Merrills::Board::VALUE{$side}
ABresult
0X0
100
111
29 7707736 return $Game::Merrills::Board::VALUE{$side};
30 }
31  
32 2976084 ▶sub _check {
Subroutine _check: called CC=3 SCAR=11.0
33 2976084 my ($position) = @_;
34 die 'a position is an arrayref of 26 values'
35 2976084 ▶ unless ref $position eq 'ARRAY' && @{$position} == 26;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "ARRAY" and @{$position;} == 26 6 2976078 2976084
Condition100%3/3
ref $position eq "ARRAY" and @{$position;} == 26
!ll&&!rl&&r
242976078
MC/DC100%2/2
ref $position eq "ARRAY" and @{$position;} == 26
ref $position eq "ARRAY" @{$position;} == 26
Truth table100%3/3
A and B
A= ref $position eq "ARRAY"B= @{$position;} == 26
ABresult
0X0
100
111
36 2976078 return $position;
37 }
38  
39 2233797 ▶sub _in_mill {
Subroutine _in_mill: called CC=6 SCAR=17.9
40 2233797 my ($position, $point) = @_;
41 2233797 ▶ my $value = $position->[$point] or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$point] or return 0 2233797 0 2233797
42 2233797 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$point] }) {
43 4123584 ▶ return 1 if $position->[ $mill->[0] ] == $value
Branch: 1 branch
BranchTrueFalseTotal
if $$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value and $$position[$mill->[2]] == $value 602738 3520846 4123584
Condition100%6/6
$$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value and $$position[$mill->[2]] == $value
!ll&&!rl&&r
3042347478499602738
$$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value
!ll&&!rl&&r
192965511126921081237
MC/DC100%3/3
$$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value and $$position[$mill->[2]] == $value
$$position[$mill->[0]] == $value $$position[$mill->[1]] == $value $$position[$mill->[2]] == $value
Truth table100%4/4
A and B and C
A= $$position[$mill->[0]] == $valueB= $$position[$mill->[1]] == $valueC= $$position[$mill->[2]] == $value
ABCresult
0XX0
10X0
1100
1111
44 && $position->[ $mill->[1] ] == $value
45 && $position->[ $mill->[2] ] == $value;
46 }
47 1631059 return 0;
48 }
49  
50 18751122 ▶sub _closes {
Subroutine _closes: called CC=7 SCAR=19.5
51 18751122 my ($position, $value, $from, $to) = @_;
52 18751122 my $closed = 0;
53 18751122 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$to] }) {
54 37502244 my $whole = 1;
55 37502244 for my $point (@{$mill}) {
56 55860664 ▶ next if $point == $to;
Branch: 1 branch
BranchTrueFalseTotal
if $point == $to 13870124 41990540 55860664
57 41990540 ▶ next if $position->[$point] == $value && !(defined $from && $point == $from);
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$point] == $value and not defined $from && $point == $from 5090887 36899653 41990540
Condition100%6/6
$position->[$point] == $value and not defined $from && $point == $from
!ll&&!rl&&r
3459199123076625090887
defined $from && $point == $from
!ll&&!rl&&r
273129523595922307662
MC/DC100%3/3
$position->[$point] == $value and not defined $from && $point == $from
$position->[$point] == $value defined $from $point == $from
Truth table100%4/4
A and not(B && C)
A= $position->[$point] == $valueB= defined $fromC= $point == $from
ABCresult
0XX0
1110
10X1
1101
58 36899653 $whole = 0;
59 36899653 last;
60 }
61 37502244 $closed += $whole;
62 }
63 18751122 return $closed;
64 }
65  
66 457055 ▶sub _removable {
Subroutine _removable: called CC=5 SCAR=16.1
67 457055 my ($position, $value) = @_;
68 457055 my (@loose, @all);
69 457055 for my $point (0 .. 23) {
70 10969320 ▶ next unless $position->[$point] == -$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$point] == -$value 8735523 2233797 10969320
71 2233797 push @all, $point;
72 2233797 ▶ push @loose, $point unless _in_mill($position, $point);
Branch: 1 branch
BranchTrueFalseTotal
unless _in_mill($position, $point) 1631059 602738 2233797
73 }
74 457055 ▶ return @loose ? @loose : @all;
Branch: 1 branch
BranchTrueFalseTotal
@loose ? : 418900 38155 457055
75 }
76  
77 3337 ▶sub position {
Subroutine position: called CC=1 SCAR=0.0
Pod documented
78 3337 my ($board) = @_;
79 3337 return [ @{ $board->cells }, $board->in_hand('white'), $board->in_hand('black') ];
80 }
81  
82 5040 ▶sub board {
Subroutine board: called CC=1 SCAR=0.0
Pod documented
83 5040 my ($position) = @_;
84 5040 _check($position);
85 return Game::Merrills::Board->new(
86 5039 cells => [ @{$position}[ 0 .. 23 ] ],
87 hand => { white => $position->[HAND_WHITE], black => $position->[HAND_BLACK] }
88 );
89 }
90  
91 697 ▶sub closes {
Subroutine closes: called CC=2 SCAR=6.9
Pod documented
92 697 my ($position, $side, $from, $to) = @_;
93 697 _check($position);
94 696 Game::Merrills::Points::_check($to);
95 695 ▶ Game::Merrills::Points::_check($from) if defined $from;
Branch: 1 branch
BranchTrueFalseTotal
if defined $from 62 633 695
96 694 return _closes($position, _value($side), $from, $to);
97 }
98  
99 24 ▶sub removable {
Subroutine removable: called CC=1 SCAR=0.0
Pod documented
100 24 my ($position, $side) = @_;
101 24 _check($position);
102 22 return _removable($position, _value($side));
103 }
104  
105 2970319 ▶sub _phase {
Subroutine _phase: called CC=5 SCAR=16.1
106 2970319 my ($position, $value, $flying) = @_;
107 2970319 ▶ return 'placing' if $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ];
Branch: 2 branches
BranchTrueFalseTotal
if $$position[$value > 0 ? 24 : 25] 1699557 1270762 2970319
$value > 0 ? : 1328014 1642305 2970319
108 1270762 ▶ return 'moving' unless $flying;
Branch: 1 branch
BranchTrueFalseTotal
unless $flying 18632 1252130 1270762
109 1252130 my $men = grep { $_ == $value } @{$position}[ 0 .. 23 ];
110 1252130 ▶ return $men == 3 ? 'flying' : 'moving';
Branch: 1 branch
BranchTrueFalseTotal
$men == 3 ? : 271903 980227 1252130
111 }
112  
113 42637 ▶sub phase {
Subroutine phase: called CC=2 SCAR=6.9
Pod documented
114 42637 my ($position, $side, $flying) = @_;
115 42637 _check($position);
116 42637 ▶ return _phase($position, _value($side), defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 42636 1 42637
117 }
118  
119 1130438 ▶sub generate {
Subroutine generate: called CC=14 SCAR=26.4
Pod documented
120 1130438 my ($position, $side, $flying) = @_;
121 1130438 _check($position);
122 1130436 my $value = _value($side);
123 1130434 ▶ my $phase = _phase($position, $value, defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 1105099 25335 1130434
124  
125 1130434 my (@moves, @takes, $asked, @own);
126 1130434 ▶ @own = grep { $position->[$_] == $value } 0 .. 23 if $phase eq 'flying';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "flying" 111747 1018687 1130434
127 1130434 for my $to (0 .. 23) {
128 27130395 ▶ next if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 10463368 16667027 27130395
129 my @from = $phase eq 'placing' ? (undef)
130 : $phase eq 'flying' ? @own
131 16667027 ▶ : grep { $position->[$_] == $value } @{ $Game::Merrills::Points::ADJACENT[$to] };
Branch: 2 branches
BranchTrueFalseTotal
$phase eq 'placing' ? : 10101236 6565791 16667027
$phase eq 'flying' ? : 1825212 4740579 6565791
132 16667027 for my $from (@from) {
133 my $flew = $phase eq 'flying'
134 18750429 ▶ && !grep { $_ == $from } @{ $Game::Merrills::Points::ADJACENT[$to] };
Condition100%3/3
$phase eq 'flying' && !grep({$_ == $from;} @{$Game::Merrills::Points::ADJACENT[$to];})
!ll&&!rl&&r
132747936036344872002
MC/DC100%2/2
$phase eq 'flying' && !grep({$_ == $from;} @{$Game::Merrills::Points::ADJACENT[$to];})
$phase eq 'flying' !grep({$_ == $from;} @{$Game::Merrills::Points::ADJACENT[$to];})
Truth table100%3/3
A && not(B)
A= $phase eq 'flying'B= !grep({$_ == $from;} @{$Game::Merrills::Points::ADJACENT[$to];})
ABresult
0X0
100
111
135 18750429 my $closed = _closes($position, $value, $from, $to);
136 18750429 ▶ unless ($closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 18148426 602003 18750429
137 18148426 ▶ push @moves, [ $from, $to, undef, 0, $flew ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$flew ? : 4818073 13330353 18148426
138 18148426 next;
139 }
140 602003 ▶ @takes = _removable($position, $value) unless $asked++;
Branch: 1 branch
BranchTrueFalseTotal
unless $asked++ 457035 144968 602003
141 602003 ▶ die 'a mill closed and there is no man to take' unless @takes;
Branch: 1 branch
BranchTrueFalseTotal
unless @takes 1 602002 602003
142 602002 ▶ push @moves, map { [ $from, $to, $_, $closed, $flew ? 1 : 0 ] } @takes;
Branch: 1 branch
BranchTrueFalseTotal
$flew ? : 207044 2080728 2287772
143 }
144 }
145 1130433 return \@moves;
146 }
147  
148 1797248 ▶sub has_move {
Subroutine has_move: called CC=8 SCAR=20.8
Pod documented
149 1797248 my ($position, $side, $flying) = @_;
150 1797248 _check($position);
151 1797248 my $value = _value($side);
152 1797248 ▶ my $phase = _phase($position, $value, defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 1797248 0 1797248
153 1797248 ▶ if ($phase eq 'moving') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "moving") 607239 1190009 1797248
154 607239 for my $from (0 .. 23) {
155 3501869 ▶ next unless $position->[$from] == $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$from] == $value 2710543 791326 3501869
156 791326 for my $to (@{ $Game::Merrills::Points::ADJACENT[$from] }) {
157 1303371 ▶ return 1 unless $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$to] 607116 696255 1303371
158 }
159 }
160 123 return 0;
161 }
162 1190009 ▶ return scalar(grep { !$_ } @{$position}[ 0 .. 23 ]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({not $_;} @{$position;}[0..23]) ? : 1190009 0 1190009
163 }
164  
165 2406004 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
166 2406004 my ($position, $side, $raw) = @_;
167 2406004 my $value = _value($side);
168 2406003 ▶ if (defined $raw->[RM_FROM]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->[0]) { } 1121488 1284515 2406003
169 1121488 $position->[ $raw->[RM_FROM] ] = 0;
170 }
171 else {
172 1284515 ▶ $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]--;
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 668128 616387 1284515
173 }
174 2406003 $position->[ $raw->[RM_TO] ] = $value;
175 2406003 ▶ $position->[ $raw->[RM_REMOVE] ] = 0 if defined $raw->[RM_REMOVE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw->[2] 936847 1469156 2406003
176 2406003 return $position;
177 }
178  
179 2330702 ▶sub unapply {
Subroutine unapply: called CC=4 SCAR=13.9
Pod documented
180 2330702 my ($position, $side, $raw) = @_;
181 2330702 my $value = _value($side);
182 2330701 ▶ $position->[ $raw->[RM_REMOVE] ] = -$value if defined $raw->[RM_REMOVE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw->[2] 926450 1404251 2330701
183 2330701 $position->[ $raw->[RM_TO] ] = 0;
184 2330701 ▶ if (defined $raw->[RM_FROM]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->[0]) { } 1084778 1245923 2330701
185 1084778 $position->[ $raw->[RM_FROM] ] = $value;
186 }
187 else {
188 1245923 ▶ $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]++;
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 649004 596919 1245923
189 }
190 2330701 return $position;
191 }
192  
193 83 1;
194  
195 __END__
« blib/lib/Game/Merrills/Result.pm ↑ Summary blib/lib/Game/Merrills.pm »