[Python] How to add chess move validation to my function?

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I have this function that takes in a piece (e.g wR would be white rook), the row its on and the column and returns a list of coordinates that it can move to. However, I would like to make it so that if the player is in check, the moves are restricted so that they have to get out of check. (The board array updates dynamically).

    board = [['bR', 'bN', 'bB', 'bQ', 'bK', 'bB', 'bN', 'bR'],
    ['bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp', 'bp'],
    ['xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx'],
    ['xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx'],
    ['xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx'],
    ['xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx', 'xx'],
    ['wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp', 'wp'],
    ['wR', 'wN', 'wB', 'wK', 'wQ', 'wB', 'wN', 'wR']]

    def get_valid_moves(piece, row, col):
    global board
    moves = []

    if piece == 'wp':
    # Movement rules for white pawn
    if row > 0 and board[row - 1][col] == 'xx': # Move forward
    moves.append((row - 1, col))
    if row == 6 and board[row - 1][col] == 'xx' and board[row - 2][col] == 'xx':
    moves.append((row - 2, col))
    if row > 0 and col > 0 and board[row - 1][col - 1][0] == 'b':
    moves.append((row - 1, col - 1))
    if row > 0 and col < 7 and board[row - 1][col + 1][0] == 'b':
    moves.append((row - 1, col + 1))
    elif piece == 'bp':
    # Movement rules for black pawn
    if row < 7 and board[row + 1][col] == 'xx':
    moves.append((row + 1, col))
    if row == 1 and board[row + 1][col] == 'xx' and board[row + 2][col] == 'xx':
    moves.append((row + 2, col))
    if row < 7 and col > 0 and board[row + 1][col - 1][0] == 'w':
    moves.append((row + 1, col - 1))
    if row < 7 and col < 7 and board[row + 1][col + 1][0] == 'w':
    moves.append((row + 1, col + 1))
    elif piece == 'wR' or piece == 'bR':
    # Movement rules for rook
    for c in range(col - 1, -1, -1):
    if board[row][c] == 'xx':
    moves.append((row, c))
    elif board[row][c][0] != piece[0]:
    moves.append((row, c))
    break
    else:
    break
    for c in range(col + 1, 8):
    if board[row][c] == 'xx':
    moves.append((row, c))
    elif board[row][c][0] != piece[0]:
    moves.append((row, c))
    break
    else:
    break
    for r in range(row - 1, -1, -1):
    if board[r][col] == 'xx':
    moves.append((r, col))
    elif board[r][col][0] != piece[0]:
    moves.append((r, col))
    break
    else:
    break
    for r in range(row + 1, 8):
    if board[r][col] == 'xx':
    moves.append((r, col))
    elif board[r][col][0] != piece[0]:
    moves.append((r, col))
    break
    else:
    break
    elif piece == 'wB' or piece == 'bB':
    # Movement rules for bishop
    for i in range(1, min(8 - row, 8 - col)):
    if board[row + i][col + i] == 'xx':
    moves.append((row + i, col + i))
    elif board[row + i][col + i][0] != piece[0]:
    moves.append((row + i, col + i))
    break
    else:
    break
    for i in range(1, min(8 - row, col + 1)):
    if board[row + i][col - i] == 'xx':
    moves.append((row + i, col - i))
    elif board[row + i][col - i][0] != piece[0]:
    moves.append((row + i, col - i))
    break
    else:
    break
    for i in range(1, min(row + 1, 8 - col)):
    if board[row - i][col + i] == 'xx':
    moves.append((row - i, col + i))
    elif board[row - i][col + i][0] != piece[0]:
    moves.append((row - i, col + i))
    break
    else:
    break
    for i in range(1, min(row + 1, col + 1)):
    if board[row - i][col - i] == 'xx':
    moves.append((row - i, col - i))
    elif board[row - i][col - i][0] != piece[0]:
    moves.append((row - i, col - i))
    break
    else:
    break
    elif piece == 'wN' or piece == 'bN':
    # Movement rules for knight
    knight_moves = [(-1, -2), (-2, -1), (-2, 1), (-1, 2), (1, -2), (2, -1), (2, 1), (1, 2)]
    for move in knight_moves:
    new_row = row + move[0]
    new_col = col + move[1]
    if 0 <= new_row < 8 and 0 <= new_col < 8:
    if board[new_row][new_col] == 'xx' or board[new_row][new_col][0] != piece[0]:
    moves.append((new_row, new_col))
    elif piece == 'wQ' or piece == 'bQ':
    # Movement rules for queen
    for c in range(col - 1, -1, -1):
    if board[row][c] == 'xx':
    moves.append((row, c))
    elif board[row][c][0] != piece[0]:
    moves.append((row, c))
    break
    else:
    break
    for c in range(col + 1, 8):
    if board[row][c] == 'xx':
    moves.append((row, c))
    elif board[row][c][0] != piece[0]:
    moves.append((row, c))
    break
    else:
    break
    # Vertical moves
    for r in range(row - 1, -1, -1):
    if board[r][col] == 'xx':
    moves.append((r, col))
    elif board[r][col][0] != piece[0]:
    moves.append((r, col))
    break
    else:
    break
    for r in range(row + 1, 8):
    if board[r][col] == 'xx':
    moves.append((r, col))
    elif board[r][col][0] != piece[0]:
    moves.append((r, col))
    break
    else:
    break
    for i in range(1, min(8 - row, 8 - col)):
    if board[row + i][col + i] == 'xx':
    moves.append((row + i, col + i))
    elif board[row + i][col + i][0] != piece[0]:
    moves.append((row + i, col + i))
    break
    else:
    break
    for i in range(1, min(8 - row, col + 1)):
    if board[row + i][col - i] == 'xx':
    moves.append((row + i, col - i))
    elif board[row + i][col - i][0] != piece[0]:
    moves.append((row + i, col - i))
    break
    else:
    break
    for i in range(1, min(row + 1, 8 - col)):
    if board[row - i][col + i] == 'xx':
    moves.append((row - i, col + i))
    elif board[row - i][col + i][0] != piece[0]:
    moves.append((row - i, col + i))
    break
    else:
    break
    for i in range(1, min(row + 1, col + 1)):
    if board[row - i][col - i] == 'xx':
    moves.append((row - i, col - i))
    elif board[row - i][col - i][0] != piece[0]:
    moves.append((row - i, col - i))
    break
    else:
    break
    elif piece == 'wK' or piece == 'bK':
    # Movement rules for king
    king_moves = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
    for move in king_moves:
    new_row = row + move[0]
    new_col = col + move[1]
    if 0 <= new_row < 8 and 0 <= new_col < 8:
    if board[new_row][new_col] == 'xx' or board[new_row][new_col][0] != piece[0]:
    moves.append((new_row, new_col))

    def find_king_coordinates(player_color):
    for r in range(8):
    for c in range(8):
    if board[r][c] == f'{player_color}K':
    return r, c

    def is_check(player_color, king_row, king_col, board):
    # Define possible directions of attack for different pieces
    knight_moves = [(1, 2), (2, 1), (-1, -2), (-2, -1), (1, -2), (2, -1), (-1, 2), (-2, 1)]
    directions = [(1, 0), (-1, 0), (0, 1), (0, -1), (1, 1), (-1, -1), (1, -1), (-1, 1)]

    # Check for attacks by knights
    for dr, dc in knight_moves:
    if 0 <= king_row + dr < 8 and 0 <= king_col + dc < 8:
    if board[king_row + dr][king_col + dc] == f'{player_color}N':
    return True

    # Check for attacks by rooks and queens (horizontal and vertical)
    for dr, dc in directions[:4]:
    r, c = king_row + dr, king_col + dc
    while 0 <= r < 8 and 0 <= c < 8:
    piece = board[r][c]
    if piece[0] == player_color:
    break
    if piece == f'{player_color}R' or piece == f'{player_color}Q':
    return True
    if piece != 'xx':
    break
    r, c = r + dr, c + dc

    # Check for attacks by bishops and queens (diagonal)
    for dr, dc in directions[4:]:
    r, c = king_row + dr, king_col + dc
    while 0 <= r < 8 and 0 <= c < 8:
    piece = board[r][c]
    if piece[0] == player_color:
    break
    if piece == f'{player_color}B' or piece == f'{player_color}Q':
    return True
    if piece != 'xx':
    break
    r, c = r + dr, c + dc

    # Check for attacks by pawns (only considering the two diagonals)
    pawn_attacks = [(1, 1), (1, -1)] if player_color == 'w' else [(-1, 1), (-1, -1)]
    for dr, dc in pawn_attacks:
    if 0 <= king_row + dr < 8 and 0 <= king_col + dc < 8:
    if board[king_row + dr][king_col + dc] == f'{player_color}p':
    return True

    return False # The king is not in check

    if is_check(piece[0], find_king_coordinates(piece[0]), board):
    temp_board = board
    pass

    Continue reading...

Compartilhe esta Página