Workshop › Battleship › Step 16 of 35
A ship sinks when every one of its squares has been hit. Each ship counts its hits; fire says 'sunk' when a hit sinks
it, and the message tells you.
```js
let enemyFleet // ships: { cells: [[r, c], ...], hits }
return { cells, hits: 0 }
const sunk = (ship) => ship.hits === ship.cells.length
ship.hits += 1
return sunk(ship) ? 'sunk' : 'hit'
message = result === 'miss' ? 'Miss' : result === 'hit' ? 'Hit!' : 'You sank a ship!'
```hits: 0; each hit adds 1.sunk(ship) is true when the hits equal the ship's length.fire now returns 'miss', 'hit' or 'sunk', and the message has three cases., hits to the let enemyFleet comment and , hits: 0 to the ship placeFleet returns.shipAt, write sunk.fire, change the end as shown.playerShoots, change the message line.Skills: Game states
← See your own fleet Darker hits on a sunk ship →