What are the circumstances for the move where you switch your king and your rook?
The King-Rook Switch is called chess castling.
Before you can do the switch some conditions must be met.
1. There must not be any piece between the King and the Rook. This is simple logic.
2. The king must not have been moved before.
For example: If you have moved the king a few moves earlier to get out of a check and have moved the king back to the original square later on you are not allowed to execute the castling procedure anymore during the whole game because the king has been moved before.
3. You are not allowed to castle to escape a check.
Example: Your king is in check. You are not allowed to castle to get away from the check.
4. The king cannot castle right into a check.
5. The king cannot castle OVER a square that is controlled by your opponents pieces.
See above: This is easy, just castle as White.
There are no pieces between king and rook, the white king has never moved before and the king is not in check now and must not cross a square that is protected by Black’s pieces.
See above: You can’t castle here because the square g1 is controlled by the black bishop at c5.