-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui_pgn.py
282 lines (226 loc) · 11.6 KB
/
gui_pgn.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
import chess.pgn
import chess.svg
import sys
import qdarkstyle
import evaluate
import copy
from PyQt5 import QtSvg, QtGui, QtCore
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout, QTableWidget, \
QTableWidgetItem, QAbstractScrollArea, QLabel, QLineEdit, QMessageBox
from PyQt5.QtCore import pyqtSlot
import io
input_move_list = []
final_move_list = []
final_evaluation_list = []
text = ""
input_text1 = "0"
input_text2 = ""
def writefile(board): # Convert svg from chess library to a svg file
boardsvg = chess.svg.board(board=board)
f = open("Images/Board.svg", "w")
f.write(boardsvg)
f.close()
def next_button_clicked(event): # move the piece if the next button is pressed
global i, final_move_list
if i < len(final_move_list): # if counter i is less than the length of move_list, move the piece and create the svg file
board.push(board.parse_san(final_move_list[i]))
writefile(board)
i += 1
board_widget.load("Images/board.svg") # Refresh the board widget
else:
pass
def previous_button_clicked(event): # undo the move if the previous button is pressed
global i
if i > 0: # if counter i is more than 0, undo the move and create the svg file
i -= 1
board.pop()
writefile(board)
board_widget.load("Images/board.svg") # Refresh the board widget
else:
pass
def generate_move_and_eval_list(game): # generate a list of all moves
move_list = []
evaluation_list = []
temp_board = copy.copy(board)
for move in game.mainline_moves():
move_list.append(temp_board.san(move))
temp_board.push(move)
evaluation_list.append(str(evaluate.evaluate(temp_board.fen())))
return move_list, evaluation_list
def set_table_content(move_list, evaluation_list): # set all the moves into the table
count = 0
for move, evaluation in zip(move_list, evaluation_list):
move_item = QTableWidgetItem(str(move))
tableWidget.setItem(count, 0, move_item)
eval_item = QTableWidgetItem(evaluation)
tableWidget.setItem(count, 1, eval_item)
move_item.setTextAlignment(QtCore.Qt.AlignCenter)
eval_item.setTextAlignment(QtCore.Qt.AlignCenter)
count += 1
def create_game(text):
# Allow the user to enter the PGN notation and convert into game
pgn = io.StringIO(text)
game = chess.pgn.read_game(pgn)
return game
def input_game(text): # retrieve PGN notation and convert into move list and evaluation list
global input_move_list, final_evaluation_list, final_move_list, input_text1, input_text2
while True:
try:
input_move_list.append(text)
input_text1 = " ".join(map(str, input_move_list))
game = create_game(input_text1)
input_text1 = str(game.mainline_moves())
board = game.board()
writefile(board)
final_move_list, final_evaluation_list = generate_move_and_eval_list(game)
input_text2 = ", ". join(map(str, final_evaluation_list))
return input_text1, input_text2
break
except:
text = "Illegal move"
return text
class App(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
global i, board_widget, tableWidget, board, final_move_list, final_evaluation_list, input_text
i = 0
# pgn = open("database/lichess_db_standard_rated_2013-01.pgn")
# game = chess.pgn.read_game(pgn)
# input_text1 = "e4"
game = create_game(input_text1)
board = game.board()
writefile(board)
final_move_list, final_evaluation_list = generate_move_and_eval_list(game)
main_layout = QVBoxLayout() # set main layout to have a Vertical layout
top_section = QHBoxLayout() # set top_section to have a Horizontal layout
table_section = QHBoxLayout() # set table_section to have a Horizontal layout
upper_section = QHBoxLayout() # set upper section to have a Horizontal layout
lower_section = QHBoxLayout() # set lower section to have a Horizontal layout
button_section = QHBoxLayout() # set button section to have a Horizontal layout
self.l1 = QLabel("Enter PGN notation:") # create a label
self.l1.setFont(QFont('Arial', 10)) # set font and size for l1
self.t1 = QLineEdit() # create a textbox for user input
self.b1 = QPushButton("Enter") # create a button
self.b1.clicked.connect(self.on_click) # click button event
self.l2 = QLabel("")
top_section.addWidget(self.l1) # add the widgets to the top section
top_section.addWidget(self.t1)
top_section.addWidget(self.b1)
top_section.addWidget(self.l2)
self.tableWidget2 = QTableWidget() # initialise table widget 2
self.tableWidget2.hide()
self.board_widget2 = QtSvg.QSvgWidget('Images/Board.svg') # initialise board widget
self.board_widget2.setFixedWidth(460)
self.board_widget2.setFixedHeight(460)
self.board_widget2.hide()
table_section.addWidget(self.board_widget2)
table_section.addWidget(self.tableWidget2)
tableWidget = QTableWidget() # initialise table widget
tableWidget.setRowCount(len(final_move_list)) # set the no of rows for the table
tableWidget.setColumnCount(2) # set the no of columns for the table
tableWidget.setHorizontalHeaderLabels(["Chess Moves", "Evaluation"]) # set column name for the table
tableWidget.setSizeAdjustPolicy(
QAbstractScrollArea.AdjustToContents) # set table size to fit the content perfectly
set_table_content(final_move_list, final_evaluation_list) # set table content using the move list generated
tableWidget.resizeColumnsToContents()
tableWidget.hide()
board_widget = QtSvg.QSvgWidget('Images/Board.svg') # initialise board widget
board_widget.setFixedWidth(460)
board_widget.setFixedHeight(460)
board_widget.hide()
upper_section.addWidget(board_widget) # add the widgets to the upper section
upper_section.addWidget(tableWidget)
self.previous_button = QPushButton() # create a push button for previous move
self.previous_button.setToolTip("Previous move")
self.previous_button.setIcon(QtGui.QIcon("Images/left-arrow.png"))
self.previous_button.clicked.connect(previous_button_clicked) # create a event handler for the previous button
self.previous_button.hide()
self.next_button = QPushButton() # create a push button for next move
self.next_button.setToolTip("Next move")
self.next_button.setIcon(QtGui.QIcon("Images/right-arrow.png"))
self.next_button.clicked.connect(next_button_clicked) # create a event handler for the next button
self.next_button.hide()
lower_section.addWidget(self.previous_button) # add the previous button to the lower section
lower_section.addWidget(self.next_button) # add the next button to the lower section
self.previous_button2 = QPushButton() # create a push button for previous move
self.previous_button2.setToolTip("Previous move")
self.previous_button2.setIcon(QtGui.QIcon("Images/left-arrow.png"))
self.previous_button2.clicked.connect(self.on_previous_click) # create a event handler for the previous button
self.previous_button2.hide()
self.next_button2 = QPushButton() # create a push button for next move
self.next_button2.setToolTip("Next move")
self.next_button2.setIcon(QtGui.QIcon("Images/right-arrow.png"))
self.next_button2.clicked.connect(self.on_next_click) # create a event handler for the next button
self.next_button2.hide()
button_section.addWidget(self.previous_button2) # add the previous button to the lower section
button_section.addWidget(self.next_button2) # add the next button to the lower section
main_layout.addLayout(top_section) # add the top section layout to the main_layout
main_layout.addLayout(table_section) # add the table section layout to the main_layout
main_layout.addLayout(upper_section) # add the upper section layout to the main_layout
main_layout.addLayout(lower_section) # add the lower section layout to the main_layout
main_layout.addLayout(button_section) # add the button section layout to the main_layout
self.setLayout(main_layout) # set the main layout for the main widget
self.setWindowTitle("Chess Board") # set title of the window
self.setFixedWidth(750)
self.setFixedHeight(750)
self.show()
@pyqtSlot()
def on_click(self):
if not board.is_stalemate() and not board.is_insufficient_material() and not board.is_game_over():
textboxValue = self.t1.text() # set user input text into textboxValue
text1, text2 = input_game(textboxValue) # parse user input text into input_game, return move list and evaluation list
self.t1.setText("") # clear textbox t1
self.tableWidget2.setRowCount(len(final_move_list)) # set the no of rows for the table
self.tableWidget2.setColumnCount(2) # set the no of columns for the table
self.tableWidget2.setHorizontalHeaderLabels(["Chess Moves", "Evaluation"]) # set column name for the table
self.tableWidget2.setSizeAdjustPolicy(
QAbstractScrollArea.AdjustToContents) # set table size to fit the content perfectly
# setting the table content
count = 0
for move, evaluation in zip(final_move_list, final_evaluation_list):
move_item = QTableWidgetItem(str(move))
self.tableWidget2.setItem(count, 0, move_item)
eval_item = QTableWidgetItem(evaluation)
self.tableWidget2.setItem(count, 1, eval_item)
move_item.setTextAlignment(QtCore.Qt.AlignCenter)
eval_item.setTextAlignment(QtCore.Qt.AlignCenter)
count += 1
self.tableWidget2.resizeColumnsToContents()
self.tableWidget2.show()
self.board_widget2.show()
self.previous_button2.show()
self.next_button2.show()
# tableWidget.hide() # hide tableWidget
# board_widget.hide() # hide board_widget
self.next_button.hide() # hide next button
self.previous_button.hide() # hide previous button
else:
QMessageBox.question(self, 'Message', "Invalid input", QMessageBox.Ok, QMessageBox.Ok)
@pyqtSlot()
def on_previous_click(self):
global i
if i > 0: # if counter i is more than 0, undo the move and create the svg file
i -= 1
board.pop()
writefile(board)
self.board_widget2.load("Images/board.svg") # Refresh the board widget
else:
pass
@pyqtSlot()
def on_next_click(self):
global i, final_move_list
if i < len(
final_move_list): # if counter i is less than the length of move_list, move the piece and create the svg file
board.push(board.parse_san(final_move_list[i]))
writefile(board)
i += 1
self.board_widget2.load("Images/board.svg") # Refresh the board widget
else:
pass
app = QApplication(sys.argv) # initialise the pyqt application
app.setStyleSheet(qdarkstyle.load_stylesheet()) # load third party dark theme
ex = App()
sys.exit(app.exec_())