blib/lib/Math/HexGrid/Hex.pm

filter: Statementstmt 53.5% 15 / 28 Branchbran 50.0% 2 / 4 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 36.3% 4 / 11 Podpod 87.5% 7 / 8 totaltotal 50.0% 37 / 74 CC 8 SCAR 14.4
CC
8
Cov
51%
new
8.1
hex_equal
3.7
hex_add
2.0
CRAP 4.2
« blib/lib/Math/HexGrid.pm ↑ Summary lib/Math/HexGrid.pm »
1 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Math::HexGrid::Hex;
4 2 $Math::HexGrid::Hex::VERSION = '0.02';
5  
6 sub new
7 {
8 39 ▶ my ($class, $q, $r, $s) = @_;
Subroutine new: called CC=6 SCAR=20.9
Pod documented
9  
10 # if s wasnt provided, calculate it
11 39 ▶ $s ||= - $q - $r;
Condition100%3/3
$s ||= -$q - $r
l!l&&r!l&&!r
81912
MC/DC100%2/2
$s ||= -$q - $r
$s -$q - $r
Truth table100%3/3
A ||= B
A= $sB= -$q - $r
ABresult
1X1
011
000
12  
13 39 ▶ die 'Invalid coordinates!'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $q and defined $r and defined $s and $q + $r + $s == 0 0 39 39
Condition33%3/9
defined $q and defined $r and defined $s and $q + $r + $s == 0
!ll&&!rl&&r
0039
defined $q and defined $r and defined $s
!ll&&!rl&&r
0039
defined $q and defined $r
!ll&&!rl&&r
0039
MC/DC0%0/4
defined $q and defined $r and defined $s and $q + $r + $s == 0
defined $q defined $r defined $s $q + $r + $s == 0
Truth table20%1/5
A and B and C and D
A= defined $qB= defined $rC= defined $sD= $q + $r + $s == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
14 unless defined $q && defined $r && defined $s
15 && $q + $r + $s == 0;
16  
17 39 bless { q => $q, r => $r, s => $s }, $class;
18 }
19  
20  
21 sub hex_equal
22 {
23 1 ▶ my ($self, $hex) = @_;
Subroutine hex_equal: called CC=3 SCAR=13.1
Pod documented
24  
25 $self->{q} == $hex->{q}
26 && $self->{r} == $hex->{r}
27 1 ▶ && $self->{s} == $hex->{s};
Branch: 1 branch
BranchTrueFalseTotal
$self->{"q"} == $hex->{"q"} and $self->{"r"} == $hex->{"r"} and $self->{"s"} == $hex->{"s"} 1 0 1
Condition33%1/3
$self->{"q"} == $hex->{"q"} and $self->{"r"} == $hex->{"r"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"q"} == $hex->{"q"} and $self->{"r"} == $hex->{"r"}
$self->{"q"} == $hex->{"q"} $self->{"r"} == $hex->{"r"}
Truth table33%1/3
A and B
A= $self->{"q"} == $hex->{"q"}B= $self->{"r"} == $hex->{"r"}
ABresult
0X0
100
111
28 }
29  
30  
31 sub hex_add
32 {
33 0 ▶ my ($self, $hex) = @_;
Subroutine hex_add: not called CC=1 SCAR=6.9
Pod documented
34 Math::HexGrid::Hex->new(
35 $self->{q} + $hex->{q},
36 $self->{r} + $hex->{r},
37 $self->{s} + $hex->{s},
38 0 );
39 }
40  
41  
42 sub hex_subtract
43 {
44 0 ▶ my ($self, $hex) = @_;
Subroutine hex_subtract: not called CC=1 SCAR=6.9
Pod documented
45 Math::HexGrid::Hex->new(
46 $self->{q} - $hex->{q},
47 $self->{r} - $hex->{r},
48 $self->{s} - $hex->{s},
49 0 );
50 }
51  
52  
53 sub hex_multiply
54 {
55 0 ▶ my ($self, $hex) = @_;
Subroutine hex_multiply: not called CC=1 SCAR=6.9
Pod documented
56 Math::HexGrid::Hex->new(
57 $self->{q} * $hex->{q},
58 $self->{r} * $hex->{r},
59 $self->{s} * $hex->{s},
60 0 );
61 }
62  
63 sub hex_length
64 {
65 0 ▶ my ($self) = @_;
Subroutine hex_length: not called CC=1 SCAR=6.9
Pod undocumented
66 0 int((abs($self->{q}) + abs($self->{r}) + abs($self->{s})) / 2);
67 }
68  
69  
70 sub hex_distance
71 {
72 0 ▶ my ($self, $hex) = @_;
Subroutine hex_distance: not called CC=1 SCAR=6.9
Pod documented
73 0 $self->hex_subtract($hex)->hex_length;
74 }
75  
76 2 my @hex_directions = (
77 Math::HexGrid::Hex->new(1,0,-1),
78 Math::HexGrid::Hex->new(1,-1,0),
79 Math::HexGrid::Hex->new(0,-1,1),
80 Math::HexGrid::Hex->new(-1,0,1),
81 Math::HexGrid::Hex->new(-1,1,0),
82 Math::HexGrid::Hex->new(0,1,-1),
83 );
84  
85 sub _hex_direction
86 {
87 # this will handle directions > 6 and < 0
88 0 ▶ $hex_directions[(6 + ($_[0] % 6)) % 6];
Subroutine _hex_direction: not called CC=1 SCAR=6.9
89 }
90  
91  
92 sub hex_neighbor
93 {
94 0 ▶ my ($self, $direction) = @_;
Subroutine hex_neighbor: not called CC=1 SCAR=6.9
Pod documented
95 0 $self->hex_add(_hex_direction($direction));
96 }
97  
98 2 1;
99  
100 __END__
« blib/lib/Math/HexGrid.pm ↑ Summary lib/Math/HexGrid.pm »