| 1 | package Games::GUI::Battleship; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.38; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use experimental 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'try'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::try'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Carp qw(croak); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 0 | ▶ | use Prima qw(Application MsgBox Cairo); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use Cairo; | ||
| 11 | use Time::HiRes qw(time); | ||
| 12 | |||
| 13 | use Games::GUI::Battleship::Ship; | ||
| 14 | use Games::GUI::Battleship::Board; | ||
| 15 | use Games::GUI::Battleship::Renderer; | ||
| 16 | use Games::GUI::Battleship::AI; | ||
| 17 | |||
| 18 | use Audio::Play::Native; | ||
| 19 | use File::Basename qw(dirname); | ||
| 20 | use File::Spec; | ||
| 21 | |||
| 22 | =pod | ||
| 23 | |||
| 24 | =head1 NAME | ||
| 25 | |||
| 26 | Games::GUI::Battleship - Play a game of Battleship through a GUI | ||
| 27 | |||
| 28 | =head1 VERSION | ||
| 29 | |||
| 30 | Version 1.1 | ||
| 31 | |||
| 32 | =head1 SYNOPSIS | ||
| 33 | |||
| 34 | battleship | ||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | Play a game of Battleship against a computer opponent. It uses L<Prima> for the GUI and L<Cairo> for the graphics. | ||
| 39 | |||
| 40 | =for HTML <p> | ||
| 41 | <img src="https://github.com/mjohnson108/p5-Games-GUI-Battleship/blob/e80cfa124775dada0a0cefe168e3b980cfb3d7e4/examples/POD/battleship_screenshot.png?raw=true" alt="Screenshot of Battleship program" width="650"> | ||
| 42 | </p> | ||
| 43 | |||
| 44 | =head2 Menus | ||
| 45 | |||
| 46 | =head3 Game | ||
| 47 | |||
| 48 | The I<New Game> option will start a new game at a given difficulty level. | ||
| 49 | |||
| 50 | I<Randomize Fleet> will reset any ongoing game and deploy your vessels in a random layout, bypassing the need for manually placing the vessels. | ||
| 51 | |||
| 52 | I<Reset Fleet> will reset the game. | ||
| 53 | |||
| 54 | I<Exit> exits the game. | ||
| 55 | |||
| 56 | =head3 Difficulty | ||
| 57 | |||
| 58 | Select the level of difficulty. There are three different settings, each backed by an "AI" with different play strategies. | ||
| 59 | |||
| 60 | =head3 Radar | ||
| 61 | |||
| 62 | Use I<Radar Sweep Effect> to toggle the scanning radar visual effect. | ||
| 63 | |||
| 64 | I<Speed> gives options on the speed of the radar sweep. | ||
| 65 | |||
| 66 | =head3 Help | ||
| 67 | |||
| 68 | I<How to Play> shows a short instructions dialog. | ||
| 69 | |||
| 70 | =head3 About | ||
| 71 | |||
| 72 | shows a small about dialog. | ||
| 73 | |||
| 74 | =head2 How to play | ||
| 75 | |||
| 76 | Place vessels by clicking the mouse in the left grid. The game will highlight where the vessel will be placed. Use the space bar, "R" key, or right-click the mouse to rotate the vessel before placement. | ||
| 77 | |||
| 78 | Once all vessels are placed, click empty cells in the right grid to launch attacks on the opponent. The cell will record a hit or miss icon. Take turns trading attacks with the computer opponent until there is a winner. | ||
| 79 | |||
| 80 | =head1 SEE ALSO | ||
| 81 | |||
| 82 | L<Prima> | ||
| 83 | |||
| 84 | L<Logic::Relational> | ||
| 85 | |||
| 86 | =head1 AUTHOR | ||
| 87 | |||
| 88 | Matt Johnson, C<< <mjohnson at affectivesilicon.com> >> | ||
| 89 | |||
| 90 | =head1 AI | ||
| 91 | |||
| 92 | This game was developed with assistance from Gemini Flash 3.8. | ||
| 93 | |||
| 94 | =head1 LICENSE AND COPYRIGHT | ||
| 95 | |||
| 96 | This software is Copyright (c) 2026 by Matt Johnson. | ||
| 97 | |||
| 98 | This is free software, licensed under: | ||
| 99 | |||
| 100 | The GNU General Public License, Version 3, June 2007 | ||
| 101 | |||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | our $VERSION = '1.1'; | ||
| 107 | |||
| 108 | my %SWEEP_PERIODS = ( | ||
| 109 | slow => 7.0, | ||
| 110 | normal => 4.5, | ||
| 111 | fast => 2.5, | ||
| 112 | ); | ||
| 113 | |||
| 114 | sub new ( $class, %args ) { | ||
| 115 | my $difficulty = $args{difficulty} // 'normal'; | ||
| 116 | |||
| 117 | my $self = { | ||
| 118 | difficulty => $difficulty, | ||
| 119 | ai => Games::GUI::Battleship::AI->create($difficulty), | ||
| 120 | player_board => Games::GUI::Battleship::Board->new, | ||
| 121 | opponent_board => Games::GUI::Battleship::Board->new, | ||
| 122 | renderer => Games::GUI::Battleship::Renderer->new, | ||
| 123 | phase => 'placement', # placement, battle, game_over | ||
| 124 | placement_idx => 0, | ||
| 125 | placement_dir => 'H', | ||
| 126 | ghost_ship => undef, | ||
| 127 | ghost_valid => 0, | ||
| 128 | hover_col => undef, | ||
| 129 | hover_row => undef, | ||
| 130 | hover_opp_col => undef, | ||
| 131 | hover_opp_row => undef, | ||
| 132 | status_msg => 'Deploy your Carrier (length 5). Left-click to place, Right-click or "R" to rotate.', | ||
| 133 | msg_type => 'info', | ||
| 134 | window => undef, | ||
| 135 | ai_timer => undef, | ||
| 136 | radar_sweep_enabled => 1, | ||
| 137 | radar_sweep_speed => 'normal', | ||
| 138 | sweep_progress => 0.0, | ||
| 139 | sweep_timer => undef, | ||
| 140 | last_sweep_time => undef, | ||
| 141 | }; | ||
| 142 | |||
| 143 | bless $self, $class; | ||
| 144 | return $self; | ||
| 145 | } | ||
| 146 | |||
| 147 | sub run ($self) { | ||
| 148 | $self->create_window; | ||
| 149 | Prima->run; | ||
| 150 | return; | ||
| 151 | } | ||
| 152 | |||
| 153 | sub create_window ($self) { | ||
| 154 | my $initial_size = [ 940, 620 ]; | ||
| 155 | |||
| 156 | my $window = Prima::MainWindow->new( | ||
| 157 | text => sprintf( 'Battleship [%s]', | ||
| 158 | ucfirst( $self->{difficulty} ) ), | ||
| 159 | size => $initial_size, | ||
| 160 | minSize => [ 820, 560 ], | ||
| 161 | layered => 1, | ||
| 162 | buffered => 1, | ||
| 163 | menuItems => $self->build_menu, | ||
| 164 | onSize => sub ( $w, @rest ) { $w->repaint }, | ||
| 165 | onPaint => sub ( $w, $canvas, @rest ) { | ||
| 166 | $self->handle_window_paint( $w, $canvas ); | ||
| 167 | }, | ||
| 168 | onMouseMove => sub ( $w, $mod, $x, $y, @rest ) { | ||
| 169 | $self->handle_window_mouse_move( $w, $x, $y ); | ||
| 170 | }, | ||
| 171 | onMouseDown => sub ( $w, $btn, $mod, $x, $y, @rest ) { | ||
| 172 | $self->handle_window_mouse_down( $w, $btn, $x, $y ); | ||
| 173 | }, | ||
| 174 | onDestroy => sub ( $w, @rest ) { | ||
| 175 | $self->{ai_timer}->stop if $self->{ai_timer}; | ||
| 176 | $self->{sweep_timer}->stop if $self->{sweep_timer}; | ||
| 177 | }, | ||
| 178 | onKeyDown => sub ( $w, $code, $key, $mod, @rest ) { | ||
| 179 | $self->handle_window_key_down( $w, $code, $key ); | ||
| 180 | }, | ||
| 181 | ); | ||
| 182 | |||
| 183 | # AI Turn Timer (allows player shot result to be perceived before AI retaliates) | ||
| 184 | $self->{ai_timer} = Prima::Timer->new( | ||
| 185 | timeout => 1800, | ||
| 186 | onTick => sub { | ||
| 187 | $self->{ai_timer}->stop; | ||
| 188 | $self->process_ai_turn; | ||
| 189 | }, | ||
| 190 | ); | ||
| 191 | $self->{ai_timer}->stop; | ||
| 192 | |||
| 193 | # Radar Sweep Animation Timer (~30 FPS, timeout 33ms) | ||
| 194 | $self->{last_sweep_time} = Time::HiRes::time(); | ||
| 195 | $self->{sweep_timer} = Prima::Timer->new( | ||
| 196 | timeout => 33, | ||
| 197 | onTick => sub { | ||
| 198 | $self->advance_sweep; | ||
| 199 | }, | ||
| 200 | ); | ||
| 201 | if ( $self->{radar_sweep_enabled} ) { | ||
| 202 | $self->{sweep_timer}->start; | ||
| 203 | } | ||
| 204 | else { | ||
| 205 | $self->{sweep_timer}->stop; | ||
| 206 | } | ||
| 207 | |||
| 208 | $self->{window} = $window; | ||
| 209 | $self->update_radar_menu; | ||
| 210 | return $window; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub handle_window_paint ( $self, $w, $canvas ) { | ||
| 214 | $w->clear; | ||
| 215 | my ( $vp_w, $vp_h ) = $w->size; | ||
| 216 | my $cr = $canvas->cairo_context( transform => 0 ); | ||
| 217 | |||
| 218 | $self->{renderer}->render( | ||
| 219 | $cr, $vp_w, $vp_h, | ||
| 220 | phase => $self->{phase}, | ||
| 221 | player_board => $self->{player_board}, | ||
| 222 | opponent_board => $self->{opponent_board}, | ||
| 223 | ghost_ship => $self->{ghost_ship}, | ||
| 224 | ghost_valid => $self->{ghost_valid}, | ||
| 225 | hover_opponent_col => $self->{hover_opp_col}, | ||
| 226 | hover_opponent_row => $self->{hover_opp_row}, | ||
| 227 | status_msg => $self->{status_msg}, | ||
| 228 | msg_type => $self->{msg_type}, | ||
| 229 | ai_name => $self->{ai}->name, | ||
| 230 | ai_difficulty => ucfirst( $self->{ai}->difficulty ), | ||
| 231 | radar_sweep_enabled => $self->{radar_sweep_enabled}, | ||
| 232 | sweep_progress => $self->{sweep_progress}, | ||
| 233 | ); | ||
| 234 | return; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub handle_window_mouse_move ( $self, $w, $x, $y ) { | ||
| 238 | my ( $vp_w, $vp_h ) = $w->size; | ||
| 239 | my $cx = $x; | ||
| 240 | my $cy = $vp_h - $y; | ||
| 241 | |||
| 242 | if ( $self->{phase} eq 'placement' ) { | ||
| 243 | my ( $col, $row ) = | ||
| 244 | $self->{renderer}->point_to_cell( $cx, $cy, 'player' ); | ||
| 245 | $self->{hover_col} = $col; | ||
| 246 | $self->{hover_row} = $row; | ||
| 247 | $self->update_placement_ghost( $col, $row ); | ||
| 248 | $w->repaint; | ||
| 249 | } | ||
| 250 | elsif ( $self->{phase} eq 'battle' ) { | ||
| 251 | my ( $col, $row ) = | ||
| 252 | $self->{renderer}->point_to_cell( $cx, $cy, 'opponent' ); | ||
| 253 | if ( ( $self->{hover_opp_col} // 0 ) != ( $col // 0 ) | ||
| 254 | || ( $self->{hover_opp_row} // 0 ) != ( $row // 0 ) ) | ||
| 255 | { | ||
| 256 | $self->{hover_opp_col} = $col; | ||
| 257 | $self->{hover_opp_row} = $row; | ||
| 258 | $w->repaint; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | return; | ||
| 262 | } | ||
| 263 | |||
| 264 | sub handle_window_mouse_down ( $self, $w, $btn, $x, $y ) { | ||
| 265 | my ( $vp_w, $vp_h ) = $w->size; | ||
| 266 | my $cx = $x; | ||
| 267 | my $cy = $vp_h - $y; | ||
| 268 | |||
| 269 | # Right-click rotates current placement ship | ||
| 270 | if ( $btn == mb::Right ) { | ||
| 271 | if ( $self->{phase} eq 'placement' ) { | ||
| 272 | $self->rotate_placement_ship; | ||
| 273 | my ( $col, $row ) = | ||
| 274 | $self->{renderer}->point_to_cell( $cx, $cy, 'player' ); | ||
| 275 | $self->{hover_col} = $col; | ||
| 276 | $self->{hover_row} = $row; | ||
| 277 | $self->update_placement_ghost( $col, $row ); | ||
| 278 | $w->repaint; | ||
| 279 | } | ||
| 280 | return; | ||
| 281 | } | ||
| 282 | |||
| 283 | return unless $btn == mb::Left; | ||
| 284 | |||
| 285 | if ( $self->{phase} eq 'placement' ) { | ||
| 286 | my ( $col, $row ) = | ||
| 287 | $self->{renderer}->point_to_cell( $cx, $cy, 'player' ); | ||
| 288 | $self->handle_placement_click( $col, $row ); | ||
| 289 | } | ||
| 290 | elsif ( $self->{phase} eq 'battle' ) { | ||
| 291 | my ( $col, $row ) = | ||
| 292 | $self->{renderer}->point_to_cell( $cx, $cy, 'opponent' ); | ||
| 293 | $self->handle_battle_click( $col, $row ); | ||
| 294 | } | ||
| 295 | return; | ||
| 296 | } | ||
| 297 | |||
| 298 | sub handle_window_key_down ( $self, $w, $code, $key ) { | ||
| 299 | if ( $key == kb::Space || chr($code) eq 'r' || chr($code) eq 'R' ) { | ||
| 300 | if ( $self->{phase} eq 'placement' ) { | ||
| 301 | $self->rotate_placement_ship; | ||
| 302 | if ( defined $self->{hover_col} | ||
| 303 | && defined $self->{hover_row} ) | ||
| 304 | { | ||
| 305 | $self->update_placement_ghost( $self->{hover_col}, | ||
| 306 | $self->{hover_row} ); | ||
| 307 | } | ||
| 308 | $w->repaint; | ||
| 309 | } | ||
| 310 | } | ||
| 311 | if ( ( $key == kb::Space || $key == kb::Enter ) | ||
| 312 | && $self->{phase} eq 'battle' ) | ||
| 313 | { | ||
| 314 | if ( defined $self->{hover_opp_col} | ||
| 315 | && defined $self->{hover_opp_row} ) | ||
| 316 | { | ||
| 317 | $self->handle_battle_click( $self->{hover_opp_col}, | ||
| 318 | $self->{hover_opp_row} ); | ||
| 319 | } | ||
| 320 | } | ||
| 321 | return; | ||
| 322 | } | ||
| 323 | |||
| 324 | sub build_menu ($self) { | ||
| 325 | return [ | ||
| 326 | [ | ||
| 327 | '~Game' => [ | ||
| 328 | [ | ||
| 329 | '~New Game' => [ | ||
| 330 | [ | ||
| 331 | '~Easy (Naive AI)' => sub { | ||
| 332 | $self->start_new_game('easy'); | ||
| 333 | } | ||
| 334 | ], | ||
| 335 | [ | ||
| 336 | '~Normal (Parity AI)' => sub { | ||
| 337 | $self->start_new_game('normal'); | ||
| 338 | } | ||
| 339 | ], | ||
| 340 | [ | ||
| 341 | '~Hard (Probability AI)' => sub { | ||
| 342 | $self->start_new_game('hard'); | ||
| 343 | } | ||
| 344 | ], | ||
| 345 | ] | ||
| 346 | ], | ||
| 347 | [ | ||
| 348 | '~Randomize Fleet' => sub { | ||
| 349 | $self->randomize_player_fleet; | ||
| 350 | } | ||
| 351 | ], | ||
| 352 | [ | ||
| 353 | 'R~eset Fleet' => sub { | ||
| 354 | $self->reset_placement; | ||
| 355 | } | ||
| 356 | ], | ||
| 357 | [], | ||
| 358 | [ | ||
| 359 | 'E~xit' => sub { | ||
| 360 | $self->{window}->close if $self->{window}; | ||
| 361 | } | ||
| 362 | ], | ||
| 363 | ] | ||
| 364 | ], | ||
| 365 | [ | ||
| 366 | '~Difficulty' => [ | ||
| 367 | [ | ||
| 368 | '~Easy (Naive AI)' => sub { | ||
| 369 | $self->start_new_game('easy'); | ||
| 370 | } | ||
| 371 | ], | ||
| 372 | [ | ||
| 373 | '~Normal (Parity AI)' => sub { | ||
| 374 | $self->start_new_game('normal'); | ||
| 375 | } | ||
| 376 | ], | ||
| 377 | [ | ||
| 378 | '~Hard (Probability AI)' => sub { | ||
| 379 | $self->start_new_game('hard'); | ||
| 380 | } | ||
| 381 | ], | ||
| 382 | ] | ||
| 383 | ], | ||
| 384 | [ | ||
| 385 | '~Radar' => [ | ||
| 386 | [ | ||
| 387 | '*sweep_toggle' => '~Radar Sweep Effect' => sub { | ||
| 388 | $self->set_sweep_enabled( | ||
| 389 | !$self->{radar_sweep_enabled} ); | ||
| 390 | } | ||
| 391 | ], | ||
| 392 | [ | ||
| 393 | '~Speed' => [ | ||
| 394 | [ | ||
| 395 | 'speed_slow' => '~Slow (7.0s)' => sub { | ||
| 396 | $self->set_sweep_speed('slow'); | ||
| 397 | } | ||
| 398 | ], | ||
| 399 | [ | ||
| 400 | '*speed_normal' => '~Normal (4.5s)' => sub { | ||
| 401 | $self->set_sweep_speed('normal'); | ||
| 402 | } | ||
| 403 | ], | ||
| 404 | [ | ||
| 405 | 'speed_fast' => '~Fast (2.5s)' => sub { | ||
| 406 | $self->set_sweep_speed('fast'); | ||
| 407 | } | ||
| 408 | ], | ||
| 409 | ] | ||
| 410 | ], | ||
| 411 | ] | ||
| 412 | ], | ||
| 413 | [ | ||
| 414 | '~Help' => [ | ||
| 415 | [ | ||
| 416 | '~How to Play' => sub { | ||
| 417 | $self->show_rules_dialog; | ||
| 418 | } | ||
| 419 | ], | ||
| 420 | |||
| 421 | ] | ||
| 422 | ], | ||
| 423 | [], | ||
| 424 | [ | ||
| 425 | 'About' => sub { | ||
| 426 | $self->show_about_dialog; | ||
| 427 | } | ||
| 428 | ], | ||
| 429 | ]; | ||
| 430 | } | ||
| 431 | |||
| 432 | # ============================================================================== | ||
| 433 | # RADAR SWEEP ANIMATION & CONTROLS | ||
| 434 | # ============================================================================== | ||
| 435 | |||
| 436 | sub advance_sweep ( $self, $elapsed_sec = undef ) { | ||
| 437 | return unless $self->{radar_sweep_enabled}; | ||
| 438 | |||
| 439 | my $now = Time::HiRes::time(); | ||
| 440 | my $dt; | ||
| 441 | if ( defined $elapsed_sec ) { | ||
| 442 | $dt = $elapsed_sec; | ||
| 443 | } | ||
| 444 | else { | ||
| 445 | $dt = | ||
| 446 | defined $self->{last_sweep_time} | ||
| 447 | ? ( $now - $self->{last_sweep_time} ) | ||
| 448 | : 0.033; | ||
| 449 | |||
| 450 | # Prevent large jumps if window was backgrounded or system paused | ||
| 451 | $dt = 0.1 if $dt > 0.1; | ||
| 452 | } | ||
| 453 | $self->{last_sweep_time} = $now; | ||
| 454 | |||
| 455 | my $period = $SWEEP_PERIODS{ $self->{radar_sweep_speed} } // 4.5; | ||
| 456 | $self->{sweep_progress} = ( $self->{sweep_progress} + ( $dt / $period ) ); | ||
| 457 | if ( $self->{sweep_progress} >= 1.0 ) { | ||
| 458 | $self->{sweep_progress} -= int( $self->{sweep_progress} ); | ||
| 459 | } | ||
| 460 | |||
| 461 | $self->{window}->repaint if $self->{window}; | ||
| 462 | return; | ||
| 463 | } | ||
| 464 | |||
| 465 | sub set_sweep_enabled ( $self, $enabled ) { | ||
| 466 | $self->{radar_sweep_enabled} = $enabled ? 1 : 0; | ||
| 467 | if ( $self->{radar_sweep_enabled} ) { | ||
| 468 | $self->{last_sweep_time} = Time::HiRes::time(); | ||
| 469 | $self->{sweep_timer}->start if $self->{sweep_timer}; | ||
| 470 | } | ||
| 471 | else { | ||
| 472 | $self->{sweep_timer}->stop if $self->{sweep_timer}; | ||
| 473 | } | ||
| 474 | $self->update_radar_menu; | ||
| 475 | $self->{window}->repaint if $self->{window}; | ||
| 476 | return; | ||
| 477 | } | ||
| 478 | |||
| 479 | sub set_sweep_speed ( $self, $speed ) { | ||
| 480 | return unless exists $SWEEP_PERIODS{$speed}; | ||
| 481 | $self->{radar_sweep_speed} = $speed; | ||
| 482 | $self->update_radar_menu; | ||
| 483 | return; | ||
| 484 | } | ||
| 485 | |||
| 486 | sub update_radar_menu ($self) { | ||
| 487 | return unless $self->{window} && $self->{window}->menu; | ||
| 488 | my $menu = $self->{window}->menu; | ||
| 489 | |||
| 490 | if ( $self->{radar_sweep_enabled} ) { | ||
| 491 | $menu->check('sweep_toggle'); | ||
| 492 | } | ||
| 493 | else { | ||
| 494 | $menu->uncheck('sweep_toggle'); | ||
| 495 | } | ||
| 496 | |||
| 497 | for my $spd (qw(slow normal fast)) { | ||
| 498 | my $tag = 'speed_' . $spd; | ||
| 499 | if ( $self->{radar_sweep_speed} eq $spd ) { | ||
| 500 | $menu->check($tag); | ||
| 501 | } | ||
| 502 | else { | ||
| 503 | $menu->uncheck($tag); | ||
| 504 | } | ||
| 505 | } | ||
| 506 | return; | ||
| 507 | } | ||
| 508 | |||
| 509 | # ============================================================================== | ||
| 510 | # GAME STATE ACTIONS | ||
| 511 | # ============================================================================== | ||
| 512 | |||
| 513 | sub start_new_game ( $self, $difficulty = undef ) { | ||
| 514 | $self->{ai_timer}->stop if $self->{ai_timer}; | ||
| 515 | $self->{difficulty} = $difficulty if defined $difficulty; | ||
| 516 | $self->{ai} = Games::GUI::Battleship::AI->create( $self->{difficulty} ); | ||
| 517 | $self->{player_board}->clear_ships->reset_shots; | ||
| 518 | $self->{opponent_board}->clear_ships->reset_shots; | ||
| 519 | |||
| 520 | $self->{phase} = 'placement'; | ||
| 521 | $self->{placement_idx} = 0; | ||
| 522 | $self->{placement_dir} = 'H'; | ||
| 523 | $self->{ghost_ship} = undef; | ||
| 524 | $self->{ghost_valid} = 0; | ||
| 525 | $self->{hover_opp_col} = undef; | ||
| 526 | $self->{hover_opp_row} = undef; | ||
| 527 | |||
| 528 | my $first_ship = $Games::GUI::Battleship::Ship::FLEET_ORDER[0]; | ||
| 529 | my $len = $Games::GUI::Battleship::Ship::SHIP_SIZES{$first_ship}; | ||
| 530 | $self->{status_msg} = "Deploy your $first_ship (length $len). Left-click to place, Right-click or 'R' to rotate."; | ||
| 531 | $self->{msg_type} = 'info'; | ||
| 532 | |||
| 533 | if ( $self->{window} ) { | ||
| 534 | $self->{window}->text( | ||
| 535 | sprintf( 'Battleship [%s]', | ||
| 536 | ucfirst( $self->{difficulty} ) ) | ||
| 537 | ); | ||
| 538 | $self->{window}->repaint; | ||
| 539 | } | ||
| 540 | return; | ||
| 541 | } | ||
| 542 | |||
| 543 | sub rotate_placement_ship ($self) { | ||
| 544 | $self->{placement_dir} = ( $self->{placement_dir} eq 'H' ) ? 'V' : 'H'; | ||
| 545 | if ( $self->{ghost_ship} ) { | ||
| 546 | $self->{ghost_ship}->set_dir( $self->{placement_dir} ); | ||
| 547 | $self->{ghost_valid} = | ||
| 548 | $self->{player_board}->can_place_ship( $self->{ghost_ship} ); | ||
| 549 | } | ||
| 550 | return; | ||
| 551 | } | ||
| 552 | |||
| 553 | sub update_placement_ghost ( $self, $col, $row ) { | ||
| 554 | if ( !defined $col || !defined $row ) { | ||
| 555 | $self->{ghost_ship} = undef; | ||
| 556 | $self->{ghost_valid} = 0; | ||
| 557 | return; | ||
| 558 | } | ||
| 559 | |||
| 560 | my $type = | ||
| 561 | $Games::GUI::Battleship::Ship::FLEET_ORDER[ $self->{placement_idx} ]; | ||
| 562 | return unless $type; | ||
| 563 | |||
| 564 | my $ship = Games::GUI::Battleship::Ship->new( | ||
| 565 | type => $type, | ||
| 566 | x => $col, | ||
| 567 | y => $row, | ||
| 568 | dir => $self->{placement_dir}, | ||
| 569 | ); | ||
| 570 | |||
| 571 | $self->{ghost_ship} = $ship; | ||
| 572 | $self->{ghost_valid} = $self->{player_board}->can_place_ship($ship); | ||
| 573 | return; | ||
| 574 | } | ||
| 575 | |||
| 576 | sub handle_placement_click ( $self, $col, $row ) { | ||
| 577 | return unless defined $col && defined $row; | ||
| 578 | return unless $self->{ghost_ship} && $self->{ghost_valid}; | ||
| 579 | |||
| 580 | # Place the ship | ||
| 581 | $self->{player_board}->place_ship( $self->{ghost_ship} ); | ||
| 582 | $self->{placement_idx}++; | ||
| 583 | |||
| 584 | if ( $self->{placement_idx} < | ||
| 585 | scalar @Games::GUI::Battleship::Ship::FLEET_ORDER ) | ||
| 586 | { | ||
| 587 | my $next_ship = | ||
| 588 | $Games::GUI::Battleship::Ship::FLEET_ORDER[ $self->{placement_idx} ]; | ||
| 589 | my $len = $Games::GUI::Battleship::Ship::SHIP_SIZES{$next_ship}; | ||
| 590 | $self->{status_msg} = "Deploy your $next_ship (length $len). Left-click to place, Right-click or 'R' to rotate."; | ||
| 591 | $self->{msg_type} = 'info'; | ||
| 592 | $self->{ghost_ship} = undef; | ||
| 593 | } | ||
| 594 | else { | ||
| 595 | # Fleet complete! Initialize AI fleet and begin battle | ||
| 596 | $self->begin_battle; | ||
| 597 | } | ||
| 598 | |||
| 599 | $self->{window}->repaint if $self->{window}; | ||
| 600 | return; | ||
| 601 | } | ||
| 602 | |||
| 603 | sub randomize_player_fleet ( $self, $force = 0 ) { | ||
| 604 | if ( !$force && $self->{phase} ne 'placement' ) { | ||
| 605 | my $res = Prima::MsgBox::message_box( | ||
| 606 | 'Restart Game?', | ||
| 607 | 'Are you sure? This will restart the game.', | ||
| 608 | mb::YesNo | mb::Warning | ||
| 609 | ); | ||
| 610 | return unless $res == mb::Yes; | ||
| 611 | } | ||
| 612 | |||
| 613 | $self->{ai_timer}->stop if $self->{ai_timer}; | ||
| 614 | $self->{player_board}->clear_ships->reset_shots; | ||
| 615 | $self->{opponent_board}->clear_ships->reset_shots; | ||
| 616 | $self->{ai} = Games::GUI::Battleship::AI->create( $self->{difficulty} ); | ||
| 617 | $self->{ghost_ship} = undef; | ||
| 618 | $self->{ghost_valid} = 0; | ||
| 619 | $self->{hover_opp_col} = undef; | ||
| 620 | $self->{hover_opp_row} = undef; | ||
| 621 | |||
| 622 | $self->{player_board}->place_random_fleet; | ||
| 623 | $self->{placement_idx} = scalar @Games::GUI::Battleship::Ship::FLEET_ORDER; | ||
| 624 | $self->begin_battle; | ||
| 625 | $self->{window}->repaint if $self->{window}; | ||
| 626 | return; | ||
| 627 | } | ||
| 628 | |||
| 629 | sub reset_placement ($self) { | ||
| 630 | $self->{ai_timer}->stop if $self->{ai_timer}; | ||
| 631 | $self->{player_board}->clear_ships->reset_shots; | ||
| 632 | $self->{opponent_board}->clear_ships->reset_shots; | ||
| 633 | $self->{phase} = 'placement'; | ||
| 634 | $self->{placement_idx} = 0; | ||
| 635 | $self->{placement_dir} = 'H'; | ||
| 636 | $self->{ghost_ship} = undef; | ||
| 637 | $self->{ghost_valid} = 0; | ||
| 638 | $self->{hover_opp_col} = undef; | ||
| 639 | $self->{hover_opp_row} = undef; | ||
| 640 | |||
| 641 | my $first_ship = $Games::GUI::Battleship::Ship::FLEET_ORDER[0]; | ||
| 642 | my $len = $Games::GUI::Battleship::Ship::SHIP_SIZES{$first_ship}; | ||
| 643 | $self->{status_msg} = "Fleet reset. Deploy your $first_ship (length $len). Left-click to place."; | ||
| 644 | $self->{msg_type} = 'info'; | ||
| 645 | $self->{window}->repaint if $self->{window}; | ||
| 646 | return; | ||
| 647 | } | ||
| 648 | |||
| 649 | sub begin_battle ($self) { | ||
| 650 | $self->{ai_timer}->stop if $self->{ai_timer}; | ||
| 651 | $self->{opponent_board}->place_random_fleet; | ||
| 652 | $self->{phase} = 'battle'; | ||
| 653 | $self->{ghost_ship} = undef; | ||
| 654 | $self->{status_msg} = "Fleet deployed. Click a coordinate on the Enemy Radar to fire."; | ||
| 655 | $self->{msg_type} = 'info'; | ||
| 656 | return; | ||
| 657 | } | ||
| 658 | |||
| 659 | sub get_sound ( $name ) { | ||
| 660 | my $base_dir = dirname(__FILE__); | ||
| 661 | return File::Spec->catfile($base_dir, 'Battleship', 'assets', $name); | ||
| 662 | } | ||
| 663 | |||
| 664 | sub handle_battle_click ( $self, $col, $row ) { | ||
| 665 | return unless defined $col && defined $row; | ||
| 666 | return unless $self->{phase} eq 'battle'; | ||
| 667 | return | ||
| 668 | if $self->{ai_timer} | ||
| 669 | && $self->{ai_timer}->get_active; # Wait while AI turn is pending | ||
| 670 | return if $self->{opponent_board}->has_shot( $col, $row ); | ||
| 671 | |||
| 672 | # Process Player Shot | ||
| 673 | my $res = $self->{opponent_board}->receive_shot( $col, $row ); | ||
| 674 | return unless $res; | ||
| 675 | |||
| 676 | my $coord_str = sprintf( "%s%d", ( 'A' .. 'J' )[ $col - 1 ], $row ); | ||
| 677 | my $ship_name = $res->{ship} ? $res->{ship}->type : 'Ship'; | ||
| 678 | |||
| 679 | if ( $res->{result} eq 'hit' ) { | ||
| 680 | $self->{status_msg} = "DIRECT HIT! You struck the Enemy $ship_name at $coord_str!"; | ||
| 681 | $self->{msg_type} = 'hit'; | ||
| 682 | Audio::Play::Native->play(get_sound('hit.wav')); | ||
| 683 | } | ||
| 684 | elsif ( $res->{result} eq 'sunk' ) { | ||
| 685 | $self->{status_msg} = "You SUNK the Enemy $ship_name at $coord_str!"; | ||
| 686 | $self->{msg_type} = 'sunk'; | ||
| 687 | Audio::Play::Native->play(get_sound('sink.wav')); | ||
| 688 | } | ||
| 689 | else { | ||
| 690 | $self->{status_msg} = "Shot at $coord_str missed."; | ||
| 691 | $self->{msg_type} = 'miss'; | ||
| 692 | } | ||
| 693 | |||
| 694 | $self->{window}->repaint if $self->{window}; | ||
| 695 | |||
| 696 | # Check Victory | ||
| 697 | if ( $self->{opponent_board}->all_sunk ) { | ||
| 698 | $self->{phase} = 'game_over'; | ||
| 699 | $self->{status_msg} = | ||
| 700 | 'VICTORY! You have completely wiped out the enemy fleet!'; | ||
| 701 | $self->{msg_type} = 'victory'; | ||
| 702 | $self->{window}->repaint if $self->{window}; | ||
| 703 | $self->show_game_over_dialog(1); | ||
| 704 | return; | ||
| 705 | } | ||
| 706 | |||
| 707 | # Queue AI retaliation with a pause so player can comfortably read shot feedback. | ||
| 708 | my $delay = | ||
| 709 | ( $res->{result} eq 'hit' || $res->{result} eq 'sunk' ) ? 1800 : 1400; | ||
| 710 | $self->{ai_timer}->timeout($delay); | ||
| 711 | $self->{ai_timer}->start; | ||
| 712 | return; | ||
| 713 | } | ||
| 714 | |||
| 715 | sub process_ai_turn ($self) { | ||
| 716 | return if $self->{phase} ne 'battle'; | ||
| 717 | |||
| 718 | my ( $ai_x, $ai_y, $ai_mode ) = $self->{ai}->select_target; | ||
| 719 | return unless defined $ai_x && defined $ai_y; | ||
| 720 | |||
| 721 | my $res = $self->{player_board}->receive_shot( $ai_x, $ai_y ); | ||
| 722 | return unless $res; | ||
| 723 | |||
| 724 | $self->{ai}->record_result( | ||
| 725 | x => $ai_x, | ||
| 726 | y => $ai_y, | ||
| 727 | result => $res->{result}, | ||
| 728 | ship => $res->{ship}, | ||
| 729 | ); | ||
| 730 | |||
| 731 | my $coord_str = sprintf( "%s%d", ( 'A' .. 'J' )[ $ai_x - 1 ], $ai_y ); | ||
| 732 | my $ship_name = $res->{ship} ? $res->{ship}->type : 'Ship'; | ||
| 733 | |||
| 734 | if ( $res->{result} eq 'hit' ) { | ||
| 735 | $self->{status_msg} = "WARNING! Enemy hit your $ship_name at $coord_str! ($ai_mode)"; | ||
| 736 | $self->{msg_type} = 'hit'; | ||
| 737 | Audio::Play::Native->play(get_sound('hit.wav')); | ||
| 738 | } | ||
| 739 | elsif ( $res->{result} eq 'sunk' ) { | ||
| 740 | $self->{status_msg} = "CRITICAL HIT! Enemy SUNK your $ship_name at $coord_str! ($ai_mode)"; | ||
| 741 | $self->{msg_type} = 'sunk'; | ||
| 742 | Audio::Play::Native->play(get_sound('sink.wav')); | ||
| 743 | } | ||
| 744 | else { | ||
| 745 | $self->{status_msg} = | ||
| 746 | "Enemy fired at $coord_str: MISS! ($ai_mode). Your turn to attack."; | ||
| 747 | $self->{msg_type} = 'info'; | ||
| 748 | } | ||
| 749 | |||
| 750 | $self->{window}->repaint if $self->{window}; | ||
| 751 | |||
| 752 | # Check Defeat | ||
| 753 | if ( $self->{player_board}->all_sunk ) { | ||
| 754 | $self->{phase} = 'game_over'; | ||
| 755 | $self->{status_msg} = | ||
| 756 | 'DEFEAT! The enemy has sunk your entire fleet.'; | ||
| 757 | $self->{msg_type} = 'defeat'; | ||
| 758 | $self->{window}->repaint if $self->{window}; | ||
| 759 | $self->show_game_over_dialog(0); | ||
| 760 | } | ||
| 761 | return; | ||
| 762 | } | ||
| 763 | |||
| 764 | # ============================================================================== | ||
| 765 | # DIALOGS | ||
| 766 | # ============================================================================== | ||
| 767 | |||
| 768 | sub show_game_over_dialog ( $self, $player_won ) { | ||
| 769 | my $title = $player_won ? 'VICTORY!' : 'DEFEAT!'; | ||
| 770 | my $text = | ||
| 771 | $player_won | ||
| 772 | ? "Congratulations! You destroyed the enemy fleet.\n\nPlay again?" | ||
| 773 | : "Your fleet was lost.\nThe enemy prevailed.\n\nPlay again?"; | ||
| 774 | |||
| 775 | my $res = | ||
| 776 | Prima::MsgBox::message_box( $title, $text, mb::YesNo ); | ||
| 777 | |||
| 778 | if ( $res == mb::Yes ) { | ||
| 779 | $self->start_new_game; | ||
| 780 | } | ||
| 781 | |||
| 782 | return; | ||
| 783 | } | ||
| 784 | |||
| 785 | sub show_rules_dialog ($self) { | ||
| 786 | my $rules = <<~'RULES'; | ||
| 787 | BATTLESHIP RULES: | ||
| 788 | 1. Deployment Phase: | ||
| 789 | - Place each of your 5 vessels on your fleet grid. | ||
| 790 | - Left-Click to place at the highlighted coordinate. | ||
| 791 | - Right-Click, press Space, or "R" key to rotate/switch between horizontal and vertical. | ||
| 792 | - Or choose "Game -> Randomize Fleet" to auto-deploy. | ||
| 793 | |||
| 794 | 2. Combat Phase: | ||
| 795 | - Take turns with the AI firing at enemy coordinates. | ||
| 796 | - Left-Click or press Space/Enter on any unexplored cell of the Tactical Radar. | ||
| 797 | - Hits and misses are marked on the grid. | ||
| 798 | - Sinking all 5 enemy ships wins the battle. | ||
| 799 | |||
| 800 | 3. AI Opponents: | ||
| 801 | - Easy: Naive AI (Linear scanning + adjacent probing) | ||
| 802 | - Normal: Parity AI (Checkerboard parity hunt + line extension) | ||
| 803 | - Hard: Probability AI (Probability density heatmap + dynamic parity) | ||
| 804 | RULES | ||
| 805 | |||
| 806 | Prima::MsgBox::message_box( 'Rules and Instructions', | ||
| 807 | $rules, mb::Ok ); | ||
| 808 | |||
| 809 | return; | ||
| 810 | } | ||
| 811 | |||
| 812 | sub show_about_dialog ($self) { | ||
| 813 | my $about = <<~'ABOUT'; | ||
| 814 | Games::GUI::Battleship v1 | ||
| 815 | by Matt Johnson (MJOHNSON) | ||
| 816 | ABOUT | ||
| 817 | |||
| 818 | Prima::MsgBox::message_box( 'About Battleship', | ||
| 819 | $about, mb::Ok | mb::Information ); | ||
| 820 | |||
| 821 | return; | ||
| 822 | } | ||
| 823 | |||
| 824 | |||
| 825 | 1; | ||
| 826 | |||