Sonar
A grid search game — find hidden targets using distance clues. Two-dimensional arrays, coordinates, and systematic thinking.
Sonar is Battleship with a twist. Three targets hide on an 8x8 grid. You call coordinates. A hit marks the cell with a star. A miss shows a number — the distance to the nearest target. The closer the number, the warmer you are.
This is the fourth game in Volume 2 — Patterns of State — and the first grid game. So far your state has been a single value or a one-dimensional list; here it lives in a two-dimensional array, rows and columns like a chessboard, and the player thinks in coordinates.
DIM you know (Quiz Master); ABS you know (Lucky Number). Sonar's new ideas are the second dimension — DIM g(8,8), addressed by g(row, col) — and the distance clue: a nested sweep of the whole grid that finds the nearest target with Manhattan distance. The 2D array is the state, and scanning it is the move.
What you will build:
- An 8×8 grid held in a two-dimensional array, with three hidden targets (the new idea)
- Probing by coordinate, and hit detection
- The distance clue — a nested sweep finding the nearest target with
ABS(the new idea) - Warmer and colder — the distance coloured red when close, blue when far
- Winning on three hits, a guess count, and already-probed checks
- The design concept: search — narrowing a location from its clues
6 units. About 5–7 hours. This builds on Meet BASIC and Volume 1 — earlier games assumed.
Unit roadmap
The grid
A two-dimensional array drawn as a grid, with hidden targets and probing
The clues
Manhattan distance to the nearest target, colour-coded