-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordle_checker.cpp
125 lines (105 loc) · 3.48 KB
/
wordle_checker.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
#include "wordle_checker.h"
#include "wordle_rules.h"
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
using namespace std;
bool WordleChecker::check(WordleGuess& wg, size_t& outNumGuesses) {
if (m_answer.size() != LETTER_COUNT) {
if (DEBUG || DEBUG_UNICODE) cerr << "Error: [checker] answer not correct size" << endl;
throw;
}
if (wg.results.size() > 0) {
if (DEBUG || DEBUG_UNICODE) cerr << "Error: [checker] no results to check" << endl;
throw;
}
if (m_dict.size() == 0) {
if (DEBUG || DEBUG_UNICODE) cerr << "Error: [checker] empty dictionary" << endl;
throw;
}
if (m_dict.find(wg.guessStr) == m_dict.end()) {
if (DEBUG || DEBUG_UNICODE) cerr << "Not in dictionary. Try again." << endl;
return false;
}
auto result = vector<WordleResult>(LETTER_COUNT, WordleResult::BLACK);
resetFrequencyMap();
if (DEBUG && !DEBUG_UNICODE) cout << " ";
for (size_t i = 0; i < LETTER_COUNT; i++) {
if (wg.guessStr[i] == m_answer[i] && m_frequencyMap[wg.guessStr[i]] != 0) {
m_frequencyMap[wg.guessStr[i]]--;
result[i] = WordleResult::GREEN;
}
}
for (size_t i = 0; i < LETTER_COUNT; i++) {
if (wg.guessStr[i] == m_answer[i]) {
// frequencyMap already updated
continue;
} else if (m_answer.find(wg.guessStr[i]) != string::npos && m_frequencyMap[wg.guessStr[i]] != 0) {
m_frequencyMap[wg.guessStr[i]]--;
result[i] = WordleResult::YELLOW;
} else {
result[i] = WordleResult::BLACK;
}
}
for (auto& r : result) {
switch(r) {
case WordleResult::GREEN:
wg.results.push_back(WordleResult::GREEN);
if (DEBUG && !DEBUG_UNICODE) cout << "G";
if (DEBUG_UNICODE) cout << "\360\237\237\251";
break;
case WordleResult::YELLOW:
wg.results.push_back(WordleResult::YELLOW);
if (DEBUG && !DEBUG_UNICODE) cout << "Y";
if (DEBUG_UNICODE) cout << "\360\237\237\250";
break;
case WordleResult::BLACK:
wg.results.push_back(WordleResult::BLACK);
if (DEBUG && !DEBUG_UNICODE) cout << "B";
if (DEBUG_UNICODE) {
if (LIGHT_MODE) cout << "\342\254\234";
else cout << "\342\254\233";
}
break;
}
}
if (DEBUG || DEBUG_UNICODE) cout << endl;
outNumGuesses = ++m_numGuesses;
return true;
}
void WordleChecker::resetFrequencyMap() {
m_frequencyMap.clear();
for (size_t i = 0; i < m_answer.size(); i++) {
if (m_frequencyMap.find(m_answer[i]) == m_frequencyMap.end()) {
m_frequencyMap[m_answer[i]] = 0;
}
m_frequencyMap[m_answer[i]]++;
}
}
bool WordleChecker::setAnswer(string answer) {
if (answer.size() != LETTER_COUNT) {
return false;
}
m_answer = answer;
if (DEBUG) cerr << "Answer set to: [" << answer << "]" << endl;
return true;
}
void WordleChecker::setRandomAnswer() {
srand (time(NULL));
size_t offset = rand() % m_dict.size();
auto it = m_dict.begin();
advance(it, offset);
setAnswer(*it);
}
void WordleChecker::loadDictionary(string filename) {
auto filein = ifstream(filename);
string word;
while (std::getline(filein, word))
{
if (word.size() == LETTER_COUNT) {
m_dict.insert(word);
}
}
}