-
Notifications
You must be signed in to change notification settings - Fork 1
/
Wordle_Algo.cpp
174 lines (149 loc) · 4.63 KB
/
Wordle_Algo.cpp
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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
using namespace std;
int vectorToBase3(const vector<int>& vec) {
int result = 0;
int base = 1;
for (int value : vec) {
result += value * base;
base *= 3;
}
return result;
}
int compareWords(const string& target, const string& guess) {
vector<int> result(5, 0);
vector<bool> Matches(5, false);
for (int i = 0; i < 5; ++i) {
if (target[i] == guess[i]) {
result[i] = 2;
Matches[i] = true;
}
}
for (int i = 0; i < 5; ++i) {
if (!Matches[i]) {
for (int j = 0; j < 5; ++j) {
if (!Matches[j] && target[j] == guess[i]) {
result[i] = 1;
Matches[j] = true;
break;
}
}
}
}
return vectorToBase3(result);
}
int t = 0;
void next(vector<int> filter, vector<string> guess_words, vector<vector<int>> comparisonMatrix, string Mystery_Word, int p1) {
if(filter.size() == 1){
cout << guess_words[filter[0]] << endl;
t++;
cout << endl << "Guessed in " << t << " try";
return;
}
int n = guess_words.size();
string previous_guess = guess_words[p1];
int result = compareWords(Mystery_Word, previous_guess);
map<int, double> probability;
vector<pair<double, string>> entropy;
for (int i=0; i < guess_words.size(); i++) {
string guess = guess_words[i];
for(int j=0; j < filter.size(); j++) {
int x = filter[j];
int result = comparisonMatrix[i][x];
probability[result]++;
}
double ent = 0;
for (const auto& prob : probability) {
ent = ent - ((prob.second) * log2(prob.second/n) / n);
}
entropy.push_back(make_pair(ent, guess));
probability.clear();
}
auto next_it = max_element(entropy.begin(), entropy.end());
// cout << next_it->first << endl;
string next_guess = next_it->second;
int p = next_it - entropy.begin();
probability.clear();
entropy.clear();
result = compareWords(Mystery_Word, next_guess);
// Next Filter
vector <int> filter_next;
for(int i=0; i < filter.size(); i++) {
int x = filter[i];
if(comparisonMatrix[x][p] == result){
filter_next.push_back(x);
}
}
if(filter.size() == filter_next.size()) {
next_guess = guess_words[filter[0]];
result = compareWords(Mystery_Word, guess_words[filter[0]]);
filter_next.clear();
for(int i=0; i < filter.size(); i++) {
int x = filter[i];
if(comparisonMatrix[x][filter[0]] == result){
filter_next.push_back(x);
}
}
}
cout << next_guess << endl;
t++;
// Result of Next Guess
if(result == 242){
cout << endl << "Guessed in " << t << " try";
return;
}
next(filter_next, guess_words, comparisonMatrix, Mystery_Word, p);
return;
}
int main() {
string Dictionary = "5_Words.txt";
vector<string> words;
ifstream inputFile(Dictionary);
string word;
while(inputFile >> word) {
words.push_back(word);
}
vector<string> guess_words = words;
int n = words.size();
int m = guess_words.size();
ifstream binaryFile("data_file.bin", ios::binary);
binaryFile.read(reinterpret_cast<char*>(&n), sizeof(int));
binaryFile.read(reinterpret_cast<char*>(&m), sizeof(int));
vector<std::vector<int>> comparisonMatrix(n, std::vector<int>(m));
for (int i = 0; i < n; ++i) {
binaryFile.read(reinterpret_cast<char*>(comparisonMatrix[i].data()), sizeof(int) * m);
}
int p1;
binaryFile.read(reinterpret_cast<char*>(&p1), sizeof(int));
int first_string_length;
binaryFile.read(reinterpret_cast<char*>(&first_string_length), sizeof(int));
string first_string;
char buffer[256];
binaryFile.read(buffer, first_string_length);
first_string.assign(buffer, first_string_length);
binaryFile.close();
// Mystery Word Input
string Mystery_Word;
cin >> Mystery_Word;
// First_Guess
cout << first_string << "(Best Opener)" << endl;
t++;
// Result of First_Guess
int result = compareWords(Mystery_Word, first_string);
if(result == 242){
cout << endl << "Guessed in " << t << " try";
return 0;
}
// Filter
vector <int> filter;
for(int i=0; i < n; i++) {
if(comparisonMatrix[i][p1] == result)
filter.push_back(i);
}
next(filter, guess_words, comparisonMatrix, Mystery_Word, p1);
return 0;
}