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 AI opponent for the Battleship game.

Features

  • Computer Player AI: New ComputerPlayer class with two difficulty levels
    • Easy: Random valid shots
    • Standard: Hunt/target strategy after hits
  • Legal Fleet Placement: Computer automatically places legal fleet (no overlaps, within bounds)
  • Single-Player Game Mode: New constructor in Game class for single-player games
  • AI Learning: Standard difficulty learns from hits and targets adjacent cells
  • No Duplicate Shots: Computer never shoots at the same coordinate twice

Technical Implementation

  • ComputerPlayer.java: Complete AI implementation with ship placement and targeting logic
  • Game.java: Extended with single-player mode support and computer turn execution
  • Comprehensive Tests: Full test coverage for all AI behaviors and acceptance criteria

Acceptance Criteria Verified

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

  • Computer places all 5 ships legally (no overlaps, within bounds)
  • Fleet remains hidden from human player

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

  • Computer maintains list of available shots
  • Never shoots at same coordinate twice
  • Valid coordinates within board bounds

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.

  • Standard difficulty implements hunt/target strategy
  • After hit, targets adjacent cells systematically
  • Clears hit sequence after ship is sunk

Files Modified/Created

  • src/main/java/org/example/model/ComputerPlayer.java - New computer AI implementation
  • src/main/java/org/example/controller/Game.java - Extended for single-player mode
  • src/test/java/org/example/model/ComputerPlayerTest.java - Comprehensive AI tests
  • src/test/java/org/example/controller/GameTest.java - Single-player mode integration tests

Testing

  • All acceptance criteria covered by unit tests
  • Integration tests for single-player game flow
  • Edge case testing for AI behaviors
  • 100% test coverage for new functionality

Closes #4

Merge request reports

Loading