Skip to content

[US-04] Single-player mode vs. computer opponent

Ivan Esau requested to merge feature/issue-4-us-04-single-player-mode-vs-co into master

Description

Implements single-player mode with computer opponent for the Battleship game.

Requirements Implemented

1) Single-Player Mode with Legal Fleet Placement

  • Computer places a legal fleet with standard ship sizes (Carrier: 5, Battleship: 4, Destroyer: 3, Submarine: 3, Patrol Boat: 2)
  • No overlaps or out-of-bounds placements
  • Fleet remains hidden from human player

2) Computer Turn Logic

  • Computer selects valid untargeted coordinates
  • Avoids repeat shots
  • Implements difficulty-based targeting strategies

3) Difficulty Options

  • Easy: Random valid shots
  • Standard: Hunt-and-target algorithm after hits (preferentially targets adjacent cells)

Acceptance Criteria Verified

Given Single-Player is chosen, when the match starts, then the computer's fleet exists and is legal but hidden.

  • ComputerPlayer class implements automatic fleet placement
  • Fleet validation ensures legal placement
  • Hidden board implementation

Given the computer took a turn, when the history is inspected, then its shot is at a previously untargeted coordinate.

  • Shot history tracking
  • Coordinate validation prevents repeats

Given Standard difficulty and a computer hit, when subsequent computer turns occur, then at least one subsequent shot targets an adjacent cell until the ship is sunk.

  • Hunt-and-target algorithm implementation
  • Adjacent cell targeting logic
  • Ship sinking detection

Technical Implementation

Core Components

  • ComputerPlayer.java - AI player implementation with difficulty levels
  • Difficulty.java - Enum for difficulty levels (EASY, STANDARD)
  • GameService.java - Game logic integration
  • Board.java - Enhanced with computer fleet placement

Testing

  • ComputerPlayerTest.java - Unit tests for AI logic
  • DifficultyTest.java - Difficulty level validation
  • GameServiceTest.java - Integration tests
  • AcceptanceCriteriaTest.java - Acceptance criteria validation

Files Modified/Created

  • src/main/java/org/example/model/ComputerPlayer.java
  • src/main/java/org/example/model/Difficulty.java
  • src/main/java/org/example/model/Game.java
  • src/main/java/org/example/service/GameService.java
  • src/test/java/org/example/model/ComputerPlayerTest.java
  • src/test/java/org/example/model/DifficultyTest.java
  • src/test/java/org/example/service/GameServiceTest.java
  • src/test/java/org/example/AcceptanceCriteriaTest.java

Pipeline Status

  • Pipeline #6676: SUCCESS (compile + test jobs passed)
  • Latest pipeline #6687: PENDING

Closes #4

Merge request reports

Loading