-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
186 lines (149 loc) · 5.52 KB
/
bot.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
import cv2
import numpy as np
import pyautogui as pg
from time import sleep
from random import randint
import keyboard
case_size = 52
precision = 0.85
# Chargement du dictionnaire
wordlist = []
with open("dict.txt", "r") as f:
for i in range(41147):
wordlist.append(f.read(9)[:-1])
def calibrate():
# Detection du repère
origin = pg.locateOnScreen("calibrage.png", confidence=precision)
return origin
def check(origin, step):
case = (origin.left-110, origin.top-400+case_size*step)
o = []
for i in range(8):
if list(screen[case[1], case[0]])[0] >= 180: o.append(0)
elif list(screen[case[1], case[0]])[2] >= 180: o.append(1)
else: o.append(2)
case = (case[0]+case_size, case[1])
return o
def choose_word(gletters, wletters, bletters, words):
sub_dict = []
dict = words[:]
# Recherche des mots contenants au moins une bonne lettre
for letter in "".join(list(set(wletters+gletters))):
for word in dict:
if letter in word:
sub_dict.append(word)
# Recherche des mots contenants toutes les lettres connues
dict = []
for word in sub_dict:
if sum([1 for l in "".join(wletters+gletters)if l in word]) == len("".join(wletters+gletters)):
dict.append(word)
dict = list(set(dict))
# Tri des mots s'ils contiennent une lettre bannies
sub_dict = []
for word in dict:
if sum([1 for l in word if l in bletters]) == 0:
sub_dict.append(word)
sub_dict = list(set(sub_dict))
# Tri des mots qui n'ont pas leurs lettres aux positions connues
dict = []
for word in sub_dict:
if sum([1 for i in range(len(gletters))if gletters[i]!="" and gletters[i]==word[i]]) == len("".join(gletters)):
dict.append(word)
dict = list(set(dict))
# Tri des mots qui ont une lettre la où elle ne peut être
sub_dict = []
for word in dict:
if sum([1 for l in range(8)if word[l] != wletters[l]]) == 8:
sub_dict.append(word)
dict = list(set(sub_dict))
try:
answer = dict.pop(randint(0, len(dict)-1))
except:
return "NO", dict
return answer, dict
def submit_word(word):
text_calib = pg.locateOnScreen("repere.png", confidence=precision)
pg.moveTo(text_calib.left+172, text_calib.top+128)
pg.leftClick()
pg.write(word, 0.01)
pg.press("enter")
sleep(2)
nb_words = 0
nb_wins = 0
nb_loose = 0
# Boucle principal
while True:
# (RE)LANCEMENT DE LA PARTIE
pg.moveTo(pg.locateOnScreen("calibrage.png", confidence=precision))
pg.leftClick()
nb_words += 1
print("###################################")
print("PARTIES JOUÉES : " + str(nb_words))
print("PARTIES GAGNÉES : " + str(nb_wins))
print("PARTIES PERDUES : " + str(nb_loose))
sleep(2)
step = 0
word = ""
bletters = ""
# Calibrage
screen = pg.screenshot()
screen = cv2.cvtColor(np.array(screen), cv2.COLOR_BGR2RGB)
origin = calibrate()
for i in range(97, 119):
if pg.locateOnScreen(f"lettres/{chr(i)}.png", confidence=0.95, grayscale=True, region=(origin.left-118, origin.top-412, 422, 375)) != None:
first_letter = chr(i).upper()
break
# Recherche d'une solution
answer, dict = choose_word([first_letter], [''for _ in 8*'_'], [], wordlist)
# Écriture de la solution
submit_word(answer)
word = answer[:]
#################################################################################
#################################################################################
#################################################################################
# Boucle de résolution
while True:
sleep(0.5)
# Calibrage
screen = pg.screenshot()
screen = cv2.cvtColor(np.array(screen), cv2.COLOR_BGR2RGB)
origin = calibrate()
# Analyse des couleurs
errors = check(origin, step)
# Vérification de l'état du jeu
if check(origin, 7) == [2 for _ in range(8)]:
nb_loose += 1
break
elif check(origin, 7) == [0 for _ in range(8)]:
nb_wins += 1
break
# Analyse des résultats
gletters = ['' for _ in 8*'_']
wletters = ['' for _ in 8*'_']
for i in range(8):
if errors[i] == 2: gletters[i] = word[i]
elif errors[i] == 1: wletters[i] = word[i]
elif not word[i] in wletters and not word[i] in gletters: bletters += word[i]
# Tri de la liste des lettres bannies
bletters = "".join(list(set(bletters)))
for l in "".join(list(set(gletters+wletters))):
bletters = bletters.replace(l, "")
# Recherche d'une solution
answer, dict = choose_word(gletters, wletters, bletters, dict)
if answer == "NO":
nb_wins += 1
break
# Écriture de la solution
submit_word(answer)
sleep(0.5)
# Prise d'un screen
screen = pg.screenshot()
screen = cv2.cvtColor(np.array(screen), cv2.COLOR_BGR2RGB)
origin = calibrate()
# Vérification de la validité du mot
if check(origin, step+1)[0] == 2:
word = answer[:]
step += 1
# Vérification de la fin du Jeu
if keyboard.is_pressed('escape'):
exit()