-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
137 lines (111 loc) · 4.35 KB
/
Copy pathmain.cpp
File metadata and controls
137 lines (111 loc) · 4.35 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
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
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void PrintWrong(vector<char> listOfWrong) {
cout << "These letters are not in the word: " << endl;
cout << "( ";
for (int i = 0; i < listOfWrong.size(); ++i) {
cout << listOfWrong.at(i) << " ";
}
cout << " )" << endl << endl;
}
int main() {
vector<string> wordsToGuess;
vector<char> listOfWrong;
const int WRONG_GUESSES = 8;
string userName;
string answer;
string randomWord;
string hiddenWord;
bool userAnswer = true;
char userGuess;
string officialGuess;
int numWrongGuesses = 0;
int numFoundLetters = 0;
int totalGuesses = 0;
int i;
wordsToGuess.push_back("chimney");
wordsToGuess.push_back("monotonous");
wordsToGuess.push_back("electricity");
wordsToGuess.push_back("abruptly");
wordsToGuess.push_back("skyscraper");
wordsToGuess.push_back("cylinder");
wordsToGuess.push_back("mount");
wordsToGuess.push_back("hymnal");
wordsToGuess.push_back("vehicle");
wordsToGuess.push_back("vaporize");
cout << "Welcome to the game of HANGMAN!" << endl;
cout << "Please enter your name: ";
getline(cin,userName);
cout << endl;
cout << "Are you ready to begin? (yes/no)";
getline (cin,answer);
cout << endl;
do {
if (answer == "yes") {
randomWord = wordsToGuess[rand() % wordsToGuess.size()];
cout << "The word you are trying to guess has " << randomWord.size() << " letters." << endl;
cout << "You can make up to " << WRONG_GUESSES << " incorrect guesses before you lose." << endl;
hiddenWord = randomWord;
for (i = 0; i < randomWord.size(); ++i) {
hiddenWord.replace(i, 1, "_");
}
numWrongGuesses = 0;
numFoundLetters = 0;
totalGuesses = 0;
cout << hiddenWord << endl;
cout << "Guess a letter!";
cin >> userGuess;
cout << endl;
officialGuess = userGuess;
while (numWrongGuesses < WRONG_GUESSES) {
totalGuesses = totalGuesses + 1;
cout << "Your number of guesses: " << totalGuesses << endl;
for (i = 0; i < randomWord.size(); ++i) {
if (userGuess == randomWord.at(i)) {
hiddenWord.replace(i, 1, officialGuess.substr(0, 1));
numFoundLetters = numFoundLetters + 1;
}
}
cout << "You have found " << numFoundLetters << "/" << randomWord.size() << " letters." << endl;
if (randomWord.find(userGuess) == string::npos) {
cout << "Sorry, the letter " << userGuess << " is not in the word." << endl;
numWrongGuesses = numWrongGuesses + 1;
listOfWrong.push_back(userGuess);
}
cout << "You have guessed incorrectly " << numWrongGuesses << "/" << WRONG_GUESSES << " time(s)."
<< endl;
PrintWrong(listOfWrong);
if (numWrongGuesses == WRONG_GUESSES) {
cout << hiddenWord << endl;
cout << "Too bad, " << userName << "! You have guessed incorrectly too many times. You lose!"
<< endl;
cout << "The word was " << randomWord << "." << endl;
break;
}
if (hiddenWord.find("_") == string::npos) {
cout << hiddenWord << endl;
cout << "Nice job " << userName << "! You guessed the word correctly!" << endl;
cout << "You guessed " << totalGuesses << " times to figure it out." << endl;
break;
}
cout << hiddenWord << endl;
cout << "Guess another letter!";
cin >> userGuess;
cout << endl;
officialGuess = userGuess;
}
cout << endl;
}
cout << "Would you like to play again? (yes/no): ";
cin >> answer;
cout << endl;
listOfWrong.clear();
if (answer == "no") {
cout << "Thanks for playing " << userName << "!" << endl;
userAnswer = false;
}
}while(userAnswer);
return 0;
}