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

Description

Implements game save and load functionality for the Battleship game. This feature allows players to save their current game state to a file and later restore it to continue playing from the exact same point.

Changes

Core Implementation

  • GameState.java: Serializable class that captures complete game state including boards, shots, turn information, difficulty, and timing
  • FileManager.java: Utility class for saving/loading game states with comprehensive error handling

Key Features

  • Complete State Preservation: Saves all game elements (boards, ship placements, shots, turn, difficulty, running time)
  • Hidden State Protection: Hidden ship placements remain hidden after loading
  • Error Handling: Proper handling of missing, corrupted, or incompatible files
  • File Selection: Players can choose file location and name
  • Consistency: Save/load operations don't affect current game state

Testing

  • FileManagerTest.java: Comprehensive test suite covering:
    • Successful save/load operations
    • Error conditions (missing files, corrupted data, empty files)
    • File path validation
    • Complete state preservation verification

Technical Details

  • Uses Java serialization for game state persistence
  • Implements Serializable interface for all game state components
  • Provides default save directory and file naming
  • Includes validation for file paths and permissions

Acceptance Criteria Verified

  • If the game is saved and loaded right away, every visible part and all internal values should be identical
  • If a corrupted file is loaded, an error is displayed and the game remains the same
  • If a completed game is saved and later loaded, it stays finished and waits for a new start

Closes #5 (closed)

Merge request reports

Loading