-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
197 lines (178 loc) · 5.78 KB
/
main.cpp
File metadata and controls
197 lines (178 loc) · 5.78 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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <iterator>
class WordleSolver {
private:
std::vector<std::string> ignoreWords;
std::vector<std::string> board;
public:
WordleSolver() {
board.resize(5, "_");
}
std::vector<std::string> mustHave(const std::vector<std::string> bigList) {
std::string must = getInput("Type in the letters to include but not sure of location: ");
std::vector<std::string> newList;
for (const auto& word : bigList) {
bool flag = true;
for (char ch : must) {
if (word.find(ch) == std::string::npos) {
flag = false;
break;
}
}
if (flag) {
newList.push_back(word);
}
}
return newList;
}
std::vector<std::string> remover(const std::vector<std::string>& bigList, const std::string& ignore) {
std::vector<std::string> newList;
for (const auto& word : bigList) {
bool flag = true;
for (char ch : ignore) {
if (word.find(ch) != std::string::npos) {
flag = false;
break;
}
}
if (flag) {
newList.push_back(word);
}
}
return newList;
}
std::vector<std::string> matcher(const std::vector<std::string>& bigList, const std::vector<std::string>& target) {
std::vector<std::string> results;
for (const auto& word : bigList) {
bool isMatch = true;
for (int i = 0; i < 5; i++) {
if (target[i] != "_" && target[i] != std::string(1, word[i])) {
isMatch = false;
break;
}
}
if (isMatch) {
results.push_back(word);
}
}
return results;
}
std::vector<std::string> letterWords() {
std::vector<std::string> rows;
std::ifstream file("words.txt");
if (!file) {
std::cerr << "Error: words.txt not found\n";
return {};
}
std::string line;
while (std::getline(file, line)) {
if (!line.empty() && line.back() == '\r')
line.pop_back();
if (line.size() == 5)
rows.push_back(line);
}
return rows;
}
void display(const std::vector<std::string>& currentBoard) {
std::cout << "[";
for (size_t i = 0; i < currentBoard.size(); ++i) {
std::cout << currentBoard[i];
if (i < currentBoard.size() - 1) {
std::cout << ", ";
}
}
std::cout << "]";
}
void resetBoard() {
std::fill(board.begin(), board.end(), "_");
}
std::string getInput(const std::string& prompt) {
std::cout << prompt << " ";
std::string input;
std::getline(std::cin, input);
return input;
}
int userEntry(int location) {
std::string initialState = board[location];
board[location] = "=";
display(board);
std::string userInput = getInput("");
if (userInput == " " || userInput.empty()) {
board[location] = initialState;
return location + 1;
} else if (userInput == "0") {
board[location] = initialState;
return 5;
} else if (userInput == "1") {
board[location] = "_";
return std::max(location - 1, 0);
} else {
board[location] = userInput;
return location + 1;
}
}
void initializeBoard() {
int i = 0;
while (i < 5) {
i = userEntry(i);
}
display(board);
std::cout << std::endl;
}
void play() {
resetBoard();
initializeBoard();
while (true) {
std::vector<std::string> rows = letterWords();
std::string ignore = getInput("Type in the letters to ignore (if any): ");
std::istringstream iss(ignore);
ignoreWords = std::vector<std::string>((std::istream_iterator<std::string>(iss)), std::istream_iterator<std::string>());
if (!ignoreWords.empty() && !ignoreWords[0].empty()) {
rows = remover(rows, ignoreWords[0]);
}
std::vector<std::string> results = matcher(rows, board);
results = mustHave(results);
std::cout << "Here are the results of the matches:" << std::endl;
if (results.empty()) {
std::cout << "[]" << std::endl;
} else {
std::cout << "[";
if (results.size() <= 10) {
for (size_t i = 0; i < results.size(); ++i) {
std::cout << results[i];
if (i < results.size() - 1) {
std::cout << ", ";
}
}
} else {
for (size_t i = 0; i < 10; ++i) {
std::cout << results[i];
if (i < 9) {
std::cout << ", ";
}
}
}
std::cout << "]" << std::endl;
}
std::string nextMove = getInput("Do you want to continue (c), reset (r), or exit (e)? ");
if (nextMove == "e") {
break;
} else if (nextMove == "r") {
resetBoard();
initializeBoard();
} else if (nextMove == "c") {
initializeBoard();
}
}
}
};
int main() {
WordleSolver solver;
solver.play();
return 0;
}