Workshop › Minesweeper › Step 1 of 26
Clear the field without touching a mine, reading the numbers as clues. Neighbour counting, a safe first click, flood fill with a stack, flags and a timer.
Minesweeper: a 9 by 9 field hides 10 mines; open every safe cell, using the numbers as clues. A cell has to remember
several things (a mine? opened? flagged?), so each cell is an object, and the board is a 2D array of objects built
with Array.from.
```js
const SIZE = 9
let grid
function newGame() {
grid = Array.from({ length: SIZE }, (_, row) =>
Array.from({ length: SIZE }, (_, col) => ({ row, col })),
)
}
newGame()
```Array.from({ length: 9 }, fn) makes an array of 9 items, each made by fn, which gets the item's index as its
second argument. _ is a common name for an argument you do not use.Array.from makes 9 rows; the inner one makes 9 cells per row, each { row, col }.( ) so its { } is not read as a function body.newGame() builds a fresh board; we call it once at the start.Write the code under the three comment lines, then press Run.
Skills: Arrays and objects