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
| import socket
import random
import json
import time
from collections import Counter
with open("5-letter-word.json", "r", encoding="utf-8") as f:
word_list = json.load(f)
# word_list = [word for word in word_list if len(word) == 5]
# with open("5-letter-word.json", "w", encoding="utf-8") as f:
# json.dump(word_list, f)
def connect_to_wordle_server():
"""连接到Wordle服务器"""
host = 'challenges.hazmat.buptmerak.cn'
port = 21473
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port)) # 连接到服务器
return sock
class WordleSovler():
def __init__(self, all_word):
self.all_word = all_word
self.possible = all_word
self.presence = set()
self.presence_letter = set()
self.vague = set()
self.absence = set()
def update_dict(self, guess, feedback):
for ind, letter in enumerate(guess):
if feedback[ind] == "O":
# print(f"{letter}确定")
self.presence.add((letter,ind))
self.presence_letter.add(letter)
elif feedback[ind] == "-":
# print(f"{letter}存在")
self.vague.add((letter,ind))
self.presence_letter.add(letter)
elif feedback[ind] == "X" and letter not in self.presence_letter:
# print(f"{letter}不存在")
self.absence.add(letter)
self.absence_copy = self.absence.copy()
for letter in self.absence_copy:
if letter in self.presence_letter:
self.absence.remove(letter)
# print(f"{self.presence=}")
# print(f"{self.vague=}")
# print(f"{self.absence=}")
def is_contain_absence_letters(self, word):
for c in word:
if c in self.absence:
return True
return False
def is_match_presence(self,word):
for letter, index in self.presence:
if word[index] != letter:
return False
return True
def is_match_vague(self,word):
for letter, index in self.vague:
if word[index] == letter or letter not in word:
return False
return True
def update_list(self):
new_list = []
for word in self.possible:
if self.is_contain_absence_letters(word) or not self.is_match_presence(
word) or not self.is_match_vague(word):
continue
new_list.append(word)
self.possible = new_list
def get_untried(self,words):
counter = Counter()
tried = self.absence | self.presence_letter
all_letter = set(chr(ord('a')+i) for i in range(26))
untried = all_letter - tried
for w in words:
for c in w:
if c in untried:
counter[c] += 1
return counter
def get_freq(self, words):
counter = Counter()
for w in words:
for c in w:
counter[c] += 1
return counter
def generator(self, attempt):
# self.possible.sort(key=lambda x:-len(set(x)))
# return random.choice(self.possible[:50])
untried = self.get_untried(self.possible)
freq = self.get_freq(self.possible)
if len(untried) > 1 and attempt < 5:
word_with_score = []
word_list = self.all_word
for word in word_list:
letters = set(word)
untried_score = sum(untried[c] if c in untried else 0 for c in letters)
freq_score = sum(freq[c] if c in freq else 0 for c in letters)
word_with_score.append((word,untried_score,freq_score))
ranked_words = sorted(word_with_score,
key=lambda item:(-item[1],-item[2],item[0]))
guess = ranked_words[0][0]
else:
guess = \
sorted(self.possible,
key=lambda word: (-len(set(word)), -sum(freq[c] for c in word), word))[0]
return guess
def update(self, guess, feedback):
self.update_dict(guess, feedback)
self.update_list()
# print(f"after update:{len(self.possible)}")
def reset(self):
self.possible = self.all_word
self.presence = set()
self.presence_letter = set()
self.vague = set()
self.absence = set()
solver = WordleSovler(word_list)
def play_one_time(sock:socket, solver:WordleSovler,win):
if win == 94:
sock.sendall(b"every")
print(f"msg:{sock.recv(4096).decode()}") # round消息
return True
if win == 95:
sock.sendall(b"smile")
print(f"msg:{sock.recv(4096).decode()}") # round消息
return True
if win == 96:
sock.sendall(b"shows")
print(f"msg:{sock.recv(4096).decode()}") # round消息
return True
if win == 97:
sock.sendall(b"great")
print(f"msg:{sock.recv(4096).decode()}") # round消息
return True
if win == 98:
sock.sendall(b"power")
print(f"msg:{sock.recv(4096).decode()}") # round消息
return True
attempt = 0
while attempt < 6:
print(f"msg:{sock.recv(4096).decode()}") # round消息
guess = solver.generator(attempt)
print(f"try:{guess}")
sock.sendall(guess.encode())
feedback = sock.recv(4096).decode().strip() #XXOO-
print(feedback)
# print(2)
if "OOOOO" in feedback:
return True
if "You lost." in feedback:
return False
if "Word not found!" in feedback:
try:
solver.all_word.remove(guess)
solver.possible.remove(guess)
except Exception:
pass
continue
solver.update(guess, feedback)
attempt += 1
sock = connect_to_wordle_server()
feedback = sock.recv(4096).decode()
print(f"初次连接:\n{feedback}")
win = 0
try:
while win < 100:
print(f"{win=}")
if play_one_time(sock, solver,win):
solver.reset()
win += 1
print(f"msg:{sock.recv(4096).decode()}") # 不要错过flag消息
print(f"msg:{sock.recv(4096).decode()}") # 不要错过flag消息
print(f"msg:{sock.recv(4096).decode()}") # 不要错过flag消息
print(f"msg:{sock.recv(4096).decode()}") # 不要错过flag消息
except Exception as e:
print(f"error:{e}")
sock.close()
|