KatInterview

Two-Player Grid Battle: Terminal Ship Placement and Combat

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designgame-logicinput-validation

Last updated

Build a complete terminal game where two players secretly place ships on grids, then take turns firing until one fleet is destroyed.

Build a complete two-player naval combat game that runs in the terminal. Two players share one machine: each secretly positions their fleet on a private grid, then they alternate firing shots at each other's grids until one player's entire fleet is destroyed.

Your code must be runnable and testable. Prioritize getting a working prototype early, then refine the validation and edge cases.

Phase 1: Fleet Positioning

  • The game uses an 8×8 grid by default. Columns are labeled A through H, rows are numbered 1 through 8. For example, D6 refers to column D, row 6.
  • The grid size and fleet composition must be configurable via constructor: GridBattle(size=10, fleet=(5, 4, 3, 3, 2)).
  • The default fleet contains ships of lengths: 4, 3, 3, 2.
  • Each player enters one ship per command: <start-cell> <length> <H|V>. For example:
    • C3 3 H places a 3-cell ship horizontally starting at C3, occupying C3, D3, E3.
    • C3 3 V places it vertically, occupying C3, C4, C5.
  • Reject invalid commands with a clear message and re-prompt without changing the grid state. Reasons for rejection:
    • Malformed input (wrong number of tokens, invalid direction).
    • Cell outside the grid bounds.
    • Length not in the remaining fleet.
    • Ship extends beyond grid boundaries.
    • Ship overlaps an already-placed ship.
  • After each successful placement, display the player's current grid showing all ships.
  • Before switching players, clear the screen and wait for confirmation (e.g., press Enter) to prevent the other player from seeing ship positions.

Phase 2: Combat

  • Players alternate turns. On each turn, display:
    • The current player's own grid, showing ship positions and any incoming hits.
    • The opponent's grid showing only previous shots (hits and misses), without revealing ship locations.
  • The player enters a single cell to target, such as F4.
  • Report the outcome: Miss., Hit!, or Hit - ship destroyed!.
  • If the player targets a cell they've already fired at, or enters an invalid cell, display an error and re-prompt without ending their turn.
  • The game ends when all ships belonging to one player are destroyed. Announce the winner.

Sample Interaction

Player 1, place a ship [2, 3, 3, 4]: A9 2 H
Rejected: row must be 1-8

Player 1, place a ship [2, 3, 3, 4]: H3 3 H
Rejected: ship would extend beyond the grid

Player 1, place a ship [2, 3, 3, 4]: A1 4 H
   A B C D E F G H
 1 V V V V . . . .
 2 . . . . . . . .
...

Player 2, fire at: D5
Hit!

Player 1, fire at: D5
You already targeted that cell. Try again.

Design Requirements

  • Inject I/O dependencies: Accept read and write parameters (e.g., read=input, write=print) so tests can drive the game with scripted inputs.
  • Centralize validation: Parse and validate commands fully before modifying any game state. A rejected command must leave the grid unchanged.
  • Model clarity: Separate concerns—ship state, grid operations, turn flow, and I/O—into distinct components or classes.

Write working, runnable code that handles the core mechanics and validation robustly.

Hints

Hint 1

Start with the data model: how will you represent ships, grids, and damage? Design so hit detection is fast.

Hint 2

Validate placement commands in full before applying them. Compute all cells the ship would occupy, check bounds and overlaps, then commit only if valid.

Hint 3

Use a reveal flag when rendering grids: show ship positions for the owner, but only shots for the opponent.