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 objectives for the iteration. We are now in a position to check all the available moves for a player at a given point in the game. We can also check whether a king is in check from the opponent.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.159.178