-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessBoard.py
48 lines (44 loc) · 1.67 KB
/
chessBoard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import chess
def generate_html_board(board):
html_board = ""
light_square_color = "#f0d9b5"
dark_square_color = "#b58863"
# Iterate through the ranks and files of the chess board
for rank in range(8, 0, -1):
html_board += "<div class='row'>"
for file in range(1, 9):
square_index = chess.square(file - 1, rank - 1)
piece = board.piece_at(square_index)
square_color = light_square_color if (rank + file) % 2 == 0 else dark_square_color
html_board += f"<div class='square' style='background-color: {square_color};'>{get_piece_symbol(piece)}</div>"
html_board += "</div>"
return html_board
def get_piece_symbol(piece):
if piece is None:
return ""
elif piece.color == chess.WHITE:
if piece.piece_type == chess.PAWN:
return "♙"
elif piece.piece_type == chess.ROOK:
return "♖"
elif piece.piece_type == chess.KNIGHT:
return "♘"
elif piece.piece_type == chess.BISHOP:
return "♗"
elif piece.piece_type == chess.QUEEN:
return "♕"
else: # King
return "♔"
else: # Black pieces
if piece.piece_type == chess.PAWN:
return "♟"
elif piece.piece_type == chess.ROOK:
return "♜"
elif piece.piece_type == chess.KNIGHT:
return "♞"
elif piece.piece_type == chess.BISHOP:
return "♝"
elif piece.piece_type == chess.QUEEN:
return "♛"
else: # King
return "♚"