Battleship/Board.pm

filter: Statementstmt 91.2% 125 / 137 Branchbran 81.2% 39 / 48 Conditioncond 61.2% 19 / 31 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 93.1% 27 / 29 Podpod 0.0% 0 / 23 totaltotal 75.7% 215 / 284 CC 50 SCAR 18.0
CC
50
Cov
85%
can_place
13.7
ship_named
12.0
receive_shot
10.2
CRAP 6.1
« Battleship/AI/Hard.pm ↑ Summary Battleship/Ship.pm »
1 package Games::GUI::Battleship::Board;
2  
3 3 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶no warnings 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Games::GUI::Battleship::Ship;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 ▶sub new ( $class, %args ) {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
11 4 ▶ my $size = $args{size} // 10;
Condition100%2/2
$args{"size"} // 10
l!l
13
MC/DC100%1/1
$args{"size"} // 10
$args{"size"}
Truth table100%2/2
A
A= $args{"size"}
Aresult
11
00
12  
13 4 my $self = {
14 size => int($size),
15 ships => [],
16 grid => {},
17 shots => {},
18 };
19  
20 4 return bless $self, $class;
21 }
22  
23 1 ▶sub size ($self) {
Subroutine size: called CC=1 SCAR=0.0
Pod undocumented
24 1 return $self->{size};
25 }
26  
27 4 ▶sub ships ($self) {
Subroutine ships: called CC=1 SCAR=0.0
Pod undocumented
28 4 return @{ $self->{ships} };
29 }
30  
31 0 ▶sub ship_count ($self) {
Subroutine ship_count: not called CC=1 SCAR=6.9
Pod undocumented
32 0 return scalar @{ $self->{ships} };
33 }
34  
35 0 ▶sub ship_named ( $self, $type ) {
Subroutine ship_named: not called CC=3 SCAR=24.8
Pod undocumented
36 0 for my $ship ( @{ $self->{ships} } ) {
37 0 ▶ return $ship if $ship->type eq $type;
Branch: 1 branch
BranchTrueFalseTotal
if $ship->type eq $type 0 0 0
38 }
39 0 return undef;
40 }
41  
42 13 ▶sub can_place_ship ( $self, $ship ) {
Subroutine can_place_ship: called CC=1 SCAR=0.0
Pod undocumented
43 13 return $self->can_place( $ship->type, $ship->x, $ship->y, $ship->dir,
44 $ship );
45 }
46  
47 22 ▶sub can_place ( $self, $type, $x, $y, $dir, $ignore_ship = undef ) {
Subroutine can_place: called CC=13 SCAR=26.2
Pod undocumented
48 22 ▶ my $len = $Games::GUI::Battleship::Ship::SHIP_SIZES{$type}
Condition50%1/2
$Games::GUI::Battleship::Ship::SHIP_SIZES{$type} // croak("Unknown ship type: $type")
l!l
220
MC/DC0%0/1
$Games::GUI::Battleship::Ship::SHIP_SIZES{$type} // croak("Unknown ship type: $type")
$Games::GUI::Battleship::Ship::SHIP_SIZES{$type}
Truth table50%1/2
A
A= $Games::GUI::Battleship::Ship::SHIP_SIZES{$type}
Aresult
11
00
49 // croak "Unknown ship type: $type";
50 22 $dir = uc($dir);
51  
52 22 my $size = $self->{size};
53  
54 22 for my $i ( 0 .. $len - 1 ) {
55 69 ▶ my $cx = ( $dir eq 'H' ) ? $x + $i : $x;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'H' ? : 33 36 69
56 69 ▶ my $cy = ( $dir eq 'V' ) ? $y + $i : $y;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'V' ? : 36 33 69
57  
58 # Bounds check (1-indexed)
59 69 ▶ return 0 if $cx < 1 || $cx > $size || $cy < 1 || $cy > $size;
Branch: 1 branch
BranchTrueFalseTotal
if $cx < 1 or $cx > $size or $cy < 1 or $cy > $size 3 66 69
Condition66%6/9
$cx < 1 or $cx > $size or $cy < 1 or $cy > $size
l!l&&r!l&&!r
3066
$cx < 1 or $cx > $size or $cy < 1
l!l&&r!l&&!r
3066
$cx < 1 or $cx > $size
l!l&&r!l&&!r
0366
MC/DC25%1/4
$cx < 1 or $cx > $size or $cy < 1 or $cy > $size
$cx < 1 $cx > $size $cy < 1 $cy > $size
Truth table40%2/5
A or B or C or D
A= $cx < 1B= $cx > $sizeC= $cy < 1D= $cy > $size
ABCDresult
1XXX1
01XX1
001X1
00011
00000
60  
61 # Collision check
62 66 my $existing = $self->ship_at( $cx, $cy );
63 66 ▶ if ($existing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($existing) 3 63 66
64 3 ▶ if ( !defined $ignore_ship
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ignore_ship or $existing->type ne $ignore_ship->type) 3 0 3
Condition66%2/3
not defined $ignore_ship or $existing->type ne $ignore_ship->type
l!l&&r!l&&!r
210
MC/DC0%0/2
not defined $ignore_ship or $existing->type ne $ignore_ship->type
not defined $ignore_ship $existing->type ne $ignore_ship->type
Truth table66%2/3
not(A) or B
A= not defined $ignore_shipB= $existing->type ne $ignore_ship->type
ABresult
1X1
011
000
65 || $existing->type ne $ignore_ship->type )
66 {
67 3 return 0;
68 }
69 }
70 }
71  
72 16 return 1;
73 }
74  
75 9 ▶sub place_ship ( $self, $ship ) {
Subroutine place_ship: called CC=3 SCAR=11.2
Pod undocumented
76 9 ▶ if ( !$self->can_place_ship($ship) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->can_place_ship($ship)) 0 9 9
77 0 croak "Cannot place ship "
78 . $ship->type . " at ("
79 . $ship->x . ","
80 . $ship->y . ") "
81 . $ship->dir;
82 }
83  
84 # Remove any existing ship of the same type first
85 9 $self->remove_ship( $ship->type );
86  
87 9 push @{ $self->{ships} }, $ship;
88 9 for my $coord ( $ship->coordinates ) {
89 29 $self->{grid}{"$coord->[0],$coord->[1]"} = $ship;
90 }
91  
92 9 return $ship;
93 }
94  
95 9 ▶sub remove_ship ( $self, $type ) {
Subroutine remove_ship: called CC=4 SCAR=15.2
Pod undocumented
96 9 my $removed;
97 my @kept;
98 9 for my $ship ( @{ $self->{ships} } ) {
99 11 ▶ if ( $ship->type eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ship->type eq $type) { } 0 11 11
100 0 $removed = $ship;
101 0 for my $coord ( $ship->coordinates ) {
102 0 delete $self->{grid}{"$coord->[0],$coord->[1]"};
103 }
104 }
105 else {
106 11 push @kept, $ship;
107 }
108 }
109 9 $self->{ships} = \@kept;
110 9 return $removed;
111 }
112  
113 1 ▶sub clear_ships ($self) {
Subroutine clear_ships: called CC=1 SCAR=0.0
Pod undocumented
114 1 $self->{ships} = [];
115 1 $self->{grid} = {};
116 1 return $self;
117 }
118  
119 1 ▶sub place_random_fleet ($self) {
Subroutine place_random_fleet: called CC=7 SCAR=19.5
Pod undocumented
120 1 $self->clear_ships;
121 1 my $size = $self->{size};
122  
123 1 for my $type (@Games::GUI::Battleship::Ship::FLEET_ORDER) {
124 5 my $placed = 0;
125 5 my $attempts = 0;
126  
127 5 ▶ while ( !$placed && $attempts < 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $placed and $attempts < 1000) 9 5 14
Condition66%2/3
not $placed and $attempts < 1000
!ll&&!rl&&r
509
MC/DC50%1/2
not $placed and $attempts < 1000
not $placed $attempts < 1000
Truth table66%2/3
not(A) and B
A= not $placedB= $attempts < 1000
ABresult
0X0
100
111
128 9 $attempts++;
129 9 ▶ my $dir = int( rand(2) ) ? 'H' : 'V';
Branch: 1 branch
BranchTrueFalseTotal
int rand 2 ? : 4 5 9
130 9 my $x = 1 + int( rand($size) );
131 9 my $y = 1 + int( rand($size) );
132  
133 9 ▶ if ( $self->can_place( $type, $x, $y, $dir ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can_place($type, $x, $y, $dir)) 5 4 9
134 5 my $ship = Games::GUI::Battleship::Ship->new(
135 type => $type,
136 x => $x,
137 y => $y,
138 dir => $dir,
139 );
140 5 $self->place_ship($ship);
141 5 $placed = 1;
142 }
143 }
144  
145 5 ▶ if ( !$placed ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($placed) 0 5 5
146  
147 # In the rare event of deadlock, restart placement
148 0 return $self->place_random_fleet;
149 }
150 }
151  
152 1 return 1;
153 }
154  
155 8 ▶sub has_shot ( $self, $x, $y ) {
Subroutine has_shot: called CC=2 SCAR=6.9
Pod undocumented
156 8 ▶ return exists $self->{shots}{"$x,$y"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'shots'}{"$x,$y"} ? : 2 6 8
157 }
158  
159 4 ▶sub shot_at ( $self, $x, $y ) {
Subroutine shot_at: called CC=1 SCAR=0.0
Pod undocumented
160 4 return $self->{shots}{"$x,$y"};
161 }
162  
163 72 ▶sub ship_at ( $self, $x, $y ) {
Subroutine ship_at: called CC=1 SCAR=0.0
Pod undocumented
164 72 return $self->{grid}{"$x,$y"};
165 }
166  
167 7 ▶sub receive_shot ( $self, $x, $y ) {
Subroutine receive_shot: called CC=9 SCAR=23.2
Pod undocumented
168 7 my $size = $self->{size};
169 7 ▶ if ( $x < 1 || $x > $size || $y < 1 || $y > $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x < 1 or $x > $size or $y < 1 or $y > $size) 0 7 7
Condition33%3/9
$x < 1 or $x > $size or $y < 1 or $y > $size
l!l&&r!l&&!r
007
$x < 1 or $x > $size or $y < 1
l!l&&r!l&&!r
007
$x < 1 or $x > $size
l!l&&r!l&&!r
007
MC/DC0%0/4
$x < 1 or $x > $size or $y < 1 or $y > $size
$x < 1 $x > $size $y < 1 $y > $size
Truth table20%1/5
A or B or C or D
A= $x < 1B= $x > $sizeC= $y < 1D= $y > $size
ABCDresult
1XXX1
01XX1
001X1
00011
00000
170 0 croak "Shot coordinates out of range: ($x, $y)";
171 }
172  
173 7 ▶ if ( $self->has_shot( $x, $y ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_shot($x, $y)) 1 6 7
174 1 return undef;
175 }
176  
177 6 my $ship = $self->ship_at( $x, $y );
178  
179 6 ▶ if ($ship) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ship) 4 2 6
180 4 $self->{shots}{"$x,$y"} = 'H';
181 4 $ship->record_hit( $x, $y );
182  
183 4 ▶ if ( $ship->is_sunk ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ship->is_sunk) 2 2 4
184  
185 # Update all coordinates of this ship to 'S'
186 2 for my $coord ( $ship->coordinates ) {
187 4 $self->{shots}{"$coord->[0],$coord->[1]"} = 'S';
188 }
189 return {
190 2 result => 'sunk',
191 ship => $ship,
192 x => $x,
193 y => $y,
194 };
195 }
196  
197 return {
198 2 result => 'hit',
199 ship => $ship,
200 x => $x,
201 y => $y,
202 };
203 }
204  
205 2 $self->{shots}{"$x,$y"} = 'M';
206 return {
207 2 result => 'miss',
208 x => $x,
209 y => $y,
210 };
211 }
212  
213 2 ▶sub all_sunk ($self) {
Subroutine all_sunk: called CC=4 SCAR=13.9
Pod undocumented
214 2 ▶ return 0 if scalar( @{ $self->{ships} } ) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"ships"};} == 0 0 2 2
215 2 for my $ship ( @{ $self->{ships} } ) {
216 2 ▶ return 0 if !$ship->is_sunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $ship->is_sunk 1 1 2
217 }
218 1 return 1;
219 }
220  
221 2 ▶sub ships_remaining ($self) {
Subroutine ships_remaining: called CC=3 SCAR=11.0
Pod undocumented
222 2 my $count = 0;
223 2 for my $ship ( @{ $self->{ships} } ) {
224 2 ▶ $count++ if !$ship->is_sunk;
Branch: 1 branch
BranchTrueFalseTotal
unless $ship->is_sunk 1 1 2
225 }
226 2 return $count;
227 }
228  
229 1 ▶sub ships_sunk ($self) {
Subroutine ships_sunk: called CC=3 SCAR=11.1
Pod undocumented
230 1 my $count = 0;
231 1 for my $ship ( @{ $self->{ships} } ) {
232 1 ▶ $count++ if $ship->is_sunk;
Branch: 1 branch
BranchTrueFalseTotal
if $ship->is_sunk 1 0 1
233 }
234 1 return $count;
235 }
236  
237 1 ▶sub reset_shots ($self) {
Subroutine reset_shots: called CC=2 SCAR=6.9
Pod undocumented
238 1 $self->{shots} = {};
239 1 for my $ship ( @{ $self->{ships} } ) {
240 1 $ship->clear_hits;
241 }
242 1 return $self;
243 }
244  
245 10 ▶sub total_shots ($self) {
Subroutine total_shots: called CC=1 SCAR=0.0
Pod undocumented
246 10 return scalar keys %{ $self->{shots} };
247 }
248  
249 8 ▶sub hit_count ($self) {
Subroutine hit_count: called CC=4 SCAR=13.9
Pod undocumented
250 8 my $count = 0;
251 8 for my $val ( values %{ $self->{shots} } ) {
252 12 ▶ $count++ if $val eq 'H' || $val eq 'S';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "H" or $val eq "S" 6 6 12
Condition100%3/3
$val eq "H" or $val eq "S"
l!l&&r!l&&!r
246
MC/DC100%2/2
$val eq "H" or $val eq "S"
$val eq "H" $val eq "S"
Truth table100%3/3
A or B
A= $val eq "H"B= $val eq "S"
ABresult
1X1
011
000
253 }
254 8 return $count;
255 }
256  
257 5 ▶sub miss_count ($self) {
Subroutine miss_count: called CC=3 SCAR=11.0
Pod undocumented
258 5 my $count = 0;
259 5 for my $val ( values %{ $self->{shots} } ) {
260 6 ▶ $count++ if $val eq 'M';
Branch: 1 branch
BranchTrueFalseTotal
if $val eq "M" 3 3 6
261 }
262 5 return $count;
263 }
264  
265 5 ▶sub shot_accuracy ($self) {
Subroutine shot_accuracy: called CC=2 SCAR=6.9
Pod undocumented
266 5 my $total = $self->total_shots;
267 5 ▶ return 0.0 if $total == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $total == 0 2 3 5
268 3 return ( $self->hit_count / $total ) * 100.0;
269 }
270  
271 3 1;
272  
« Battleship/AI/Hard.pm ↑ Summary Battleship/Ship.pm »