Skip to content

[US-05] Save, load, and resume a match

Ivan Esau requested to merge feature/issue-5-us-05-save-load-and-resume-a-m into master

Implementation Summary

This merge request implements the complete save/load functionality for the Battleship game as specified in issue #5.

Requirements Implemented

1) Persist complete match state to file

  • SaveLoadService handles serialization of all game state
  • Saves board size, fleet layouts, shot history, sunk statuses, current turn, and game state
  • Hidden information remains concealed from opponent after load

2) Load and restore exact game state

  • GameSaveService restores complete game state from files
  • Preserves shot history, visible cell states, sunk indicators, current turn
  • Maintains game difficulty/mode and timer value

3) Validate file and version

  • isValidSaveFile() method validates file integrity
  • Handles missing/invalid/corrupted/incompatible files
  • Reports errors without modifying current game state

4) Ensure idempotent round-trip

  • Save immediately followed by load preserves all observable state
  • Comprehensive tests validate round-trip consistency
  • No state changes during save/load cycle

5) User file selection

  • Service methods accept file path parameters
  • Controller layer handles user file selection
  • Flexible file location and naming

Acceptance Criteria Validated

AC1: Given a mid-game state S, when the user saves and immediately loads that file, then the restored game equals S in all observable aspects

  • Tested in testAcceptanceCriterion1_SaveLoadPreservesCompleteState()
  • Validates shot history, visible states, sunk indicators, current turn

AC2: Given a deliberately corrupted or incompatible file, when the user attempts to load it, then an error is shown and the current game remains unchanged

  • Tested in testAcceptanceCriterion2_CorruptedFileHandling()
  • Validates error handling and state preservation

AC3: Given a finished game (win or loss), when it is saved and later loaded, then the game remains finished with the same outcome

  • Tested in testAcceptanceCriterion3_FinishedGamePreservation()
  • Validates finished state preservation

Technical Implementation

Core Classes:

  • SaveLoadService: Main service for save/load operations
  • GameSaveService: Enhanced service with validation and testing
  • GameSaveData: Serializable data structure for game state
  • PlayerSaveData: Serializable data structure for player state
  • ShipSaveData: Serializable data structure for ship state

Key Features:

  • Java serialization for efficient state persistence
  • Comprehensive error handling for file operations
  • Complete state preservation including shot history
  • Validation of save file integrity
  • Round-trip consistency guarantees

Test Coverage:

  • Unit tests for individual service methods
  • Integration tests for complete save/load workflow
  • Acceptance criteria validation tests
  • Edge case handling (corrupted files, finished games)

Closes #5

Merge request reports

Loading