Workshop › Chess › Step 33 of 48
Once the king moves, both of its castlings are gone for good; once a rook leaves its corner (or is captured there),
that side is gone. makeMove turns the flags off, and since that is a change too, undo keeps a copy of the flags
to restore.
```js
const undo = { m, piece, captured: board[tr][tc], castling: { ...castling } }
// Moving the king or a rook, or capturing a rook on its first square, ends castling on that side for good.
if (piece[1] === 'K') castling[piece[0] + 'K'] = castling[piece[0] + 'Q'] = false
for (const [row, col, side] of [[7, 7, 'wK'], [7, 0, 'wQ'], [0, 7, 'bK'], [0, 0, 'bQ']]) {
if ((fr === row && fc === col) || (tr === row && tc === col)) castling[side] = false
}
castling = undo.castling
```{ ...castling } makes a new object with the same four flags: a copy. Saving castling itself would save the
same object that is about to change.a = b = false sets both flags.makeMove, add castling: { ...castling } to the undo object.makeMove, write the comment, the king line and the corner loop.undoMove, under its castling lines, write castling = undo.castling.Skills: Game states
← The rook jumps over No castling through check →