Fix piece movement generation
This commit is contained in:
22
app.py
22
app.py
@@ -365,14 +365,12 @@ def get_piece_moves(piece_kind, board: Board, is_white, src: str) -> list[Coord]
|
||||
if (y == 1 or y == 6) and board.index_xy(x, y + 2 * dir) == Piece.EMPTY:
|
||||
valids.append(Coord(x=x, y=y + 2 * dir))
|
||||
if (
|
||||
board.index_xy(x + 1, y + dir)
|
||||
not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING]
|
||||
board.index_xy(x + 1, y + dir) not in [Piece.EMPTY, Piece.NONE]
|
||||
and board.index_xy(x + 1, y + dir).value.isupper() != is_white
|
||||
):
|
||||
valids.append(Coord(x=x + 1, y=y + dir))
|
||||
if (
|
||||
board.index_xy(x - 1, y + dir)
|
||||
not in [Piece.EMPTY, Piece.NONE, Piece.BLACK_KING, Piece.WHITE_KING]
|
||||
board.index_xy(x - 1, y + dir) not in [Piece.EMPTY, Piece.NONE]
|
||||
and board.index_xy(x - 1, y + dir).value.isupper() != is_white
|
||||
):
|
||||
valids.append(Coord(x=x - 1, y=y + dir))
|
||||
@@ -469,7 +467,6 @@ def generate_valid_moves(
|
||||
):
|
||||
king = Coord(j, i)
|
||||
break
|
||||
|
||||
for m in possible_moves:
|
||||
fake_board = board.copy()
|
||||
fake_board.grid[m.y][m.x] = fake_board.index(src)
|
||||
@@ -478,24 +475,13 @@ def generate_valid_moves(
|
||||
king_safe = True
|
||||
for i in range(8):
|
||||
for j in range(8):
|
||||
p = board.grid[j][i]
|
||||
p = board.index_xy(j, i)
|
||||
if p != Piece.EMPTY and p.value.isupper() != is_white:
|
||||
# Enemy
|
||||
enemy_moves = get_piece_moves(
|
||||
p.value.lower(), board, not is_white, xy_to_pos_safe(j, i)
|
||||
)
|
||||
if king not in enemy_moves:
|
||||
continue
|
||||
ni = i
|
||||
nj = j
|
||||
if j == sx and i == sy:
|
||||
ni = m.y
|
||||
nj = m.x
|
||||
new_enemy_moves = get_piece_moves(
|
||||
p.value.lower(),
|
||||
fake_board,
|
||||
not is_white,
|
||||
xy_to_pos_safe(nj, ni),
|
||||
xy_to_pos_safe(j, i),
|
||||
)
|
||||
if king in new_enemy_moves:
|
||||
king_safe = False
|
||||
|
||||
Reference in New Issue
Block a user