-
Notifications
You must be signed in to change notification settings - Fork 1
/
Hangman.cpp
241 lines (222 loc) · 5.69 KB
/
Hangman.cpp
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
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <iostream> // for console input/output
#include <fstream> // for file handling
using namespace std;
const int MAX_TRIES = 6; // Maximum number of tries allowed
struct Word {
string easy_words[10];
string medium_words[10];
string hard_words[10];
string easy_filename = "easy.txt";
string medium_filename = "medium.txt";
string hard_filename = "hard.txt";
void loadWords(string words[], int size, string filename) {
ifstream file(filename);
if (file.is_open()) {
for (int i = 0; i < size; i++) {
if (!getline(file, words[i])) {
break;
}
}
file.close();
}
}
Word() {
loadWords(easy_words, 10, easy_filename);
loadWords(medium_words, 10, medium_filename);
loadWords(hard_words, 10, hard_filename);
}
};
class Hangman {
private: int tries;
string word;
string hidden;
bool gameover;
bool used_letters[26];
public: Hangman() {
system("cls");
tries = 0;
gameover = false;
Word w;
int level;
cout << "Welcome to Hangman!" << endl;
ifstream load_file("hangman_save.txt");
if (!load_file) {
cout << "Choose a level (1 for easy, 2 for medium, 3 for hard): ";
cin >> level;
while (level < 1 || level > 3) {
cout << "Invalid input. Please enter a valid level (1 for easy, 2 for "
"medium, 3 for hard): ";
cin >> level;
}
srand(time(NULL)); // Seed random number generator
int index = rand() % 10; // Choose a random index
switch (level) {
case 1:
word = w.easy_words[index];
break;
case 2:
word = w.medium_words[index];
break;
case 3:
word = w.hard_words[index];
break;
}
}
load_file.close();
hidden = string(word.length(), '_'); // Initialize hidden word with underscores
for (int i = 0; i < 26; i++) {
used_letters[i] = false; // Initialize used letters array to all false
}
}
void saveGame() {
ofstream save_file("hangman_save.txt");
save_file << tries << "\n" <<
word << "\n" <<
hidden << "\n" <<
gameover << "\n";
for (int i = 0; i < 26; i++) {
save_file << used_letters[i] << " ";
}
save_file << "\n";
save_file.close();
cout << "Game saved successfully." << endl;
}
void loadGame() {
ifstream load_file("hangman_save.txt");
if (!load_file) {
return;
}
load_file >> tries >> word >> hidden >> gameover;
for (int i = 0; i < 26; i++) {
load_file >> used_letters[i];
}
load_file.close();
cout << "Game loaded successfully." << endl;
}
void drawHangman() {
cout << endl;
switch (tries) {
case 0:
cout << endl;
break;
case 1:
cout << endl;
cout << " |----------" << endl;
cout << " | O " << endl;
cout << " | " << endl;
cout << " | " << endl;
break;
case 2:
cout << endl;
cout << " |----------" << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | " << endl;
break;
case 3:
cout << endl;
cout << " |----------" << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | / " << endl;
break;
case 4:
cout << endl;
cout << " |----------" << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | /| " << endl;
break;
case 5:
cout << endl;
cout << " |----------" << endl;
cout << " | O " << endl;
cout << " | | " << endl;
cout << " | /|\\ " << endl;
break;
case 6:
cout << endl;
cout << " |----------" << endl;
cout << " | X " << endl;
cout << " | | " << endl;
cout << " | /|\\ " << endl;
break;
}
}
void displayStatus() {
system("cls");
cout << "Word: " << hidden << endl;
cout << "Wrong Tries: " << tries << endl;
cout << "Remaining Wrong Tries: " << (6 - tries) << endl;
cout << "Used Letters: ";
for (int i = 0; i < 26; i++) {
if (used_letters[i]) {
cout << (char)('a' + i) << " ";
}
}
cout << endl;
drawHangman();
}
void checkLetter(char letter) {
if (used_letters[letter - 'a'] != true) {
bool correct = false;
for (int i = 0; i < word.length(); i++) {
if (word[i] == letter) {
hidden[i] = letter;
correct = true;
}
}
if (!correct) {
tries++;
}
used_letters[letter - 'a'] = true; // Mark the letter as used
} else {
cout << "You have already used this letter" << endl;
system("pause");
}
}
bool checkGameOver() {
if (tries == MAX_TRIES) {
gameover = true;
displayStatus();
cout << "Game over! The word was " << word << "." << endl;
remove("hangman_save.txt");
} else if (word == hidden) {
gameover = true;
displayStatus();
cout << "Congratulations! You guessed the word " << word << "!" << endl;
remove("hangman_save.txt");
}
return gameover;
}
void play() {
loadGame();
while (!checkGameOver()) {
saveGame();
displayStatus();
char letter;
cout << "Guess a letter: ";
cin >> letter;
letter = tolower(letter);
checkLetter(letter);
}
}
};
int main() {
int check=1;
while(check=1){
Hangman game;
game.play();
do{
cout<<"Do you want to play again? (1 for yes, 0 for no): ";
cin>>check;
if(check==0)
{
return 0;
}
}
while(check!=1);
}
}