HexGrid/Hex.pm

filter: Statementstmt 55.8% 19 / 34 Branchbran 50.0% 2 / 4 Conditioncond 46.6% 7 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 40.0% 6 / 15 Podpod 81.8% 9 / 11 totaltotal 51.7% 45 / 87 CC 8 SCAR 13.1
CC
8
Cov
53%
new
8.1
hex_equal
3.7
id
2.0
CRAP 3.7
↑ Summary 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.03';
5 use overload
6 2 ▶ '""' => 'to_string',
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 fallback => 1;
8  
9 4 ▶sub to_string { $_[0]->{id} }
Subroutine to_string: called CC=1 SCAR=0.0
Pod undocumented
10  
11  
12 sub new
13 39 ▶{
Subroutine new: called CC=6 SCAR=20.9
Pod documented
14 39 my ($class, $q, $r, $s) = @_;
15  
16 # if s wasnt provided, calculate it
17 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
18  
19 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
20 unless defined $q && defined $r && defined $s
21 && $q + $r + $s == 0;
22  
23 39 bless { q => $q, r => $r, s => $s, id => "$q,$r" }, $class;
24 }
25  
26  
27 0 ▶sub id { $_[0]->{id} }
Subroutine id: not called CC=1 SCAR=6.9
Pod documented
28  
29  
30 sub hex_equal
31 1 ▶{
Subroutine hex_equal: called CC=3 SCAR=13.1
Pod documented
32 1 my ($self, $hex) = @_;
33  
34 $self->{q} == $hex->{q}
35 && $self->{r} == $hex->{r}
36 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
37 }
38  
39  
40 0 ▶sub is_colliding { $_[0]->hex_equal($_[1]) }
Subroutine is_colliding: not called CC=1 SCAR=6.9
Pod documented
41  
42  
43 sub hex_add
44 0 ▶{
Subroutine hex_add: not called CC=1 SCAR=6.9
Pod documented
45 0 my ($self, $hex) = @_;
46 Math::HexGrid::Hex->new(
47 $self->{q} + $hex->{q},
48 $self->{r} + $hex->{r},
49 $self->{s} + $hex->{s},
50 0 );
51 }
52  
53  
54 sub hex_subtract
55 0 ▶{
Subroutine hex_subtract: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($self, $hex) = @_;
57 Math::HexGrid::Hex->new(
58 $self->{q} - $hex->{q},
59 $self->{r} - $hex->{r},
60 $self->{s} - $hex->{s},
61 0 );
62 }
63  
64  
65 sub hex_multiply
66 0 ▶{
Subroutine hex_multiply: not called CC=1 SCAR=6.9
Pod documented
67 0 my ($self, $hex) = @_;
68 Math::HexGrid::Hex->new(
69 $self->{q} * $hex->{q},
70 $self->{r} * $hex->{r},
71 $self->{s} * $hex->{s},
72 0 );
73 }
74  
75 sub hex_length
76 0 ▶{
Subroutine hex_length: not called CC=1 SCAR=6.9
Pod undocumented
77 0 my ($self) = @_;
78 0 int((abs($self->{q}) + abs($self->{r}) + abs($self->{s})) / 2);
79 }
80  
81  
82 sub hex_distance
83 0 ▶{
Subroutine hex_distance: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self, $hex) = @_;
85 0 $self->hex_subtract($hex)->hex_length;
86 }
87  
88 2 my @hex_directions = (
89 Math::HexGrid::Hex->new(1,0,-1),
90 Math::HexGrid::Hex->new(1,-1,0),
91 Math::HexGrid::Hex->new(0,-1,1),
92 Math::HexGrid::Hex->new(-1,0,1),
93 Math::HexGrid::Hex->new(-1,1,0),
94 Math::HexGrid::Hex->new(0,1,-1),
95 );
96  
97 sub _hex_direction
98 0 ▶{
Subroutine _hex_direction: not called CC=1 SCAR=6.9
99 # this will handle directions > 6 and < 0
100 0 $hex_directions[(6 + ($_[0] % 6)) % 6];
101 }
102  
103  
104 sub hex_neighbor
105 0 ▶{
Subroutine hex_neighbor: not called CC=1 SCAR=6.9
Pod documented
106 0 my ($self, $direction) = @_;
107 0 $self->hex_add(_hex_direction($direction));
108 }
109  
110 2 1;
111  
112 __END__
↑ Summary HexGrid.pm »