-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
148 lines (107 loc) · 4.05 KB
/
main.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
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <string>
#include <vector>
std::ifstream inputFile;
std::string line;
char findBestWord(std::list<std::string> dictionaryWords, std::string word, std::string incorrectWords) {
for (auto it = dictionaryWords.begin(); it != dictionaryWords.end();) {
std::string currentWord = *it;
int currentWordSize = currentWord.length() - 1; //some reason wrong size?
if (currentWordSize != word.length()){
//std::cout << "removed small : " << currentWord << std::endl;
it = dictionaryWords.erase(it);
}else{
bool possibleWord = true;
for (int i = 0; i < word.length(); i++) {
//make sure banned word is not in list
if (incorrectWords.find(currentWord[i]) != std::string::npos) {
possibleWord = false;
break;
}
//if it is a _ ignore it as could be anything
if (word[i] != '_'){
if (currentWord[i] != word[i]){
possibleWord = false;
}
}
}
if (possibleWord == false){
it = dictionaryWords.erase(it);
}else{
++it;
}
}
}
for (auto const &i: dictionaryWords) {
//std::cout << i << std::endl;
}
//get most used letter
std::vector<int> letterCounts(26, 0);
for (const std::string& word : dictionaryWords) {
for (char c : word) {
if (std::islower(c)) {
letterCounts[c - 'a']++;
}
}
}
char mostFrequentLetter = '_';
int maxCount = 0;
for (int i = 0; i < 26; ++i) {
//make sure word is not already done
if (word.find('a' + i) == std::string::npos) {
if (letterCounts[i] > maxCount) {
maxCount = letterCounts[i];
mostFrequentLetter = 'a' + i;
}
}
}
std::cout << "letter to guess: " << mostFrequentLetter << std::endl;
//std::cout << "Count: " << maxCount << std::endl;
return mostFrequentLetter;
}
int main() {
std::cout << "loading word list" << std::endl;
inputFile.open("wordlist.txt");
if (!inputFile.is_open()) {
std::cerr << "Error opening the file." << std::endl;
return 1; // Return an error code
}
std::list<std::string> dictionaryWords = {};
while (std::getline(inputFile, line)) {
dictionaryWords.push_front(line);
}
std::cout << "loaded " << dictionaryWords.size() << " words" << std::endl;
//std::string currentWords = "i__n";
//to use the ai enter in word and words already used here, use _ for not known
std::string word = "";
std::string incorrectWords = "";
char mostCommonLetter = '_';
while (true){
std::cout << std::endl;
std::cout << "Type word state (make all lowercase & unkown is underscore): "; // Type a number and press enter
std::cin >> word;
if (mostCommonLetter != '_'){
if (word.find(mostCommonLetter) == std::string::npos) {
std::cout << mostCommonLetter << " not used, adding to incorrect words list" << std::endl;
incorrectWords.push_back(mostCommonLetter);
}
}
if (word.find('_') == std::string::npos) {
std::cout << "congrats you won!" << std::endl;
return 0;
}
mostCommonLetter = findBestWord(dictionaryWords, word, incorrectWords);
if (mostCommonLetter == '_'){
std::cout << "no words left to guess, game over :(" << std::endl;
return 0;
}
}
}
//todo
//multi word support
//make sure input size is same after inputting new word
// g++ main.cpp -O2 -o builds/hangManAi && x86_64-w64-mingw32-g++ main.cpp -O2 -o builds/hangManAi-64x && i686-w64-mingw32-g++ main.cpp -O2 -o builds/hangManAi-86x
//linux test : g++ main.cpp -O2 -o hangManAi && ./hangManAi