import pygame from config.constants import COLS, ROWS, LIGHT_BROWN, DARK_BROWN, SQUARE_SIZE, PIECES class ChessBoard: def __init__(self): self.board = self.create_board() self.selected_piece = None self.first_player = 'white' print(self.board) def create_board(self): board = [['' for _ in range(COLS)] for _ in range(ROWS)] # pawns for i in range(COLS): board[1][i] = 'black_pawn' board[6][i] = 'white_pawn' # rooks board[0][0] = board[0][7] = 'black_rook' board[7][0] = board[7][7] = 'white_rook' # horses board[0][1] = board[0][6] = 'black_knight' board[7][1] = board[7][6] = 'white_knight' # bishops board[0][2] = board[0][5] = 'black_bishop' board[7][2] = board[7][5] = 'white_bishop' # queens board[0][3] = 'black_queen' board[7][3] = 'white_queen' # kings board[0][4] = 'black_king' board[7][4] = 'white_king' return board def draw(self, win): self.draw_squares(win) self.draw_pieces(win) def draw_squares(self, win): # Draw chessboard squares for row in range(ROWS): for col in range(COLS): color = LIGHT_BROWN if (row + col) % 2 == 0 else DARK_BROWN pygame.draw.rect(win, color, (col * SQUARE_SIZE, row * SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE)) def draw_pieces(self, win): # Draw pieces on the chessboard for row in range(ROWS): for col in range(COLS): piece = self.board[row][col] if piece != '': piece_image = PIECES[piece] piece_image = pygame.transform.scale(piece_image, (SQUARE_SIZE, SQUARE_SIZE)) win.blit(piece_image, (col * SQUARE_SIZE, row * SQUARE_SIZE)) def move_piece(self, piece_name, dest_square, turn_counter): col_map = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7} row_map = {'1': 7, '2': 6, '3': 5, '4': 4, '5': 3, '6': 2, '7': 1, '8': 0} dest_col = col_map[dest_square[0]] dest_row = row_map[dest_square[1]] if turn_counter % 2 ==0: piece_name = 'white_' + piece_name else: piece_name = 'black_' + piece_name print(piece_name) # Find the piece's current location for row in range(ROWS): for col in range(COLS): # TODO: Replace primitive logic # TODO: Have the moves use special characters "[< rook from e4 to e5>]" # is_legal(piece_name, starting_pos, final_pos) # is_capture(piece_name, final_pos) if self.board[row][col] == piece_name: print(row, col) print(dest_row, dest_col) # Move the piece to the destination self.board[dest_row][dest_col] = piece_name self.board[row][col] = '' return True return False if __name__ == '__main__': board = ChessBoard()