-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchess.com RAPID FINAL.py
262 lines (207 loc) · 7.44 KB
/
chess.com RAPID FINAL.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
from stockfish import Stockfish
import chess
import time
import pyautogui as pp
import chess.engine
import keyboard
import tensorflow_chessbot as ttt
from datetime import datetime
global fen_play
pp.FAILSAFE = True
global chessboard
global my_fen
global k
# brave 100% theater mode
stockfish_path = r"C:\stockfish-windows-x86-64-avx2\stockfish\stockfish-windows-x86-64-avx2.exe"
stockfish = Stockfish(path=stockfish_path)
stockfish.reset_engine_parameters()
stockfish.set_skill_level(12)
stockfish.set_depth(8)
#stockfish.set_elo_rating(1300)
print("StockFish READYY!!!!")
pp.alert("Proceeddd??")
def get_SS():
pp.screenshot(r"D:\CODEE\Hackathon\#PASHA CHESS\tensorflow_chessbot-chessfenbot\input.png",
region=(300, 150, 810, 800))
try:
return ttt.mymain() # find fen of the board
except:
pp.alert("Chess Board is not in focus . make sure it is there in tthe vieww!!!")
time.sleep(2)
pp.screenshot(r"D:\CODEE\Hackathon\#PASHA CHESS\tensorflow_chessbot-chessfenbot\input.png",
region=(300, 150, 810, 800))
return ttt.mymain()
def flip():
# pp.moveTo(1130, 234)
time.sleep(2)
pp.click(1130, 234)
def is_red():
if pp.pixel(63, 463)[0] in range(170, 176):
return 1
else:
return 0
def get_all_the_pos():
xx = 353
yy = 153
global chessboard
chessboard = {}
positions = []
# defining keys
for n in range(1, 9):
for alpha in "abcdefgh":
keyy = alpha + str(n)
chessboard[keyy] = (0, 0)
# defining x y pos
for i in range(8):
xx = 353
for j in range(8):
# row
positions.append((xx, yy))
# time.sleep(0.2)
xx += 106
yy += 106
time.sleep(2)
# map all the loc to keys
loc = 0
for n in range(8, 0, -1):
for alpha in "abcdefgh":
keyy = alpha + str(n)
chessboard[keyy] = positions[loc]
loc += 1
# down withe a8 , b8 ..
# X: 361 Y: 898 RGB: (248, 248, 248)
if pp.pixel(361, 901)[1] == 255:
print("DOWN WHITE")
global fen_play
fen_play = 'w'
loc = 0
for n in range(8, 0, -1):
for alpha in "abcdefgh":
keyy = alpha + str(n)
chessboard[keyy] = positions[loc]
loc += 1
# down_black h1 , g1 ...
# X: 364 Y: 901 RGB: ( 86, 83, 82)
if pp.pixel(361, 901)[1] == 0:
loc = 0
print("DOWN BLACK so flipping !! ")
# X: 1130 Y: 234
flip()
fen_play = "b"
return chessboard
def move_on_chess(move):
a = move[0:2]
b = move[2:4]
# if(True):
if (a not in chessboard.keys()) or (b not in chessboard.keys()):
pp.alert(a + " to " + b + "\n You only move")
else:
pp.click(chessboard[a])
pp.sleep(0.2)
pp.click(chessboard[b])
print(f"MOVED : {a} ---> {b}")
# else:
# pp.alert(a + " to " + b + "\n is the BEST MOVE")
def juts_best_move():
board = chess.Board(my_fen)
stockfish.set_fen_position(my_fen)
bm = str(stockfish.get_best_move())
return bm
def find_best_move():
global k
k += 1
board = chess.Board(my_fen)
print(board)
stockfish.set_fen_position(my_fen)
if(k%10==0):
# top 5 movess
five_top_moves = stockfish.get_top_moves(3)
bm = [x['Move'] for x in five_top_moves][1]
else:
bm = str(stockfish.get_best_move())
print(bm)
return bm
def whiteorblack():
if pp.pixel(361, 901)[2] == 255: # white
return 255
if pp.pixel(361, 901)[2] == 0: # black
return 255
# ------------------------MAIN----------------------------
location_ka_board = get_all_the_pos() # get all locations once
print(location_ka_board)
global fen_play
print(f"fen play == {fen_play}")
option = "OK"
while True:
if option == "OK":
global k
k = 0
lis = list(range(0, 10, 2))
sleep_time_loc = 0
if fen_play == 'b':
colour = 36
while not keyboard.is_pressed('q'):
# X: 100 Y: 490 RGB: ( 43, 41, 38) - off ours black when witched
# X: 101 Y: 486 RGB: ( 38, 36, 33) - ours onn
# (173, 31, 36) - red colour last 1 min
# 38, 36, 33)
if (is_red()):
my_fen = str(get_SS())
my_fen = my_fen.replace('w', 'b')
best_move = juts_best_move()
move_on_chess(best_move)
if pp.pixel(63, 463)[1] in range(colour-3 , colour+3): # TIMER
time.sleep(2)
my_fen = str(get_SS()) # take ss and get FEN
print(f"fen play is {fen_play}")
my_fen = my_fen.replace('w', 'b')
print(f"GOT FEN! {my_fen}")
best_move = find_best_move() # find best move
# sleeeping condition here
if sleep_time_loc>4:
sleep_time_loc=0
sleep_time = lis[sleep_time_loc]
sleep_time_loc += 1
time.sleep(sleep_time)
move_on_chess(best_move) # move that shitt
print("---------------------------------------------------")
if fen_play == 'w':
colour = 255
time.sleep(1)
my_fen = str(get_SS()) # take ss and get FEN
my_fen = my_fen.replace('w', fen_play)
print(f"GOT FEN!: {my_fen}")
best_move = find_best_move()
move_on_chess(best_move) # move that shitt
print("---------------------------------------------------")
while not keyboard.is_pressed('q'):
# X: 61 Y: 595 RGB: (255, 255, 255) - pure white
if (is_red()):
my_fen = str(get_SS())
my_fen = my_fen.replace('w', 'w')
best_move = juts_best_move()
move_on_chess(best_move)
if pp.pixel(61, 592)[1] in range(colour - 3, colour + 3): # TIMER
time.sleep(2)
my_fen = str(get_SS()) # take ss and get FEN
print(f"fen play is {fen_play}")
my_fen = my_fen.replace('w', 'w')
print(f"GOT FEN! {my_fen}")
# sleeeping condition here
if sleep_time_loc>4:
sleep_time_loc=0
sleep_time = lis[sleep_time_loc]
sleep_time_loc += 1
time.sleep(sleep_time)
best_move = find_best_move() # find best move
move_on_chess(best_move) # move that shitt
print("---------------------------------------------------")
print("done babyy")
option = pp.confirm("Wanna Play Again ??")
if option == "Cancel":
exit()
time.sleep(3)
location_ka_board = get_all_the_pos() # get all locations once
print("------------------------NEXT-MATCH--------------------------")
print(location_ka_board)
colour = whiteorblack()