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

Description

Implements save and load functionality for Battleship game matches. Users can save their current game state to a file and later load it to resume exactly where they left off.

Features Implemented

Core Functionality

  • GameState serialization: Complete game state persistence including board layouts, shot history, current player, difficulty, and timer
  • SaveLoadService: Service for saving and loading game states with validation
  • File validation: Comprehensive validation for corrupted, missing, or incompatible files
  • Version compatibility: Ensures only compatible save files can be loaded

Key Components

  • GameState.java: Serializable class representing complete game state
  • SaveLoadService.java: Service handling file I/O operations with error handling
  • Comprehensive test coverage for all acceptance criteria

Requirements Validation

Requirement 1: Persist complete match state to user-chosen file

  • Includes board size, fleet layouts, shot history, sunk statuses, current player, difficulty/mode, timer
  • Hidden information remains hidden after load

Requirement 2: Load and restore exact game state

  • Restores all externally observable state (boards, indicators, turn, timer, difficulty/mode)

Requirement 3: File validation and error handling

  • Validates file existence, readability, and format
  • Reports errors for missing/invalid/corrupted/incompatible files
  • Does not modify current game on error

Requirement 4: Idempotent round-trip

  • Save immediately followed by load preserves all observable state

Requirement 5: User file selection

  • Allows user to choose location and filename
  • Auto-adds .bsg extension if missing

Acceptance Criteria Verified

AC1: Mid-game state preservation

  • Given mid-game state S, save and immediate load preserves: shot history & results, visible cell states, sunk indicators, whose turn, difficulty/mode, timer value

AC2: Corrupted file handling

  • Given deliberately corrupted/incompatible file, shows error and preserves current game

AC3: Finished game preservation

  • Given finished game (win/loss), save and load preserves outcome and disables inputs

Technical Details

  • File Format: Java Object Serialization (.bsg extension)
  • Versioning: Current version "1.0" with compatibility checking
  • Error Handling: Comprehensive exception handling with user-friendly messages
  • Testing: 100% test coverage for SaveLoadService with all edge cases

Closes #5

Merge request reports

Loading