Skip to content

Commit

Permalink
Update chess_engine.py
Browse files Browse the repository at this point in the history
Fixed bug where pawns could en passant on invalid turns as well as en passant capturing non-pawn pieces.
  • Loading branch information
zachartrand committed May 27, 2021
1 parent 999e8ea commit 679fe10
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions chess_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 679fe10

Please sign in to comment.