Professional Documents
Culture Documents
Hangman
Hangman
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
int main() {
const int MAX_WRONG_ATTEMPTS = 7;
std::vector<std::string> words;
words.push_back("HELLO");
words.push_back("WORLD");
words.push_back("COMPUTER");
words.push_back("PROGRAMMING");
words.push_back("HANGMAN");
std::srand(static_cast<unsigned int>(std::time(nullptr)));
char playAgain;
do {
std::string secretWord = words[std::rand() % words.size()];
std::string guessedLetters(secretWord.length(), '_');
int wrongAttempts = 0;
std::string usedLetters = "";
char guess;
std::cout << "Enter a letter: ";
std::cin >> guess;
guess = toupper(guess);
if (usedLetters.find(guess) != std::string::npos) {
std::cout << "You've already guessed the letter '" << guess << "'.
Try again.\n";
continue;
}
usedLetters += guess;
if (secretWord.find(guess) != std::string::npos) {
std::cout << "Correct guess!\n";
if (guessedLetters == secretWord) {
std::cout << "Congratulations! You guessed the word: " << secretWord <<
"\n";
} else {
std::cout << "Sorry, you ran out of attempts! The word was: " <<
secretWord << "\n";
}
playAgain = tolower(playAgain);
return 0;
}