| 1 | package Games::GUI::Battleship::Board; | ||
| 2 | |||
| 3 | use v5.38; | ||
| 4 | use experimental 'signatures'; | ||
| 5 | use feature 'try'; | ||
| 6 | no warnings 'experimental::try'; | ||
| 7 | use Carp qw(croak); | ||
| 8 | use Games::GUI::Battleship::Ship; | ||
| 9 | |||
| 10 | sub new ( $class, %args ) { | ||
| 11 | my $size = $args{size} // 10; | ||
| 12 | |||
| 13 | my $self = { | ||
| 14 | size => int($size), | ||
| 15 | ships => [], | ||
| 16 | grid => {}, | ||
| 17 | shots => {}, | ||
| 18 | }; | ||
| 19 | |||
| 20 | return bless $self, $class; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub size ($self) { | ||
| 24 | return $self->{size}; | ||
| 25 | } | ||
| 26 | |||
| 27 | sub ships ($self) { | ||
| 28 | return @{ $self->{ships} }; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub ship_count ($self) { | ||
| 32 | return scalar @{ $self->{ships} }; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub ship_named ( $self, $type ) { | ||
| 36 | for my $ship ( @{ $self->{ships} } ) { | ||
| 37 | return $ship if $ship->type eq $type; | ||
| 38 | } | ||
| 39 | return undef; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub can_place_ship ( $self, $ship ) { | ||
| 43 | return $self->can_place( $ship->type, $ship->x, $ship->y, $ship->dir, | ||
| 44 | $ship ); | ||
| 45 | } | ||
| 46 | |||
| 47 | sub can_place ( $self, $type, $x, $y, $dir, $ignore_ship = undef ) { | ||
| 48 | my $len = $Games::GUI::Battleship::Ship::SHIP_SIZES{$type} | ||
| 49 | // croak "Unknown ship type: $type"; | ||
| 50 | $dir = uc($dir); | ||
| 51 | |||
| 52 | my $size = $self->{size}; | ||
| 53 | |||
| 54 | for my $i ( 0 .. $len - 1 ) { | ||
| 55 | my $cx = ( $dir eq 'H' ) ? $x + $i : $x; | ||
| 56 | my $cy = ( $dir eq 'V' ) ? $y + $i : $y; | ||
| 57 | |||
| 58 | # Bounds check (1-indexed) | ||
| 59 | return 0 if $cx < 1 || $cx > $size || $cy < 1 || $cy > $size; | ||
| 60 | |||
| 61 | # Collision check | ||
| 62 | my $existing = $self->ship_at( $cx, $cy ); | ||
| 63 | if ($existing) { | ||
| 64 | if ( !defined $ignore_ship | ||
| 65 | || $existing->type ne $ignore_ship->type ) | ||
| 66 | { | ||
| 67 | return 0; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | return 1; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub place_ship ( $self, $ship ) { | ||
| 76 | if ( !$self->can_place_ship($ship) ) { | ||
| 77 | 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 | $self->remove_ship( $ship->type ); | ||
| 86 | |||
| 87 | push @{ $self->{ships} }, $ship; | ||
| 88 | for my $coord ( $ship->coordinates ) { | ||
| 89 | $self->{grid}{"$coord->[0],$coord->[1]"} = $ship; | ||
| 90 | } | ||
| 91 | |||
| 92 | return $ship; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub remove_ship ( $self, $type ) { | ||
| 96 | my $removed; | ||
| 97 | my @kept; | ||
| 98 | for my $ship ( @{ $self->{ships} } ) { | ||
| 99 | if ( $ship->type eq $type ) { | ||
| 100 | $removed = $ship; | ||
| 101 | for my $coord ( $ship->coordinates ) { | ||
| 102 | delete $self->{grid}{"$coord->[0],$coord->[1]"}; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | else { | ||
| 106 | push @kept, $ship; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $self->{ships} = \@kept; | ||
| 110 | return $removed; | ||
| 111 | } | ||
| 112 | |||
| 113 | sub clear_ships ($self) { | ||
| 114 | $self->{ships} = []; | ||
| 115 | $self->{grid} = {}; | ||
| 116 | return $self; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub place_random_fleet ($self) { | ||
| 120 | $self->clear_ships; | ||
| 121 | my $size = $self->{size}; | ||
| 122 | |||
| 123 | for my $type (@Games::GUI::Battleship::Ship::FLEET_ORDER) { | ||
| 124 | my $placed = 0; | ||
| 125 | my $attempts = 0; | ||
| 126 | |||
| 127 | while ( !$placed && $attempts < 1000 ) { | ||
| 128 | $attempts++; | ||
| 129 | my $dir = int( rand(2) ) ? 'H' : 'V'; | ||
| 130 | my $x = 1 + int( rand($size) ); | ||
| 131 | my $y = 1 + int( rand($size) ); | ||
| 132 | |||
| 133 | if ( $self->can_place( $type, $x, $y, $dir ) ) { | ||
| 134 | my $ship = Games::GUI::Battleship::Ship->new( | ||
| 135 | type => $type, | ||
| 136 | x => $x, | ||
| 137 | y => $y, | ||
| 138 | dir => $dir, | ||
| 139 | ); | ||
| 140 | $self->place_ship($ship); | ||
| 141 | $placed = 1; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | if ( !$placed ) { | ||
| 146 | |||
| 147 | # In the rare event of deadlock, restart placement | ||
| 148 | return $self->place_random_fleet; | ||
| 149 | } | ||
| 150 | } | ||
| 151 | |||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub has_shot ( $self, $x, $y ) { | ||
| 156 | return exists $self->{shots}{"$x,$y"} ? 1 : 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub shot_at ( $self, $x, $y ) { | ||
| 160 | return $self->{shots}{"$x,$y"}; | ||
| 161 | } | ||
| 162 | |||
| 163 | sub ship_at ( $self, $x, $y ) { | ||
| 164 | return $self->{grid}{"$x,$y"}; | ||
| 165 | } | ||
| 166 | |||
| 167 | sub receive_shot ( $self, $x, $y ) { | ||
| 168 | my $size = $self->{size}; | ||
| 169 | if ( $x < 1 || $x > $size || $y < 1 || $y > $size ) { | ||
| 170 | croak "Shot coordinates out of range: ($x, $y)"; | ||
| 171 | } | ||
| 172 | |||
| 173 | if ( $self->has_shot( $x, $y ) ) { | ||
| 174 | return undef; | ||
| 175 | } | ||
| 176 | |||
| 177 | my $ship = $self->ship_at( $x, $y ); | ||
| 178 | |||
| 179 | if ($ship) { | ||
| 180 | $self->{shots}{"$x,$y"} = 'H'; | ||
| 181 | $ship->record_hit( $x, $y ); | ||
| 182 | |||
| 183 | if ( $ship->is_sunk ) { | ||
| 184 | |||
| 185 | # Update all coordinates of this ship to 'S' | ||
| 186 | for my $coord ( $ship->coordinates ) { | ||
| 187 | $self->{shots}{"$coord->[0],$coord->[1]"} = 'S'; | ||
| 188 | } | ||
| 189 | return { | ||
| 190 | result => 'sunk', | ||
| 191 | ship => $ship, | ||
| 192 | x => $x, | ||
| 193 | y => $y, | ||
| 194 | }; | ||
| 195 | } | ||
| 196 | |||
| 197 | return { | ||
| 198 | result => 'hit', | ||
| 199 | ship => $ship, | ||
| 200 | x => $x, | ||
| 201 | y => $y, | ||
| 202 | }; | ||
| 203 | } | ||
| 204 | |||
| 205 | $self->{shots}{"$x,$y"} = 'M'; | ||
| 206 | return { | ||
| 207 | result => 'miss', | ||
| 208 | x => $x, | ||
| 209 | y => $y, | ||
| 210 | }; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub all_sunk ($self) { | ||
| 214 | return 0 if scalar( @{ $self->{ships} } ) == 0; | ||
| 215 | for my $ship ( @{ $self->{ships} } ) { | ||
| 216 | return 0 if !$ship->is_sunk; | ||
| 217 | } | ||
| 218 | return 1; | ||
| 219 | } | ||
| 220 | |||
| 221 | sub ships_remaining ($self) { | ||
| 222 | my $count = 0; | ||
| 223 | for my $ship ( @{ $self->{ships} } ) { | ||
| 224 | $count++ if !$ship->is_sunk; | ||
| 225 | } | ||
| 226 | return $count; | ||
| 227 | } | ||
| 228 | |||
| 229 | sub ships_sunk ($self) { | ||
| 230 | my $count = 0; | ||
| 231 | for my $ship ( @{ $self->{ships} } ) { | ||
| 232 | $count++ if $ship->is_sunk; | ||
| 233 | } | ||
| 234 | return $count; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub reset_shots ($self) { | ||
| 238 | $self->{shots} = {}; | ||
| 239 | for my $ship ( @{ $self->{ships} } ) { | ||
| 240 | $ship->clear_hits; | ||
| 241 | } | ||
| 242 | return $self; | ||
| 243 | } | ||
| 244 | |||
| 245 | sub total_shots ($self) { | ||
| 246 | return scalar keys %{ $self->{shots} }; | ||
| 247 | } | ||
| 248 | |||
| 249 | sub hit_count ($self) { | ||
| 250 | my $count = 0; | ||
| 251 | for my $val ( values %{ $self->{shots} } ) { | ||
| 252 | $count++ if $val eq 'H' || $val eq 'S'; | ||
| 253 | } | ||
| 254 | return $count; | ||
| 255 | } | ||
| 256 | |||
| 257 | sub miss_count ($self) { | ||
| 258 | my $count = 0; | ||
| 259 | for my $val ( values %{ $self->{shots} } ) { | ||
| 260 | $count++ if $val eq 'M'; | ||
| 261 | } | ||
| 262 | return $count; | ||
| 263 | } | ||
| 264 | |||
| 265 | sub shot_accuracy ($self) { | ||
| 266 | my $total = $self->total_shots; | ||
| 267 | return 0.0 if $total == 0; | ||
| 268 | return ( $self->hit_count / $total ) * 100.0; | ||
| 269 | } | ||
| 270 | |||
| 271 | 1; | ||
| 272 |