-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessgui.py
325 lines (275 loc) · 11.7 KB
/
chessgui.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
from chesslogic import *
from chessai import *
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtCore
from functools import partial
# Size of window
WINDOW_DIM = 800
# Colors
DARK = "grey"
BRIGHT = "rgb(255, 253, 208)"
BG = "rgb(0,139,139)"
PIECE_PNG = {
Pawn: "pawn.png",
Rook: "rook.png",
Knight: "knight.png",
Bishop: "bishop.png",
Queen: "queen.png",
King: "king.png"
}
COLOR_PNG = {
Colors.Black: "b_",
Colors.White: "w_"
}
class BoardDialog(QtWidgets.QDialog):
def __init__(self, parent,
layout=QtWidgets.QBoxLayout(QtWidgets.QBoxLayout.TopToBottom)
):
super().__init__(parent)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.layout = layout
self.setStyleSheet(f"QDialog {{background-color: {BG};}}")
self.setLayout(self.layout)
def add_button(self, text, connect_funcs=None):
button = QtWidgets.QPushButton(text, self)
button.setStyleSheet("QPushButton {border: none};")
button.setStyleSheet(f"QPushButton {{background-color: {BRIGHT};}}")
button.isFlat = True
self.layout.addWidget(button)
if connect_funcs:
for func in connect_funcs:
button.clicked.connect(func)
class BoardPiece(QtWidgets.QLabel):
''' A piece in the GUI '''
def __init__(self, parent, color, kind):
super().__init__()
self.kind = kind
self.color = color
if color and kind:
pix_map = QtGui.QPixmap("pieces/"
f"{COLOR_PNG[color]}{PIECE_PNG[kind]}")
self.setPixmap(pix_map.scaled(int(parent.width()*0.5),
int(parent.width()*0.5), QtCore.Qt.KeepAspectRatio))
class BoardSquare(QtWidgets.QFrame):
''' A square in the GUI '''
def __init__(self, board_window, parent_widget, square_color, gui_coords):
super().__init__(parent_widget)
self.board_window = board_window
self.board_widget = parent_widget
self.gui_coords = gui_coords
self.piece = None
self.square_layout = QtWidgets.QVBoxLayout()
self.square_layout.setAlignment(QtCore.Qt.AlignCenter)
self.setLayout(self.square_layout)
self.setStyleSheet(f"background-color: {square_color};")
self.setLineWidth(0)
def place_piece(self, color, kind):
self.remove_piece()
piece = BoardPiece(self, color, kind)
self.square_layout.addWidget(piece)
self.piece = piece
def remove_piece(self):
if self.piece:
self.square_layout.removeWidget(self.piece)
self.piece = None
def mouseDoubleClickEvent(self, a0: QtGui.QMouseEvent) -> None:
self.board_window.handleDoubleClick(self)
return super().mousePressEvent(a0)
class BoardWindow(QtWidgets.QWidget):
''' The game window '''
# @param white_perspective: direction of board
def __init__(self, white_perspective = True):
super().__init__()
# Lock the board if game is stalemate or mate
self.locked = False
# Marks if a piece is lifted and if so saves its coords.
self.piece_lifted = False
self.origin_square = None
self.white_perspective = white_perspective
self.computer_player = None
self.computer_turn = False
window_layout = QtWidgets.QGridLayout()
self.setLayout(window_layout)
self.setMinimumSize(WINDOW_DIM-100, WINDOW_DIM-100)
self.resize(WINDOW_DIM,WINDOW_DIM)
self.setStyleSheet(f"BoardWindow {{background-color: {BG};}}")
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.board = Board()
self.board_widget = QtWidgets.QFrame(self)
self.board_squares = [[] for i in range(8)]
board_layout = QtWidgets.QGridLayout()
board_layout.setSpacing(0)
self.board_widget.setLayout(board_layout)
positions = [(i, j) for i in range(8) for j in range(8)]
for i, j in positions:
r, c = self.gui_to_board_coords((i, j))
# Square color based on logic board coordinates
square_color = DARK if (r + c) % 2 == 1 else BRIGHT
square = BoardSquare(self, self.board_widget, square_color, (i, j))
self.board_squares[i].append(square)
board_layout.addWidget(square, i, j)
print(INTRO)
print("revert: r")
print("exit: alt + f4 or cmd + w")
window_layout.addWidget(self.board_widget)
self.set_pieces(self.board.get_state())
self.show()
# Used to move window by dragging
self.offset = None
# TODO add color choice
self.intro_dialog = BoardDialog(self)
self.intro_dialog.add_button("Human", [self.intro_dialog.close])
self.intro_dialog.add_button("Computer", [self.set_ai, self.intro_dialog.close])
self.intro_dialog.exec()
# TODO pass also color choice
def set_ai(self):
self.computer_player = RandomPlayer(self.board)
def play_ai_move(self):
# TODO change so player's class performs move (no while)
while True and not self.locked:
try:
move = self.computer_player.get_ai_move()
origin, target, promotion = move
game_status = self.board.move_piece(origin, target, promotion)
except Exception as e:
continue
else:
self.handle_game_status(game_status)
self.set_pieces(self.board.get_state())
self.computer_turn = False
break
# @param coords: a tuple of gui board coordinates
# @return: coordinates on logic board as Coords.
def gui_to_board_coords(self, coords):
row, col = coords
if self.white_perspective:
return Coords(7 - row, col)
else:
return Coords(row, col)
# @param square: a square in the gui
# @return: coordinates on logic board as Coords.
def square_to_board_coords(self, square):
return self.gui_to_board_coords(square.gui_coords)
# @param coords: a tuple of logic board coordinates
# @return: coordinates on gui board.
def board_to_gui_coords(self, coords):
row, col = coords
return tuple(self.gui_to_board_coords((row, col)))
def handle_game_status(self, status):
status_msg = {BoardStatus.Stalemate: "Stalemate",
BoardStatus.Checkmate: "Checkmate"
}
if (status == BoardStatus.Stalemate or status == BoardStatus.Checkmate):
# Lock the board
self.locked = True
# TODO refactor this
msg = QtWidgets.QDialog(self)
msg.setWindowFlag(QtCore.Qt.FramelessWindowHint)
msg_layout = QtWidgets.QBoxLayout(QtWidgets.QBoxLayout.TopToBottom)
msg.setStyleSheet(f"QDialog {{background-color: {BG};}}")
msg.setLayout(msg_layout)
button = QtWidgets.QPushButton(status_msg[status], msg)
button.setStyleSheet("QPushButton {border: none};")
button.setStyleSheet(f"QPushButton {{background-color: {BRIGHT};}}")
button.isFlat = True
button.clicked.connect(msg.close)
msg_layout.addWidget(button)
msg.exec()
def call_promote(self, origin, target, piece_type):
game_status = self.board.move_piece(origin, target, piece_type)
self.set_pieces(self.board.get_state())
self.handle_game_status(game_status)
def promote_dialog(self, origin, target):
promote_pieces = {
Knight: "Knight",
Bishop: "Bishop",
Rook: "Rook",
Queen: "Queen"
}
dialog = QtWidgets.QDialog(self)
dialog.setWindowFlag(QtCore.Qt.FramelessWindowHint)
dialog_layout = QtWidgets.QBoxLayout(QtWidgets.QBoxLayout.TopToBottom)
dialog.setStyleSheet(f"QDialog {{background-color: {BG};}}")
dialog.setLayout(dialog_layout)
for piece_type in promote_pieces:
button = QtWidgets.QPushButton(promote_pieces[piece_type], dialog)
button.setStyleSheet("QPushButton {border: none};")
button.setStyleSheet(f"QPushButton {{background-color: {BRIGHT};}}")
button.isFlat = True
button.clicked.connect(partial(self.call_promote, origin, target, piece_type))
button.clicked.connect(dialog.close)
dialog_layout.addWidget(button)
dialog.exec()
def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
if event.key() == QtCore.Qt.Key_R:
self.board.revert_last_move()
self.set_pieces(self.board.get_state())
return super().keyPressEvent(event)
# When left button is pressed start to keep track of offset
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.offset = event.pos()
else:
super().mousePressEvent(event)
# When mouse moves and left button pressed move to new position
def mouseMoveEvent(self, event):
if (self.offset is not None and
event.buttons() == QtCore.Qt.LeftButton
):
# Make drag less sensitive (reduce unintentional repositions)
delta = event.pos() - self.offset
delta_len = delta.x() ** 2 + delta.y() ** 2
if delta_len > 15 ** 2:
self.move(self.pos() + delta)
else:
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
self.offset = None
super().mouseReleaseEvent(event)
# Sets pieces in GUI according to logic board.
# @param board_state: a representation of the current logic board state
def set_pieces(self, board_state):
board_coords = [(r, c) for r in range(8) for c in range(8)]
for r, c in board_coords:
color, kind = board_state[r][c]
gui_r, gui_c = self.board_to_gui_coords((r, c))
square = self.board_squares[gui_r][gui_c]
if color and kind:
square.place_piece(color, kind)
elif (color, kind) == (None, None):
square.remove_piece()
# @param: a square that has been double clicked to lift a piece
def lift_piece(self, square):
self.origin_square = square
self.piece_lifted = True
# @param square: a square that was double clicked
def handleDoubleClick(self, square):
if not self.computer_turn:
if not self.locked:
if square.piece and not self.piece_lifted:
self.lift_piece(square)
elif self.piece_lifted:
try:
self.piece_lifted = False
origin = self.square_to_board_coords(self.origin_square)
target = self.square_to_board_coords(square)
game_status = self.board.move_piece(origin, target, promotion=None)
except MissingPromotionChoice as e:
self.promote_dialog(origin, target)
except Exception as e:
print(e)
self.origin_square = None
else:
self.set_pieces(self.board.get_state())
# This is also called after promotion
self.handle_game_status(game_status)
if self.computer_player:
self.computer_turn = True
self.play_ai_move()
else:
print("game is over")
if __name__=="__main__":
app = QtWidgets.QApplication([])
mw = BoardWindow()
app.exec_()