Skip to content

[US-02] Take turns firing with hit/miss feedback

Ivan Esau requested to merge feature/issue-2-us-02-take-turns-firing-with-h into master

Implementation Summary

This merge request implements the turn-based firing system for the Battleship game as specified in Issue #2.

Requirements Implemented

1) Enforce alternating turns

  • Game state management with GameState.PLAYER1_TURN and GameState.PLAYER2_TURN
  • Automatic turn switching on misses
  • Turn preservation on hits (player gets another turn)

2) Let the active player select a target coordinate on the opponent's grid

  • Game.executeShot() method handles shot execution
  • Coordinate validation and boundary checking
  • Integration with player tracking boards

3) Report 'hit' or 'miss' immediately and record the result on both players' tracking views

  • Player.ShotResult enum with HIT, MISS, SUNK, ALREADY_TARGETED, INVALID
  • Automatic tracking board updates for both players
  • Immediate feedback through return values

4) Prevent firing at the same coordinate twice

  • Board.isCellTargeted() method checks if cell was already hit
  • ShotResult.ALREADY_TARGETED returned for duplicate shots
  • Turn state preserved when duplicate shot attempted

Acceptance Criteria Validated

AC1: Given A1 has not been targeted, when the player fires at A1, then the result is shown (hit/miss) and A1 becomes marked as used

  • Test: testAcceptanceCriteria1_FirstShotAtUntargetedCoordinate_ShowsResultAndMarksUsed

AC2: Given A1 is already targeted, when the player attempts A1 again, then the system blocks the action and preserves turn order rules

  • Test: testAcceptanceCriteria2_AlreadyTargetedCoordinate_BlocksActionAndPreservesTurn

AC3: Given a turn completes, when control passes, then only the next player can act

  • Test: testAcceptanceCriteria3_TurnCompletes_OnlyNextPlayerCanAct

Technical Implementation

Core Classes:

  • Game: Manages game state and turn logic
  • Player: Handles player actions and tracking boards
  • Board: Manages grid state and shot processing
  • Cell: Represents individual grid cells

Key Features:

  • Turn-based gameplay with proper state transitions
  • Shot validation and result reporting
  • Tracking board synchronization
  • Comprehensive test coverage for all acceptance criteria

Testing

All acceptance criteria are covered by integration tests in AcceptanceCriteriaTest.java:

  • Turn switching logic
  • Shot result handling
  • Duplicate shot prevention
  • Game state preservation

Closes #2

Merge request reports

Loading