Workshop › Minesweeper › Step 6 of 26

Mines, but not under the first click

What we are doing

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
}
```

What it means

Your turn

  1. Under the TOP line, write const MINES = 10.
  2. In newGame, add mine: false to the cell object.
  3. Under neighbors, leave an empty line and write the comment and placeMines. Press Run.

Skills: Arrays and objects

All steps

  1. A grid of cell objects
  2. The canvas and the pen
  3. Draw the field
  4. Draw every frame
  5. Neighbours
  6. Mines, but not under the first click
  7. Shuffle the candidates
  8. Count the mines around
  9. Open a cell
  10. Open cells look different
  11. Coloured numbers
  12. Click to open
  13. Stepping on a mine
  14. Show the mines
  15. Flood fill
  16. Flags with the right button
  17. A flag protects its cell
  18. Draw the flags
  19. Mines left
  20. Winning
  21. The end and a new game
  22. What time is it?
  23. The clock
  24. Stop the clock
  25. The best time
  26. Build it yourself: wrong flags