Katkat

Harbor Clash: A Two-Captain Terminal Naval Game, Setup Then Combat

CitadelSoftware Engineer · Technical Screen

Last updated

Write a runnable two-player hidden-fleet game for the terminal: captains place vessels via typed commands with full validation, then alternate firing until one fleet is sunk; tests modeling, parsing, turn flow and shipping working code fast.

Build Harbor Clash, a hot-seat game for two captains sharing one terminal. Each captain secretly positions a fleet of vessels on their own square grid; then they take turns firing at cells in the opponent's grid until one fleet is entirely sunk.

You must produce runnable code. Aim for a playable vertical slice early, then harden it.

Part 1 - Setup phase

  • Default grid: 8 x 8. Columns are letters A..H, rows are numbers 1..8 (cell C5 = column C, row 5). Grid size and fleet must be constructor parameters (e.g. HarborClash(size=10, fleet=(5, 4, 3, 3, 2))).
  • Default fleet: lengths 4, 3, 3, 2.
  • A captain places one vessel per command in the form <bow-cell> <length> <H|V>, e.g. B2 3 H occupies B2, C2, D2; B2 3 V occupies B2, B3, B4.
  • Reject (with a message, then re-prompt) any command that is malformed, uses a column/row outside the grid, uses a length not remaining in the fleet, leaves the grid, or overlaps another vessel. A rejected command must not change the grid.
  • After each placement show the captain their own grid. Before each captain's turn, clear the screen and wait for Enter so the other captain cannot peek.

Part 2 - Combat phase

  • Captains alternate. On a turn, show the captain's own grid (with incoming hits) and the opponent's grid without vessel positions (only hits and misses).
  • Input: a single cell, e.g. E6. Report Miss., Hit! or Hit - vessel sunk!.
  • Firing at an already-targeted cell or entering an invalid cell re-prompts without ending the turn.
  • When every vessel of a captain is sunk, announce the winner and return.

Example session fragment

Captain 1, place a vessel [2, 3, 3, 4]: Z9 2 V
Rejected: column must be A-H
Captain 1, place a vessel [2, 3, 3, 4]: G2 3 H
Rejected: vessel would leave the grid
...
Captain 2, fire at: E5
Hit!
Captain 1, fire at: E5
You already fired there. Choose another cell.

Design constraints

  • Inject input/output (e.g. read=input, write=print) so the game can be driven by a test script.
  • Keep validation in one place so a rejection never leaves partial state.

Hints

Hint 1

Model Game -> Captain -> Grid -> Vessel. A vessel that knows its cells and which of them are damaged makes 'sunk' trivial.

Hint 2

Compute the candidate cells first, validate all of them, and only then mutate the grid.

Hint 3

Wrap parsing in a function that raises ValueError with a user-facing message; the input loop catches it and re-prompts.

Hint 4

Make read/write injectable so you can test a full game with a list of scripted lines.

Follow-up questions

  • Add a computer opponent that hunts intelligently after a hit. How do you structure it so humans and bots share the same interface?
  • How would you let two captains play over a network instead of a shared terminal?
  • Add a rule that vessels may not touch, even diagonally. Where does that change go, and how do you test it?
  • How would you add save/load of a game in progress?