-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordMind.cpp
More file actions
293 lines (276 loc) · 11.7 KB
/
WordMind.cpp
File metadata and controls
293 lines (276 loc) · 11.7 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
// Load in all libraries and external functions
#include <iostream>
#include <string>
#include <cstdlib>
#include <algorithm>
#include "edit_load_profile.h"
#include "generate_extract_word.h"
#include "generate_hint_validate.h"
#include "instructions_intro_leaderboard.h"
#include "display_word.h"
using namespace std;
// Define a struct to store user data effectively
struct UserData
{
string name;
int puzzlesSolved;
int hintsPoints;
int hintsUsed;
};
// This function is the main gameplay function. It takes in a struct of type UserData and returns an int depending on if the user wants to play again or not.
int gameplay(UserData &user)
{
int choice, type;
cout << endl << endl << "Please select an option below to get started" << endl;
cout << "1: Instructions" << endl << "2: Leaderboard" << endl << "3: Play" << endl << "4: Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
// Ask user if they want to see instructions, leaderboard, play or exit. If invalid input, ask again.
if (choice < 1 || choice > 4){
while (choice < 1 || choice > 4){
if (choice < 1 || choice > 4){
cout << "Invalid choice. Please try again." << endl;
}
cout << "Please select an option below" << endl;
cout << "1: Instructions" << endl << "2: Leaderboard" << endl << "3: Play" << endl << "4: Exit" << endl;
cin >> choice;
}
} else if (choice == 1){
cout << endl;
instructions_intro(2);
gameplay(user);
} else if (choice == 2){
cout << endl << endl;
displayLeaderboard();
gameplay(user);
} else if (choice == 3){
cout << "Lets get guessing!" << endl;
} else if (choice == 4){
cout << "Thanks for playing!" << endl;
exit(0);
}
system("clear"); // Clear screen
// Ask user what difficulty (theme) they want to play on. If invalid input, ask again.
cout << "Please choose your difficulty/theme: " << endl;
cout << "1: Random English Word (Very Hard)\n"
<< "2: Languages (Hard)\n"
<< "3: Food (Hard)\n"
<< "4: Animals (Medium)\n"
<< "5: Countries (Easy)\n"
<< "6: Colors (Easy)\n";
cout << "Enter your choice: ";
cin >> type;
if (type != 1 && type != 2 && type != 3 && type != 4 && type != 5 && type != 6){
while (type < 1 || type > 6){
if (type < 1 || type > 6){
cout << "Invalid choice. Please try again." << endl;
}
cout << "Please choose your difficulty/theme: ";
cout << "1: Random English Word (Very Hard)\n"
<< "2: Languages (Hard)\n"
<< "3: Food (Hard)\n"
<< "4: Animals (Medium)\n"
<< "5: Countries (Easy)\n"
<< "6: Colors (Easy)\n";
cout << "Enter your choice: ";
cin >> type;
}
}
// Generate word based on difficulty/theme and store in string. If invalid input, ask again.
string guess_word;
string input_word[6];
if (type == 1){
guess_word = generate_word();
} else {
guess_word = theme_word_extractor(type);
if (guess_word == "Invalid input type" || guess_word == "Unable to open file"){
cout << "Error with program, please make sure all files are downloaded properly" << endl;
return 0;
}
}
system("clear"); // Clear screen
instructions_intro(1);
cout << endl << "------------------------------------------------------------------------" << endl;
bool win = false;
for (int i = 0; i < 6; i++){
bool valid = false;
cout << endl << endl << "Enter your 5 letter guess: ";
cin >> input_word[i];
input_word[i] = convert_to_upper(input_word[i]);
// Validate input using validate_word function, if invalid, ask again.
if (validate_word(input_word[i]) == false) {
cout << "Invalid input. Please input a 5 character english word." << endl;
cout << "Enter your 5 letter guess: ";
while (valid == false) {
cout << "Enter your 5 letter guess: ";
cin >> input_word[i];
valid = validate_word(input_word[i]);
if (valid == false){
cout << "Invalid input. Please input a 5 character english word." << endl;
}
}
}
// Check if word is already guessed, if yes, ask again, if no, continue loop.
if (i > 0){
for (int j = 0; j < i; j++){
if (input_word[i] == input_word[j]){
cout << "Word already guessed. Please try again." << endl;
cout << "Enter your 5 letter guess: ";
cin >> input_word[i];
if (input_word[i] == input_word[j]){
while (valid == false) {
cout << "Enter your 5 letter guess: ";
cin >> input_word[i];
input_word[i] = convert_to_upper(input_word[i]);
valid = validate_word(input_word[i]);
if (valid == false){
cout << "Invalid input. Please input a 5 character english word." << endl;
}
}
}
}
}
}
// Check if user has won, if yes, break loop, if no, continue loop.
if (input_word[i] == guess_word)
{
win = true;
break;
} else {
win = false;
}
system("clear"); // Clear screen
cout << " WORD MIND " << endl;
cout << "---------------" << endl;
for (int j = 0; j <= i; j++){
display_word(input_word[j], guess_word);
}
cout << "---------------" << endl << endl;
// If user has not guessed the word and it is not their last guess, ask if they want a hint.
if (i != 5 && win == false){
int hint_choice;
// Ask user if they want a hint, if yes, generate hint and display, if no, continue loop.
cout << "Would you like a hint? " << endl << "1: Yes" << endl << "2: No (Continue)" << endl << "Enter your choice: ";
string temp;
cin >> temp;
hint_choice = stoi(temp); // Terminating program if user inputs a string instead of an integer.
// If user has no hint points left, tell them they have no hint points left and continue loop.
if (hint_choice == 1){
if (user.hintsPoints == 0){
cout << "Sorry, you have no hint points left. You got this!" << endl;
} else {
cout << "Here is your hint: " << generate_hint(guess_word, input_word[i]);
user.hintsPoints -= 1;
user.hintsUsed += 1;
edit_profile(user.name, user.puzzlesSolved, user.hintsPoints, user.hintsUsed);
}
} else if (hint_choice == 2){
cout << "You got this, lets continue!" << endl << endl;
}
}
}
// Check if user has won or lost after 6 guesses, if won, congratulate, if lost, display correct word.
if (win){
cout << "Congratulations! You guessed the word!" << endl;
edit_profile(user.name, user.puzzlesSolved + 1, user.hintsPoints, user.hintsUsed);
} else {
cout << "Sorry, you have lost. The word was " << guess_word << endl;
edit_profile(user.name, user.puzzlesSolved, user.hintsPoints + 1, user.hintsUsed);
}
// Ask user if they want to play again, if yes, call function again, if no, return 0
cout << "Would you like to play again? " << endl << "1: Yes" << endl << "2: No" << endl;
cout << "Enter your choice: ";
cin >> choice;
if (choice != 1 && choice != 2){
while (choice != 1 && choice != 2){
if (choice != 1 && choice != 2){
cout << "Invalid choice. 1 for Yes, 2 for No. Please try again." << endl;
}
cout << "Enter your choice: ";
cin >> choice;
}
}
if (choice == 1){
gameplay(user);
} else if (choice == 2){
cout << "Thanks for playing! See you soon again :)" << endl;
exit(0);
}
return 0;
}
// The main function, calls all other functions and runs the game.
int main(){
UserData* user = new UserData;
system("clear"); // Clear screen
instructions_intro(1); // Display Introduction & Ask if new user or not
cout << endl << "Are you a first time player?" << endl << "1: Yes" << endl << "2: No" << endl;
int choice, run;
cout << endl << "Enter your choice: ";
cin >> choice;
// Validate input, if invalid, ask again. Checks if user is new to game or not. If new, create new profile. If not, load profile. Stores relevant data in struct.
if (choice != 1 && choice != 2){
while (choice != 1 && choice != 2){
if (choice != 1 && choice != 2){
cout << "Invalid choice. 1 for Yes, 2 for No. Please try again." << endl;
}
cout << "Enter your choice: ";
cin >> choice;
}
}
if (choice == 1){
cout << "Enter your username: ";
cin >> user->name;
user->name = convert_to_upper(user->name);
edit_profile(user->name, 0, 3, 0); // Create new profile with default values
cout << endl << "Welcome, " << user->name << "!" << endl;
user->puzzlesSolved = 0;
user->hintsPoints = 3;
user->hintsUsed = 0;
} else if (choice == 2){
cout << "Please enter your username: ";
cin >> user->name;
user->name = convert_to_upper(user->name);
int load = load_profile(user->name, user->puzzlesSolved, user->hintsPoints, user->hintsUsed); // Load profile and store data in struct
if (load == 0){
cout << endl << "Welcome back, " << user->name << "!" << endl;
} else if (load == 1){
cout << endl << "Sorry, " << user->name << " does not exist. Please try again or create a new profile." << endl;
cout << "Would you like to create a new profile?" << endl << "1: Yes" << endl << "2: No" << endl;
if (choice != 1 && choice != 2){
while (choice != 1 && choice != 2){
if (choice != 1 && choice != 2){
cout << "Invalid choice. 1 for Yes, 2 for No. Please try again." << endl;
}
cout << "Enter your choice: ";
cin >> choice;
}
}
if (choice == 1){
cout << "Enter your username: ";
cin >> user->name;
user->name = convert_to_upper(user->name);
edit_profile(user->name, 0, 3, 0); // Create new profile with default values
cout << endl << "Welcome, " << user->name << "!" << endl;
user->puzzlesSolved = 0;
user->hintsPoints = 3;
user->hintsUsed = 0;
} else if (choice == 2){
cout << "Thanks for playing! See you soon again :)" << endl;
delete user; // Free dynamically allocated memory
return 0;
}
}
}
// If gameplay returns 0, The user wants to end the program. It returns 1 if the user wants to play again.
run = gameplay(*user);
if (run == 0){
delete user; // Free dynamically allocated memory
return 0;
} else if (run == 1){
system("clear"); // Clear screen
gameplay(*user);
} else if (run == 2){
gameplay(*user);
}
delete user; // Free dynamically allocated memory
}