-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaster.py
More file actions
357 lines (309 loc) · 11 KB
/
Copy pathmaster.py
File metadata and controls
357 lines (309 loc) · 11 KB
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
import asyncio
import threading
import puppet
import stockfishapi
import parserFEN
import time
import json
import os
import win32file
import win32con
# === MASTER VARIABLES ===
_running=False
StartButton = False
GuiCheckbox1 = True
GuiCheckbox2 = False
TimeCheckBox = False
DepthCheckBox = True
SkillCheckBox = False
ColorSelector0 = "#FF0000"
Slider0 = 1
ColorSelector1 = "#00FF00"
Slider1 = 1
ColorSelector2 = "#0000FF"
Slider2 = 1320
NoBlunder = False
Engine = True
# === GAME STATE VARIABLES ===
lastBoard = None
lastscore=0
Didmove = False
WhoNext = "white"
Moves = 0
Board = None
def update_variables():
global StartButton, GuiCheckbox1, GuiCheckbox2, TimeCheckBox, DepthCheckBox
global SkillCheckBox, ColorSelector0, Slider0,ColorSelector1, Slider1,ColorSelector2, Slider2, Teacher, NoBlunder, Engine
if os.path.exists("gui.txt") and os.path.getsize("gui.txt") > 0:
try:
with open("gui.txt", "r") as f:
lines = f.readlines()
with open("gui.txt", "w") as f:
pass
for line in lines:
try:
line = (
line.strip()
.replace("'", "\"")
.replace("True", "true")
.replace("False", "false")
)
if line.startswith("{") and line.endswith("}"):
entry = json.loads(line)
else:
print(f"Skipping invalid line: {line}")
continue
id_name = entry["id"]
value = entry["value"]
if id_name == "Reload":
ReloadGui()
if id_name == "StartButton":
StartButton = not StartButton
elif id_name == "GuiCheckbox1":
GuiCheckbox1 = bool(value)
GuiCheckbox2 = False
elif id_name == "GuiCheckbox2":
GuiCheckbox2 = bool(value)
GuiCheckbox1 = False
elif id_name == "TimeCheckBox":
TimeCheckBox = bool(value)
DepthCheckBox = False
SkillCheckBox = False
elif id_name == "DepthCheckBox":
DepthCheckBox = bool(value)
TimeCheckBox = False
SkillCheckBox = False
elif id_name == "SkillCheckBox":
SkillCheckBox = bool(value)
TimeCheckBox = False
DepthCheckBox = False
elif id_name == "ColorSelector0":
ColorSelector0 = value
elif id_name == "Slider0":
Slider0 = int(value)
elif id_name == "ColorSelector1":
ColorSelector1 = value
elif id_name == "Slider1":
Slider1 = int(value)
elif id_name == "ColorSelector2":
ColorSelector2 = value
elif id_name == "Slider2":
Slider2 = int(value)
print(Slider2)
elif id_name == "NoBlunder":
NoBlunder = bool(value)
elif id_name == "Engine":
Engine = bool(value)
except json.JSONDecodeError:
print(f"Skipping invalid line: {line}")
except Exception as e:
print(f"Error reading gui.txt: {e}")
def read_file_contents(filepath):
try:
with open(filepath, "r") as f:
return f.read()
except Exception:
return None
#---------------------------------------------------------------------------------------------------------------------
def update_chessboard():
global Didmove,WhoNext,Moves,Board,appstart,_running,GuiCheckbox2,GuiCheckbox1
try:
with open("chessboard.txt", "r") as f:
lines = f.readlines()
if not lines:
#print("⚠️ chessboard.txt is empty.")
return None, None,None, []
didMove_str = lines[0].strip()
if didMove_str=="CLOSE":
_running=False
return None, None, None, []
Didmove = (didMove_str == "True")
WhoNext = lines[1].strip()
Moves = int(lines[2].strip())
Board = [line.strip().split() for line in lines[3:]]
if (Didmove == True):
#RESET CASTLING DACA TREBUIE
Restart = isStartBoard(Board)
if Restart:
parserFEN.ResetCastling()
# Pornesc mutari daca start este on
if StartButton:
DoMove()
return Didmove, WhoNext,Moves ,Board
except FileNotFoundError:
print("❌ chessboard.txt not found.")
return None, None, None, []
stockfish_path = "./stockfish/stockfish-windows-x86-64-avx2.exe"
stockfish = stockfishapi.start_stockfish(stockfish_path)
def isStartBoard(Board):
expected = [
['r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R']
]
expected2 = [
['R', 'N', 'B', 'K', 'Q', 'B', 'N', 'R'],
['P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['.', '.', '.', '.', '.', '.', '.', '.'],
['p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'],
['r', 'n', 'b', 'k', 'q', 'b', 'n', 'r']
]
if Board == expected or Board == expected2:
return True
else:
return False
def ReloadGui():
global StartButton, GuiCheckbox1, GuiCheckbox2, TimeCheckBox, DepthCheckBox
global SkillCheckBox, ColorSelector0, Slider0,ColorSelector1, Slider1,ColorSelector2, Slider2, Teacher, NoBlunder, Engine
print(StartButton)
future = asyncio.run_coroutine_threadsafe(
puppet.reload_gui(StartButton,GuiCheckbox1,GuiCheckbox2,TimeCheckBox,DepthCheckBox,SkillCheckBox,ColorSelector0,Slider0,ColorSelector1,Slider1,ColorSelector2,Slider2,NoBlunder,Engine),
puppet._loop
)
future.result()
def detect_move_simple(lastBoard, Board):
tolet = {
0: "a",
1: "b",
2: "c",
3: "d",
4: "e",
5: "f",
6: "g",
7: "h"
}
piesa = ""
ip,jp,ia,ja = None,None,None,None
rez=""
for i in range(0,8,1):
for j in range(0,8,1):
if Board[i][j]!=lastBoard[i][j]:
if (lastBoard[i][j]!='.' and Board[i][j]=='.'):
ip=i
jp=j
piesa=lastBoard[i][j]
else:
ia=i
ja=j
if ip!=None and jp!=None and ia!=None and ja!=None:
rez=rez+piesa+tolet[jp]+str(8-ip)+tolet[ja]+str(8-ia)
return rez
def DoMove():
global lastscore,GuiCheckbox1,lastBoard
initial_fen = parserFEN.matrix_to_fen(Board, Moves, WhoNext,GuiCheckbox1)
if initial_fen =="8/8/8/8/8/8/8/8 w - - 0 1":
return 0
score,mate_score,pv1,pv2,pv3 = stockfishapi.get_stockfish_score(stockfish,15,initial_fen)
stockfishapi.set_position_fen(stockfish, initial_fen)
movecode="None"
if lastBoard!=None:
movecode = detect_move_simple(lastBoard,Board)
print(initial_fen)
print(score)
deltascore = score-lastscore
future = asyncio.run_coroutine_threadsafe(
puppet.update_gui(initial_fen,Board,score,mate_score,pv1,pv2,pv3,deltascore,Moves,GuiCheckbox1,movecode),
puppet._loop
)
future.result()
best_move = None
if(SkillCheckBox==True):
stockfishapi.set_elo_rating(stockfish,Slider2)
best_move = stockfishapi.get_depth_move(stockfish, 15)
if Engine:
puppet.show_best_move_sync(best_move,GuiCheckbox1,ColorSelector2,1)
else:
stockfishapi.reset_elo_rating(stockfish)
if(DepthCheckBox==True):
best_move = stockfishapi.get_depth_move(stockfish, Slider0)
if Engine:
puppet.show_best_move_sync(best_move,GuiCheckbox1,ColorSelector0,1)
if(TimeCheckBox==True):
best_move = stockfishapi.get_time_move(stockfish, Slider1)
if Engine:
puppet.show_best_move_sync(best_move,GuiCheckbox1,ColorSelector1,1)
print(best_move)
lastscore=score
lastBoard=Board
def reset():
global _running,StartButton,GuiCheckbox1,GuiCheckbox2,TimeCheckBox,DepthCheckBox,SkillCheckBox,ColorSelector0,Slider0,ColorSelector1,Slider1,ColorSelector2,Slider2,Teacher,NoBlunder,Engine,Didmove,WhoNext,Moves,Board
_running=True
StartButton = False
GuiCheckbox1 = True
GuiCheckbox2 = False
TimeCheckBox = False
DepthCheckBox = True
SkillCheckBox = False
ColorSelector0 = "#FF0000"
Slider0 = 1
ColorSelector1 = "#00FF00"
Slider1 = 1
ColorSelector2 = "#0000FF"
Slider2 = 1320
NoBlunder = False
Engine = True
# === GAME STATE VARIABLES ===
Didmove = False
WhoNext = "white"
Moves = 0
Board = None
lastBoard = None
def main():
parserFEN.ResetCastling()
reset()
def run_puppet_in_thread():
asyncio.run(puppet.main())
puppet_thread = threading.Thread(target=run_puppet_in_thread, daemon=True)
puppet_thread.start()
# === FILE CHANGE LISTENER ===
path_to_watch = "."
last_contents = {
"gui.txt": None,
"chessboard.txt": None,
}
hDir = win32file.CreateFile(
path_to_watch,
win32con.GENERIC_READ,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
# ==== ----------------- ====
print("Master did setup!")
global _running
while _running:
results = win32file.ReadDirectoryChangesW(
hDir,
32768*2,
False,
win32con.FILE_NOTIFY_CHANGE_SIZE,
None,
None
)
for action, file in results:
full_filename = os.path.join(path_to_watch, file)
if file in last_contents:
new_content = read_file_contents(full_filename)
if new_content is None:
continue
if new_content != last_contents[file]:
last_contents[file] = new_content
if file == "gui.txt":
update_variables()
elif file == "chessboard.txt":
#time.sleep(0.05)
update_chessboard()
time.sleep(0.2)
_running=True
if __name__ == "__main__":
main()