Workshop › Minesweeper › Step 8 of 26
Count the mines around
What we are doing
Each cell counts its mine neighbours once, right after the mines are placed, so the numbers never have to be worked out
again.
```js
Array.from({ length: SIZE }, (_, col) => ({ row, col, mine: false, count: 0 })),
for (const cell of grid.flat()) cell.count = neighbors(cell).filter((n) => n.mine).length
```
What it means
- Every cell starts with
count: 0.
- For each cell,
filter keeps the neighbours that are mines, and .length is how many there are.
Your turn
- In
newGame, add count: 0 to the cell object.
- At the end of
placeMines, write the counting line. Press Run.
Skills: Arrays and objects
← Shuffle the candidates Open a cell →
All steps
- A grid of cell objects
- The canvas and the pen
- Draw the field
- Draw every frame
- Neighbours
- Mines, but not under the first click
- Shuffle the candidates
- Count the mines around
- Open a cell
- Open cells look different
- Coloured numbers
- Click to open
- Stepping on a mine
- Show the mines
- Flood fill
- Flags with the right button
- A flag protects its cell
- Draw the flags
- Mines left
- Winning
- The end and a new game
- What time is it?
- The clock
- Stop the clock
- The best time
- Build it yourself: wrong flags