-
Notifications
You must be signed in to change notification settings - Fork 0
/
hangman_player.cc
241 lines (201 loc) · 6.72 KB
/
hangman_player.cc
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
/* Hangman player, HPC implementation.
HPC stands for "highest probability character".
GPL Copyright 2016, Nathan Parker
*/
#include "hangman_player.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "hangman_maker.h"
// ------- HPCGame class and methods ---------
typedef std::unordered_set<char> CharSet;
class HPCGame : public HangmanPlayer::Game {
public:
// Constructor w/ precomputed data. Ptr not owned.
HPCGame(size_t word_len, const HPCData* data);
// HangmanPlayer::Game methods
char GuessNextChar(const std::string& pattern, std::string* word_guess)
override;
private:
void UpdateStateWithPattern(const std::string& new_pattern);
const HPCData* data_;
CharSet tried_; // Characters already tried.
StringSet active_set_; // Words remaining
std::string last_pattern_; // The pattern we had last time.
char last_guessed_char_; // The char we guessed last time.
};
HPCGame::HPCGame(size_t word_len, const HPCData* data)
: data_(data), last_guessed_char_('!') {
auto itr = data->words_by_len.find(word_len);
if (itr != data->words_by_len.end()) {
// Deep copy.
active_set_ = itr->second;
} else {
printf("We have no %zu-char words!\n", word_len);
exit(1);
}
}
// We call this after we get an updated pattern in response to our guess.
void HPCGame::UpdateStateWithPattern(const std::string& new_pattern) {
bool matched = (new_pattern != last_pattern_);
// Shorten our active_set_
StringSet new_set;
if (matched) {
// Filter out words that don't match the new pattern.
for (const auto& w : active_set_) {
if (HangmanMaker::PatternMatches(new_pattern, w))
new_set.insert(w);
}
} else {
// Remove words that don't have the last_guess char.
for (const auto& w : active_set_) {
if (w.find(last_guessed_char_) == std::string::npos)
new_set.insert(w);
}
}
printf("%s: %s guess of '%c' reduced domain from %zu to %zu (by %.1f%%).\n",
new_pattern.c_str(),
(matched ? "Successful" : "Failed"),
last_guessed_char_, active_set_.size(), new_set.size(),
(100.0 * (active_set_.size() - new_set.size())) / active_set_.size());
active_set_.swap(new_set);
}
char HPCGame::GuessNextChar(const std::string& new_pattern,
std::string* word_guess) {
// Was there a previous guess?
if (!last_pattern_.empty())
UpdateStateWithPattern(new_pattern);
last_pattern_= new_pattern;
const std::string& all_chars = HangmanMaker::AllChars();
// Number of active words with a given char, indexed by position
// in all_chars.
std::vector<size_t> active_counts;
active_counts.resize(all_chars.size());
size_t char_pos = 0;
size_t biggest_count = 0;
char biggest_char = '!';
const std::string* potential_word_guess = NULL;
bool multiple_potential = false;
for (size_t char_pos = 0; char_pos < all_chars.size(); char_pos++) {
char c = all_chars[char_pos];
if (tried_.count(c) != 0)
continue;
// Count how many words have this char.
for (const std::string& w : active_set_) {
if (w.find(c) == std::string::npos)
continue;
active_counts[char_pos]++;
if (active_counts[char_pos] >= biggest_count) {
biggest_count = active_counts[char_pos];
biggest_char = c;
// Save this word as a possible guess. If there is ever
// another possible guess, we won't guess.
if (!potential_word_guess) {
potential_word_guess = &w;
} else {
if (potential_word_guess != &w) {
multiple_potential = true;
}
}
}
}
}
if (biggest_count == 1 and !multiple_potential) {
// We think we've got it -- guess the word!
*word_guess = *potential_word_guess;
return '!'; // not used.
}
//printf(" Picking '%c' since there are %zu possible matches (%0.2f%%)\n",
// biggest_char, biggest_count, 100.0*biggest_count / active_set_.size());
if (tried_.count(biggest_char)) {
printf("Ack! trying one I've tried before\n");
exit(-1);
} else if (biggest_count == 0) {
printf("Failed to find a new char!\n");
exit(-1);
}
// Set state for next time.
tried_.insert(biggest_char);
last_guessed_char_ = biggest_char;
return biggest_char;
}
/*
// Play against ourselves with a given word.
void RunWordMatch(const std::string& word_to_find) {
printf("Going to play against word '%s'. %zu chars.\n", word_to_find.c_str(),
word_to_find.size());
StringSet possible_set = words_by_len_[word_to_find.size()];
// Pattern is all _'s.
std::string pattern;
for (auto z : word_to_find) {
pattern += "_";
}
// While we still have some unmatched chars in the pattern,
// keep guessing.
CharSet tried;
size_t failed_guess_count = 0;
while (pattern.find('_') != std::string::npos) {
std::string word_guess;
char choice = PickNextChar(tried, possible_set, &word_guess);
if (!word_guess.empty()) {
printf("Guessing word '%s'\n", word_guess.c_str());
if (word_guess != word_to_find) {
printf("WRONG!\n");
} else {
printf("Found it in %zu guesses, %zu were wrong\n",
tried.size() + 1, failed_guess_count);
}
return;
}
// See if we got a match.
size_t found = 0;
std::string old_pattern = pattern;
for (size_t i=0; i < word_to_find.size(); i++) {
if (word_to_find[i] == choice) {
pattern[i] = choice;
found++;
}
}
// Remember that we've tried this char.
tried.insert(choice);
// Shorten our possible_set
StringSet new_set;
if (found) {
// Compare against the new pattern
for (const auto& w : possible_set) {
if (HangmanMaker::PatternMatches(pattern, w)) {
new_set.insert(w);
}
}
} else {
failed_guess_count++;
// Remove words that have letter not present.
for (const auto& w : possible_set) {
if (w.find(choice) == std::string::npos) {
new_set.insert(w);
}
}
}
printf("Guess #%zu (%zu): '%c'. Pattern '%s'. Possibilities: %zu -> %zu\n",
tried.size(), failed_guess_count, choice, pattern.c_str(),
possible_set.size(), new_set.size());
possible_set.swap(new_set);
}
printf("Found it in %zu guesses, %zu were wrong\n",
tried.size(), failed_guess_count);
}
*/
// ------- HPCPlayer methods ---------
HPCPlayer::HPCPlayer(const StringSet& dictionary) {
// Pre-compute stuff
for (auto w : dictionary)
data_.words_by_len[w.size()].insert(w);
}
std::unique_ptr<HangmanPlayer::Game> HPCPlayer::MakeNewGame(size_t word_len) {
return std::unique_ptr<HangmanPlayer::Game>(new HPCGame(word_len, &data_));
}