-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.cpp
More file actions
195 lines (167 loc) · 5.34 KB
/
Copy pathtest.cpp
File metadata and controls
195 lines (167 loc) · 5.34 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <cmath>
#include <random>
using namespace std;
vector<string> test(const vector<string>& words, size_t numElements) {
random_device rd;
mt19937 gen(rd());
vector<string> test_words = words;
shuffle(test_words.begin(), test_words.end(), gen);
test_words.resize(numElements);
return test_words;
}
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 l = 0;
int q_value(vector<int> filter, vector<string> guess_words, vector<vector<int>> comparisonMatrix, string Mystery_Word, int p1) {
if(filter.size() == 1){
l++;
return l;
}
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);
}
}
}
l++;
// Result of Next Guess
if(result == 242){
return l;
}
q_value(filter_next, guess_words, comparisonMatrix, Mystery_Word, p);
return l;
}
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();
//test_words size
int test_words_size;
cin >> test_words_size;
// Creating a Random vector of Strings for Testing
vector<string> test_words = test(words, test_words_size);
int S = 0;
int win = 0;
cout << "Mystery_Word" << "\t" << "No of Tries\n\n";
for(int i=0; i<test_words.size(); i++){
string Mystery_Word = test_words[i];
l++;
// Result of First_Guess
int result = compareWords(Mystery_Word, first_string);
if(result == 242){
continue;
}
// Filter
vector <int> filter;
for(int i=0; i < n; i++) {
if(comparisonMatrix[i][p1] == result)
filter.push_back(i);
}
S += q_value(filter, guess_words, comparisonMatrix, Mystery_Word, p1);
if(l <= 6)
win++;
cout << "\t" << Mystery_Word << "\t\t\t" << l << endl;
l = 0;
}
cout << endl;
cout << "Correct Guess within 6 tries- " << win << endl;
cout << "Success Rate- " << double(win)/(test_words.size()) * 100 << "%" << endl;
cout << "Average Tries- " << double(S)/(test_words.size()) << endl;
return 0;
}