Skip to content

Hunt & target · probability density

Battleship

Place your fleet, then take turns firing at the computer’s waters. On Hard it fires wherever the most possible ship positions overlap, and you can watch that probability density as it hunts.

Battleship game

Setup

Your waters

Arrow keys move, Enter places the selected ship, R rotates it.

Difficulty

Fires where the most possible ship positions overlap: a probability density.

Your fleet

Horizontal

Ships run right or down from the cell you choose. They may touch, but not overlap.

Place all five ships to start.

Game over

You

Shots
Hits
Accuracy

How to play

Setting up

Pick a ship in the list, then pick the cell where it starts: the ship runs right or down from there, depending on its orientation. Rotate it with the button or the R key. Ships may touch, but not overlap.

To move a ship, select it on the board or in the list and pick a new cell. Random places the whole fleet at once.

The battle

You and the computer take turns, one shot each, and you fire first. A dot marks a miss, a cross a hit. When every cell of a ship has been hit, it sinks: the game names the ship and shows its outline. Sink all five ships to win.

Keyboard

  • Tab moves between the boards and the controls; each board is a single stop.
  • Arrow keys move one cell. Home and End jump to the ends of a row, Page Up and Page Down to the ends of a column.
  • Enter or Space places the selected ship during setup and fires during the battle.
  • R rotates the ship during setup.

Screen readers hear each cell’s coordinate and state (“C7, hit”), and every shot from the battle log.

How the computer aims

All three levels see only what you would: where they have fired, what they hit, and which ships have sunk.

  • Easy fires at a random cell it has not tried yet.
  • Normal hunts and targets. While hunting, it fires only where the row and column numbers add up to a multiple of the shortest ship still afloat, so that ship cannot hide between its shots. After a hit it tries the four neighbours, and once two hits line up it follows the line.
  • Hard estimates where the ships are. Before every shot it goes through each position every remaining ship could still take (straight, on the board, clear of misses and sunk ships) and counts, for each cell, how many of those positions cover it:

d(c) = ∑s ∑p ∈ Ps, c ∈ p wh(p)

Here Ps is the set of positions left for ship s, h(p) is the number of hits on unsunk ships that a position covers, and w is a weight larger than any count without hits (35 for the full fleet), so after a hit the count concentrates on that ship. The computer fires at the highest value and breaks ties at random. Turn on the heat map during a battle to watch d over your own board.

Counting each ship on its own ignores that ships cannot overlap one another; counting whole fleets exactly would mean going through an enormous number of arrangements, so the per-ship count is the usual approximation. In 300 simulated games against random fleets, Easy needed about 95 shots on average to sink the fleet, Normal about 51 and Hard about 44.

Behind the game

The story starts around 2005, when I got to know Battleship by playing it on paper and on screen. It was the game I played with my father as a child, which is why I wanted to design my own.

My first digital attempt goes back to 2018, my high-school years. I was still new to programming when I wrote a plain desktop game in C# and presented it as one of my graduation projects (source on GitHub). The Hunt & Target algorithm behind Normal came later.

In 2026 I rewrote the game from scratch, and that is the version on this page. Written in TypeScript, it runs in the browser and has a modern interface. The computer opponent decides where to fire with an algorithm: on Normal it hunts on a chequerboard pattern and closes in on every hit; on Hard it fires where a probability density map is highest. A blog post explains, step by step, how it aims.