-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
134 lines (117 loc) · 4.57 KB
/
Copy pathmain.py
File metadata and controls
134 lines (117 loc) · 4.57 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
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.by import By
from check_is_whatching import is_watching
from pieces_logic import rook, queen, bishop, knight
dictionaryOfPices = {
}
browser = webdriver.Firefox()
browser.get('https://www.chess.com/play/computer')
youre_color = ''
def main():
coordinates = browser.find_element(By.CLASS_NAME, "board")
pieces = coordinates.find_elements(By.CLASS_NAME, "piece")
def getCoordinates(pieces):
tempDict = {}
blackDict = {}
whiteDict = {}
for piece in pieces:
# piece bp square-47 - exemple (4 - horizontal) (7 - vertical)
temp = piece.get_attribute("class").split(" ")
if temp[0] == 'element-pool':
continue
try:
if temp[1][0] == "b":
blackDict[temp[2]] = temp[1][1]
else:
whiteDict[temp[2]] = temp[1][1]
except:
print(temp)
tempDict["black"] = blackDict
tempDict["white"] = whiteDict
return tempDict
def isChanged(pieces):
global dictionaryOfPices, youre_color
compareDict = getCoordinates(pieces)
if compareDict != dictionaryOfPices:
dictionaryOfPices = compareDict
return True
else:
return False
def addHighlight(browser, x, y, color):
data = 'var board1 = document.getElementsByClassName("board")[0]; '
data += 'var el = document.createElement("div"); '
data += 'el.className = "highlight square-' + \
str(x) + str(y) + ' extra-highlight"; '
data += 'el.style.opacity = "0.8"; '
data += 'el.style.background = "' + color + '"; '
data += 'board1.prepend(el); '
browser.execute_script(data)
def removeHighlights(browser):
data = 'var elements = document.getElementsByClassName("extra-highlight"); '
data += 'for (var i = 0; i < elements.length; i++) {'
data += ' elements[i].parentNode.removeChild(elements[i]); }'
browser.execute_script(data)
while True:
if isChanged(pieces):
print('Changed')
newHighlights = []
removeHighlights(browser)
sleep(0.5)
removeHighlights(browser)
list_of_colors_keys = list(dictionaryOfPices.keys())
for color in list_of_colors_keys:
list_of_keys = list(dictionaryOfPices[color].keys())
for key in list_of_keys:
temp = key.split("-")
x = int(temp[1][0])
y = int(temp[1][1])
if dictionaryOfPices[color][key] == "r":
highlights = rook(x, y, color, dictionaryOfPices)
newHighlights.extend(highlights)
elif dictionaryOfPices[color][key] == "q":
highlights = queen(x, y, color, dictionaryOfPices)
newHighlights.extend(highlights)
elif dictionaryOfPices[color][key] == "b":
highlights = bishop(x, y, color, dictionaryOfPices)
newHighlights.extend(highlights)
elif dictionaryOfPices[color][key] == "n":
highlights = knight(x, y, color, dictionaryOfPices)
newHighlights.extend(highlights)
for highlight in newHighlights:
if is_watching(highlight, color, dictionaryOfPices, youre_color) == False:
addHighlight(
browser, highlight['x'], highlight['y'], "red")
newHighlights.remove(highlight)
for highlight in newHighlights:
addHighlight(
browser, highlight['x'], highlight['y'], "yellow")
def setColor(color):
global youre_color
match color:
case "w":
youre_color = "white"
case "b":
youre_color = "black"
case "":
print(youre_color)
case _:
print("Invalid color")
setColor(input("Enter yore color (w/b):"))
def getColor():
global youre_color
match youre_color:
case "white":
setColor(input("Enter yore color (W/b):"))
case "black":
setColor(input("Enter yore color (w/B):"))
case _:
setColor(input("Enter yore color (w/b):"))
getColor()
while True:
try:
main()
sleep(0.5)
except:
print("Catch error")
getColor()