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. Players can save the current game state to a file and later load it to continue from the same point.

Features Implemented

  • GameStateSerializer: Core serialization service for saving/loading game states
  • Complete Game State Persistence: Saves boards, ship placements, shots, turn state, difficulty, and running time
  • File Validation: Validates save files and handles corrupted/missing files gracefully
  • Error Handling: Comprehensive error handling for file operations
  • Hidden State Preservation: Hidden ship placements remain hidden after loading
  • User File Selection: Players can choose file location and name

Technical Implementation

  • Serialization: Java Object Serialization for complete game state
  • File Operations: Robust file handling with validation
  • Integration: Seamlessly integrated with existing GameService
  • Testing: Comprehensive unit and integration tests

Acceptance Criteria Validation

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

Testing

  • Unit Tests: GameStateSerializerTest with 8 comprehensive test cases
  • Integration Tests: GameServiceSaveLoadTest with 8 integration test cases
  • Coverage: All save/load scenarios tested including edge cases

Files Modified

  • src/main/java/org/example/battleship/service/GameStateSerializer.java - Core serialization service
  • src/test/java/org/example/battleship/service/GameStateSerializerTest.java - Unit tests
  • src/test/java/org/example/battleship/service/GameServiceSaveLoadTest.java - Integration tests

Pipeline Status

Pipeline #7814: SUCCESS - All tests passed

  • Compile job: Success
  • Test job: Success

Closes #5 (closed)

Merge request reports

Loading