Workshop › Chess › Step 31 of 48
Castling moves the king two squares towards a rook. It needs history the board cannot show (has the king or that
rook ever moved?), so castling keeps four flags: wK, wQ, bK, bQ (king side and queen side for each color).
A king on its first square gets a castling move when the flag is on, the squares between are empty and the rook is in
its corner.
```js
let castling // which castlings are still allowed
castling = { wK: true, wQ: true, bK: true, bQ: true }
// Castling: the king still on its first square, the squares between free and the rook in its corner.
if (kind === 'K' && c === 4 && r === (color === 'w' ? 7 : 0)) {
const rook = color + 'R'
if (castling[color + 'K'] && !board[r][5] && !board[r][6] && board[r][7] === rook) add(r, c, r, 6, { castle: 'K' })
if (castling[color + 'Q'] && !board[r][1] && !board[r][2] && !board[r][3] && board[r][0] === rook) add(r, c, r, 2, { castle: 'Q' })
}
```castling[color + 'K'] reads a flag by a name built from text, like castling['wK'].castle: 'K' or castle: 'Q' through add's extra fields.let turn ... write let castling ...; in reset, under turn = 'w' write the flags.pseudoMoves, under the pawn block (after its closing }), write the castling block.Skills: Game states
← Only legal moves The rook jumps over →