-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangman.cpp
More file actions
83 lines (71 loc) · 2.63 KB
/
hangman.cpp
File metadata and controls
83 lines (71 loc) · 2.63 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
#include <iostream>
#include <string>
using namespace std;
int main() {
int wr = 0;
string word;
char letter;
char empty[5] = { '_', '_', '_', '_', '_' };
char wrong[5] = { '_', '_', '_', '_', '_' };
bool correctGuess;
cout << "This is a Hang Man game!!\nRules:\n1. Player 1 enters a 5 letter word.\n2. Player 2 has to guess the letters of the word one-by-one.\n3. Player 2 has three chances.\n4. When the fourth guess letter is wrong, the man will be fully hung and the game is over\nEnjoy!\n" << endl;
cout << "Player 1 enters a 5 letter word:" << endl;
cin >> word;
if (word.length() != 5) {
cout << "Error! Please enter a 5 letter word!" << endl;
exit(0);
}
while (wr < 5 && (empty[0] == '_' || empty[1] == '_' || empty[2] == '_' || empty[3] == '_' || empty[4] == '_')) {
// Display Hangman
if (wr == 0) {
cout << "|________\n|\n|\n|\n|\n|--------" << endl;
}
else if (wr == 1) {
cout << "|________\n| |\n|\n|\n|\n|--------" << endl;
}
else if (wr == 2) {
cout << "|________\n| |\n| 0\n|\n|\n--------" << endl;
}
else if (wr == 3) {
cout << "|________\n| |\n| 0\n| /|\\\n|\n|--------" << endl;
}
else if (wr == 4) {
cout << "|________\n| |\n| 0\n| /|\\\n| / \\ \n|\n--------\nGAME IS OVER!!!!!" << endl;
break;
}
// Display guessed letters
cout << "Correct letters: ";
for (char c : empty) cout << c << ' ';
cout << endl;
cout << "Wrong letters: ";
for (char c : wrong) cout << c << ' ';
cout << endl;
cout << "\nEnter a letter:" << endl;
cin >> letter;
correctGuess = false;
for (int i = 0; i < word.length(); i++) {
if (letter == word[i]) {
empty[i] = letter;
word[i] = '_'; // Mark this letter as guessed
correctGuess = true;
}
}
if (!correctGuess) {
if (wr < 5) {
wrong[wr] = letter;
wr++;
}
}
}
if (empty[0] != '_' && empty[1] != '_' && empty[2] != '_' && empty[3] != '_' && empty[4] != '_') {
cout << "Congratulations! You've guessed the word: ";
for (char c : empty) cout << c;
cout << endl;
}
else {
cout << "Game over! The correct word was: ";
for (char c : empty) cout << c;
cout << endl;
}
return 0;
}