Battleship/Grid.pm

filter: Statementstmt 88.7% 79 / 89 Branchbran 61.5% 32 / 52 Conditioncond 66.6% 20 / 30 MC/DCmcdc 55.5% 10 / 18 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 1 totaltotal 75.2% 149 / 198 CC 42 SCAR 36.5
CC
42
Cov
77%
_segment_intersection
63.2
BUILD
15.1
_tail_coordinates
3.0
CRAP 38.5
↑ Summary Battleship.pm »
1 package Game::Battleship::Grid;
2 4 $Game::Battleship::Grid::VERSION = '0.0602';
3 4 our $AUTHORITY = 'cpan:GENE';
4  
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Game::Battleship::Craft;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Types::Standard qw( ArrayRef Int );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 has dimension => (
11 is => 'ro',
12 isa => ArrayRef[Int],
13 5 ▶ default => sub { [ 9, 9 ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
14 4 );
15  
16 4 has fleet => (
17 is => 'ro',
18 isa => ArrayRef,
19 );
20  
21 # Place the array reference of craft on the grid.
22 5 ▶sub BUILD {
Subroutine BUILD: called CC=15 SCAR=27.2
Pod undocumented
23 5 my $self = shift;
24  
25 # Initialize the matrix.
26 5 for my $i (0 .. $self->dimension->[0]) {
27 50 for my $j (0 .. $self->dimension->[1]) {
28 500 $self->{matrix}[$i][$j] = '.';
29 }
30 }
31  
32 # Place the fleet on the grid.
33 5 for my $craft (@{ $self->{fleet} }) {
34 15 my ($ok, $x0, $y0, $x1, $y1, $orient);
35  
36 15 ▶ if (defined $craft->position) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $craft->position) { } 0 15 15
37 0 ($x0, $y0) = ($craft->position->[0], $craft->position->[1]);
38  
39 # Set the craft orientation and tail coordinates.
40 0 ($orient, $x1, $y1) = _tail_coordinates(
41 $x0, $y0,
42 $craft->points - 1,
43 $craft->orient
44 );
45 }
46 else {
47 # XXX This looping is needlessly brutish. refactoring please
48 15 ▶ while (not $ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 22 15 37
49 # Grab a random coordinate that we haven't seen.
50 22 $x0 = int(rand($self->dimension->[0] + 1));
51 22 $y0 = int(rand($self->dimension->[1] + 1));
52  
53 # Set the craft orientation and tail coordinates.
54 22 ($orient, $x1, $y1) = _tail_coordinates(
55 $x0, $y0,
56 $craft->points - 1,
57 $craft->orient
58 );
59  
60 # If the craft is not placed off the grid and it does
61 # not collide with another craft, then we are ok to
62 # move on.
63 # XXX regex constraint rules here?
64 22 ▶ if ($x1 <= $self->dimension->[0] &&
Branch: 1 branch
BranchTrueFalseTotal
if ($x1 <= $self->dimension->[0] and $y1 <= $self->dimension->[1]) 20 2 22
Condition66%2/3
$x1 <= $self->dimension->[0] and $y1 <= $self->dimension->[1]
!ll&&!rl&&r
0220
MC/DC50%1/2
$x1 <= $self->dimension->[0] and $y1 <= $self->dimension->[1]
$x1 <= $self->dimension->[0] $y1 <= $self->dimension->[1]
Truth table66%2/3
A and B
A= $x1 <= $self->dimension->[0]B= $y1 <= $self->dimension->[1]
ABresult
0X0
100
111
65 $y1 <= $self->dimension->[1]
66 ) {
67 # For each craft (except the current one) that has
68 # a position, do the craft share a common point?
69 20 my $collide = 0;
70  
71 20 for (@{ $self->{fleet} }) {
72 # Ships can't be the same.
73 85 ▶ if ($craft->name ne $_->name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($craft->name ne $_->name) 70 15 85
74 # Ships can't intersect.
75 70 ▶ if (defined $_->position &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->position and _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})) 5 65 70
Condition100%3/3
defined $_->position and _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
!ll&&!rl&&r
30355
MC/DC100%2/2
defined $_->position and _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
defined $_->position _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
Truth table100%3/3
A and B
A= defined $_->positionB= _segment_intersection($x0, $y0, $x1, $y1, @{$_->position->[0];}, @{$_->position->[1];})
ABresult
0X0
100
111
76 _segment_intersection(
77 $x0, $y0,
78 $x1, $y1,
79 40 @{ $_->position->[0] },
80 40 @{ $_->position->[1] }
81 )
82 ) {
83 5 $collide = 1;
84 5 last;
85 }
86 }
87 }
88  
89 20 ▶ $ok = 1 unless $collide;
Branch: 1 branch
BranchTrueFalseTotal
unless $collide 15 5 20
90 }
91 }
92  
93 # Set the craft position.
94 15 $craft->{position} = [[$x0, $y0], [$x1, $y1]];
95 }
96 #warn "$craft->{name}: [$x0, $y0], [$x1, $y1], $craft->{points}\n";
97  
98 # Add the craft to the grid.
99 15 for my $n (0 .. $craft->points - 1) {
100 51 ▶ if ($orient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orient) { } 28 23 51
101 28 $self->{matrix}[$x0 + $n][$y0] = $craft->{id};
102 }
103 else {
104 23 $self->{matrix}[$x0][$y0 + $n] = $craft->{id};
105 }
106 }
107 }
108 }
109  
110 22 ▶sub _tail_coordinates {
Subroutine _tail_coordinates: called CC=3 SCAR=11.0
111 # Get the coordinates of the end of the segment based on a given
112 # span.
113 22 my ($x0, $y0, $span, $orient) = @_;
114  
115 # Set orientation to 0 (vertical) or 1 (horizontal).
116 22 ▶ $orient = int rand 2
Branch: 1 branch
BranchTrueFalseTotal
unless defined $orient 22 0 22
117 unless defined $orient;
118  
119 22 my ($x1, $y1) = ($x0, $y0);
120  
121 22 ▶ if ($orient) {
Branch: 1 branch
BranchTrueFalseTotal
if ($orient) { } 12 10 22
122 12 $x1 += $span;
123 }
124 else {
125 10 $y1 += $span;
126 }
127  
128 22 return $orient, $x1, $y1;
129 }
130  
131 40 ▶sub _segment_intersection {
Subroutine _segment_intersection: called CC=26 SCAR=41.5
132 # 0 - Intersection doesn't exist.
133 # 1 - Intersection exists.
134 # NOTE: In Battleship, we only care about yes/no, but the
135 # original code can tell much more:
136 # 0 (was 2) - line segments are parallel
137 # 0 (was 3) - line segments are collinear but do not overlap.
138 # 4 - line segments are collinear and share an end point.
139 # 5 - line segments are collinear and overlap.
140  
141 40 ▶ croak "segment_intersection needs 4 points\n" unless @_ == 8;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 8 0 40 40
142 my(
143 40 $x0, $y0, $x1, $y1, # AB segment 1
144 $x2, $y2, $x3, $y3 # CD segment 2
145 ) = @_;
146 #warn "[$x0, $y0]-[$x1, $y1], [$x2, $y2]-[$x3, $y3]\n";
147  
148 40 my $xba = $x1 - $x0;
149 40 my $yba = $y1 - $y0;
150 40 my $xdc = $x3 - $x2;
151 40 my $ydc = $y3 - $y2;
152 40 my $xca = $x2 - $x0;
153 40 my $yca = $y2 - $y0;
154  
155 40 my $delta = $xba * $ydc - $yba * $xdc;
156 40 my $t1 = $xca * $ydc - $yca * $xdc;
157 40 my $t2 = $xca * $yba - $yca * $xba;
158  
159 40 ▶ if ($delta != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($delta != 0) { } 17 23 40
160 17 my $u = $t1 / $delta;
161 17 my $v = $t2 / $delta;
162  
163 # Two segments intersect (including at end points).
164 17 ▶ return ($u <= 1 && $u >= 0 && $v <= 1 && $v >= 0) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$u <= 1 && $u >= 0 && $v <= 1 && $v >= 0 ? : 1 16 17
Condition100%9/9
$u <= 1 and $u >= 0 and $v <= 1 and $v >= 0
!ll&&!rl&&r
1511
$u <= 1 and $u >= 0 and $v <= 1
!ll&&!rl&&r
1412
$u <= 1 and $u >= 0
!ll&&!rl&&r
773
MC/DC100%4/4
$u <= 1 and $u >= 0 and $v <= 1 and $v >= 0
$u <= 1 $u >= 0 $v <= 1 $v >= 0
Truth table100%5/5
A and B and C and D
A= $u <= 1B= $u >= 0C= $v <= 1D= $v >= 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
165 }
166 else {
167 # AB & CD are parallel.
168 23 ▶ return 0 if $t1 != 0 && $t2 != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t1 != 0 and $t2 != 0 18 5 23
Condition66%2/3
$t1 != 0 and $t2 != 0
!ll&&!rl&&r
5018
MC/DC50%1/2
$t1 != 0 and $t2 != 0
$t1 != 0 $t2 != 0
Truth table66%2/3
A and B
A= $t1 != 0B= $t2 != 0
ABresult
0X0
100
111
169 # NOTE: We just care about yes/no, so this is the old way:
170 # return 2 if $t1 != 0 && $t2 != 0;
171  
172 # When AB & CD are collinear...
173 5 my ($a, $b, $c, $d);
174  
175 # If AB isn't a vertical line segment, project to x-axis.
176 5 ▶ if ($x0 != $x1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x0 != $x1) { } 5 0 5
177 # < is min, > is max
178 5 ▶ $a = $x0 < $x1 ? $x0 : $x1;
Branch: 1 branch
BranchTrueFalseTotal
$x0 < $x1 ? : 5 0 5
179 5 ▶ $b = $x0 > $x1 ? $x0 : $x1;
Branch: 1 branch
BranchTrueFalseTotal
$x0 > $x1 ? : 0 5 5
180 5 ▶ $c = $x2 < $x3 ? $x2 : $x3;
Branch: 1 branch
BranchTrueFalseTotal
$x2 < $x3 ? : 5 0 5
181 5 ▶ $d = $x2 > $x3 ? $x2 : $x3;
Branch: 1 branch
BranchTrueFalseTotal
$x2 > $x3 ? : 0 5 5
182  
183 5 ▶ if ($d < $a || $c > $b) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d < $a or $c > $b) { } 1 4 5
elsif ($d == $a or $c == $b) { } 2 2 4
Condition66%2/3
$d < $a or $c > $b
l!l&&r!l&&!r
014
MC/DC50%1/2
$d < $a or $c > $b
$d < $a $c > $b
Truth table66%2/3
A or B
A= $d < $aB= $c > $b
ABresult
1X1
011
000
184 # NOTE: We just care about yes/no. The old way returns 3:
185 1 return 0;#3;
186 }
187 ▶ elsif ($d == $a || $c == $b) {
Condition66%2/3
$d == $a or $c == $b
l!l&&r!l&&!r
202
MC/DC50%1/2
$d == $a or $c == $b
$d == $a $c == $b
Truth table66%2/3
A or B
A= $d == $aB= $c == $b
ABresult
1X1
011
000
188 2 return 4;
189 }
190 else {
191 2 return 5;
192 }
193 }
194 # If AB is a vertical line segment, project to y-axis.
195 else {
196 # < is min, > is max
197 0 ▶ $a = $y0 < $y1 ? $y0 : $y1;
Branch: 1 branch
BranchTrueFalseTotal
$y0 < $y1 ? : 0 0 0
198 0 ▶ $b = $y0 > $y1 ? $y0 : $y1;
Branch: 1 branch
BranchTrueFalseTotal
$y0 > $y1 ? : 0 0 0
199 0 ▶ $c = $y2 < $y3 ? $y2 : $y3;
Branch: 1 branch
BranchTrueFalseTotal
$y2 < $y3 ? : 0 0 0
200 0 ▶ $d = $y2 > $y3 ? $y2 : $y3;
Branch: 1 branch
BranchTrueFalseTotal
$y2 > $y3 ? : 0 0 0
201  
202 0 ▶ if ($d < $a || $c > $b) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d < $a or $c > $b) { } 0 0 0
elsif ($d == $a or $c == $b) { } 0 0 0
Condition0%0/3
$d < $a or $c > $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$d < $a or $c > $b
$d < $a $c > $b
Truth table0%0/3
A or B
A= $d < $aB= $c > $b
ABresult
1X1
011
000
203 # NOTE: We just care about yes/no. The old way returns 3:
204 0 return 0;#3;
205 }
206 ▶ elsif ($d == $a || $c == $b) {
Condition0%0/3
$d == $a or $c == $b
l!l&&r!l&&!r
000
MC/DC0%0/2
$d == $a or $c == $b
$d == $a $c == $b
Truth table0%0/3
A or B
A= $d == $aB= $c == $b
ABresult
1X1
011
000
207 0 return 4;
208 }
209 else {
210 0 return 5;
211 }
212 }
213 }
214 }
215  
216 4 1;
217  
218 __END__
↑ Summary Battleship.pm »