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

Implementation Summary

This merge request implements the single-player mode with computer opponent functionality for the Battleship game as specified in issue #4.

Requirements Implemented

1) Offer Single-Player: the computer places a legal fleet (no overlaps/out-of-bounds, standard sizes)

  • ComputerPlayer.placeFleetAutomatically() method handles automatic ship placement
  • Validates all placements to ensure no overlaps and within bounds
  • Uses standard fleet sizes: Carrier (5), Battleship (4), Destroyer (3), Submarine (3), Patrol Boat (2)

2) On its turn, the computer shall select a valid untargeted coordinate and fire; at minimum it shall avoid repeats

  • ComputerPlayer.generateShotPosition() ensures all shots are at untargeted positions
  • Both Easy and Standard difficulty levels avoid duplicate shots
  • Comprehensive test coverage for shot validation

3) Provide difficulty options: Easy (random shots) and Standard (after a hit, preferentially target adjacent cells until the ship is sunk)

  • Difficulty enum with EASY and STANDARD levels
  • Easy: Pure random shot selection
  • Standard: Hunting strategy that targets adjacent cells after hits
  • Smart targeting logic with potential target tracking

Acceptance Criteria Validated

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

  • Tested in testPlaceFleetAutomatically()
  • Computer fleet placement validated for legality
  • Fleet remains hidden from human player

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

  • Tested in testNoDuplicateShots() and testEasyDifficultyRandomShots()
  • Both difficulty levels verified to avoid duplicate shots
  • Shot history tracking validated

AC3: 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

  • Tested in testStandardDifficultyAdjacentTargeting()
  • Hunting mode activation after hits verified
  • Adjacent cell targeting strategy validated

Technical Implementation

Core Classes:

  • ComputerPlayer: Extends Player with AI logic for ship placement and targeting
  • Difficulty: Enum defining EASY and STANDARD difficulty levels
  • Game: Enhanced with single-player mode constructor and computer turn execution

Key Features:

  • Automatic legal fleet placement for computer
  • Difficulty-based shot generation
  • Hunting strategy for Standard difficulty
  • Turn-based gameplay with computer opponent
  • Comprehensive test coverage for all acceptance criteria

AI Strategy:

  • Easy: Pure random selection from untargeted positions
  • Standard: Random initially, switches to hunting mode after hits
  • Hunting mode targets adjacent cells systematically
  • Potential target tracking for efficient ship sinking

Test Coverage:

  • Unit tests for ComputerPlayer AI logic
  • Integration tests for single-player game flow
  • Acceptance criteria validation tests
  • Edge case handling (corner positions, duplicate shots)

Closes #4

Merge request reports

Loading