Battleship Probability Algorithm: How the AI Chooses Every Shot
HuntBot, this site's AI opponent, does not guess. Before every shot it enumerates every legal placement of every ship still afloat, adds up how many of those placements cover each unknown cell, and fires at the cell with the highest total. This page shows the exact scoring, the numbers on an empty board, and why the same math makes you a better player.
The score on an empty board
The hunt phase answers one question for every unknown cell: how many legal placements of the surviving ships pass through this cell? Each placement is weighted by the ship's length, so a 5-cell carrier placement is worth 5 points and a 2-cell destroyer placement is worth 2. The highest total gets the next shot.
The placement counts themselves are easy to reproduce. A 5-cell ship has 60 horizontal and 60 vertical positions (120 in total), a 4-cell ship 140, a 3-cell ship 160 and a 2-cell ship 180. Summing those placements over every cell gives the heat map.
- Centre E5: 126 points — the first shot of every game
- Edge midpoint E1: 80 points
- Corner A1: 34 points — 3.7× colder than the centre
- Board average: 88.4 points
Why the middle is hot
A placement only counts if it fits entirely on the board and does not cross a known miss. The middle is inside far more placements: a 5-cell ship can pass through E5 in five horizontal and five vertical positions (10 placements), but it can touch A1 in only one horizontal and one vertical position.
Multiply that by all five ship lengths and the gap becomes 126 versus 34. That is why the opening shot is always E5: the four centre cells tie at 126, and the scan order visits E5 first.
Parity first, density second
While three or more ships are afloat, HuntBot only scores cells of one checkerboard colour — the even cells where (x + y) is even. That is sound: two adjacent cells always have opposite parity, so every ship of length 2 or more must touch both colours. Scanning one colour cannot miss a ship, and it removes half the board from the search.
The fleet occupies 17 cells and covers 7 to 10 of them on the even colour depending on alignment. Random layouts average 8.5. In the first phase, then, the bot is searching a 50-cell grid that holds about 8.5 hull cells — roughly one hull cell in six.
- Minimum even-colour coverage: 7 cells (carrier 2, battleship 2, both cruisers 1, destroyer 1)
- Maximum: 10 cells (every ship starts on an even cell)
- Random layouts: 8.5 cells on average
The map moves after every miss
Density is recomputed after every shot: any placement that crosses a known miss is deleted. Four misses in the middle (E5, F6, D4, G7) drop the hottest cell from 126 to about 102, while the corner stays at 34. The bot is not fooled by a single miss — its aim drifts inward only as the middle fills with water.
This is the mechanical reason a fleet on the outer ring survives longer: the map keeps pointing at cells the fleet is not using. The companion page measures that effect.
- Empty board: E5 = 126, A1 = 34
- After E5 miss: F6 = 126, A1 = 34
- After four middle misses: hottest cell ≈ 102, A1 = 34
After a hit: block, axis, sink
A hit switches the bot to target mode. It collects the connected block of hits, then enumerates every placement of every surviving ship that covers the whole block without crossing a miss or another ship's hits. Each unknown neighbour of those placements earns points, and the highest wins.
After a second aligned hit the direction is fixed: the bot extends to the far end of the line instead of hugging the hits. When a ship sinks, all of its cells become blocked, so no later placement may cross them. That is why the bot never wastes shots on a wreck, and why every sunk ship shrinks the search space.
The arithmetic behind 17, 44 and 96
The five ships occupy 17 cells, so no strategy can finish in fewer than 17 hits. Random fire is a different story: if you fire at random unknown cells, the game ends when the last of the 17 hull cells is hit. In a random ordering of the 100 cells, the expected position of that last hull cell is 17 × 101 ÷ 18 = 95.4 — the familiar “about 96”.
HuntBot's measured average is about 44 shots per solo game: 44.8 over 600 games, 43.9 over an earlier 200-game run. The gap between 44 and 96 is search efficiency. The gap between 44 and 17 is information you cannot avoid paying for: shots that miss while you locate the fleet.
- Theoretical floor: 17 hits
- Random fire: 95.4 expected shots
- HuntBot: 44.8 measured average
- Search overhead: 27.8 shots
Measured on this site's engine
One thousand solo games per strategy with the same fixed seeds (1–1,000) and a fresh random fleet every game. All three numbers come from this repository's rules engine; reproduce them with npm run figures.
| Strategy | Mean shots |
|---|---|
| Random unknown cell | 95.4 |
| Even-colour sweep only, feedback ignored | 95.5 |
| HuntBot: heat map + target mode | 44.8 |
Random fire lands on the 95.4-shots expectation. Sweeping only the even colour without finishing hits saved nothing (95.5): the saving comes from re-scoring the map after every miss and from target mode, not from the checkerboard alone.
Use the same math as a human
You do not need to enumerate placements in your head. Three habits capture most of the gain: start on one parity colour; when only a few ships remain, stop scanning and ask which cells can still hold the longest survivor; after a hit, count which ship lengths can cover the block before choosing a direction.
For placement, the same map works backwards. Cells the bot prices at 126 are the worst place to hide; corners at 34 are the best. The placement guide turns that into a measured recipe.
Next: placement that costs HuntBot about 8 extra shots, the hunting guide, or the minimum-shots breakdown.
What the algorithm does not do
It does not remember your previous games, does not learn your habits and does not see your ships. Its only input is what your shots reveal: hit, miss, sunk. The heat map is therefore a fixed prior plus your misses — which is exactly why a layout that avoids the high-priced cells is a real counter, and why a “clever” pattern the map already expects is not.
FAQ
How does the Battleship AI choose its shots?
It enumerates every legal placement of every ship still afloat, gives each cell a score equal to the summed length of the placements crossing it, and fires at the highest score. While three or more ships remain it only scores one checkerboard colour; with two or fewer it scores every unknown cell.
Why does the AI shoot the centre first?
The centre is inside more legal ship placements than any other cell: 126 points at E5 versus 34 at A1 on an empty board. The bot breaks ties toward the centre, so E5 is the opening shot.
Does the AI cheat or see my fleet?
No. It only receives hit, miss and sunk feedback for the shots it fires — the same information you get. Its advantage is arithmetic, not hidden data.