. . . .
The first example illustrates how the king moves. It can move to any square marked with an "X"
In the second diagram, the king can move to any of the marked squares or capture the black rook because the rook is one square away and is undefended. But the king may not move to one of the squares controlled by the black rook even though the rook is undefended.
In the third diagram, the black rooks defend each other. The white king has only one legal move because the king cannot move to a square controlled by the enemy rooks.
Finally, it is important to add that if the king is under attack (in check), the next move MUST make sure that the attack has been stopped. There are three possibilities. The king must move out of check. Or... the attacking piece must be captured. Or... a piece must be moved between the attacker and the king in order to eliminate the direct attack. If the king is under attack AND there is no immediate way to end the attack, the game is over. CHECKMATE!
The king is permitted to take part in a very special move, the only chess move that actually involves two pieces at the same time! In the following diagram, the white king can castle on either side of the board. To castle, move the king two squares toward the rook, and then move the rook to the square immediately on the other side of the king.
For castling to be legal, make sure
(1) that your king and rook have never moved.
(2) that your king is not under attack. You may not castle out of check.
(3) that your king is not passing through or arriving upon a square controlled by the opponent.
(4) that all of the squares between the king and rook are vacant.
You may castle if your rook is under attack. You may even castle if your rook passes through a square controlled by your opponent.
Here's where the king and rook end up after castling on each side of the board.
So why castle? See below under the section "King strategy"