PlayNook

How a Computer Plays Battleship: by Counting

strategygame-design

A ten by ten grid rendered as a probability heat map, hot orange squares around known hits, cool blue elsewhere, with search pattern and hit statistics in the margins

Want to try it? Sea Battle is playable right here.

Play Sea Battle

There is one good idea in computer sea battle and it fits in a sentence: count how many ways a ship could still be lying across each square, and shoot the square with the highest count.

Everything a strong player does by feel — spacing out early shots, chasing a hit down its line, ruling out the squares beside a sunk ship — falls out of that one rule. No separate hunting mode, no targeting mode, no list of special cases.

Counting an empty sea

Before a single shot, every square on the board has a number: how many positions of the five classic ships would cover it.

A ten by ten grid where each square carries a number: 10 in the corners, rising steadily inwards to 34 in the four central squares, shaded from deep blue to brass
Ten ways to cover a corner. Thirty-four to cover the middle. Nobody wrote a rule about the centre — this is just arithmetic.

A corner can be reached by ten positions. A central square by 34. That is the whole reason the middle is the right opening shot, and it is worth noticing that nobody had to know anything about sea battle to work it out. It is a counting problem.

Counting after a hit

Now put one hit on the board and re-run exactly the same count. Positions that cover an unsunk hit are weighted far more heavily, because one of them is certainly right.

A ten by ten grid with six misses marked as dots and one hit marked as a cross, the four squares around the hit glowing brass against a cool blue board
The four squares around the cross light up on their own. There is no rule in the program that says chase a hit.

The four squares around the cross now dominate the board — not because anything told the program to chase, but because every ship position that explains that hit runs through one of them. Sink the ship and those positions vanish, the map flattens, and the search resumes. One rule, both behaviours.

Each miss does work too. No ship can lie across it, so every position through that square disappears and the counts around it drop. A board with twenty misses on it is a much better-understood board than a blank one, even though you have hit nothing.

The one thing counting misses

Density counts all ship lengths together, and the shortest ship gets lost in the total. That matters, because the shortest ship is what decides how coarsely you can search.

Two ten by ten grids: the left with fifty squares marked in a checkerboard for a shortest ship of two, the right with thirty-four squares marked on a diagonal lattice for a shortest ship of three
A two-square ship must cover one of the fifty marked squares wherever it lies. The other fifty can be skipped entirely while hunting.

A ship two squares long always covers one square of a checkerboard, whichever way round it lies. So while no hit is open, half the board can be ignored. Sink the two and the shortest remaining ship is a three, and now only every third square needs a shot — thirty-four of the hundred.

Adding that lattice to the counting is worth 0.7 shots a game, measured over 400 games on identical arrangements: 44.1 against 44.8. Small, free, and it is the difference between a program that has read about the game and one that has thought about it.

Under Russian rules it almost never applies, and correctly so — four of those ten ships are a single square, and nothing can be spaced around a ship of length one.

What it is actually worth

Three hundred games at each setting, counting shots to clear a whole fleet. A perfect game is seventeen shots under classic rules, twenty under Russian, thirty under German:

OpponentClassicRussianGerman
Shooting at random95.475.080.8
Easy — random, but follows a hit62.062.162.8
Medium — counts, plays loosely55.358.653.9
Hard — counts, plays the lattice44.255.348.3

Random fire is the baseline nobody should be below: 95 shots to clear seventeen squares means missing about five times for every hit. Simply following up a hit — the thing every human does without being told — saves thirty-three shots. Counting saves another eighteen.

A note on the numbers you will find elsewhere: a widely quoted figure for the probability density method is 42 shots, from Nick Berry's analysis, reported at second hand in Slate and elsewhere. We land at 44.2 and could not reach 42 with any weighting we tried — and the original write-up is not currently reachable to compare method against method. So: 44.2 is our measurement of our program, not a claim about anybody else's.

Where should you hide, then?

This is the question every player actually has, and it is the one with the least evidence behind it online. So we measured it: 300 games for each arrangement, all against the strongest opponent above.

ArrangementClassicRussian
Pushed to the edges52.169.9
Crammed into a corner50.965.2
Random44.655.9
Kept to the middle40.955.0
Spread evenly38.552.9

Higher is better hidden. The edges win by seven and a half shots under classic rules and by fourteen under Russian — a large margin for a decision that costs you nothing.

And the worst arrangement is the one that sounds most careful: ships spread evenly across the board. That is exactly the shape a counting opponent is searching for. Deliberate, even spacing is not camouflage; it is cooperation.

The reason is the first diagram, read backwards. If the centre is where the most ship positions live, the centre is where the searching happens. Do not be there.

Where that advice stops being true

Against this opponent, always hug the edge. Against a person, no.

That opponent counts positions and knows nothing whatever about human habits. A human who has read the same advice will open on the edges, and then the edge is the worst place in the world to be.

This is not hand-waving; it has been modelled. A study at Yale put a placement agent and a targeting agent against each other and looked for an equilibrium. It found a mixed one: the placer rejects edge and adjacent positions only about 2% of the time, while the targeter rejects edge and adjacent shots 57% of the time and partially rejects them the rest. Neither pure strategy wins. There is no best place to hide — only a distribution.

Which is the honest ending for an article like this. A computer can be given a rule that is provably good against an opponent who is not watching it. Against one who is, the only good rule is to not have one.

Try it

The counting described here is what the Hard setting does on the sea battle board, before every shot, with the lattice on top. It sees exactly what you see — the squares it has called and how many ships are still afloat — and never reads your fleet.

Three rule sets are on the same page: classic, Russian and German. And if you would rather play the way it was played for sixty years before anyone boxed it, there are printable grids here too.