-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhangmangame.cpp
More file actions
101 lines (83 loc) · 2.83 KB
/
hangmangame.cpp
File metadata and controls
101 lines (83 loc) · 2.83 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
#include <iostream>
#include <ctime>
#include <vector>
using namespace std;
int tries = 3;
string message = "You have 3 chances!";
void hangman(char);
int checkGuess(char, const string&, string&);
int main() {
char letter;
string month;
vector<string> months = {
"january", "february", "march", "april",
"may", "june", "july", "august",
"september", "october", "november", "december"
};
srand(static_cast<unsigned int>(time(NULL)));
int monthnum = rand() % 12;
month = months[monthnum];
string hidemonth(month.length(), 'X');
system("cls");
while (tries > 0) {
hangman('n');
cout << "\t\t\t\tLife: " << tries << endl;
cout << hidemonth << endl;
cout << "\t\t\t\tGuess a letter: ";
cin >> letter;
system("cls");
if (checkGuess(letter, month, hidemonth) == 0) {
message = "Incorrect Letter!";
tries--;
} else {
message = "Nice Correct Guess!";
}
if (hidemonth == month) {
message = "You are Free!";
hangman('f');
cout << "\t\t\t\tLife: " << tries << endl;
cout << "\t\t\t\tThe month is: " << month << endl;
break;
}
}
if (tries == 0) {
message = "You are Hanged!";
hangman('h');
cout << "\t\t\t\tLife: " << tries << endl;
cout << "\t\t\t\tThe month is: " << month << endl;
}
return 0;
}
int checkGuess(char guess, const string& realmonth, string& hiddenmonth) {
int matches = 0;
int len = realmonth.length();
for (int i = 0; i < len; i++) {
if (guess == hiddenmonth[i]) {
return 0;
}
if (guess == realmonth[i]) {
hiddenmonth[i] = guess;
matches++;
}
}
return matches;
}
void hangman(char state) {
string headstring = "|";
string stage = "=";
if (state == 'f') {
headstring = " ";
} else if (state == 'h') {
stage = " ";
}
cout << "\t\t\t\t______________________________________" << endl;
cout << "\t\t\t\t " << headstring << " |" << endl;
cout << "\t\t\t\t O |" << endl;
cout << "\t\t\t\t / \\ |" << "\t " << message << endl;
cout << "\t\t\t\t | |" << "\t /" << endl;
cout << "\t\t\t\t / \\ | O " << endl;
cout << "\t\t\t\t ======" << stage << "===== | / \\" << endl;
cout << "\t\t\t\t | | | |" << endl;
cout << "\t\t\t\t | | | / \\" << endl;
cout << "\t\t\t\t=============================================================" << endl;
}