-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle_helpers.h
169 lines (138 loc) · 4.84 KB
/
wordle_helpers.h
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
#pragma once
#include "wordle_rules.h"
#include "wordle_solver.h"
#include "wordlist_wordle_solver.h"
#include <algorithm>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
inline size_t countOccurs(char letter, const string& word) {
return std::count(word.begin(), word.end(), letter);
}
class Helpers {
public:
static unique_ptr<WordleSolverImpl> createWordleSolver(const string& solverType) {
unique_ptr<WordleSolverImpl> solverPtr;
if (solverType == "trie") {
solverPtr = make_unique<TrieBasedWordleSolver>();
} else if (solverType == "wordlist") {
solverPtr = make_unique<WordlistWordleSolver>();
// } else {
// printUsage();
}
return solverPtr;
}
static WordleGuess promptUserToCheckGuess(const string& output, size_t guessNumber) {
cout << "Guess #" << guessNumber << ": " << output << endl;
string input = collectUserInput(/*isRepeat=*/false, "checking");
while(!isUserInputValidAnswer(input)) {
input = collectUserInput(/*isRepeat=*/true, "checking");
}
return stringToWordleGuess(output, input);
}
static WordleGuess promptUserToProvideGuess(size_t guessNumber) {
cout << "Guess #" << guessNumber << ": " << endl;
string input = collectUserInput(/*isRepeat=*/false, "checking");
while(!isUserInputValidAnswer(input)) {
input = collectUserInput(/*isRepeat=*/true, "checking");
}
return stringToWordleGuess("", input);
}
static string promptUserToMakeGuess(size_t guessNumber) {
cout << "Guess #" << guessNumber << endl;
string input = collectUserInput(/*isRepeat=*/false, "guessing");
while(!isUserInputValidGuess(input)) {
input = collectUserInput(/*isRepeat=*/true, "guessing");
}
return input;
}
static string promptUserForSolution() {
cout << "Enter Solution:" << endl;
string input = collectUserInput(/*isRepeat=*/false, "solution");
while(!isUserInputValidGuess(input)) {
input = collectUserInput(/*isRepeat=*/true, "solution");
}
cout << "Solution received:" << input << endl;
return input;
}
static vector<string> getDictionary(const string& filename=DICTIONARY_FILENAME) {
auto filein = ifstream(filename);
vector<string> words;
string word;
while (std::getline(filein, word)) { words.push_back(word); }
return words;
}
static bool isWordInDictionary(const string& word, const vector<string>& dict) {
return find(dict.begin(), dict.end(), word) != dict.end();
}
static bool isWordInDictionary(const string& word) {
return isWordInDictionary(word, getDictionary());
}
private:
static WordleGuess stringToWordleGuess(const string& guess, const string& str) {
vector<WordleResult> wr;
for (auto& s : str) {
switch(tolower(s)) {
case 'g':
wr.push_back(WordleResult::GREEN);
break;
case 'y':
wr.push_back(WordleResult::YELLOW);
break;
case 'b':
wr.push_back(WordleResult::BLACK);
break;
default:
if (DEBUG) {
cerr << "Error: [helpers] invalid wordle answer" << endl;
}
throw;
}
}
return WordleGuess(guess, wr);
}
static string collectUserInput(bool isRepeat, const string& promptString) {
if (isRepeat) {
cout << "Invalid response. Try again." << endl;
}
cout << promptString << "> ";
string userInput;
cin >> userInput;
return userInput;
}
static bool isUserInputValidGuess(string& userInput) {
if (userInput.size() != LETTER_COUNT) {
return false;
}
for (size_t i = 0; i < userInput.size(); i++) {
userInput[i] = tolower(userInput[i]);
if (!isalpha(userInput[i])) {
return false;
}
}
return true;
}
static bool isUserInputValidAnswer(const string& userInput, const vector<string>& dict = {}) {
if (userInput.size() != LETTER_COUNT) {
return false;
}
for (auto& s_any_case : userInput) {
char s = tolower(s_any_case);
if (!(s == 'g' || s == 'y' || s == 'b')) {
return false;
}
}
if (dict.size() > 0) {
bool isWord = isWordInDictionary(userInput, dict);
if (!isWord) {
cerr << "Error: Not in word list." << endl;
}
return isWord;
} else {
return true;
}
}
};