Before we code the method that checks whether a king is in check, we first need to know the exact position of the king. Let's define a method to find out the current position of the king, as follows (see 4.05—model.py):
def get_alphanumeric_position_of_king(self, color):
for position in self.keys():
this_piece = self.get_piece_at(position)
if isinstance(this_piece, piece.King) and this_piece.color == color:
return position
The preceding code simply iterates through all the items in the dictionary. If a given position is an instance of the King class, it simply returns its position.