-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.cpp
More file actions
261 lines (208 loc) · 8.32 KB
/
index.cpp
File metadata and controls
261 lines (208 loc) · 8.32 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
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <random>
#include <algorithm>
#include <cctype>
#include <unordered_map>
#include <sstream>
#include <curl/curl.h>
#undef min
#undef max
using namespace std;
random_device rd;
mt19937 gen(rd());
string url = "https://gist.githubusercontent.com/cfreshman/a03ef2cba789d8cf00c08f767e0fad7b/raw/c46f451920d5cf6326d550fb2d6abb1642717852/wordle-answers-alphabetical.txt";
size_t writeCallback(void* contents, size_t size, size_t nmemb, void* userp) {
size_t totalSize = size * nmemb;
string* buffer = static_cast<string*>(userp);
buffer->append(static_cast<char*>(contents), totalSize);
return totalSize;
}
void fetchWords(string &responseStr) {
CURL *curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responseStr);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK) {
cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << "\n";
}
curl_easy_cleanup(curl);
}
}
bool validateResult(const string &r) {
if (r.size() != 5) return false;
for (char c : r)
if (c != 'G' && c != 'Y' && c != 'B') return false;
return true;
}
string getFeedback(const string& word, const string& guess) {
string feedback(5, 'B');
unordered_map<char, int> freq;
for (char c : word) freq[c]++;
for (int i = 0; i < 5; i++)
if (word[i] == guess[i]) {
feedback[i] = 'G';
freq[guess[i]]--;
}
for (int i = 0; i < 5; i++)
if (feedback[i] == 'B' && freq.count(guess[i]) && freq[guess[i]] > 0) {
feedback[i] = 'Y';
freq[guess[i]]--;
}
return feedback;
}
void eliminateOptions(vector<string> &words, const string &guess, const string& result) {
words.erase(
remove_if(words.begin(), words.end(),
[&](const string &word) {
return getFeedback(word, guess) != result;
}),
words.end());
}
string guessWord(const vector<string> &words) {
if (words.empty()) return "";
if (words.size() <= 2) return words[0];
string bestWord;
int bestScore = INT_MAX;
const vector<string>& candidates = words;
for (const string &candidate : candidates) {
unordered_map<string, int> partitions;
for (const string &target : words)
partitions[getFeedback(target, candidate)]++;
int worstCase = 0;
for (const auto &p : partitions)
worstCase = max(worstCase, p.second);
if (worstCase < bestScore) {
bestScore = worstCase;
bestWord = candidate;
}
}
return bestWord;
}
int solveForWord(const string &hiddenWord, const vector<string> &allWords) {
vector<string> possibleWords = allWords;
for (int guesses = 0; guesses < 6; ++guesses) {
string guess;
if (guesses == 0) guess = "crane";
else guess = guessWord(possibleWords);
if (guess.empty()) return -1;
if (guess == hiddenWord) return guesses + 1;
string feedback = getFeedback(hiddenWord, guess);
eliminateOptions(possibleWords, guess, feedback);
}
return -1;
}
void playInteractive() {
string fetchResponse;
fetchWords(fetchResponse);
vector<string> words;
stringstream ss(fetchResponse);
string line;
while (getline(ss, line))
if (!line.empty()) words.push_back(line);
cout << "\nThe game begins! Good luck...\n";
for (int guesses = 0; guesses < 6; ++guesses) {
string guess;
if (guesses == 0) {
uniform_int_distribution<int> dist(0, static_cast<int>(words.size()) - 1);
guess = words[dist(gen)];
} else guess = guessWord(words);
if (guess.empty()) {
cout << "I'm out of words! You must have made a mistake in one of your results. Play the game again without making a mistake\n";
return;
}
cout << "\nGuess #" << guesses + 1 << ": " << guess << "\n";
string result;
cout << "Enter the result of the guess (e.g., BGYBB): ";
cin >> result;
transform(result.begin(), result.end(), result.begin(), ::toupper);
if (!validateResult(result)) {
cout << "Invalid Result! Please use 5 characters: G (Green), Y (Yellow), or B (Black/Grey).\n";
return;
}
if (result == "GGGGG") {
cout << "\nI won in " << guesses + 1 << " guesses! HAHA\n";
return;
}
eliminateOptions(words, guess, result);
}
cout << "\nUgh, FINE, I lose. I couldn't guess it in 6 tries :(\n";
}
int main() {
curl_global_init(CURL_GLOBAL_DEFAULT);
int option;
cout << "Hi! I'm a Wordle Solver developed by SinisterDeveloper (https://github.com/SinisterDeveloper)\n\n[1] - New Game\n\n[2] - How to Use\n\n[3] - View Code Repository\n\n[4] - Testing\n\nPick an option(number): ";
cin >> option;
switch (option) {
case 1:
playInteractive();
break;
case 2:
cout << "\n\nI will display the word I guess\n\nUse the word in the official Wordle gane and tell me the result\n\nUse 'G' for a Green square, 'Y' for a Yellow square, and 'B' for a Black/Grey square"
<< "\n\nFor example, if the result is [Green][Yellow][Black][Black][Green], you would type GYBBG and press Enter" << "\n\n";
break;
case 3:
cout << "\n\nThe code for this program is FOSS (Free and Open Source Software)\n\nYou can view the code repository here: https://github.com/SinisterDeveloper/wordle-bot\n\nStar the repository if you like the bot!\n\n";
break;
case 4: {
cout << "Fetching word list for testing...\n";
string fetchResponse;
fetchWords(fetchResponse);
vector<string> allWords;
stringstream ss(fetchResponse);
string line;
while (getline(ss, line)) {
if (!line.empty()) allWords.push_back(line);
}
if (allWords.empty()) {
cerr << "Failed to fetch or parse the word list. Cannot run tests.\n";
return 1;
}
cout << "Starting tests against the fetched word list (" << allWords.size() << " words)...\n";
cout << "This will take some time...\n";
int totalGuesses = 0;
int failures = 0;
int maxGuesses = 0;
string worstWord;
int wordsTested = 0;
for (const auto &hidden : allWords) {
wordsTested++;
cout << "\rTesting [" << wordsTested << "/" << allWords.size() << "]: " << hidden << flush;
int result = solveForWord(hidden, allWords);
if (result == -1) {
++failures;
cout << "\n >> Failed on: " << hidden << '\n';
} else {
totalGuesses += result;
if (result > maxGuesses) {
maxGuesses = result;
worstWord = hidden;
}
}
}
cout << "\n";
int succeeded = static_cast<int>(allWords.size()) - failures;
cout << "\n----------- Testing Complete -----------\n";
cout << "Total Words Tested: " << allWords.size() << '\n';
cout << "Successes: " << succeeded << " (" << (double)succeeded / allWords.size() * 100.0 << "%)\n";
cout << "Failures: " << failures << '\n';
if (succeeded > 0)
cout << "Average Guesses (for successes): " << static_cast<double>(totalGuesses) / succeeded << '\n';
if (!worstWord.empty())
cout << "Hardest Word: " << worstWord << " (" << maxGuesses << " guesses)\n";
break;
}
default:
cout << "Invalid Option Chosen!";
}
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Press Enter to exit...";
cin.get();
curl_global_cleanup();
return 0;
}