Skip to content

[US-03] Track ship damage, announce sunk, and end the game

Ivan Esau requested to merge feature/issue-3-us-03-track-ship-damage-announ into master

Implementation Summary

This merge request implements the complete ship damage tracking and game completion functionality for US-03.

Requirements Implemented

1) Track hits per ship and determine when all squares of a ship are hit

  • Ship class tracks hits per position with boolean array
  • isSunk() method checks if all positions are hit
  • getHitCount() method provides current damage status

2) Upon completion, announce the ship as 'sunk'

  • Ship sinking detection in Board.allShipsSunk()
  • Game state management in Game.executeShot()
  • Integration with game flow for announcements

3) When all ships of a player are sunk, declare victory for the opponent and disable further input

  • Game.getWinner() method identifies winner
  • Game.executeShot() throws IllegalStateException when game finished
  • Player.hasLost() method checks if player has lost

4) Provide New Game to start a fresh match

  • GameService.createNewGame() resets all state
  • Complete board and ship reset functionality

Acceptance Criteria Validated

AC1: Given the last undamaged square of a ship is hit, when the result is processed, then that ship is announced 'sunk'

  • Test: acceptanceCriterion1_lastUndamagedSquareHit_shipAnnouncedSunk()
  • Validates ship sinking detection and announcement

AC2: Given the final enemy ship is sunk, when the board updates, then the match ends, the winner is indicated, and no further shots are allowed

  • Test: acceptanceCriterion2_finalEnemyShipSunk_matchEndsWithWinnerAndNoFurtherShots()
  • Validates game completion and input blocking

AC3: Given a finished match, when New Game is chosen, then both boards reset to covered, unplaced state

  • Test: acceptanceCriterion3_finishedMatch_newGameResetsBoards()
  • Validates complete game state reset

Technical Implementation

Core Classes:

  • Ship: Damage tracking with hits[] array and isSunk() method
  • Board: Ship management with allShipsSunk() detection
  • Game: Game state management with FINISHED state and winner determination
  • Player: Loss detection with hasLost() method

Test Coverage:

  • Comprehensive acceptance criteria tests in AcceptanceCriteriaTest_Issue3.java
  • Additional validation tests for edge cases
  • Integration tests for complete game flow

Files Modified/Created

Model Layer:

  • Ship.java - Enhanced with damage tracking
  • Board.java - Added ship sinking detection
  • Game.java - Added game completion logic
  • Player.java - Added loss detection

Service Layer:

  • GameService.java - Game flow management

Test Layer:

  • AcceptanceCriteriaTest_Issue3.java - Complete acceptance criteria validation

Closes #3

Merge request reports

Loading