KatInterview

Terminal Naval Combat: Fleet Setup and Attack System

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designinput-validationstate-management

Last updated

Implement a console-based naval battle game where two players configure their fleets through text input, then exchange fire until one side is eliminated.

Design and implement a naval combat game that runs entirely in a terminal window. Two players will use the same console, alternating control to configure their fleets and then engage in combat.

Core Requirements

Build a game system where:

  1. Each player configures their fleet by entering ship specifications via text commands
  2. After both fleets are ready, players alternate attempting to strike enemy vessels
  3. The game concludes when all ships belonging to one player have been destroyed

Technical Expectations

  • Deliver executable code, not just class diagrams or pseudocode
  • Handle all text input gracefully—malformed entries should prompt retry, not crash the program
  • Maintain information hiding: no player should see the opponent's ship locations
  • The codebase should be substantial enough to demonstrate production-quality engineering

Design Decisions Required

You must define and clearly communicate:

  • Grid dimensions (make this configurable)
  • Fleet composition—how many ships and what sizes (make this configurable)
  • Input syntax for specifying ship position (e.g., row/column pairs, algebraic notation)
  • Placement rules: Can ships touch? Overlap? Must they fit entirely within bounds?

Implementation Phases

Phase One: Fleet Configuration

Create the necessary classes and game loop so both players can:

  • Enter ship specifications through terminal prompts
  • See their own grid during placement
  • Receive clear error messages for invalid placements (out of bounds, collision, invalid format)
  • Complete placement before the opponent begins their turn

Expected capabilities:

  • Parse text input into structured placement data
  • Validate every placement without leaving partial or corrupted state
  • Switch control between players during the setup phase
  • Display each player's grid showing their own fleet

Phase Two: Combat Operations

Extend the system to support the attack phase:

  • Players alternate selecting target coordinates
  • Each attack returns a result: miss, hit, or vessel destroyed
  • Attackers see a tracking view showing their previous strikes and outcomes
  • Game detects victory condition (all enemy ships eliminated)

Additional considerations:

  • Should hitting a vessel grant an immediate follow-up shot?
  • Should the system distinguish between damaging and destroying a vessel?
  • How should repeated attacks on the same position be handled?

Evaluation Criteria

Strong implementations demonstrate:

  • Separation of game logic from input/output operations
  • Robust input validation at every interaction point
  • Clean data structures that simplify collision detection and damage tracking
  • Incremental development: basic playable version first, then enhancements
  • Clear method naming and responsibilities, despite the code volume

Hints

Hint 1

Start with core data structures—Board and Ship classes—before building the game loop, and test placement logic independently of terminal I/O

Hint 2

Use a 2D structure to track each cell's state (empty, occupied by which ship, hit or not) rather than searching ship coordinates on every operation

Hint 3

Handle setup and combat as separate phases with distinct state, and use a tracking board for each player to remember what they've learned about the opponent