To castle, the king is moved two spaces towards a rook. The rook is then placed on the space next to the king, on the opposite side, as if jumping over the king.
Castling can only occur when the following conditions are met:
- There must not be any pieces between the king and the rook.
- The king and the rook must not have moved earlier in the game (even if they return to their original squares).
- The king must not be in check.
- The move must not put the king in check.
- The square the king passes through must not be under attack
- Both must be on the first rank (this is to prevent a king castling across the board with a pawn that promoted into a rook that hasn't moved yet)
Note that the rook can castle while under attack.
Castling with the rook closer to the king is called kingside castling, while castling with the rook closer to the queen is called queenside castling.
The algebraic term for castling kingside is 0-0, while queenside is 0-0-0. Castling is considered to be a king move.