Recognize exactly when the king and rook can castle.
To castle, move the king two squares toward the rook. The rook lands on the square the king crossed. Short castling uses the h-file rook, long castling the a-file rook.
The king and chosen rook must retain their castling rights, and the squares between them must be empty. The king may neither start in check, cross an attacked square nor land on one.
On the long side, the b-file square must be clear, but the king never crosses it. The rook being attacked is also separate from the king’s safety.
Worked example
For White, O-O sends the king to g1 and rook to f1. O-O-O sends the king to c1 and rook to d1.
Try it on the board
Choose a square by tapping it, or tab to the board and use the arrow keys and Enter.
Castle on the kingside.
Choose a piece and its destination.
Promote to
Read this solution
O-O. A legal castle moves the rook with the king.
Castle on the side that is safe.
Choose a piece and its destination.
Promote to
Read this solution
O-O-O. White can castle queenside. The kingside route crosses f1 under attack.