-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
250 lines (229 loc) · 7.67 KB
/
Copy pathhangman.cpp
File metadata and controls
250 lines (229 loc) · 7.67 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
#include<conio.h>
#include <iostream>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <ctime>
#include<limits>
using namespace std;
void game(vector<string> wordlist, int lives);
void rules();
void displayHangman(int lives);
void easy();
void medium();
void hard();
int main()
{
srand(time(0));
int b;
char choice;
cout << "Welcome to Hangman! Press any key to start" << endl;
getch();
cout << "" << endl;
do{
cout << "If you want to see the rules, press 4" << endl;
cout<<endl;
do {
cout << "Please select the difficulty level: " << endl;
cout << "1: Easy" << endl;
cout << "2: Medium" << endl;
cout << "3: Hard : ";
cin >> b;
cout<<"\n";
if (cin.fail() || (b < 1 || b > 4)) {
cout << "Invalid input! Please enter a number between 1 and 4." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
if (b == 4) {
rules();
cout << "Now, let's start the game!" << endl;
}
} while (b != 1 && b != 2 && b != 3);
switch (b) {
case 1:
easy();
break;
case 2:
medium();
break;
case 3:
hard();
break;
default:
cout << "Invalid choice" << endl;
break;
}
cout << "Do you want to play again? (y/n): ";
cin >> choice;
cout << endl;
} while (choice == 'y' || choice == 'Y');
cout << "Thanks for playing Hangman! Goodbye!" << endl;
return 0;
}
void game(vector<string> wordlist, int lives)
{
string choose = wordlist[rand() % wordlist.size()];
string display(choose.length(), '_');
char guess;
vector<char> guessedLetters;
cout<<" "<<endl;
cout << "The game begins. You have " << lives << " lives. Now guess the word: " << endl;
while (display != choose && lives > 0)
{
cout << "Word: ";
for (size_t i = 0; i < display.length(); ++i) {
cout << display[i];
if (i < display.length() - 1) cout << " ";
}
cout << "Lives left: " << lives << endl;
displayHangman(lives);
cout << "Enter your guess: ";
cin >> guess;
if (find(guessedLetters.begin(), guessedLetters.end(), guess) != guessedLetters.end()) {
cout << "You already guessed '" << guess << "'. Try a different letter." << endl;
continue;
}
guessedLetters.push_back(guess);
bool correctGuess = false;
for (size_t i = 0; i < choose.length(); ++i) {
if (choose[i] == guess) {
display[i] = guess;
correctGuess = true;
}
}
if (!correctGuess) {
--lives;
cout << "\nIncorrect guess! You have " << lives << " lives left." << endl;
}
if (display == choose) {
cout << "Congratulations! You guessed the word: " << choose << endl;
} else if (lives == 0) {
cout << "You lose! The word was: " << choose << endl;
displayHangman(0);
}
}
}
void rules()
{
cout << "You have certain lives to guess the word" << endl;
cout << "If your guess is wrong, one life will be deducted." << endl;
cout << "You have categories in each difficulty level. Choose a category to start." << endl;
cout << "Guess the letters (a-z) to form the word!" << endl;
cout<<"Please give letter in lower case.";
cout<<endl;
cout<<endl;
}
void displayHangman(int lives)
{
cout << " _______" << endl;
cout << " | |" << endl;
if (lives <= 4) cout << " O |" << endl;
else cout << " |" << endl;
if (lives == 4) cout << " | |" << endl;
else if (lives == 3) cout << " /| |" << endl;
else if (lives <= 2) cout << " /|\\ |" << endl;
else cout << " |" << endl;
if (lives == 1) cout << " / |" << endl;
else if (lives == 0) cout << " / \\ |" << endl;
else cout << " |" << endl;
cout << " |" << endl;
cout << "=========" << endl;
}
void easy()
{
int choice;
cout << "For easy mode, you have 3 categories:" << endl;
cout << "1: Animal" << endl;
cout << "2: Nature" << endl;
cout << "3: Fruits" << endl;
do {
cout << " Which category would you like to select? ";
cin >> choice;
if (cin.fail() || (choice < 1 || choice > 4)) {
cout << "Invalid input! Please enter a number between 1 and 4." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;}
if (choice == 1) {
vector<string> Animal = {"cat", "dog", "bear", "fly", "fish", "lion", "frog"};
game(Animal, 5);
}
else if (choice == 2) {
vector<string> Nature = {"tree", "river", "sun", "cloud", "grass", "rock"};
game(Nature, 5);
}
else if (choice == 3) {
vector<string> Fruits = {"apple", "mango", "berry", "strawberry", "grapes", "orange"};
game(Fruits, 5);
}
else {
cout << "Invalid category choice. Please enter 1, 2, or 3." << endl;
}
} while (choice != 1 && choice != 2 && choice != 3);
}
void medium()
{
int choice;
cout << "For medium mode, you have 3 categories:" << endl;
cout << "1: Animal" << endl;
cout << "2: Cities" << endl;
cout << "3: Fruits" << endl;
do {
cout << "Which category would you like to select? ";
cin >> choice;
if (cin.fail() || (choice < 1 || choice > 4)) {
cout << "Invalid input! Please enter a number between 1 and 4." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;}
if (choice == 1) {
vector<string> Animal = {"kangaroo", "elephant", "ostrich", "giraffe", "gorilla", "koala"};
game(Animal, 5);
}
else if (choice == 2) {
vector<string> Cities = {"barcelona", "sydney", "mumbai", "karachi"};
game(Cities, 5);
}
else if (choice == 3) {
vector<string> Fruits = {"papaya", "kiwi", "guava", "dragonfruit", "apricot"};
game(Fruits, 5);
}
else {
cout << "Invalid category choice. Please enter 1, 2, or 3." << endl;
}
} while (choice != 1 && choice != 2 && choice != 3);
}
void hard()
{
int choice;
cout << "For hard mode, you have 3 categories:" << endl;
cout << "1: Countries" << endl;
cout << "2: Space" << endl;
cout << "3: Sports" << endl;
do {
cout << "Which category would you like to select? ";
cin >> choice;
if (cin.fail() || (choice < 1 || choice > 4)) {
cout << "Invalid input! Please enter a number between 1 and 4." << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;}
if (choice == 1) {
vector<string> Countries = {"spain", "england", "newzealand", "australia", "afghanistan", "india"};
game(Countries, 5);
}
else if (choice == 2) {
vector<string> Space = {"blackhole", "jupiter", "darkmatter", "hypernova"};
game(Space, 5);
}
else if (choice == 3) {
vector<string> Sports = {"volleyball", "badminton", "cricket", "judo"};
game(Sports, 5);
}
else {
cout << "Invalid category choice. Please enter 1, 2, or 3." << endl;
}
} while (choice != 1 && choice != 2 && choice != 3);
}