Workshop › Minesweeper › Step 6 of 26
Nobody likes losing on the very first click, so the real game places the mines after it, never on the clicked cell
or next to it. placeMines(safe) leaves those cells out and puts mines on the others.
```js
const MINES = 10
Array.from({ length: SIZE }, (_, col) => ({ row, col, mine: false })),
// Mines are placed on the first click, never on or next to the clicked cell, so the first click always opens space.
function placeMines(safe) {
const forbidden = new Set([safe, ...neighbors(safe)])
const candidates = grid.flat().filter((cell) => !forbidden.has(cell))
for (const cell of candidates.slice(0, MINES)) cell.mine = true
}
```mine: false.forbidden is a Set of the safe cell and its neighbours; ... spreads the neighbours list into the array.filter keeps only the cells that are not forbidden.slice(0, MINES) takes the first 10 of them, and each gets a mine.TOP line, write const MINES = 10.newGame, add mine: false to the cell object.neighbors, leave an empty line and write the comment and placeMines. Press Run.Skills: Arrays and objects
← Neighbours Shuffle the candidates →