Workshop › Chess › Step 37 of 48
The end of the game comes almost for free. After every move, ask whether the side to move has any legal move. If
not, it is checkmate when its king is attacked and stalemate (a draw) when it is not. state keeps the
result.
```js
let state // 'playing', 'checkmate' or 'stalemate'
state = 'playing'
function play(m) {
makeMove(m)
selected = null
targets = []
if (legalMoves().length === 0) state = inCheck(turn) ? 'checkmate' : 'stalemate'
}
```makeMove, turn is already the other side, so legalMoves() and inCheck(turn) are about the side that must
answer.legalMoves().length === 0: an empty list has length 0.let targets ... write let state ...; in reset, under targets = [] write state = 'playing'.play write the if line.Skills: Game states
← En passant Check! and the result →