blib/lib/Game/Checkers/Squares.pm

filter: Statementstmt 93.6% 88 / 94 Branchbran 77.5% 31 / 40 Conditioncond 71.4% 30 / 42 MC/DCmcdc 52.1% 12 / 23 Subroutinesub 90.9% 20 / 22 Podpod 100% 12 / 12 totaltotal 82.8% 193 / 233 CC 35 SCAR 15.3
CC
35
Cov
85%
dir_index
7.9
crowning
7.1
_check
6.6
CRAP 4.6
« blib/lib/Game/Checkers.pm ↑ Summary blib/lib/Game/Checkers/Piece.pm »
1 package Game::Checkers::Squares;
2  
3 28 ▶use strict;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 29 our $VERSION = '0.03';
7  
8 28 ▶use constant {
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 28 NE => 0,
10 NW => 1,
11 SE => 2,
12 SW => 3,
13 28 };
14  
15 29 our (@DIRS, @DIR_NAME, %DIR_INDEX, @STEP, @JUMP_OVER, @JUMP_TO, @ROW, @COL, @DELTA, %FORWARD);
16  
17 29 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
18 29 @DIRS = (NE, NW, SE, SW);
19 29 @DIR_NAME = qw/NE NW SE SW/;
20 29 %DIR_INDEX = (NE => NE, NW => NW, SE => SE, SW => SW);
21  
22 29 @DELTA = ([-1, 1], [-1, -1], [1, 1], [1, -1]);
23  
24 29 %FORWARD = (
25 black => [SE, SW],
26 white => [NE, NW],
27 );
28 }
29  
30 410 ▶sub _check {
Subroutine _check: called CC=6 SCAR=18.8
31 410 my ($n) = @_;
32 410 ▶ die "square must be 1 .. 32, got " . (defined $n ? "'$n'" : 'undef')
Branch: 2 branches
BranchTrueFalseTotal
unless defined $n and $n =~ /^[0-9]+$/ and $n >= 1 and $n <= 32 2 408 410
defined $n ? : 2 0 2
Condition66%6/9
defined $n and $n =~ /^[0-9]+$/ and $n >= 1 and $n <= 32
!ll&&!rl&&r
11408
defined $n and $n =~ /^[0-9]+$/ and $n >= 1
!ll&&!rl&&r
01409
defined $n and $n =~ /^[0-9]+$/
!ll&&!rl&&r
00410
MC/DC50%2/4
defined $n and $n =~ /^[0-9]+$/ and $n >= 1 and $n <= 32
defined $n $n =~ /^[0-9]+$/ $n >= 1 $n <= 32
Truth table60%3/5
A and B and C and D
A= defined $nB= $n =~ /^[0-9]+$/C= $n >= 1D= $n <= 32
ABCDresult
0XXX0
10XX0
110X0
11100
11111
33 unless defined $n && $n =~ m/^[0-9]+$/ && $n >= 1 && $n <= 32;
34 408 return $n;
35 }
36  
37 928 ▶sub _coords {
Subroutine _coords: called CC=2 SCAR=6.9
38 928 my ($n) = @_;
39 928 my $row = int(($n - 1) / 4);
40 928 my $i = ($n - 1) % 4;
41 928 ▶ return ($row, $row % 2 == 0 ? ($i * 2) + 1 : $i * 2);
Branch: 1 branch
BranchTrueFalseTotal
$row % 2 == 0 ? : 464 464 928
42 }
43  
44 10302 ▶sub _square {
Subroutine _square: called CC=6 SCAR=17.9
45 10302 my ($row, $col) = @_;
46 10302 ▶ return 0 if $row < 0 || $row > 7 || $col < 0 || $col > 7;
Branch: 1 branch
BranchTrueFalseTotal
if $row < 0 or $row > 7 or $col < 0 or $col > 7 1627 8675 10302
Condition100%9/9
$row < 0 or $row > 7 or $col < 0 or $col > 7
l!l&&r!l&&!r
12493788675
$row < 0 or $row > 7 or $col < 0
l!l&&r!l&&!r
8723779053
$row < 0 or $row > 7
l!l&&r!l&&!r
4364369430
MC/DC100%4/4
$row < 0 or $row > 7 or $col < 0 or $col > 7
$row < 0 $row > 7 $col < 0 $col > 7
Truth table100%5/5
A or B or C or D
A= $row < 0B= $row > 7C= $col < 0D= $col > 7
ABCDresult
1XXX1
01XX1
001X1
00011
00000
47 8675 ▶ return 0 unless ($row + $col) % 2;
Branch: 1 branch
BranchTrueFalseTotal
unless ($row + $col) % 2 1828 6847 8675
48 6847 return ($row * 4) + ($col >> 1) + 1;
49 }
50  
51 29 for my $n (1 .. 32) {
52 928 my ($row, $col) = _coords($n);
53 928 $ROW[$n] = $row;
54 928 $COL[$n] = $col;
55 928 for my $dir (@DIRS) {
56 3712 my ($dr, $dc) = @{$DELTA[$dir]};
57 3712 my $i = ($n * 4) + $dir;
58 3712 my $step = _square($row + $dr, $col + $dc);
59 3712 $STEP[$i] = $step;
60 3712 ▶ my $landing = $step ? _square($row + ($dr * 2), $col + ($dc * 2)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$step ? : 2842 870 3712
61 3712 ▶ $JUMP_OVER[$i] = $landing ? $step : 0;
Branch: 1 branch
BranchTrueFalseTotal
$landing ? : 2088 1624 3712
62 3712 $JUMP_TO[$i] = $landing;
63 }
64 }
65  
66 34 ▶sub coords {
Subroutine coords: called CC=1 SCAR=0.0
Pod documented
67 34 my ($n) = @_;
68 34 _check($n);
69 32 return ($ROW[$n], $COL[$n]);
70 }
71  
72 3684 ▶sub square {
Subroutine square: called CC=4 SCAR=14.9
Pod documented
73 3684 my ($row, $col) = @_;
74 3684 ▶ return undef unless defined $row && defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $row and defined $col 0 3684 3684
Condition33%1/3
defined $row and defined $col
!ll&&!rl&&r
003684
MC/DC0%0/2
defined $row and defined $col
defined $row defined $col
Truth table33%1/3
A and B
A= defined $rowB= defined $col
ABresult
0X0
100
111
75 3684 ▶ return _square($row, $col) || undef;
Condition100%2/2
_square($row, $col) || undef
l!l
18561828
MC/DC100%1/1
_square($row, $col) || undef
_square($row, $col)
Truth table100%2/2
A
A= _square($row, $col)
Aresult
11
00
76 }
77  
78 0 ▶sub row_of {
Subroutine row_of: not called CC=1 SCAR=6.9
Pod documented
79 0 my ($n) = @_;
80 0 _check($n);
81 0 return $ROW[$n];
82 }
83  
84 0 ▶sub col_of {
Subroutine col_of: not called CC=1 SCAR=6.9
Pod documented
85 0 my ($n) = @_;
86 0 _check($n);
87 0 return $COL[$n];
88 }
89  
90 342 ▶sub coord_name {
Subroutine coord_name: called CC=1 SCAR=0.0
Pod documented
91 342 my ($n) = @_;
92 342 _check($n);
93 342 return sprintf '%s%d', chr(ord('a') + $COL[$n]), 8 - $ROW[$n];
94 }
95  
96 68 ▶sub coord_square {
Subroutine coord_square: called CC=4 SCAR=13.9
Pod documented
97 68 my ($name) = @_;
98 68 ▶ return undef unless defined $name && $name =~ m/^([a-h])([1-8])$/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and $name =~ /^([a-h])([1-8])$/i 4 64 68
Condition66%2/3
defined $name and $name =~ /^([a-h])([1-8])$/i
!ll&&!rl&&r
0464
MC/DC50%1/2
defined $name and $name =~ /^([a-h])([1-8])$/i
defined $name $name =~ /^([a-h])([1-8])$/i
Truth table66%2/3
A and B
A= defined $nameB= $name =~ /^([a-h])([1-8])$/i
ABresult
0X0
100
111
99 64 ▶ return _square(8 - $2, ord(lc $1) - ord('a')) || undef;
Condition100%2/2
_square(8 - $2, ord(lc $1) - 97) || undef
l!l
613
MC/DC100%1/1
_square(8 - $2, ord(lc $1) - 97) || undef
_square(8 - $2, ord(lc $1) - 97)
Truth table100%2/2
A
A= _square(8 - $2, ord(lc $1) - 97)
Aresult
11
00
100 }
101  
102 19 ▶sub steps {
Subroutine steps: called CC=3 SCAR=11.0
Pod documented
103 19 my ($n) = @_;
104 19 _check($n);
105 19 my %step;
106 19 for my $dir (@DIRS) {
107 76 ▶ $step{$DIR_NAME[$dir]} = $STEP[($n * 4) + $dir] || undef;
Condition100%2/2
$Game::Checkers::Squares::STEP[$n * 4 + $dir] || undef
l!l
5026
MC/DC100%1/1
$Game::Checkers::Squares::STEP[$n * 4 + $dir] || undef
$Game::Checkers::Squares::STEP[$n * 4 + $dir]
Truth table100%2/2
A
A= $Game::Checkers::Squares::STEP[$n * 4 + $dir]
Aresult
11
00
108 }
109 19 return \%step;
110 }
111  
112 8 ▶sub jump {
Subroutine jump: called CC=2 SCAR=6.9
Pod documented
113 8 my ($n, $dir) = @_;
114 8 _check($n);
115 8 $dir = dir_index($dir);
116 8 my $i = ($n * 4) + $dir;
117 8 ▶ return () unless $JUMP_TO[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless $Game::Checkers::Squares::JUMP_TO[$i] 2 6 8
118 6 return ($JUMP_OVER[$i], $JUMP_TO[$i]);
119 }
120  
121 10 ▶sub dir_index {
Subroutine dir_index: called CC=6 SCAR=20.7
Pod documented
122 10 my ($dir) = @_;
123 10 ▶ return $dir if defined $dir && $dir =~ m/^[0-3]$/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dir and $dir =~ /^[0-3]$/ 1 9 10
Condition66%2/3
defined $dir and $dir =~ /^[0-3]$/
!ll&&!rl&&r
091
MC/DC50%1/2
defined $dir and $dir =~ /^[0-3]$/
defined $dir $dir =~ /^[0-3]$/
Truth table66%2/3
A and B
A= defined $dirB= $dir =~ /^[0-3]$/
ABresult
0X0
100
111
124 die "direction must be NE, NW, SE, SW or 0 .. 3, got " . (defined $dir ? "'$dir'" : 'undef')
125 9 ▶ unless defined $dir && exists $DIR_INDEX{uc $dir};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $dir and exists $Game::Checkers::Squares::DIR_INDEX{uc $dir} 0 9 9
defined $dir ? : 0 0 0
Condition33%1/3
defined $dir and exists $Game::Checkers::Squares::DIR_INDEX{uc $dir}
!ll&&!rl&&r
009
MC/DC0%0/2
defined $dir and exists $Game::Checkers::Squares::DIR_INDEX{uc $dir}
defined $dir exists $Game::Checkers::Squares::DIR_INDEX{uc $dir}
Truth table33%1/3
A and B
A= defined $dirB= exists $Game::Checkers::Squares::DIR_INDEX{uc $dir}
ABresult
0X0
100
111
126 9 return $DIR_INDEX{uc $dir};
127 }
128  
129 1 ▶sub dir_name {
Subroutine dir_name: called CC=1 SCAR=0.0
Pod documented
130 1 my ($dir) = @_;
131 1 return $DIR_NAME[dir_index($dir)];
132 }
133  
134 2 ▶sub forward_dirs {
Subroutine forward_dirs: called CC=4 SCAR=17.1
Pod documented
135 2 my ($side) = @_;
136 die "side must be black or white, got " . (defined $side ? "'$side'" : 'undef')
137 2 ▶ unless defined $side && $FORWARD{$side};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $side and $Game::Checkers::Squares::FORWARD{$side} 0 2 2
defined $side ? : 0 0 0
Condition33%1/3
defined $side and $Game::Checkers::Squares::FORWARD{$side}
!ll&&!rl&&r
002
MC/DC0%0/2
defined $side and $Game::Checkers::Squares::FORWARD{$side}
defined $side $Game::Checkers::Squares::FORWARD{$side}
Truth table33%1/3
A and B
A= defined $sideB= $Game::Checkers::Squares::FORWARD{$side}
ABresult
0X0
100
111
138 2 return @{$FORWARD{$side}};
139 }
140  
141 7 ▶sub crowning {
Subroutine crowning: called CC=7 SCAR=19.6
Pod documented
142 7 my ($n, $side) = @_;
143 7 _check($n);
144 die "side must be black or white, got " . (defined $side ? "'$side'" : 'undef')
145 7 ▶ unless defined $side && $FORWARD{$side};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $side and $Game::Checkers::Squares::FORWARD{$side} 1 6 7
defined $side ? : 1 0 1
Condition66%2/3
defined $side and $Game::Checkers::Squares::FORWARD{$side}
!ll&&!rl&&r
016
MC/DC50%1/2
defined $side and $Game::Checkers::Squares::FORWARD{$side}
defined $side $Game::Checkers::Squares::FORWARD{$side}
Truth table66%2/3
A and B
A= defined $sideB= $Game::Checkers::Squares::FORWARD{$side}
ABresult
0X0
100
111
146 6 ▶ return $side eq 'black' ? ($n >= 29 ? 1 : 0) : ($n <= 4 ? 1 : 0);
Branch: 3 branches
BranchTrueFalseTotal
$side eq 'black' ? : 3 3 6
$n >= 29 ? : 2 1 3
$n <= 4 ? : 2 1 3
147 }
148  
149 29 1;
150  
151 __END__
« blib/lib/Game/Checkers.pm ↑ Summary blib/lib/Game/Checkers/Piece.pm »