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.01';
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 6482465 ▶sub _value {
Subroutine _value: called CC=4 SCAR=13.9
26 6482465 my ($side) = @_;
27 die 'side must be white or black, got ' . (defined $side ? "'$side'" : 'undef')
28 6482465 ▶ unless defined $side && exists $Game::Merrills::Board::VALUE{$side};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $side and exists $Game::Merrills::Board::VALUE{$side} 7 6482458 6482465
defined $side ? : 5 2 7
Condition100%3/3
defined $side and exists $Game::Merrills::Board::VALUE{$side}
!ll&&!rl&&r
256482458
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 6482458 return $Game::Merrills::Board::VALUE{$side};
30 }
31  
32 2622420 ▶sub _check {
Subroutine _check: called CC=3 SCAR=11.0
33 2622420 my ($position) = @_;
34 die 'a position is an arrayref of 26 values'
35 2622420 ▶ unless ref $position eq 'ARRAY' && @{$position} == 26;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $position eq "ARRAY" and @{$position;} == 26 6 2622414 2622420
Condition100%3/3
ref $position eq "ARRAY" and @{$position;} == 26
!ll&&!rl&&r
242622414
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 2622414 return $position;
37 }
38  
39 2030093 ▶sub _in_mill {
Subroutine _in_mill: called CC=6 SCAR=17.9
40 2030093 my ($position, $point) = @_;
41 2030093 ▶ my $value = $position->[$point] or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $position->[$point] or return 0 2030093 0 2030093
42 2030093 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$point] }) {
43 3743842 ▶ 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 542109 3201733 3743842
Condition100%6/6
$$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value and $$position[$mill->[2]] == $value
!ll&&!rl&&r
2730009471724542109
$$position[$mill->[0]] == $value and $$position[$mill->[1]] == $value
!ll&&!rl&&r
172994310000661013833
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 1487984 return 0;
48 }
49  
50 14495297 ▶sub _closes {
Subroutine _closes: called CC=7 SCAR=19.5
51 14495297 my ($position, $value, $from, $to) = @_;
52 14495297 my $closed = 0;
53 14495297 for my $mill (@{ $Game::Merrills::Points::MILLS_OF[$to] }) {
54 28990594 my $whole = 1;
55 28990594 for my $point (@{$mill}) {
56 43471819 ▶ next if $point == $to;
Branch: 1 branch
BranchTrueFalseTotal
if $point == $to 10731628 32740191 43471819
57 32740191 ▶ next if $position->[$point] == $value && !(defined $from && $point == $from);
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$point] == $value and not defined $from && $point == $from 4219564 28520627 32740191
Condition100%6/6
$position->[$point] == $value and not defined $from && $point == $from
!ll&&!rl&&r
2689256116280664219564
defined $from && $point == $from
!ll&&!rl&&r
272654514930191628066
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 28520627 $whole = 0;
59 28520627 last;
60 }
61 28990594 $closed += $whole;
62 }
63 14495297 return $closed;
64 }
65  
66 378051 ▶sub _removable {
Subroutine _removable: called CC=5 SCAR=16.1
67 378051 my ($position, $value) = @_;
68 378051 my (@loose, @all);
69 378051 for my $point (0 .. 23) {
70 9073224 ▶ next unless $position->[$point] == -$value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$point] == -$value 7043131 2030093 9073224
71 2030093 push @all, $point;
72 2030093 ▶ push @loose, $point unless _in_mill($position, $point);
Branch: 1 branch
BranchTrueFalseTotal
unless _in_mill($position, $point) 1487984 542109 2030093
73 }
74 378051 ▶ return @loose ? @loose : @all;
Branch: 1 branch
BranchTrueFalseTotal
@loose ? : 369940 8111 378051
75 }
76  
77 3266 ▶sub position {
Subroutine position: called CC=1 SCAR=0.0
Pod documented
78 3266 my ($board) = @_;
79 3266 return [ @{ $board->cells }, $board->in_hand('white'), $board->in_hand('black') ];
80 }
81  
82 5137 ▶sub board {
Subroutine board: called CC=1 SCAR=0.0
Pod documented
83 5137 my ($position) = @_;
84 5137 _check($position);
85 return Game::Merrills::Board->new(
86 5136 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 2616558 ▶sub _phase {
Subroutine _phase: called CC=5 SCAR=16.1
106 2616558 my ($position, $value, $flying) = @_;
107 2616558 ▶ return 'placing' if $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ];
Branch: 2 branches
BranchTrueFalseTotal
if $$position[$value > 0 ? 24 : 25] 1698302 918256 2616558
$value > 0 ? : 1260252 1356306 2616558
108 918256 ▶ return 'moving' unless $flying;
Branch: 1 branch
BranchTrueFalseTotal
unless $flying 18632 899624 918256
109 899624 my $men = grep { $_ == $value } @{$position}[ 0 .. 23 ];
110 899624 ▶ return $men == 3 ? 'flying' : 'moving';
Branch: 1 branch
BranchTrueFalseTotal
$men == 3 ? : 118736 780888 899624
111 }
112  
113 42693 ▶sub phase {
Subroutine phase: called CC=2 SCAR=6.9
Pod documented
114 42693 my ($position, $side, $flying) = @_;
115 42693 _check($position);
116 42693 ▶ return _phase($position, _value($side), defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 42692 1 42693
117 }
118  
119 996759 ▶sub generate {
Subroutine generate: called CC=14 SCAR=26.4
Pod documented
120 996759 my ($position, $side, $flying) = @_;
121 996759 _check($position);
122 996757 my $value = _value($side);
123 996755 ▶ my $phase = _phase($position, $value, defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 971420 25335 996755
124  
125 996755 my (@moves, @takes, $asked, @own);
126 996755 ▶ @own = grep { $position->[$_] == $value } 0 .. 23 if $phase eq 'flying';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "flying" 45281 951474 996755
127 996755 for my $to (0 .. 23) {
128 23922099 ▶ next if $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
if $position->[$to] 9877556 14044543 23922099
129 my @from = $phase eq 'placing' ? (undef)
130 : $phase eq 'flying' ? @own
131 14044543 ▶ : grep { $position->[$_] == $value } @{ $Game::Merrills::Points::ADJACENT[$to] };
Branch: 2 branches
BranchTrueFalseTotal
$phase eq 'placing' ? : 10102717 3941826 14044543
$phase eq 'flying' ? : 641217 3300609 3941826
132 14044543 for my $from (@from) {
133 my $flew = $phase eq 'flying'
134 14494604 ▶ && !grep { $_ == $from } @{ $Game::Merrills::Points::ADJACENT[$to] };
Condition100%3/3
$phase eq 'flying' && !grep({$_ == $from;} @{$Game::Merrills::Points::ADJACENT[$to];})
!ll&&!rl&&r
125709532234861700165
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 14494604 my $closed = _closes($position, $value, $from, $to);
136 14494604 ▶ unless ($closed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($closed) 14025231 469373 14494604
137 14025231 ▶ push @moves, [ $from, $to, undef, 0, $flew ? 1 : 0 ];
Branch: 1 branch
BranchTrueFalseTotal
$flew ? : 1683436 12341795 14025231
138 14025231 next;
139 }
140 469373 ▶ @takes = _removable($position, $value) unless $asked++;
Branch: 1 branch
BranchTrueFalseTotal
unless $asked++ 378031 91342 469373
141 469373 ▶ die 'a mill closed and there is no man to take' unless @takes;
Branch: 1 branch
BranchTrueFalseTotal
unless @takes 1 469372 469373
142 469372 ▶ push @moves, map { [ $from, $to, $_, $closed, $flew ? 1 : 0 ] } @takes;
Branch: 1 branch
BranchTrueFalseTotal
$flew ? : 66143 1830961 1897104
143 }
144 }
145 996754 return \@moves;
146 }
147  
148 1577110 ▶sub has_move {
Subroutine has_move: called CC=8 SCAR=20.8
Pod documented
149 1577110 my ($position, $side, $flying) = @_;
150 1577110 _check($position);
151 1577110 my $value = _value($side);
152 1577110 ▶ my $phase = _phase($position, $value, defined $flying ? $flying : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $flying ? : 1577110 0 1577110
153 1577110 ▶ if ($phase eq 'moving') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "moving") 474718 1102392 1577110
154 474718 for my $from (0 .. 23) {
155 2293985 ▶ next unless $position->[$from] == $value;
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$from] == $value 1621084 672901 2293985
156 672901 for my $to (@{ $Game::Merrills::Points::ADJACENT[$from] }) {
157 1209526 ▶ return 1 unless $position->[$to];
Branch: 1 branch
BranchTrueFalseTotal
unless $position->[$to] 474569 734957 1209526
158 }
159 }
160 149 return 0;
161 }
162 1102392 ▶ return scalar(grep { !$_ } @{$position}[ 0 .. 23 ]) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
scalar grep({not $_;} @{$position;}[0..23]) ? : 1102392 0 1102392
163 }
164  
165 1970212 ▶sub apply {
Subroutine apply: called CC=4 SCAR=13.9
Pod documented
166 1970212 my ($position, $side, $raw) = @_;
167 1970212 my $value = _value($side);
168 1970211 ▶ if (defined $raw->[RM_FROM]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->[0]) { } 686433 1283778 1970211
169 686433 $position->[ $raw->[RM_FROM] ] = 0;
170 }
171 else {
172 1283778 ▶ $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]--;
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 666594 617184 1283778
173 }
174 1970211 $position->[ $raw->[RM_TO] ] = $value;
175 1970211 ▶ $position->[ $raw->[RM_REMOVE] ] = 0 if defined $raw->[RM_REMOVE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw->[2] 845599 1124612 1970211
176 1970211 return $position;
177 }
178  
179 1894977 ▶sub unapply {
Subroutine unapply: called CC=4 SCAR=13.9
Pod documented
180 1894977 my ($position, $side, $raw) = @_;
181 1894977 my $value = _value($side);
182 1894976 ▶ $position->[ $raw->[RM_REMOVE] ] = -$value if defined $raw->[RM_REMOVE];
Branch: 1 branch
BranchTrueFalseTotal
if defined $raw->[2] 835209 1059767 1894976
183 1894976 $position->[ $raw->[RM_TO] ] = 0;
184 1894976 ▶ if (defined $raw->[RM_FROM]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $raw->[0]) { } 649790 1245186 1894976
185 649790 $position->[ $raw->[RM_FROM] ] = $value;
186 }
187 else {
188 1245186 ▶ $position->[ $value > 0 ? HAND_WHITE : HAND_BLACK ]++;
Branch: 1 branch
BranchTrueFalseTotal
$value > 0 ? : 647470 597716 1245186
189 }
190 1894976 return $position;
191 }
192  
193 83 1;
194  
195 __END__
« blib/lib/Game/Merrills/Result.pm ↑ Summary blib/lib/Game/Merrills.pm »