Workshop › Minesweeper › Step 20 of 26
You win when every cell without a mine is open; flags do not count. The check runs after every reveal. On a win, the remaining mines are flagged automatically to show the solved board.
```js
if (grid.flat().every((cell) => cell.mine || cell.revealed)) win()
}
function win() {
state = 'won'
for (const cell of grid.flat()) if (cell.mine) cell.flagged = true
}
if (cell.revealed || state === 'won' || state === 'lost') return
if (state === 'won' || state === 'lost') return
```every is true when the test holds for every cell: each is either a mine or open.win sets 'won' and flags every mine.toggleFlag and the click listener now also stop after a win.reveal, write the every line.function cellAt, write win.state === 'won' || to the checks in toggleFlag and in the click listener. Press Run.Skills: Game states
← Mines left The end and a new game →