-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheader.h
More file actions
319 lines (274 loc) · 7.01 KB
/
header.h
File metadata and controls
319 lines (274 loc) · 7.01 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef HEADER_H_
#define HEADER_H_
#define FILE_PATH "./wordlist.txt"
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <ctime>
#include <algorithm>
//-----------------------------------------------CLASSES INIT----------------------------------------------------
class Player
{
public:
void request_guess(std::string);
char guess;
protected:
bool is_letter_in_string(char, std::string);
};
class Guess
{
public:
std::string get_remaining();
std::string guessed;
void set_guess(char);
private:
bool is_letter_in_string(char, std::string);
};
class Game
{
public:
Game();
bool is_word_guessed(std::string);
int word_length;
std::string print(std::string letters_guessed);
std::vector<std::string> wordlist;
bool is_guess_good(char);
int binary_search_word();
void choose_word();
char hint(std::string letters_guessed);
private:
void load_words();
std::string secret_word;
bool is_letter_in_string(char, std::string);
};
class AI : public Player
{
public:
AI(int, std::vector<std::string>);
void request_guess(std::string, std::string);
std::vector<std::string> wordlist;
std::string found_word;
private:
std::vector<std::string> words;
std::string good_guesses(std::string);
int word_length;
std::string guess_choices;
bool match(std::string, std::string);
};
//---------------------------------------------PLAYER CLASS METHODS-------------------------------------------------
bool Player::is_letter_in_string(char letter, std::string s){
for (int i = 0; i < s.size(); ++i){
if(s[i] == letter) return true;
}
return false;
}
void Player::request_guess(std::string remaining_letters){
bool flag = true;
while (flag) {
std::cout << "Please choose from the remaining letters: ";
std::cin >> guess;
guess = tolower(guess);
std::cout << '\n';
if (is_letter_in_string(guess, remaining_letters))
flag = false;
}
}
//-------------------------------------------------GUESS CLASS METHODS----------------------------------------------
void Guess::set_guess(char letter){
guessed += letter;
}
std::string Guess::get_remaining(){
std::string letters = "abcdefghijklmnopqrstuvwxyz";
std::string remaining_letters = "";
for (int i = 0; i < letters.size(); ++i){
if (!is_letter_in_string(letters[i], guessed)){
remaining_letters += letters[i];
}
}
return remaining_letters;
}
bool Guess::is_letter_in_string(char letter, std::string s){
for (int i = 0; i < s.size(); ++i){
if(s[i] == letter) return true;
}
return false;
}
//-----------------------------------------------------GAME CLASS METHODS---------------------------------------------
Game::Game() {
load_words();
bool admin = false;
std::string password;
char ans;
std::cout << "\nWould you like to login as an admin? (y/n): ";
std::cin >> ans;
if (ans == 'y' || ans == 'Y'){
int i = 2;
while (i > 0 && !admin){
std::cout << "admin password: ";
std::cin >> password;
if (password != "bradnickles"){
std::cout << "\nwrong password. try again.\n";
i--;
} else {
admin = true;
}
}
}
while (admin) {
std::string word;
std::cout << "Enter word: ";
std::cin >> word;
wordlist.push_back(word);
std::cout << "Enter a new word? (y/n):";
std::cin >> ans;
if (ans != 'y')
admin = false;
}
choose_word();
word_length = secret_word.size();
}
void Game::load_words(){
std::ifstream File(FILE_PATH, std::ifstream::in);
if (File.good()){
std::string line;
while(!File.eof()) {
getline(File, line);
wordlist.push_back(line);
}
}
File.close();
}
void Game::choose_word(){
srand(time(NULL));
int index = rand() % wordlist.size();
secret_word = wordlist[index];
sort(wordlist.begin(), wordlist.end());
}
bool Game::is_word_guessed(std::string letters_guessed){
bool found;
for (int i = 0; i < secret_word.size(); ++i) {
char letter = secret_word[i];
found = false;
for (int j = 0; j < letters_guessed.size(); ++j){
if (letter == letters_guessed[j]){
found = true;
break;
}
}
if (!found){
return false;
}
}
return true;
}
bool Game::is_letter_in_string(char letter, std::string s){
for (int i = 0; i < s.size(); ++i){
if(s[i] == letter) return true;
}
return false;
}
std::string Game::print(std::string letters_guessed){
std::string guessed_word = "";
for (int i = 0; i < secret_word.size(); ++i){
if (is_letter_in_string(secret_word[i], letters_guessed)){
guessed_word += secret_word[i];
guessed_word += ' ';
}else{
guessed_word += '_';
guessed_word += ' ';
}
}
return guessed_word;
}
bool Game::is_guess_good(char guess){
if (is_letter_in_string(guess, secret_word))
return true;
else
return false;
}
int Game::binary_search_word(){
int mid, left = 0;
int right = wordlist.size();
while (left < right) {
mid = left + (right - left)/2;
if (secret_word > wordlist[mid]){
left = mid+1;
}else if (secret_word < wordlist[mid]){
right = mid;
}else{
return mid;
}
}
return 0;
}
char Game::hint (std::string letters_guessed){
for (int i = 0; i < secret_word.size(); ++i) {
if (!is_letter_in_string(secret_word[i], letters_guessed))
return secret_word[i];
}
return ' ';
}
//--------------------------------------------------------AI CLASS METHOD---------------------------------------------------
AI::AI(int num, std::vector<std::string> list_of_words){
word_length = num;
guess_choices = "";
wordlist = list_of_words;
found_word = " ";
for (int i = 0; i < wordlist.size(); ++i){
if (wordlist[i].size() == word_length){
words.push_back(wordlist[i]);
}
}
}
std::string AI::good_guesses(std::string print_format){
std::string temp = ""; std::string guesses = "";
for (int i = 0; i < print_format.size(); ++i){
if (print_format[i] != ' ' && print_format[i] != '_' )
temp += print_format[i];
}
char letter; bool duplicate = false;
for (int i = 0; i < temp.size(); ++i){
letter = temp[i];
if (is_letter_in_string(letter, guesses))
guesses += letter;
}
return guesses;
}
bool AI::match(std::string word, std::string print_format){
for(int i = 0; i < word_length; ++i){
if (print_format[i*2] != '_'){
if (word[i] != print_format[i*2])
return false;
}
}
return true;
}
void AI::request_guess(std::string remaining_letters, std::string print_format){
srand(time(NULL));
std::string word;
std::vector<std::string> temp_words;
for (int i = 0; i < words.size(); ++i){
if (match(words[i], print_format) ){
temp_words.push_back(words[i]);
}
}
if (temp_words.size() == 1){
found_word = temp_words[0];
//std::cout << "\nfound_word from class: " << found_word << std::endl;
}
guess_choices = "";
for (int i = 0; i < temp_words.size(); ++i){
word = temp_words[i];
for (int j = 0; j < word.size(); ++j){
if (is_letter_in_string(word[j], remaining_letters) && !is_letter_in_string(word[j], guess_choices))
guess_choices += word[j];
}
}
if (guess_choices == "")
guess = remaining_letters[rand() % remaining_letters.size()];
else
guess = guess_choices[rand() % guess_choices.size()];
}
#endif