Workshop › Battleship › Step 26 of 35
Once the computer hits a ship, the rest of it must be next to that hit. Placements through a hit (of a ship not sunk yet) count 50 times more, and sunk ships are taken off the map like misses.
```js
// Every placement that avoids misses and sunk ships adds 1 to each of its squares; placements through a hit that
// is not sunk yet are far more likely, so they count much more. The most counted square is the best shot.
const sunkCells = new Set(myFleet.filter(sunk).flatMap((s) => s.cells.map(([r, c]) => r * N + c)))
if (cells.some(([cr, cc]) => theirShots[cr][cc] === 'miss' || sunkCells.has(cr * N + cc))) continue
const hits = cells.filter(([cr, cc]) => theirShots[cr][cc] === 'hit').length
for (const [cr, cc] of cells) heat[cr][cc] += 1 + hits * 50
```sunkCells is a Set of the squares of sunk ships; r * N + c turns a square into one number (0–99) so the set can
look it up quickly.hits counts how many of the placement's squares are hits; each one adds 50 to the placement's weight.heatMap, and the sunkCells line under const heat = grid(0).|| sunkCells.has(cr * N + cc).heat[cr][cc] += 1 line with the hits line and the new sum.Skills: Arrays and objects
← Shoot the hottest square See the heat map →