Added en passant to possible moves

master
Tibor Bizjak 2019-09-09 14:10:55 +02:00
parent 8256ba1bf6
commit 1237549567
1 changed files with 9 additions and 4 deletions

View File

@ -198,9 +198,9 @@ class Game:
if piece == "pawn": if piece == "pawn":
r = [] r = []
dir = up dir, back = up, down
if color == "black": if color == "black":
dir = down dir, back = down, up
frwd = move(sq, dir) frwd = move(sq, dir)
jump = move(frwd, dir) jump = move(frwd, dir)
@ -209,7 +209,12 @@ class Game:
is_on_pawn_rank = pawn_ranks[color] == sq[1] is_on_pawn_rank = pawn_ranks[color] == sq[1]
if is_on_pawn_rank and is_empty(jump): if is_on_pawn_rank and is_empty(jump):
r.append(jump) r.append(jump)
return r + [sq for sq in targets if can_eat(sq)] for t in targets:
a, b = move(t, dir), move(t, back)
en_passant = can_eat(b) and self.moves[-1] == (a,b)
if can_eat(t) or en_passant:
r.append(t)
return r
elif piece == "king": elif piece == "king":
return [sq for sq in targets if not self.is_attacked(color, sq)] return [sq for sq in targets if not self.is_attacked(color, sq)]
else: else:
@ -265,7 +270,7 @@ def test():
game = Game() game = Game()
assert len(squares) == 8**2 assert len(squares) == 8**2
assert sum(map(len, init_positions.values())) == 8*4 assert sum(map(len, init_positions.values())) == 8*4
moves = [("a2", "a4"), ("b1", "c3"), ("c3", "b5"), ("b8", "c6")] moves = [("a2", "a4"), ("b8", "c6"), ("a4", "a5"), ("b7", "b5"), ("a5", "b6")]
for m in moves: for m in moves:
game.move(*m) game.move(*m)
print (game) print (game)