From 679fe101a9a684c9edd74010221274f43ea69e94 Mon Sep 17 00:00:00 2001 From: zachartrand <80857317+zachartrand@users.noreply.github.com> Date: Wed, 26 May 2021 22:46:58 -0400 Subject: [PATCH] Update chess_engine.py Fixed bug where pawns could en passant on invalid turns as well as en passant capturing non-pawn pieces. --- chess_engine.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/chess_engine.py b/chess_engine.py index 649a263..f3a18e1 100644 --- a/chess_engine.py +++ b/chess_engine.py @@ -12,6 +12,7 @@ from chess_pieces import Queen, Rook, Bishop, Knight from chess_pieces import DIRECTIONS from chess_board import makeStandardBoard, Square +# from chess_board import makeTwoRooksEndgameBoard, makeQueenEndgameBoard class GameState(): @@ -93,7 +94,11 @@ def make_move(self, move): and (abs(move.end_square.get_rank() - move.start_square.get_rank()) == 2)): self.enpassant_coords = move.piece_moved.get_coords() - elif self.enpassant_coords != (): + elif (self.enpassant_coords + or move.piece_moved.get_name() != "Pawn"): + self.enpassant_coords = () + else: + if self.enpassant_coords: self.enpassant_coords = () self.white_to_move = not self.white_to_move @@ -121,7 +126,7 @@ def undo_move(self): rookStartSquare.set_piece(rook) if move.contains_promotion(): pieces_removed.append(move.promotion_piece) - if self.move_log: + if self.move_log: # Needed to prevent AI bugs. previousMove, _ = self.move_log.copy().pop() if (previousMove.piece_moved.get_name() == 'Pawn' and (abs(previousMove.end_square.get_rank() @@ -623,6 +628,8 @@ def get_chess_notation(self, gs: GameState): """ Returns the move in algebraic notation. """ + # TODO: Check if pieces of the same name are on the same rank as the + # piece moved in case more specific notation is needed. if self.contains_castle(): rookFile = self.castle[1].get_coords()[0] kingFile = self.start_square.get_coords()[0]