## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Checking whether the king is in check

Let's define a method to check whether the king is in check from the opponent, as follows:

`def is_king_under_check(self, color):  position_of_king = self.get_alphanumeric_position_of_king(color)  opponent = 'black' if color =='white' else 'white'  return position_of_king in self.get_all_available_moves(opponent) `

The following is a description of the preceding code:

• First, we obtained the current position of the king and the color of the opponent.
• We then found out all the possible moves for all the chess pieces of the opponent. If the position of the king coincides with any position from all the possible moves, the king is in check and we return True. Otherwise, we return False.

This accomplishes the ...

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required