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