Skip to content

Commit

Permalink
Stop playing back moves at pawn moves and captures
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Nov 3, 2016
1 parent 318084d commit 3be053a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
4 changes: 4 additions & 0 deletions chess/__init__.py
Expand Up @@ -2870,6 +2870,10 @@ def is_capture(self, move):
"""Checks if the given pseudo-legal move is a capture."""
return BB_SQUARES[move.to_square] & self.occupied_co[not self.turn] or self.is_en_passant(move)

def is_zeroing(self, move):
"""Checks if the given pseudo-legal move is a capture or pawn move."""
return BB_SQUARES[move.from_square] & self.pawns or BB_SQUARES[move.to_square] & self.occupied_co[not self.turn]

def is_castling(self, move):
"""Checks if the given pseudo-legal move is a castling move."""
if BB_SQUARES[move.to_square] & self.occupied_co[self.turn] & self.rooks:
Expand Down
13 changes: 9 additions & 4 deletions chess/uci.py
Expand Up @@ -1061,18 +1061,23 @@ def position(self, board, async_callback=None):
builder = []
builder.append("position")

# Take back moves to obtain the first FEN we know. Later giving the
# moves explicitly allows for transposition detection.
# Take back moves to obtain the FEN at the latest pawn move or
# capture. Later giving the moves explicitly allows for transposition
# detection.
switchyard = collections.deque()
while board.move_stack:
switchyard.append(board.pop())
move = board.pop()
switchyard.append(move)

if board.is_zeroing(move):
break

# Validate castling rights.
if not self.uci_chess960 and board.chess960:
if board.has_chess960_castling_rights():
LOGGER.error("not in UCI_Chess960 mode but position has non-standard castling rights")

# Just send the final FEN without transpositions in hops
# Just send the final FEN without transpositions in hopes
# that this will work.
while switchyard:
board.push(switchyard.pop())
Expand Down

0 comments on commit 3be053a

Please sign in to comment.