-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.py
524 lines (434 loc) · 18.8 KB
/
gui.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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
from functools import partial
from board import Board
import os
import ctypes.util
from tkinter.constants import ANCHOR, N, NSEW,S,E,W
from camera import Camera
import datetime
def set_dll_search_path():
# Python 3.8 no longer searches for DLLs in PATH, so we have to add
# everything in PATH manually. Note that unlike PATH add_dll_directory
# has no defined order, so if there are two cairo DLLs in PATH we
# might get a random one.
if os.name != "nt" or not hasattr(os, "add_dll_directory"):
return
for p in os.environ.get("PATH", "").split(os.pathsep):
try:
os.add_dll_directory(p)
except OSError:
pass
print("Running on os: " + os.name)
if os.name == 'nt':
dllPath = f'{os.path.dirname(os.path.realpath(__file__))}\\bin'
print("Dll path: " + dllPath)
os.environ['path'] += ";" + dllPath
set_dll_search_path()
path = ctypes.util.find_library('libcairo-2')
print("Found libcairo at: " + path)
print ("Total system path = " + os.environ['path'])
from profiler import Profiler
from PIL import ImageTk,Image
import chess
import chess.engine
import chess.svg
from cairosvg import svg2png
from io import BytesIO
from tkinter import Canvas, StringVar, Tk, ttk
import tkinter as tk
from engine import Engine
import time
from equipment import Marker
from collections import deque
from typing import Deque
class ChessClock():
def __init__(self):
self.started = False
self.paused = False
self.canUndo = False
def setAttributes(self, timePerSide,increment):
self.timePerSide = timePerSide
self.increment = increment
def blackButtonPressed(self) -> bool:
if (not self.started):
self.started = True
self.whiteRemaining = self.timePerSide
self.blackRemaning = self.timePerSide
self.whiteEndTime = self.whiteRemaining + time.time()
self.turn = chess.WHITE
self.canUndo = False
return True
else:
finished = self.gameFinished()
if (self.turn == chess.BLACK and not finished and not self.paused):
self.whiteEndTime = self.whiteRemaining + time.time()
self.blackRemaning = (self.blackEndTime - time.time()) + self.increment
self.turn = chess.WHITE
self.canUndo = True
return True
elif finished:
return False
elif self.turn == chess.WHITE:
return False
elif self.paused:
return False
raise RuntimeError("Shouldnt have arrived at this point")
def whiteButtonPressed(self) -> bool:
finished = self.gameFinished()
if (self.started and self.turn == chess.WHITE and not finished and not self.paused):
self.blackEndTime = self.blackRemaning + time.time()
self.whiteRemaining = (self.whiteEndTime - time.time()) + self.increment
self.turn = chess.BLACK
self.canUndo = True
return True
elif not self.started:
return False
elif finished:
return False
elif self.turn == chess.BLACK:
return False
elif self.paused:
return False
raise RuntimeError("Shouldnt have arrived at this point")
def undoButtonPress(self) -> bool:
if (self.canUndo):
if (self.turn == chess.WHITE):
self.turn = chess.BLACK
self.blackEndTime = (self.blackRemaning - self.increment) + time.time()
elif (self.turn == chess.BLACK):
self.turn = chess.WHITE
self.whiteEndTime = (self.whiteRemaining - self.increment) + time.time()
self.canUndo = False
return True
else:
print("Cant undo")
return False
def pause(self) -> bool:
if self.started and not self.gameFinished() and not self.paused:
if (self.turn == chess.WHITE):
self.whiteRemaining = self.whiteEndTime - time.time()
elif (self.turn == chess.BLACK):
self.blackRemaning = self.blackEndTime - time.time()
self.paused = True
return True
else:
return False
def unpause(self) -> bool:
if self.started and not self.gameFinished() and self.paused:
self.paused = False
if self.turn == chess.BLACK:
self.blackEndTime = self.blackRemaning + time.time()
elif self.turn == chess.WHITE:
self.whiteEndTime = self.whiteRemaining + time.time()
return True
else:
return False
def getRemainingTimes(self):
if (not self.started):
return ChessClock.formatTime(self.timePerSide), ChessClock.formatTime(self.timePerSide)
else:
if (self.paused):
blackTime = self.blackRemaning
whiteTime = self.whiteRemaining
elif (self.turn == chess.WHITE):
whiteTime = self.whiteEndTime - time.time()
blackTime = self.blackRemaning
elif (self.turn == chess.BLACK):
whiteTime = self.whiteRemaining
blackTime = self.blackEndTime - time.time()
if (whiteTime <= 0):
whiteTime = 0
if (blackTime <= 0):
blackTime = 0
return ChessClock.formatTime(whiteTime), ChessClock.formatTime(blackTime)
def whiteFinished(self) -> bool:
if (self.started and not self.paused):
if self.turn == chess.WHITE:
whiteTime = self.whiteEndTime - time.time()
return (whiteTime <= 0)
elif self.turn == chess.BLACK:
return self.whiteRemaining <= 0
else:
return False
def blackFinished(self) -> bool:
if (self.started and not self.paused):
if (self.turn == chess.BLACK):
blackTime = self.blackEndTime - time.time()
return (blackTime < 0)
elif (self.turn == chess.WHITE):
return self.blackRemaning <= 0
else:
return False
def gameFinished(self) -> bool:
return self.whiteFinished() or self.blackFinished()
def reset(self):
self.started = False
self.paused = False
self.canUndo = False
def formatTime(time : float):
retval = "NOTHING"
hours = int(time / 3600)
minutes = int((time % 3600) / 60)
seconds = int(time) % 60
tenths = int(time * 10) % 10
if hours > 0:
retval = f'{hours:01}' + ':' + f'{minutes:02}' + ':' + f'{seconds:02}'
elif minutes > 0:
retval = f'{minutes:02}' + ':' + f'{seconds:02}'
else:
retval=f'{minutes:02}' + ':' + f'{seconds:02}' + '.' + f'{tenths:01}'
return retval
class Variant():
def __init__(self, score, depth, pvNum, moves, board : Board):
self.score = score
self.depth = depth
self.pvNum = pvNum
self.moves = moves
self.board = board
#self.score.relative.cp/100
def toString(self):
if (self.score.is_mate()):
score = f'M{self.score.relative.moves}'
else:
score = f'{self.score.relative.cp/100:+}'
retval = f'L{self.pvNum}({self.depth}): {score} {ChessGui.generateForward(self.board, self.moves)}'
return retval
class ChessGui:
def __init__(self):
self.cameras = [] # Camera.listPorts()
def start(self, showDebugButtons = False):
self.restartGameFlag = False
self.changeCameraFlag = -1
self.clock = ChessClock()
self.clock.setAttributes(60*60, 5)
self.doLayout(showDebugButtons)
self.updateClock()
self.root.mainloop()
#pass #self.root.mainloop()
# def doMainLoop(self):
# self.root.mainloop()
def doLayout(self, showDebugButtons):
self.root = Tk()
style = ttk.Style(self.root)
self.root.geometry("800x400")
self.root.configure()
self.whiteRemaining = StringVar()
self.whiteClock = ttk.Label(self.root, font=("Arial", 36), background="white", textvariable=self.whiteRemaining)
self.whiteClock.grid(column=0, row=0, sticky=E, pady=2)
self.blackRemaining = StringVar()
self.blackClock = ttk.Label(self.root, font=("Arial", 36), background="white", textvariable=self.blackRemaining)
self.blackClock.grid(column=2, row=0, sticky=W, pady=2)
if (showDebugButtons):
self.whiteTurn = ttk.Button(self.root, text="White Finished", command=self.whiteButtonPressed)
self.whiteTurn.grid(column=0, row=10, sticky=N, pady=10)
self.blackTurn = ttk.Button(self.root, text="Black Finished", command=self.blackButtonPressed)
self.blackTurn.grid(column=3, row=10, sticky=NSEW, pady=10)
self.undoButton = ttk.Button(self.root, text="Undo", command=self.undoButtonPress)
self.undoButton.grid(column=1, row=10, sticky=S, pady=10)
# self.pauseButton = ttk.Button(self.root, text="Pause", command=self.togglePause)
# self.pauseButton.grid(column=1, row=2, sticky=S, pady=10)
self.historyFrame = ttk.LabelFrame(self.root, text="Turn history")
self.historyFrame.grid(column=0, row=1, rowspan=1, sticky=NSEW)
self.historyString=StringVar()
self.history = ttk.Label(self.historyFrame, textvariable=self.historyString)
self.history.grid(column=0, row=0)
self.engineFrame = ttk.LabelFrame(self.root, text="Engine Analysis")
self.engineFrame.grid(column=2, row=1, rowspan=1, sticky=NSEW)
self.engineString=StringVar()
self.engineLabel = ttk.Label(self.engineFrame, textvariable=self.engineString, wraplength=270)
self.engineLabel.grid(column=0, row=0)
self.canvas = Canvas(self.root, width = 300, height = 300)
self.canvas.grid(column=1,row=0, rowspan=2, columnspan=1, sticky=NSEW, pady=2, padx=2)
#self.canvas.pack()
self.imageContainer = self.canvas.create_image(0, 0, anchor=tk.NW)
self.updateChessBoard(chess.Board.empty())
#self.img = ImageTk.PhotoImage(Image.open("./assets/blank.png"))
menubar = tk.Menu(self.root)
self.gameMenu = tk.Menu(menubar, tearoff=0)
self.gameMenu.add_command(label="New Game", command=self.restartGame)
self.gameMenu.add_command(label="Pause", command=self.togglePause)
menubar.add_cascade(label="Game", menu=self.gameMenu)
self.cameraMenu = tk.Menu(menubar, tearoff=0)
for camera in self.cameras:
handler = partial(self.changeCamera, camera.port)
self.cameraMenu.add_command(label=camera.description(), command=handler)
menubar.add_cascade(label="Camera", menu=self.cameraMenu)
self.clockMenu = tk.Menu(menubar, tearoff=0)
self.clockMenu.add_command(label="Classical (60 | 0)", command=lambda: self.setClock(60 * 60,0))
self.clockMenu.add_command(label="Rapid (15 | 10)", command=lambda: self.setClock(15 * 60, 10))
self.clockMenu.add_command(label="Blitz (3 | 2)", command=lambda: self.setClock(3 * 60, 2))
self.clockMenu.add_command(label="Bullet (2 | 1)", command=lambda: self.setClock(2 * 60, 1))
menubar.add_cascade(label="Clock", menu=self.clockMenu)
self.root.config(menu=menubar)
# self.root.rowconfigure(2, weight=3)
self.root.rowconfigure(1, weight=3)
self.root.columnconfigure(0, weight=3)
self.root.columnconfigure(2, weight=3)
def setClock(self, timePerSide, increment):
print(f'Setting clock to {timePerSide} | {increment}')
self.clock.setAttributes(timePerSide, increment)
def changeCamera(self, port):
print(f'changing camera to: {port}')
self.changeCameraFlag = port
def restartGame(self):
self.restartGameFlag = True
print("setting restartGame to true")
self.clock.reset()
self.updateClock()
self.whiteClock.config(background="white")
self.blackClock.config(background="white")
def whiteButtonPressed(self):
success = self.clock.whiteButtonPressed()
if success:
self.blackClock.config(background="light yellow")
self.whiteClock.config(background="white")
return success
def togglePause(self):
if self.clock.paused:
success = self.clock.unpause()
if success:
self.pauseButton.configure(text="Pause")
self.gameMenu.entryconfigure(1, label="Pause")
elif not self.clock.paused:
success = self.clock.pause()
if success:
self.pauseButton.configure(text="Unpause")
self.gameMenu.entryconfigure(1, label="Unpause")
def blackButtonPressed(self):
success = self.clock.blackButtonPressed()
if success:
self.whiteClock.config(background="light yellow")
self.blackClock.config(background="white")
return success
def undoButtonPress(self):
success = self.clock.undoButtonPress()
if success:
if self.clock.turn == chess.WHITE:
self.whiteClock.config(background="light yellow")
self.blackClock.config(background="white")
elif self.clock.turn == chess.BLACK:
self.blackClock.config(background="light yellow")
self.whiteClock.config(background="white")
return success
def buttonPressed(self, button : Marker):
if button == Marker.WHITE_BUTTON:
return self.whiteButtonPressed()
elif button == Marker.BLACK_BUTTON:
return self.blackButtonPressed()
else:
return False
def updateClock(self):
white, black = self.clock.getRemainingTimes()
self.whiteRemaining.set(white)
self.blackRemaining.set(black)
if (self.clock.whiteFinished()):
self.whiteClock.config(background="red")
if (self.clock.blackFinished()):
self.blackClock.config(background="red")
#self.whiteRemaining, self.blackRemaining = self.clock.getRemainingTimes()
self.root.after(20, self.updateClock)
def updateChessBoard(self, board : chess.Board):
#self.text.set("Scoring...")
self.lastUpdated = datetime.datetime.now()
localUpdated = self.lastUpdated
lastmove = None
if len(board.move_stack) > 0:
lastmove = board.peek()
svgImage = chess.svg.board(board, lastmove=lastmove)
# profiler.log(52, "Generated SVG")
png = svg2png(bytestring=svgImage, output_width=300)
# profiler.log(52, "Made png from svg")
pil_img = Image.open(BytesIO(png))
self.img = ImageTk.PhotoImage(pil_img)
self.canvas.itemconfig(self.imageContainer, image=self.img)
self.historyString.set(ChessGui.generateHistory(board))
exe = "./bin/stockfish_14_x64_avx2.exe"
if os.name != 'nt':
exe = './bin/stockfish_14_x64_avx2'
engine = chess.engine.SimpleEngine.popen_uci(exe)
if (localUpdated == self.lastUpdated and board.is_valid()):
print(f'Starting engine on {board.fen()}')
with engine.analysis(board, multipv=3) as analysis:
pvs = {}
for info in analysis:
if info.get("multipv") is not None:
pvs[info.get("multipv")] = Variant(info.get("score"), info.get("depth"), info.get("multipv"), info.get("pv"), board)
analysis = ""
for key in sorted(pvs.keys()):
analysis += pvs[key].toString() + "\n"
self.engineString.set(analysis)
#print(info.get("score"), info.get("depth"), info.get("multipv"))
#print(info.get("pv"))
#print("********************************")
if info.get("depth", 0) > 20:
print(f'Stopping for {board.fen()}, max depth reached')
break
if localUpdated != self.lastUpdated:
print(f'Stopping for {board.fen()}, board was updated')
break
#print("----------------------------------------------------")
engine.quit()
#profiler.log(53, "Made image")
def generateHistory(board:Board):
moves = board.move_stack
temp = chess.Board()
retval = ""
for ctr, move in enumerate(moves):
if ctr % 2 == 0:
retval += f'{int(ctr/2) + 1}. {temp.san(move)} '
else:
retval += f'{temp.san(move)} \n'
#print(temp.san(move))
temp.push(move)
return retval
def generateForward(board:chess.Board, moves):
temp = board.copy()
retval = ""
history = len(board.move_stack)
first = True
for ctr, move in enumerate(moves):
moveCtr = ctr + history
if moveCtr % 2 == 0:
retval += f'{int(moveCtr/2) + 1}.{temp.san(move)} '
first = False
else:
if first:
retval += f'{int(moveCtr/2) + 1}...'
first = False
retval += f'{temp.san(move)} '
#print(temp.san(move))
temp.push(move)
return retval
if __name__ == "__main__":
pass
# board = chess.Board()
# board.push_uci("e2e4")
# board.push_uci("e7e5")
# board.push_uci("d2d4")
# print(ChessGui.generateHistory(board))
# print(ChessClock.formatTime(300))
# print(ChessClock.formatTime(3725.5))
# print(ChessClock.formatTime(12.1))
# x = 23
# clock = ChessClock()
# clock.setAttributes(300, 5)
# print(clock.getRemainingTimes())
# clock.blackButtonPressed()
# time.sleep(2)
# print(clock.getRemainingTimes())
# clock.whiteButtonPressed()
# time.sleep(3)
# print(clock.getRemainingTimes())
# time.sleep(3)
# print(clock.getRemainingTimes())
c = ChessGui()
c.start(False)
# threading.Thread(target=c.start, args=()).start()
# time.sleep(2)
# board = chess.Board().empty()
# svg = chess.svg.board(board)
# png = svg2png(bytestring=svg, output_width=300)
# pil_img = Image.open(BytesIO(png))
#cv_img = cv2.cvtColor(np.array(pil_img), cv2.COLOR_RGBA2BGRA)
#cv2.imwrite("blank.png", cv_img)
#board.set_piece_at(chess.A1, chess.Piece.from_symbol('q'))
#c.updateChessBoard(svg, Profiler())