def get_theoretical_ep_right(self, x): """Checks if a player could have an ep-move in theory from looking just at the piece positions. :param file: The file to check as a letter between `"a"` and `"h"`. :return: A boolean indicating whether the player could theoretically have that en-passant move. """ if x < 0 or x > 7: raise ValueError(x) ''' 3 states of en-passant p. pP .. .. .. .p .P .. .. ''' # Check there is a pawn on the right rank for e.p. y = 3 if self.fen._to_move == WHITE else 4 x88 = X88.from_x_and_y(x, y) piece = self._pieces[x88] if not piece: return False # If the square is not an opposite colored pawn then its not possible. ocolor = Piece.opposite_color(self.fen._to_move) if not Piece.is_klass_and_color(piece, PAWN, ocolor): return False # If the square below the pawn is not empty then it not possible. y = 2 if self.fen.turn == WHITE else 5 x88 = X88.from_x_and_y(x, y) if self[x88]: return False # If there is not pawn of opposite color on a neighboring file then its not possible. xs = [_x for _x in range(8) if _x >= 0 and _x < 8 and abs(x - _x) == 1] for _x in xs: x88 = X88.from_x_and_y(_x, y) piece = self._pieces[x88] if Piece.is_klass_and_color( piece, PAWN, Piece.opposite_color(self.fen._to_move)): return True # Else its just not possible. return False
def get_theoretical_ep_right(self, x): """Checks if a player could have an ep-move in theory from looking just at the piece positions. :param file: The file to check as a letter between `"a"` and `"h"`. :return: A boolean indicating whether the player could theoretically have that en-passant move. """ if x < 0 or x > 7: raise ValueError(x) ''' 3 states of en-passant p. pP .. .. .. .p .P .. .. ''' # Check there is a pawn on the right rank for e.p. y = 3 if self.fen._to_move == WHITE else 4 x88 = X88.from_x_and_y(x, y) piece = self._pieces[x88] if not piece: return False # If the square is not an opposite colored pawn then its not possible. ocolor = Piece.opposite_color(self.fen._to_move) if not Piece.is_klass_and_color(piece, PAWN, ocolor): return False # If the square below the pawn is not empty then it not possible. y = 2 if self.fen.turn == WHITE else 5 x88 = X88.from_x_and_y(x, y) if self[x88]: return False # If there is not pawn of opposite color on a neighboring file then its not possible. xs = [_x for _x in range(8) if _x>=0 and _x<8 and abs(x-_x) == 1] for _x in xs: x88 = X88.from_x_and_y(_x, y) piece = self._pieces[x88] if Piece.is_klass_and_color(piece, PAWN, Piece.opposite_color(self.fen._to_move)): return True # Else its just not possible. return False
def get_king(self, color): """Gets the square of the king. :param color: `"w"` for the white players king. `"b"` for the black players king. :return: The first square with a matching king or `None` if that player has no king. """ #if not color in ["w", "b"]: # raise KeyError("Invalid color: %s." % repr(color)) for square, piece in [(s, self._pieces[s._x88]) for s in Square.get_all() if self._pieces[s._x88]]: if Piece.is_klass_and_color(piece, KING, color): return square
def get_king(self, color): """Gets the square of the king. :param color: `"w"` for the white players king. `"b"` for the black players king. :return: The first square with a matching king or `None` if that player has no king. """ #if not color in ["w", "b"]: # raise KeyError("Invalid color: %s." % repr(color)) for square, piece in [(s, self._pieces[s._x88]) for s in Square.get_all() if self._pieces[s._x88]]: if Piece.is_klass_and_color(piece, KING, color): return square
def test_is_klass_and_color(self): self.assertEqual(True, Piece.is_klass_and_color('P', piece.PAWN, piece.WHITE))