Workshop › Chess › Step 34 of 48
The king may not castle out of check, through an attacked square or into one. The usual try-and-check test already covers the landing square; we add the start square and the square the king passes over.
```js
return pseudoMoves(color).filter((m) => {
if (m.castle) {
const row = m.from[0]
// No castling out of check or across an attacked square.
if (attacked(row, 4, other(color)) || attacked(row, m.castle === 'K' ? 5 : 3, other(color))) return false
}
```return false drops the move from the list.In legalMoves, right under the filter((m) => { line, write the castling check.
Skills: Game states
← Losing the right to castle Remember the double step →