-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewgame2edited.cpp
More file actions
258 lines (227 loc) · 8.77 KB
/
Copy pathnewgame2edited.cpp
File metadata and controls
258 lines (227 loc) · 8.77 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
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <vector>
using namespace std;
// Function to choose a random word from an array
string chooseRandomWord(const string words[], int numWords) {
srand(static_cast<unsigned int>(time(0)));
int randomIndex = rand() % numWords;
return words[randomIndex];
}
// Function to choose a random word from a vector
string chooseRandomWordFromVector(const vector<string>& words) {
srand(static_cast<unsigned int>(time(0)));
int randomIndex = rand() % words.size();
return words[randomIndex];
}
// Function to display the current state of the word with guessed letters
void displayWord(const string& word, const bool guessed[], int wordLength) {
for (int i = 0; i < wordLength; i++) {
if (guessed[i]) {
cout << word[i] << " ";
} else {
cout << "_ ";
}
}
cout << endl;
}
// Function to display hangman drawing based on the number of attempts
void displayHangman(int attempts) {
switch (attempts) {
case 0:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
break;
case 1:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "|" << endl;
cout << "|" << endl;
cout << "|" << endl;
break;
case 2:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| |" << endl;
cout << "|" << endl;
cout << "|" << endl;
break;
case 3:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /|" << endl;
cout << "|" << endl;
cout << "|" << endl;
break;
case 4:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "|" << endl;
cout << "|" << endl;
break;
case 5:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "| / " << endl;
cout << "|" << endl;
break;
case 6:
cout << "_________" << endl;
cout << "| |" << endl;
cout << "| O" << endl;
cout << "| /|\\" << endl;
cout << "| / \\" << endl;
cout << "|" << endl;
break;
}
}
// Function to load words from a file into a vector
vector<string> loadWordsFromFile(const string& filename) {
vector<string> words;
ifstream file(filename.c_str());
string word;
while (file >> word) {
words.push_back(word);
}
return words;
}
// Function to handle hints
string provideHint(const string& word) {
return string(1, word[0]);
}
// Function to validate user input
char getValidatedInput() {
char input;
while (true) {
cin >> input;
if (isalpha(input)) {
input = tolower(input);
break;
} else {
cout << "Invalid input. Please enter a letter: ";
}
}
return input;
}
// Function to display a creative design when the player wins
void displayWinningDesign() {
cout << " __ __ _ _ _ _ _ ___ " << endl;
cout << " | \\/ | | | | | / \\ | \\ | | / _ \\ " << endl;
cout << " | |\\/| | | | | | / _ \\ | \\| | | | | |" << endl;
cout << " | | | | | |_| |/ ___ \\| |\\ | | |_| |" << endl;
cout << " |_| |_| \\___//_/ \\_\\_| \\_| \\___/ " << endl;
cout << " " << endl;
cout << " Congratulations, you won! " << endl;
cout << " Keep up the great work! " << endl;
cout << " " << endl;
}
int main() {
const int maxAttempts = 6;
int score = 0;
// Word categories
string word_fruit[] = {"apple", "banana", "cherry", "grape", "orange", "avocado", "papaya", "strawberry", "mango", "guava"};
string word_animal[] = {"cat", "horse", "dog", "snake", "rabbit", "elephant", "jaguar", "panda", "scorpion", "sheep"};
string word_vehicle[] = {"boat", "bicycle", "ship", "tractor", "plane", "truck", "excavator", "ambulance", "helicopter", "scooter"};
string word_movie[] = {"titanic", "jurassic", "inception", "tenet", "oppenheimer", "interstellar", "avatar", "matrix", "gravity", "gladiator"};
string word_planet[] = {"mercury", "venus", "earth", "mars", "jupiter", "saturn", "uranus", "neptune", "pluto", "eris"};
string word_country[] = {"usa", "canada", "france", "japan", "india", "brazil", "australia", "russia", "germany", "china"};
vector<string> dynamic_words = loadWordsFromFile("words.txt");
string wordToGuess;
bool guessed[20];
string game_level, category;
char playAgain;
cout << "Welcome to the Word Master Game!" << endl;
cout << "Guess the word" << endl;
cout << "Select game level (easy/medium/hard): ";
cin >> game_level;
do {
if (game_level == "easy") {
cout << "Choose your field (fruit/animal/vehicle/movie/planet/country): ";
cin >> category;
if (category == "fruit") {
wordToGuess = chooseRandomWord(word_fruit, 10);
} else if (category == "animal") {
wordToGuess = chooseRandomWord(word_animal, 10);
} else if (category == "vehicle") {
wordToGuess = chooseRandomWord(word_vehicle, 10);
} else if (category == "movie") {
wordToGuess = chooseRandomWord(word_movie, 10);
} else if (category == "planet") {
wordToGuess = chooseRandomWord(word_planet, 10);
} else if (category == "country") {
wordToGuess = chooseRandomWord(word_country, 10);
}
} else if (game_level == "medium") {
wordToGuess = chooseRandomWordFromVector(dynamic_words);
} else if (game_level == "hard") {
vector<string> hard_words = loadWordsFromFile("hard_words.txt");
wordToGuess = chooseRandomWordFromVector(hard_words);
}
int wordLength = wordToGuess.length();
for (int i = 0; i < wordLength; i++) {
guessed[i] = false;
}
int attempts = 0;
while (attempts < maxAttempts) {
cout << "Attempts remaining: " << maxAttempts - attempts << endl;
displayHangman(attempts);
displayWord(wordToGuess, guessed, wordLength);
char guess = getValidatedInput();
bool correctGuess = false;
for (int i = 0; i < wordLength; i++) {
if (!guessed[i] && wordToGuess[i] == guess) {
guessed[i] = true;
correctGuess = true;
}
}
if (correctGuess) {
cout << "Correct guess!" << endl;
} else {
cout << "Incorrect guess." << endl;
attempts++;
}
if (attempts == maxAttempts - 1 && game_level != "hard") {
cout << "Would you like a hint? (y/n): ";
char hintOption;
cin >> hintOption;
if (hintOption == 'y' || hintOption == 'Y') {
cout << "Hint: The word starts with " << provideHint(wordToGuess) << endl;
}
}
bool wordGuessed = true;
for (int i = 0; i < wordLength; i++) {
if (!guessed[i]) {
wordGuessed = false;
break;
}
}
if (wordGuessed) {
displayWinningDesign();
score++;
break;
}
}
if (attempts == maxAttempts) {
cout << "Game over! The word was: " << wordToGuess << endl;
}
cout << "Your score: " << score << endl;
cout << "Do you want to play again? (y/n): ";
cin >> playAgain;
} while (playAgain == 'y' || playAgain == 'Y');
cout << "Thank you for playing the Word Master Game!" << endl;
return 0;
}