Repository navigation
Expand file tree
/
Copy pathmain (5).cpp
More file actions
56 lines (46 loc) · 1.42 KB
/
Copy pathmain (5).cpp
File metadata and controls
56 lines (46 loc) · 1.42 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
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
// Word bank
vector<string> words = {"computer", "hangman", "programming", "cplusplus", "game"};
// Pick a random word
srand(time(0));
string word = words[rand() % words.size()];
string guessed(word.length(), '_'); // Hidden word with underscores
int attempts = 6; // Number of chances
char guess;
bool won = false;
cout << "=== 🎮 Hangman Game ===" << endl;
while (attempts > 0 && !won) {
cout << "\nWord: " << guessed << endl;
cout << "Attempts left: " << attempts << endl;
cout << "Enter a letter: ";
cin >> guess;
bool correct = false;
for (size_t i = 0; i < word.length(); i++) {
if (word[i] == guess && guessed[i] == '_') {
guessed[i] = guess;
correct = true;
}
}
if (!correct) {
cout << "❌ Wrong guess!" << endl;
attempts--;
} else {
cout << "✅ Good guess!" << endl;
}
if (guessed == word) {
won = true;
}
}
if (won) {
cout << "\n🎉 Congratulations! You guessed the word: " << word << endl;
} else {
cout << "\n💀 Game Over! The word was: " << word << endl;
}
return 0;
}