blib/lib/Games/GUI/Battleship/Ship.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 141 CC 11 SCAR 16.2
CC
11
Cov
0%
coordinates
12.0
new
6.0
set_position
6.0
CRAP 5.0
1 package Games::GUI::Battleship::Ship;
2  
3 use v5.38;
4 use experimental 'signatures';
5 use feature 'try';
6 no warnings 'experimental::try';
7 use Carp qw(croak);
8  
9 # Standard Battleship fleet specifications: name => length
10 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 our @FLEET_ORDER =
20 ( 'Carrier', 'Battleship', 'Cruiser', 'Submarine', 'Destroyer', );
21  
22 sub new ( $class, %args ) {
23 my $type = $args{type} // croak 'type is required';
24 my $x = $args{x} // croak 'x coordinate is required';
25 my $y = $args{y} // croak 'y coordinate is required';
26 my $dir = $args{dir} // 'H';
27  
28 my $len = $SHIP_SIZES{$type} // croak "Unknown ship type: $type";
29  
30 $dir = uc($dir);
31 if ( $dir ne 'H' && $dir ne 'V' ) {
32 croak "Invalid direction: $dir (must be 'H' or 'V')";
33 }
34  
35 my $self = {
36 type => $type,
37 x => int($x),
38 y => int($y),
39 dir => $dir,
40 len => $len,
41 hits => {},
42 };
43  
44 return bless $self, $class;
45 }
46  
47 sub type ($self) {
48 return $self->{type};
49 }
50  
51 sub length ($self) {
52 return $self->{len};
53 }
54  
55 sub x ($self) {
56 return $self->{x};
57 }
58  
59 sub y ($self) {
60 return $self->{y};
61 }
62  
63 sub dir ($self) {
64 return $self->{dir};
65 }
66  
67 sub set_position ( $self, $x, $y, $dir = undef ) {
68 $self->{x} = int($x);
69 $self->{y} = int($y);
70 $self->{dir} = uc($dir) if defined $dir;
71 return $self;
72 }
73  
74 sub set_dir ( $self, $dir ) {
75 $dir = uc($dir);
76 if ( $dir ne 'H' && $dir ne 'V' ) {
77 croak "Invalid direction: $dir";
78 }
79 $self->{dir} = $dir;
80 return $self;
81 }
82  
83 sub rotate ($self) {
84 $self->{dir} = ( $self->{dir} eq 'H' ) ? 'V' : 'H';
85 return $self->{dir};
86 }
87  
88 sub coordinates ($self) {
89 my @coords;
90 my $len = $self->{len};
91 my $x = $self->{x};
92 my $y = $self->{y};
93 my $dir = $self->{dir};
94  
95 for my $i ( 0 .. $len - 1 ) {
96 my $cx = ( $dir eq 'H' ) ? $x + $i : $x;
97 my $cy = ( $dir eq 'V' ) ? $y + $i : $y;
98 push @coords, [ $cx, $cy ];
99 }
100 return @coords;
101 }
102  
103 sub occupies ( $self, $x, $y ) {
104 my $key = "$x,$y";
105 for my $coord ( $self->coordinates ) {
106 return 1 if "$coord->[0],$coord->[1]" eq $key;
107 }
108 return 0;
109 }
110  
111 sub record_hit ( $self, $x, $y ) {
112 if ( $self->occupies( $x, $y ) ) {
113 $self->{hits}{"$x,$y"} = 1;
114 return 1;
115 }
116 return 0;
117 }
118  
119 sub hit_count ($self) {
120 return scalar keys %{ $self->{hits} };
121 }
122  
123 sub is_sunk ($self) {
124 return $self->hit_count >= $self->{len} ? 1 : 0;
125 }
126  
127 sub is_hit_at ( $self, $x, $y ) {
128 return $self->{hits}{"$x,$y"} ? 1 : 0;
129 }
130  
131 sub clear_hits ($self) {
132 $self->{hits} = {};
133 return $self;
134 }
135  
136 1;
137