Added en passant and pawn promotion logic

master
Tibor Bizjak 2019-09-10 13:08:06 +02:00
parent 1237549567
commit c0f2df0b59
1 changed files with 25 additions and 5 deletions

View File

@ -155,23 +155,42 @@ class Game:
def occupying(self, piece):
return [sq for sq in squares if self.board[sq] == piece]
def move(self, source, target):
if not self.is_legal(source, target):
def move(self, source, target, promotion=None):
if not self.is_legal(source, target, promotion):
return False
board = self.board
moved = board[source]
eaten = board[target]
if promotion == None:
promotion = moved
else:
promotion = Piece(self.turn, promotion)
if moved.piece == "pawn" and target in moves(source, bishop_dirs):
# En passant
a, b = move(target, up), move(target, down)
board[a] = Piece()
board[b] = Piece()
self.moves.append((source, target))
self.stack.append(eaten)
board[source] = Piece()
board[target] = moved
board[target] = promotion
self.turn = invert(moved.color)
return True
def is_legal(self, source, target):
def is_legal(self, source, target, promotion):
piece = self.board[source].piece
color = self.board[source].color
pawn_on_last_rank = piece == "pawn" and \
home_ranks[invert(color)] == target[1]
if promotion != None and not pawn_on_last_rank:
return False
if promotion == None and pawn_on_last_rank:
return False
if promotion == "pawn":
return False
return color == self.turn and target in self.possible_moves(source)
def is_attacked(self, color, sq):
@ -270,7 +289,8 @@ def test():
game = Game()
assert len(squares) == 8**2
assert sum(map(len, init_positions.values())) == 8*4
moves = [("a2", "a4"), ("b8", "c6"), ("a4", "a5"), ("b7", "b5"), ("a5", "b6")]
moves = [("a2", "a4"), ("b8", "c6"), ("a4", "a5"), ("b7", "b5"), ("a5", "b6"),
("c6", "e5"), ("b6", "b7"), ("h7", "h5"), ("b7", "b8", "pawn")]
for m in moves:
game.move(*m)
print (game)