This repository has been archived by the owner on Dec 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAuto-Chess.py
241 lines (225 loc) · 10.6 KB
/
Auto-Chess.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
import chess.engine
import chess.pgn
import os
import time
import asyncio
import pyautogui
import random
import pyscreenshot
from chesstenso import tensorflow_chessbot
from chesstenso import chessboard_finder
from pyclick import HumanClicker
import pytweening
from pyclick.humancurve import HumanCurve
nums = {1:"a", 2:"b", 3:"c", 4:"d", 5:"e", 6:"f", 7:"g", 8:"h"}
def get_uci(board1, board2, who_moved):
str_board = str(board1).split("\n")
str_board2 = str(board2).split("\n")
move = ""
moved_piece = ""
flip = False
leave = False
if who_moved == "w":
for i in range(8)[::-1]:
for x in range(15)[::-1]:
if str_board[i][x] != str_board2[i][x]:
if moved_piece != "" and (str_board[i][x] != moved_piece and str_board2[i][x] != moved_piece):
continue
if str_board[i][x] == "." and move == "":
flip = True
moved_piece = str_board2[i][x]if str_board[i][x] == "."else str_board[i][x]
move+=str(nums.get(round(x/2)+1))+str(9-(i+1))
if len(move) == 4:
leave = True
break
if leave:
break
else:
for i in range(8):
for x in range(15):
if str_board[i][x] != str_board2[i][x]:
if moved_piece != "" and (str_board[i][x] != moved_piece and str_board2[i][x] != moved_piece):
continue
if str_board[i][x] == "." and move == "":
flip = True
moved_piece = str_board2[i][x] if str_board[i][x] == "." else str_board[i][x]
move += str(nums.get(round(x / 2) + 1)) + str(9 - (i + 1))
if len(move) == 4:
leave = True
break
if leave:
break
if flip:
move = move[2]+move[3]+move[0]+move[1]
return move
wait_interval = 0.3 # The wait time between taking screenshots and retrying commands
engine_path = r"Engine Path" # The absolute path to the engine executable
engine = chess.engine.SimpleEngine.popen_uci(engine_path)
engine_think_time = 1 # <----- The higher this value is the better the engine plays, but also the slower it plays
os.chdir('chesstenso')
while 1:
legit = input("Do you want legit mode? (y/n): ")
if legit!="y" and legit!="n":
print("Please type y or n.")
continue
break
while 1:
who = input("Are you playing as white or black?: ")
if who == "white":
who = "w"
other = "b"
flip = False
prev_fen = "IDEK"
elif who == "black":
who = "b"
other = "w"
flip = True
prev_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR"
else:
print("Invalid option given (white/black), please try again.")
continue
break
invalid = 0
board = chess.Board()
first_move = True
while True:
image = pyscreenshot.grab()
image.save("board.png")
try:
result = tensorflow_chessbot.main(img='board.png', active=who, unflip=flip)
accuracy = result[1]
except:
time.sleep(wait_interval)
continue
if str(result[0]).split(" ")[0] == prev_fen:
time.sleep(wait_interval)
continue
board1 = chess.Board(result[0])
if not board1.is_valid():
if invalid >= 10:
print(f"Unable to detect a valid board position... Detected board position with {round(accuracy, 2)}% confidence was:")
print(board1)
break
print("Invalid board position detected, retrying...")
invalid+=1
time.sleep(wait_interval)
continue
invalid = 0
if not first_move:
try:
move_made = get_uci(board, board1, other)
print("Detected move was: " + move_made)
board.push_uci(move_made)
if str(board) != str(board1):
board = chess.Board(result[0])
except:
board = chess.Board(result[0])
else:
first_move = False
board = chess.Board(result[0])
while 1:
try:
result = engine.play(board, chess.engine.Limit(time=engine_think_time))
break
except asyncio.exceptions.TimeoutError:
continue
except chess.engine.EngineTerminatedError:
engine = chess.engine.SimpleEngine.popen_uci(engine_path)
continue
print(f"Detected board position with {round(accuracy, 2)}% confidence:")
print(board)
print("Playing Move: " + str(result.move))
print()
move = result.move
try:
board.push(result.move)
prev_fen = str(board.fen().split(" ")[0])
except:
print("Looks like I got checkmated, how is that even possible?")
break
board_pos = chessboard_finder.main(url=os.path.abspath('board.png'))
board_width = board_pos[2] - board_pos[0]
board_height = board_pos[3] - board_pos[1]
square_mar_wi = board_width / 8
square_mar_he = board_height / 8
if who == "b":
x_square1 = 9 - (ord(str(move)[0]) - 96)
y_square1 = int(str(move)[1])
x_square2 = 9 - (ord(str(move)[2]) - 96)
y_square2 = int(str(move)[3])
else:
x_square1 = ord(str(move)[0]) - 96
y_square1 = 9 - int(str(move)[1])
x_square2 = ord(str(move)[2]) - 96
y_square2 = 9 - int(str(move)[3])
if legit == "y":
time.sleep(random.randint(1,35)/10)
hc = HumanClicker()
curve = HumanCurve(pyautogui.position(), (round(board_pos[0] + (square_mar_wi * x_square1) - square_mar_wi / 2)-random.randint(-13,13),
round(board_pos[1] + (square_mar_he * y_square1) - square_mar_he / 2)-random.randint(-13,13)), distortionFrequency=0, tweening=pytweening.easeInOutQuad,
offsetBoundaryY=8, offsetBoundaryX=8, targetPoints=random.randint(30,40))
hc.move((round(board_pos[0] + (square_mar_wi * x_square1) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * y_square1) - square_mar_he / 2)), duration=0.1, humanCurve=curve)
pyautogui.click()
curve = HumanCurve(pyautogui.position(), (round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2)-random.randint(-5,5),
round(board_pos[1] + (square_mar_he * y_square2) - square_mar_he / 2)-random.randint(-10,10)),
distortionFrequency=0, tweening=pytweening.easeInOutQuad,
offsetBoundaryY=8, offsetBoundaryX=8, targetPoints=random.randint(30,40))
hc.move((round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * y_square2) - square_mar_he / 2)), duration=0.1, humanCurve = curve)
pyautogui.click()
else:
pyautogui.click(round(board_pos[0] + (square_mar_wi * x_square1) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * y_square1) - square_mar_he / 2))
pyautogui.click(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * y_square2) - square_mar_he / 2))
try:
if str(move)[4] == "q":
pyautogui.click()
elif str(move)[4] == "n":
if legit=="y":
curve = HumanCurve(pyautogui.position(),
(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2)-random.randint(-7,7),
round(board_pos[1] + (square_mar_he * (y_square2+1)) - square_mar_he / 2)-random.randint(-7,7)),
distortionFrequency=0, tweening=pytweening.easeInOutQuad,
offsetBoundaryY=8, offsetBoundaryX=8, targetPoints=random.randint(30,40))
hc.move((round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2+1)) - square_mar_he / 2)), duration=0.1,
humanCurve=curve)
pyautogui.click()
else:
pyautogui.click(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2+1)) - square_mar_he / 2))
elif str(move)[4] == "r":
if legit == "y":
curve = HumanCurve(pyautogui.position(),
(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2)-random.randint(-7,7),
round(board_pos[1] + (square_mar_he * (y_square2 + 2)) - square_mar_he / 2)-random.randint(-7,7)),
distortionFrequency=0, tweening=pytweening.easeInOutQuad,
offsetBoundaryY=8, offsetBoundaryX=8, targetPoints=random.randint(30,40))
hc.move((round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2 + 2)) - square_mar_he / 2)), duration=0.1,
humanCurve=curve)
pyautogui.click()
else:
pyautogui.click(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2 + 2)) - square_mar_he / 2))
elif str(move)[4] == "b":
if legit == "y":
curve = HumanCurve(pyautogui.position(),
(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2)-random.randint(-7,7),
round(board_pos[1] + (square_mar_he * (y_square2 + 3)) - square_mar_he / 2)-random.randint(-7,7)),
distortionFrequency=0, tweening=pytweening.easeInOutQuad,
offsetBoundaryY=8, offsetBoundaryX=8, targetPoints=random.randint(30,40))
hc.move((round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2 + 3)) - square_mar_he / 2)), duration=0.1,
humanCurve=curve)
pyautogui.click()
else:
pyautogui.click(round(board_pos[0] + (square_mar_wi * x_square2) - square_mar_wi / 2),
round(board_pos[1] + (square_mar_he * (y_square2 + 3)) - square_mar_he / 2))
except:
pass
if board.is_game_over():
print("Looks like I won again!")
break