[US-05] Saving and restoring the game
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
testSaveLoadCycle_preservesGameState
testLoadGame_corruptedFile_throwsException
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
- Compile job: Success
- Test job: Success
Closes #5 (closed)