Skip to content

Commit

Permalink
Fixed en passant detection bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hrushikeshrv committed Jan 16, 2023
1 parent 80bde5f commit 9cadf7d
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions chessengine/bitboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
coords_to_pos,
pos_to_coords,
san_piece_map,
piece_square_table
piece_square_table,
)
from chessengine.utils import (
get_bit_positions,
Expand Down Expand Up @@ -233,23 +233,23 @@ def score(self):
"""
s = 0
piece_values = {
'pawns': 100,
'rooks': 500,
'knights': 320,
'bishops': 330,
'queens': 900,
'kings': 20000
"pawns": 100,
"rooks": 500,
"knights": 320,
"bishops": 330,
"queens": 900,
"kings": 20000,
}
for i in range(64):
pos = 2 ** i
pos = 2**i
side, piece, _ = self.identify_piece_at(pos)
if side is None:
continue
elif side == 'white':
elif side == "white":
s += piece_values[piece] + piece_square_table[(side, piece)][i]
else:
s -= piece_values[piece] + piece_square_table[(side, piece)][i]
if self.side == 'white':
if self.side == "white":
return s
return -s

Expand Down Expand Up @@ -494,11 +494,11 @@ def move(self, start: int, end: int, track: bool = True) -> None:
if start_piece == "pawns":
if start_side == "white":
# Check en passant status
if get_rank(end) - get_rank(start) == 2:
if get_rank(start) == 2 and get_rank(end) == 4:
self.en_passant_position = start << 8

# Check if a pawn captured by an en passant move
elif get_file(start) != get_file(end):
elif end == self.en_passant_position:
# White pawn made an en passant move
black_pawn_bb = self.get_bitboard("black", "pawns")
black_pawn_bb &= clear_position[end >> 8]
Expand All @@ -512,11 +512,11 @@ def move(self, start: int, end: int, track: bool = True) -> None:

else:
# Check en passant status
if get_rank(start) - get_rank(end) == 2:
if get_rank(start) == 7 and get_rank(end) == 5:
self.en_passant_position = start >> 8

# Check if a pawn captured by an en passant move
elif get_file(start) != get_file(end):
elif end == self.en_passant_position:
# Black pawn made an en passant move
white_pawn_bb = self.get_bitboard("white", "pawns")
white_pawn_bb &= clear_position[end << 8]
Expand Down Expand Up @@ -775,7 +775,6 @@ def get_moves(
("black", "knights"): get_black_knight_moves,
("black", "pawns"): get_black_pawn_moves,
}
# TODO - Add support for en passant move detection
return move_gens[(side, piece)](self, position)
else:
moves = []
Expand Down

0 comments on commit 9cadf7d

Please sign in to comment.