qiweunvjah
qiweunvjah

Reputation: 132

Chess: Get All Legal Moves Python

Here is my code so far:

from colorama import init, Back, Fore
init()



def create_board():
    letters = ["a", "b", "c", "d", "e", "f", "g", "h"]
    numbers = ["1", "2", "3", "4", "5", "6", "7", "8"]
    board = {}
    for number in numbers:
        for letter in letters:
            board[letter + number] = letter + number
    return board

def print_pieces(square_board):
    letters = ["a", "b", "c", "d", "e", "f", "g", "h"]

    pawn_squares = ["a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2", "a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7"]
    knight_squares = ["b1", "g1", "b8", "g8"]
    rook_squares = ["a1", "h1", "a8", "h8"]
    queen_squares = ["e1", "e8"]
    king_squares = ["d1", "d8"]
    bishop_squares = ["c1", "f1", "c8", "f8"]
    piece_squares = {"pawn_squares": pawn_squares, "knight_squares": knight_squares, "bishop_squares": bishop_squares, "rook_squares": rook_squares, "king_squares": king_squares, "queen_squares": queen_squares}
    pieces = {"pawn": "♙", "king": "♚", "queen": "♛", "rook": "♜", "bishop": "♝", "knight": "♞"}
    board = square_board
    piece = ["pawn", "knight", "bishop", "rook", "king", "queen"]

    for square in board:
        for pces in piece:
            if board[square] in piece_squares[f"{pces}_squares"]:
                board[square] = pieces[pces]

    letters = ["a", "b", "c", "d", "e", "f", "g", "h"]
    numbers = ["3", "4", "5", "6"]
    squares = []
    for letter in letters:
        for number in numbers:
            squares.append(letter + number)

    for square in squares:
        board[square] = " "

    board = dict(reversed(list(board.items())))
    return board

def draw_board(board):
    colors = [Back.MAGENTA, Back.YELLOW]
    line = 0
    squares = 0
    color = Fore.RESET
    for square in board:
        if line == 8:
            print(color + Back.RESET + "\n", end="")
            colors = colors[::-1]
            line = 0
        if squares == 48:
            color = Fore.BLACK
        print(color + colors[0] + board[square], flush=True, end=" ")
        colors = colors[::-1]
        squares += 1
        line += 1

board = create_board()
draw_board(print_pieces(board))

This works by storing each square in a dictionary and what is on the square (- indicates nothing on the square).

I want to list all possible legal moves (castling, en passant, promotion not included), given the state of the board.

Does anyone have any idea how to do this? My code is kind of messy, and I understand this is not really the best approach on making a chess board.

Upvotes: 0

Views: 1410

Answers (1)

eligolf
eligolf

Reputation: 1856

There are tons of ways of representing a chess board and then calculating all legal moves in a given position.

The easiest way which is also reasonable fast is to represent the board as a 1D array/list. You can read more about this on e.g. Chessprogramming.org. The way I usually do it is to have a -2 on places which are outside of the board (these will always be on the same indexes are are only used to calculate legal moves), -1 on empty squares, and a number if there is a piece on the square (0 for white pawn, 1 for white knight and so on).

There is too much complexity and code specific things in giving you code for move generation here. I suggest you look at Chessprogramming.org which has all you need for learning about programming a chess engine, there are also some good Youtube tutorials you can follow.

Upvotes: 1

Related Questions