Workshop › Memory › Step 20 of 25
The game is won when every card is matched. A small function won() works the answer out from the cards each time,
and draw shows the result over a dimmed board.
```js
function won() {
return cards.every((card) => card.matched)
}
if (won()) {
ctx.fillStyle = 'rgba(0, 0, 0, 0.6)'
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = 'white'
ctx.font = 'bold 30px sans-serif'
ctx.fillText('You found them all!', canvas.width / 2, 190)
ctx.font = '20px sans-serif'
ctx.fillText('in ' + moves + ' moves', canvas.width / 2, 230)
ctx.font = '16px sans-serif'
ctx.fillText('Click to play again', canvas.width / 2, 270)
}
```cards.every(test) is true only if the test is true for every card.rgba(0, 0, 0, 0.6) is black at 60% strength: a curtain over the board.canvas.width / 2, because textAlign is still 'center'.function flip, write won and an empty line.draw, after the card loop's closing }, leave an empty line and write the if (won()) block.Skills: Game states, Drawing on a canvas
← Show moves and pairs Play again →