Implement single-player mode against computer opponent
Description
This merge request implements the single-player mode for the Battleship game, allowing players to play against a computer opponent.
Features Implemented
Core Functionality
-
Computer Player AI:
ComputerPlayerclass with intelligent ship placement and shooting logic - Difficulty Levels: Easy (random shooting) and Normal (smart targeting around hits)
- Automatic Ship Placement: Computer places ships validly without overlaps or boundary violations
- Smart Targeting: In Normal mode, computer targets adjacent cells after successful hits
Game Flow
- Single-Player Mode: Complete game flow with human vs computer turns
- Game States: Setup, human turn, computer turn, win conditions
- Shot Validation: Prevents duplicate shots and validates coordinates
Architecture
- MVC Pattern: Clear separation between model, service, and view layers
- Extensible Design: Easy to add new difficulty levels or AI strategies
- Test Coverage: Comprehensive unit tests and acceptance criteria validation
Acceptance Criteria Validation
- Computer automatically places standard fleet (Carrier-5, Battleship-4, Cruiser-3, Submarine-3, Destroyer-2)
- All ships placed within board boundaries without overlaps
- Ship positions hidden from human player
- Computer tracks used coordinates and avoids duplicates
- Validates coordinate availability before shooting
- Handles edge cases when no coordinates remain
- Smart targeting algorithm prioritizes adjacent cells after hits
- Continues hunting pattern until ship is sunk
- Falls back to random shooting when no adjacent targets available
Technical Implementation
Key Classes
-
ComputerPlayer: AI logic for ship placement and shooting -
GameService: Manages single-player game flow and state transitions -
AcceptanceCriteriaTest: Comprehensive test suite validating all requirements
Testing
- Unit tests for individual components
- Integration tests for complete game flow
- Acceptance criteria tests covering all user story requirements
- Test coverage meets 70% minimum requirement
Files Modified/Created
New Files
-
src/main/java/org/example/service/ComputerPlayer.java- Computer AI implementation -
src/main/java/org/example/service/GameService.java- Game flow management -
src/test/java/org/example/AcceptanceCriteriaTest.java- Acceptance criteria validation -
src/test/java/org/example/service/ComputerPlayerTest.java- Unit tests for computer AI -
src/test/java/org/example/service/GameServiceTest.java- Unit tests for game service
Modified Files
-
src/main/java/org/example/model/Player.java- Extended for computer player functionality -
src/main/java/org/example/model/Board.java- Enhanced for shot validation and tracking
Dependencies
This implementation builds upon the foundation established in previous issues:
- Issue #1: Ship placement and board management
- Issue #2: Turn-based shooting mechanics
- Issue #3: Ship damage tracking and game completion
Closes #4