-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathHangman cpp
103 lines (86 loc) · 2.86 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
#include <iostream>
#include <vector>
#include <cstdlib> // for rand() and srand()
#include <ctime> // for time()
#include <algorithm> // for find()
using namespace std;
// Function to display the current state of the guessed word
void displayWord(const string& word, const vector<bool>& guessed) {
for (int i = 0; i < word.length(); i++) {
if (guessed[i]) {
cout << word[i] << " ";
} else {
cout << "_ ";
}
}
cout << endl;
}
// Function to check if the word is fully guessed
bool isWordGuessed(const vector<bool>& guessed) {
for (bool g : guessed) {
if (!g) {
return false;
}
}
return true;
}
int main() {
// List of words for the hangman game
vector<string> words = {"programming", "hangman", "challenge", "computer", "science"};
// Seed random number generator
srand(time(0));
// Randomly select a word
string word = words[rand() % words.size()];
// Variables for the game
int lives = 6; // Number of wrong guesses allowed
vector<bool> guessed(word.length(), false); // Tracks which letters have been guessed correctly
vector<char> wrongGuesses; // Tracks wrong guesses
cout << "Welcome to Hangman!" << endl;
// Main game loop
while (lives > 0) {
// Display the current state of the word
cout << "\nCurrent word: ";
displayWord(word, guessed);
// Display wrong guesses
if (!wrongGuesses.empty()) {
cout << "Wrong guesses: ";
for (char c : wrongGuesses) {
cout << c << " ";
}
cout << endl;
}
// Prompt the user for a guess
char guess;
cout << "Enter a letter: ";
cin >> guess;
// Check if the letter has already been guessed
if (find(wrongGuesses.begin(), wrongGuesses.end(), guess) != wrongGuesses.end()) {
cout << "You already guessed that letter! Try again." << endl;
continue;
}
// Check if the guessed letter is in the word
bool correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (word[i] == guess) {
guessed[i] = true;
correctGuess = true;
}
}
// If the guess was incorrect, reduce lives
if (!correctGuess) {
lives--;
wrongGuesses.push_back(guess);
cout << "Wrong guess! You have " << lives << " lives remaining." << endl;
}
// Check if the word is fully guessed
if (isWordGuessed(guessed)) {
cout << "Congratulations! You've guessed the word: " << word << endl;
break;
}
}
// If the player runs out of lives
if (lives == 0) {
cout << "You've run out of lives! The word was: " << word << endl;
}
return 0;
}