-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver_hangman.cpp
386 lines (300 loc) · 9.18 KB
/
solver_hangman.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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <string.h>
#include <ctime>
#include <vector>
#include <new>
using namespace std;
//Stores the dictionary without duplicates.
vector<string> all_words;
//Stores the dictionary.
vector<string> all_words_with_duplicates;
//Stores the "first guess" order of letters (for each length).
vector<vector<char> > finalTable;
//Stores a list of positions of strings (all_words) that are of length i, at a position j, have a charachter k.
vector<vector<vector<vector<int> > > > list;
//Stores the position of the first charater guessed.
int firstCharPos;
//Stores the max word length in the dictionary.
int maxWordLength;
ofstream printSteps;
//Returns true if the word contains any alphabets marked by list.
bool wordContainsLetters(string word, vector<int> list){
int i;
int l = word.size();
for(i = 0; i < l; i++){
if(list[word[i]-'a'] != 0) return true;
}
return false;
}
//Creates the vector finalTable (first guess order of letters).
void createFinalTable(){
string word;
finalTable.resize(maxWordLength+1);
//Storing the final count of letters in the words in the entire dictionary.
vector<vector<int> > alphaList, finalTableAsInt;
alphaList.resize(maxWordLength+1);
finalTableAsInt.resize(maxWordLength+1);
int i, j; //iterator
for(i = 0; i <= maxWordLength; i++){
alphaList[i].resize(26, 0);
finalTableAsInt[i].resize(26, 0);
}
//Storing letters that have already occurred in a word.
vector<int> currentWord;
//To switch rows in the dictionary vector alternatively reading/writing.
int switchFiles = 0;
int switchComplement = 1;
vector<vector<string> > dictionary;
dictionary.resize(2);
dictionary[0] = all_words;
//To terminate when all words have been processed.
bool process = true;
while(process){
process = false;
int len = dictionary[switchFiles].size();
for(int j = 0; j < len; j++){
currentWord.clear();
currentWord.resize(26, 0);
word = dictionary[switchFiles][j];
int l = word.size();
if(wordContainsLetters(word, finalTableAsInt[l])) continue;
dictionary[switchComplement].push_back(word);
process = true;
for(i = 0; i < l; i++){
int ch = word[i]-'a';
if(currentWord[ch] != 0) continue;
alphaList[l][ch]++;
currentWord[ch]++;
}
}
//Finds the best next guess for each word length.
for(i = 1; i <= maxWordLength; i++){
int max = 0, maxVal = alphaList[i][0];
alphaList[i][0] = 0;
for(j = 1; j < 26; j++){
if(alphaList[i][j] > maxVal){
max = j;
maxVal = alphaList[i][max];
}
alphaList[i][j] = 0;
}
if(maxVal == 0) continue;
finalTable[i].push_back((char)('a'+max));
finalTableAsInt[i][max]++;
}
dictionary[switchFiles].clear();
switchFiles = (switchFiles+1)%2;
switchComplement = (switchComplement+1)%2;
}
}
/*
Returns number of positions newly filled by guessed character.
Alters the answer being printed to new state.
Alters the guess vector according to the new guesses.
*/
int fillUpWordAccToGuess(char guess, string wordToGuess, char *answer, vector<int> &guessed){
int l = wordToGuess.size();
int numOfGuesses = 0;
for(int i = 0; i < l; i++){
if(guess == wordToGuess[i]){
answer[i] = guess;
guessed[i] = 1;
numOfGuesses++;
firstCharPos = i;
}
}
return numOfGuesses;
}
/*
Creates all_words and all_words_with_duplicates lists from the dictionary given.
Also creates the list vector for fast access.
*/
void makeVector(){
string word, prevWord = "";
//to calculate the max word length and store values into all_word vectors
while(cin >> word){
all_words_with_duplicates.push_back(word);
if(word == prevWord) continue;
prevWord = word;
all_words.push_back(word);
int l = word.size();
if(maxWordLength < l) maxWordLength = l;
}
list.resize(maxWordLength+1);
int i, j;
for(i = 1; i <= maxWordLength; i++){
list[i].resize(i);
for(int j = 0; j < i; j++){
list[i][j].resize(26);
}
}
int l = all_words.size();
for(i = 0; i < l; i++){
string str_word = all_words[i];
int len = str_word.size();
for(j = 0; j < len; j++){
list[len][j][str_word[j]-'a'].push_back(i);
}
}
}
//Returns the string list asked (by specifying length, postion known and character at that position).
vector<string> createDictionaryCopy(int l, int pos, int ch){
vector<string> copyListAsked;
int len = list[l][pos][ch].size();
for(int i = 0; i < len; i++){
copyListAsked.push_back(all_words[list[l][pos][ch][i]]);
}
return copyListAsked;
}
//Returns true if word being tested matches already guessed pattern and does not contain misses.
bool wordMatches(string word, string guessWord, vector<int> &guessed, vector<int> &misses){
int l = word.size();
for(int i = 0 ; i < l; i++){
if(guessed[i] == 1){
if(word[i] != guessWord[i]) return false;
}
else if(misses[word[i]-'a'] == 1) return false;
}
return true;
}
/*
Solves hangman for each word.
Uses finalTable already generated and conditional probablity to make further guesses.
*/
int solver(string guessWord){
printSteps <<"Word to guess : "<<guessWord<<endl;
firstCharPos = -1;
//Used to generate further guesses after first guess.
vector<vector<string> > dictionary;
dictionary.resize(2);
//Length of word to guess.
int l = guessWord.size();
//Stores the state of the word being guessed.
char *answer = (char*)malloc(l+1);
//Stores the characters that were missed to print.
string missPrint;
for(int i = 0; i < l; i++){
answer[i] = '_';
}
answer[l] = '\0';
//Stores the alphabets hit and missed.
vector<int> misses, hits;
misses.resize(26, 0);
hits.resize(26, 0);
//Stores the positions that have been guessed.
vector<int> guessesPos;
guessesPos.resize(l, 0);
int guessResult;
//Stores total guessed and total missed to terminate / end word processing when needed.
int totalGuessed = 0, totalMisses = 0;
int i; //iterator
int tableLength = finalTable[l].size();
//Guesses the first letter in the unknown word.
for(i = 0; i < tableLength && totalMisses < 6; i++){
guessResult = fillUpWordAccToGuess(finalTable[l][i], guessWord, answer, guessesPos);
printSteps <<"Guess : "<<finalTable[l][i]<<endl;
if(guessResult){
totalGuessed += guessResult;
hits[finalTable[l][i]-'a']++;
printSteps <<answer<<endl;
printSteps <<"Misses : "<<missPrint<<endl;
printSteps <<"-------------------------------"<<endl;
break;
}
missPrint += finalTable[l][i];
totalMisses++;
misses[finalTable[l][i]-'a']++;
printSteps <<answer<<endl;
printSteps <<"Misses : "<<missPrint<<endl;
printSteps <<"-------------------------------"<<endl;
}
if(totalMisses < 6) dictionary[0] = createDictionaryCopy(l, firstCharPos, guessWord[firstCharPos]-'a');
string word, prevWord = "";
int switchFiles = 0;
int switchComplement = 1;
//Same work as createFinalTable().
while(totalGuessed < l && totalMisses < 6){
vector<int> alphaList;
alphaList.resize(26, 0);
vector<int> currentWord;
int j;
int len = dictionary[switchFiles].size();
for(j = 0; j < len; j++){
currentWord.clear();
currentWord.resize(26, 0);
word = dictionary[switchFiles][j];
int l = word.size();
if(!wordMatches(word, guessWord, guessesPos, misses)) continue;
//write into file which will be read next
dictionary[switchComplement].push_back(word);
for(i = 0; i < l; i++){
int ch = word[i]-'a';
if(currentWord[ch] != 0) continue;
alphaList[ch]++;
currentWord[ch]++;
}
}
int max = -1, maxVal = -1;
for(i = 0; i < 26; i++){
if(alphaList[i] > maxVal && hits[i] == 0){
max = i;
maxVal = alphaList[max];
}
alphaList[i] = 0;
}
dictionary[switchFiles].clear();
switchFiles = (switchFiles+1)%2;
switchComplement = (switchComplement+1)%2;
if(maxVal <= 0){
continue;
}
guessResult = fillUpWordAccToGuess((char)('a'+max), guessWord, answer, guessesPos);
printSteps <<"Guess : "<<(char)('a'+max)<<endl;
if(guessResult > 0){
totalGuessed += guessResult;
hits[max]++;
printSteps <<answer<<endl;
printSteps <<"Misses : "<<missPrint<<endl;
}
else{
missPrint += (char)('a'+max);
misses[max]++;
totalMisses++;
printSteps <<answer<<endl;
printSteps <<"Misses : "<<missPrint<<endl;
}
printSteps<<"-------------------------------"<<endl;
}
//Returns 1 if the word was correctly guessed before having 6 misses else 0.
if(totalGuessed == l){
printSteps<<"Solved"<<endl<<"-------------------------------"<<endl<<"-------------------------------"<<endl;
return 1;
}
else{
printSteps<<"Failed"<<endl<<"-------------------------------"<<endl<<"-------------------------------"<<endl;
return 0;
}
}
int main(void){
clock_t start_time = clock();
maxWordLength = 0;
makeVector();
createFinalTable();
printSteps.open("output.txt");
int correctlyGuessed = 0;
int i, len = all_words_with_duplicates.size();
for(i = 0; i < len; i++){
int solved = solver(all_words_with_duplicates[i]);
if(solved) correctlyGuessed ++;
}
printSteps.close();
clock_t end_time = clock();
cout<<"Number of words tested : "<<len<<endl;
cout<<"Number of words correctly guessed : "<<correctlyGuessed<<endl;
cout<<"Accuracy in guessing : "<<((correctlyGuessed*100.0)/len)<<endl;
cout<<"Time taken : "<<((end_time - start_time)/CLOCKS_PER_SEC)<<endl;
return 0;
}