About Sonar Hunt
Treasure chests lie somewhere on the sea floor. Your sonar is cheap: it tells you how far away the nearest chest is, but not which way. Click or tap a square to drop a sonar device there (or move with the arrow keys and press Enter). Each drop costs one device. Drop one right on a chest and it is hauled up. Find them all before the devices run out.
Every reading draws a ring: the nearest chest is somewhere on it. Two rings cross in only a few places, and a third usually settles it. Turn on Show where it could be to light every square a chest could still be on, given all your readings. A square the readings force to hold a chest gets a bright box. Computer drop makes one move for you and says why; Let it play plays out the rest of the game. Keys: N new game, S show where it could be, C computer drop, A let it play.
THE STORY
The game is "Sonar Treasure Hunt" from Al Sweigart's book Invent Your Own Computer Games with Python, where it teaches lists and coordinates on a 60 by 15 sea of ~ characters, with 3 chests and 20 devices. This version keeps those rules as the medium game and adds the rings, the shading and the computer player.
Real sonar works by sending out a pulse of sound and timing the echo. It grew out of the hunt for submarines in the First World War: in France, Paul Langevin used quartz crystals to send and hear ultrasound under water. A single echo gives a distance, and finding a point from distances alone is called trilateration. GPS does the same thing with radio: your phone works out its distance to several satellites and finds the one place where those spheres meet.
HOW IT WORKS
Distance is measured in squares, in a straight line from the middle of one square to the middle of another (Pythagoras), then rounded to the nearest whole number, as in the original. A chest 3 across and 4 down reads 5. So a ring is a band one square wide: every square whose distance rounds to the reading. Beyond the sonar's range (9 squares as standard) a device reads nothing, and you learn that no chest is that close. When a chest is hauled up, every device on the map reads again, now for the next nearest chest, so a reading can change (the log shows what it was).
Show where it could be uses only what you can see. Each reading says that no chest is nearer than it, and that at least one chest is exactly on its ring. With three chests left, one chest can explain several rings at once where they cross, but no more than three chests are allowed in all. A square is lit when there is some way to place the chests, including one there, that gives every reading you have seen. That is worked out exactly, not guessed. Squares away from every ring stay lit while there are more chests than it takes to explain the rings.
The computer first takes any square that must hold a chest. When the rings have narrowed one chest down to a few dozen squares or fewer, it digs there, choosing the square that sits on the most rings. Otherwise it scouts: it looks at every square it could drop on and sorts the lit squares by what a ping there would read for each of them. The best scouting drop splits them into many small groups, so that whatever it reads, few squares are left. With the standard settings it found all three chests in each of 500 test games, using about 10 of its 20 devices.
FURTHER WATCHING AND READING