Skip to content

[US-05] Saving and restoring the game

Ivan Esau requested to merge feature/issue-5-us-05-saving-and-restoring-the into master

Implementation Summary

This merge request implements comprehensive game save/load functionality for the Battleship game, addressing all acceptance criteria from Issue #5.

Key Features Implemented:

  1. Game State Serialization (GameState.java)

    • Complete game state capture including players, boards, ships, shots, current turn, difficulty, and timing
    • Serializable implementation for file persistence
    • Running time calculation for game duration tracking
  2. Save/Load Service (SaveLoadService.java)

    • Robust file operations with comprehensive error handling
    • Validation for corrupted, missing, and incompatible files
    • Directory creation support for custom file paths
    • File integrity checks and user-friendly error reporting
  3. Game Service Integration (GameService.java)

    • Seamless integration with existing game logic
    • State preservation during save/load operations
    • Error recovery that preserves current game on failed loads
  4. User Interface (Main.java)

    • Main menu with save/load options
    • File path selection for user flexibility
    • Clear error messages and status feedback

Acceptance Criteria Coverage:

Criterion 1: "If the game is saved and loaded right away, every visible part and all internal values should be identical"

  • Complete state preservation including player turns, board states, ship damage, and game timing
  • Hidden information remains properly hidden after loading

Criterion 2: "If a corrupted file is loaded, an error is displayed and the game remains the same"

  • Comprehensive error handling for corrupted, missing, and incompatible files
  • Current game state preserved on failed load attempts
  • Clear error messages to the user

Criterion 3: "If a completed game is saved and later loaded, it stays finished and waits for a new start"

  • Finished game state properly serialized and restored
  • Game completion status preserved across save/load cycles

Technical Implementation:

  • Architecture: Follows layered MVC pattern as specified in ORCH_PLAN.json
  • Serialization: Java Object Serialization for efficient state persistence
  • Error Handling: Comprehensive exception handling with user-friendly messages
  • File Operations: Support for user-selectable file locations and names
  • State Management: Complete game state capture and restoration

Testing:

Comprehensive test suite created with:

  • 25 test cases for SaveLoadService covering file operations and error scenarios
  • 15 test cases for GameService integration testing
  • Full coverage of all acceptance criteria
  • Edge case testing for corrupted files and error conditions

Dependencies Verified:

  • All prerequisite issues (#1-4) completed as per ORCH_PLAN.json dependency graph
  • Implementation builds upon existing game infrastructure
  • No breaking changes to existing functionality

Closes #5

Edited by Ivan Esau

Merge request reports

Loading