Battleship/Ship.pm

filter: Statementstmt 82.3% 56 / 68 Branchbran 60.0% 12 / 20 Conditioncond 43.7% 7 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 85.7% 18 / 21 Podpod 0.0% 0 / 16 totaltotal 62.0% 93 / 150 CC 21 SCAR 16.5
CC
21
Cov
72%
set_position
12.0
set_dir
12.0
new
10.1
CRAP 5.2
« Battleship/Board.pm ↑ Summary Battleship/AI.pm »
1 package Games::GUI::Battleship::Ship;
2  
3 4 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use feature 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶no warnings 'experimental::try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Standard Battleship fleet specifications: name => length
10 4 our %SHIP_SIZES = (
11 Carrier => 5,
12 Battleship => 4,
13 Cruiser => 3,
14 Submarine => 3,
15 Destroyer => 2,
16 );
17  
18 # Standard fleet deployment order
19 4 our @FLEET_ORDER =
20 ( 'Carrier', 'Battleship', 'Cruiser', 'Submarine', 'Destroyer', );
21  
22 14 ▶sub new ( $class, %args ) {
Subroutine new: called CC=8 SCAR=23.1
Pod undocumented
23 14 ▶ my $type = $args{type} // croak 'type is required';
Condition50%1/2
$args{"type"} // croak("type is required")
l!l
140
MC/DC0%0/1
$args{"type"} // croak("type is required")
$args{"type"}
Truth table50%1/2
A
A= $args{"type"}
Aresult
11
00
24 14 ▶ my $x = $args{x} // croak 'x coordinate is required';
Condition50%1/2
$args{"x"} // croak("x coordinate is required")
l!l
140
MC/DC0%0/1
$args{"x"} // croak("x coordinate is required")
$args{"x"}
Truth table50%1/2
A
A= $args{"x"}
Aresult
11
00
25 14 ▶ my $y = $args{y} // croak 'y coordinate is required';
Condition50%1/2
$args{"y"} // croak("y coordinate is required")
l!l
140
MC/DC0%0/1
$args{"y"} // croak("y coordinate is required")
$args{"y"}
Truth table50%1/2
A
A= $args{"y"}
Aresult
11
00
26 14 ▶ my $dir = $args{dir} // 'H';
Condition50%1/2
$args{"dir"} // "H"
l!l
140
MC/DC0%0/1
$args{"dir"} // "H"
$args{"dir"}
Truth table50%1/2
A
A= $args{"dir"}
Aresult
11
00
27  
28 14 ▶ my $len = $SHIP_SIZES{$type} // croak "Unknown ship type: $type";
Condition50%1/2
$Games::GUI::Battleship::Ship::SHIP_SIZES{$type} // croak("Unknown ship type: $type")
l!l
140
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
29  
30 14 $dir = uc($dir);
31 14 ▶ if ( $dir ne 'H' && $dir ne 'V' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "H" and $dir ne "V") 0 14 14
Condition66%2/3
$dir ne "H" and $dir ne "V"
!ll&&!rl&&r
860
MC/DC0%0/2
$dir ne "H" and $dir ne "V"
$dir ne "H" $dir ne "V"
Truth table66%2/3
A and B
A= $dir ne "H"B= $dir ne "V"
ABresult
0X0
100
111
32 0 croak "Invalid direction: $dir (must be 'H' or 'V')";
33 }
34  
35 14 my $self = {
36 type => $type,
37 x => int($x),
38 y => int($y),
39 dir => $dir,
40 len => $len,
41 hits => {},
42 };
43  
44 14 return bless $self, $class;
45 }
46  
47 72 ▶sub type ($self) {
Subroutine type: called CC=1 SCAR=0.0
Pod undocumented
48 72 return $self->{type};
49 }
50  
51 1 ▶sub length ($self) {
Subroutine length: called CC=1 SCAR=0.0
Pod undocumented
52 1 return $self->{len};
53 }
54  
55 14 ▶sub x ($self) {
Subroutine x: called CC=1 SCAR=0.0
Pod undocumented
56 14 return $self->{x};
57 }
58  
59 14 ▶sub y ($self) {
Subroutine y: called CC=1 SCAR=0.0
Pod undocumented
60 14 return $self->{y};
61 }
62  
63 14 ▶sub dir ($self) {
Subroutine dir: called CC=1 SCAR=0.0
Pod undocumented
64 14 return $self->{dir};
65 }
66  
67 0 ▶sub set_position ( $self, $x, $y, $dir = undef ) {
Subroutine set_position: not called CC=3 SCAR=24.8
Pod undocumented
68 0 $self->{x} = int($x);
69 0 $self->{y} = int($y);
70 0 ▶ $self->{dir} = uc($dir) if defined $dir;
Branch: 1 branch
BranchTrueFalseTotal
if defined $dir 0 0 0
71 0 return $self;
72 }
73  
74 0 ▶sub set_dir ( $self, $dir ) {
Subroutine set_dir: not called CC=3 SCAR=24.8
Pod undocumented
75 0 $dir = uc($dir);
76 0 ▶ if ( $dir ne 'H' && $dir ne 'V' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "H" and $dir ne "V") 0 0 0
Condition0%0/3
$dir ne "H" and $dir ne "V"
!ll&&!rl&&r
000
MC/DC0%0/2
$dir ne "H" and $dir ne "V"
$dir ne "H" $dir ne "V"
Truth table0%0/3
A and B
A= $dir ne "H"B= $dir ne "V"
ABresult
0X0
100
111
77 0 croak "Invalid direction: $dir";
78 }
79 0 $self->{dir} = $dir;
80 0 return $self;
81 }
82  
83 1 ▶sub rotate ($self) {
Subroutine rotate: called CC=2 SCAR=7.2
Pod undocumented
84 1 ▶ $self->{dir} = ( $self->{dir} eq 'H' ) ? 'V' : 'H';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'dir'} eq 'H' ? : 1 0 1
85 1 return $self->{dir};
86 }
87  
88 37 ▶sub coordinates ($self) {
Subroutine coordinates: called CC=4 SCAR=13.9
Pod undocumented
89 37 my @coords;
90 37 my $len = $self->{len};
91 37 my $x = $self->{x};
92 37 my $y = $self->{y};
93 37 my $dir = $self->{dir};
94  
95 37 for my $i ( 0 .. $len - 1 ) {
96 112 ▶ my $cx = ( $dir eq 'H' ) ? $x + $i : $x;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'H' ? : 57 55 112
97 112 ▶ my $cy = ( $dir eq 'V' ) ? $y + $i : $y;
Branch: 1 branch
BranchTrueFalseTotal
$dir eq 'V' ? : 55 57 112
98 112 push @coords, [ $cx, $cy ];
99 }
100 37 return @coords;
101 }
102  
103 16 ▶sub occupies ( $self, $x, $y ) {
Subroutine occupies: called CC=3 SCAR=11.0
Pod undocumented
104 16 my $key = "$x,$y";
105 16 for my $coord ( $self->coordinates ) {
106 36 ▶ return 1 if "$coord->[0],$coord->[1]" eq $key;
Branch: 1 branch
BranchTrueFalseTotal
if "$coord->[0],$coord->[1]" eq $key 13 23 36
107 }
108 3 return 0;
109 }
110  
111 13 ▶sub record_hit ( $self, $x, $y ) {
Subroutine record_hit: called CC=2 SCAR=6.9
Pod undocumented
112 13 ▶ if ( $self->occupies( $x, $y ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->occupies($x, $y)) 12 1 13
113 12 $self->{hits}{"$x,$y"} = 1;
114 12 return 1;
115 }
116 1 return 0;
117 }
118  
119 16 ▶sub hit_count ($self) {
Subroutine hit_count: called CC=1 SCAR=0.0
Pod undocumented
120 16 return scalar keys %{ $self->{hits} };
121 }
122  
123 12 ▶sub is_sunk ($self) {
Subroutine is_sunk: called CC=2 SCAR=6.9
Pod undocumented
124 12 ▶ return $self->hit_count >= $self->{len} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->hit_count >= $self->{'len'} ? : 6 6 12
125 }
126  
127 0 ▶sub is_hit_at ( $self, $x, $y ) {
Subroutine is_hit_at: not called CC=2 SCAR=17.9
Pod undocumented
128 0 ▶ return $self->{hits}{"$x,$y"} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'hits'}{"$x,$y"} ? : 0 0 0
129 }
130  
131 1 ▶sub clear_hits ($self) {
Subroutine clear_hits: called CC=1 SCAR=0.0
Pod undocumented
132 1 $self->{hits} = {};
133 1 return $self;
134 }
135  
136 4 1;
137  
« Battleship/Board.pm ↑ Summary Battleship/AI.pm »