-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchessboard.py
291 lines (250 loc) · 10.9 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import random
import chess
from PySide6 import QtCore, QtGui, QtSvg, QtSvgWidgets, QtWidgets
import vars
from chesspieces import ChessPieces
from movemanager import MoveManager
class ChessBoard:
def __init__(self):
self.fischer_random = False
self.board = chess.Board(chess960=self.fischer_random)
self.is_board_flipped = False
self.move_manager = MoveManager(self)
self.starting_board_position_fen = None
def set_chess960_board(self):
self.board.set_chess960_pos(random.randint(1, 959))
def get_pieces_squares(self, piece_name, piece_color):
"""
returns the list of squares coordinates of the given piece name & color
get_pieces_squares(chess.ROOK, chess.WHITE) => [(0, 7), (7, 7)]
"""
squares = []
for square in self.board.pieces(piece_name, piece_color):
if self.is_board_flipped:
squares.append(
(7 - chess.square_file(square), chess.square_rank(square))
)
else:
squares.append(
(chess.square_file(square), 7 - chess.square_rank(square))
)
return squares
def get_square_coordinates(self, square_number):
"""
returns the coordinates of given square number
col, row, x, y = self.get_square_coordinates(20)
"""
if self.is_board_flipped:
col = 7 - chess.square_file(square_number)
row = chess.square_rank(square_number)
else:
col = chess.square_file(square_number)
row = 7 - chess.square_rank(square_number)
return col, row, col * vars.SQUARE_SIZE, row * vars.SQUARE_SIZE
def get_selected_square_number(self, event):
"""
return the selected square number
"""
pos = event.position().toPoint()
mapped_pos = self.mapToScene(pos)
col = int(mapped_pos.x() / vars.SQUARE_SIZE)
row = int(mapped_pos.y() / vars.SQUARE_SIZE)
if self.is_board_flipped:
return chess.square(7 - col, row)
return chess.square(col, 7 - row)
def get_source_square_from_move(self, move):
"""
returns a square coordinates where piece is moved from
source_square = get_source_square_from_move(e2e4)
=> (4, 6)
"""
if self.is_board_flipped:
return (
7 - chess.square_file(move.from_square),
chess.square_rank(move.from_square),
)
return chess.square_file(move.from_square), 7 - chess.square_rank(
move.from_square
)
def get_destination_square_from_move(self, move):
"""
returns a square coordinates where piece is moved to
destination_square = get_destination_square_from_move(e2e4)
=> (4, 4)
"""
if self.is_board_flipped:
return (
7 - chess.square_file(move.to_square),
chess.square_rank(move.to_square),
)
return chess.square_file(move.to_square), 7 - chess.square_rank(move.to_square)
def get_selected_piece_color_and_name(self, square_number):
"""
returns the color and name of the piece at the selected square
"""
piece = self.board.piece_at(square_number)
if piece:
piece_color = "w" if piece.color == chess.WHITE else "b"
piece_name = piece.symbol().upper()
return piece_color, piece_name
return None, None
def get_board_turn(self):
"""
returns which player's turn to play
"""
return "w" if self.board.turn == chess.WHITE else "b"
def highlight_legal_moves(self, scene, square_number):
"""
highlights the legal moves of a selected piece/square
"""
legal_moves = self.move_manager.get_legal_moves(square_number)
for target_square in set(move.to_square for move in legal_moves):
col, row, x, y = self.get_square_coordinates(target_square)
# Add a circle in the center of the square
circle = scene.addEllipse(
x + vars.SQUARE_SIZE / 3,
y + vars.SQUARE_SIZE / 3,
vars.SQUARE_SIZE / 3,
vars.SQUARE_SIZE / 3,
)
circle.setPen(QtCore.Qt.NoPen)
circle.setBrush(QtGui.QColor(vars.THEME_COLORS["highlight_legal_moves"]))
circle.setOpacity(0.45)
def delete_highlighted_legal_moves(self, scene):
items = scene.items()
for item in items:
if isinstance(item, QtWidgets.QGraphicsEllipseItem):
brush_color = item.brush().color()
if brush_color == QtGui.QColor(
vars.THEME_COLORS["highlight_legal_moves"]
):
scene.removeItem(item)
class ChessBoardEvents:
def __init__(self, chessboard):
self.chessboard = chessboard
def mousePress(self, event):
square_number = self.chessboard.get_selected_square_number(event)
if event.buttons() == QtCore.Qt.LeftButton:
if self.chessboard.move_manager.selected_square is None:
self.chessboard.move_manager.selected_square = square_number
self.chessboard.highlight_legal_moves(
self.chessboard.scene, self.chessboard.move_manager.selected_square
)
else:
if square_number == self.chessboard.move_manager.selected_square:
self.chessboard.move_manager.selected_square = None
self.chessboard.delete_highlighted_legal_moves(
self.chessboard.scene
)
return
self.chessboard.move_manager.move_piece(square_number)
if self.chessboard.move_manager.is_piece_moved is True:
# this if condition is here because, in chess960 variant, user
# have to click on a rook to do castling and don't know why the
# `get_selected_piece_color_and_name` method returns None if user
# try to click on a rook to do castling.
# (tested on chess960 position number 665, 342 or similar positions)
if self.chessboard.fischer_random and (
self.chessboard.move_manager.is_queenside_castling
or self.chessboard.move_manager.is_kingside_castling
):
piece_color = (
"b" if self.chessboard.board.turn == chess.WHITE else "w"
)
piece_name = "R"
else:
piece_color, piece_name = (
self.chessboard.get_selected_piece_color_and_name(
square_number
)
)
last_move = self.chessboard.move_manager.get_last_move()
source_square = self.chessboard.get_source_square_from_move(
last_move
)
destination_square = (
self.chessboard.get_destination_square_from_move(last_move)
)
self.chessboard.chess_pieces.delete_piece(source_square)
if self.chessboard.move_manager.is_capture:
self.chessboard.chess_pieces.delete_piece(destination_square)
self.chessboard.move_manager.is_capture = False
self.chessboard.chess_pieces.draw_piece(
piece_name, piece_color, destination_square
)
self.chessboard.move_manager.is_piece_moved = False
self.chessboard.move_manager.selected_square = None
self.chessboard.delete_highlighted_legal_moves(
self.chessboard.scene
)
square_number = None
class DrawChessBoard(QtWidgets.QGraphicsView, ChessBoard):
def __init__(self):
super().__init__()
self.scene = QtWidgets.QGraphicsScene()
self.setScene(self.scene)
self.setRenderHints(
QtGui.QPainter.Antialiasing | QtGui.QPainter.SmoothPixmapTransform
)
self.chess_pieces = ChessPieces(self, self.scene, "cardinal")
self.chess_pieces.load_chess_piece_images()
self.show_labels = True
self.events = ChessBoardEvents(self)
def draw_squares(self):
"""
draws squares forming a chessboard
"""
for square in chess.SQUARES:
col, row, x, y = self.get_square_coordinates(square)
rect = self.scene.addRect(x, y, vars.SQUARE_SIZE, vars.SQUARE_SIZE)
rect_color = (
vars.THEME_COLORS["light_square"]
if (row + col) % 2 == 0
else vars.THEME_COLORS["dark_square"]
)
rect.setPen(QtCore.Qt.NoPen)
rect.setBrush(QtGui.QColor(rect_color))
def draw_labels(self):
"""
draws rank (1-8) & file (a-h) label
"""
for square in chess.SQUARES:
col, row, x, y = self.get_square_coordinates(square)
# Determine label position
row_label_x = x + vars.SQUARE_SIZE / 8 - 10
row_label_y = y + vars.SQUARE_SIZE / 8 - 10
col_label_x = x + vars.SQUARE_SIZE - vars.SQUARE_SIZE / 15 - 10
col_label_y = y + vars.SQUARE_SIZE - vars.SQUARE_SIZE / 8 - 15
label_color = (
vars.THEME_COLORS["light_square"]
if (row + col) % 2 != 0
else vars.THEME_COLORS["dark_square"]
)
# Add label for the first set of columns (a-h)
if row == 7:
if self.is_board_flipped:
label = self.scene.addText(f'{chr(ord("h")-col)}')
else:
label = self.scene.addText(f'{chr(ord("a")+col)}')
label.setDefaultTextColor(QtGui.QColor(label_color))
label.setPos(col_label_x, col_label_y)
# Add label for the first set of rows (1-8)
if col == 0:
if self.is_board_flipped:
label = self.scene.addText(f"{row+1}")
else:
label = self.scene.addText(f"{8-row}")
label.setDefaultTextColor(QtGui.QColor(label_color))
label.setPos(row_label_x, row_label_y)
def draw_chessboard(self):
if self.fischer_random:
self.set_chess960_board()
self.draw_squares()
if self.show_labels:
self.draw_labels()
self.chess_pieces.draw_pieces()
self.starting_board_position_fen = (
self.board.board_fen()
) # hack to get the fen of only starting position
def mousePressEvent(self, event):
self.events.mousePress(event)