Simple Hangman Game in C++
Enjoy a simple version of the Hangman game in C++. Copy this code!
// Include iostream, cstdlib and ctime #include class HangmanGame { private: static const std::string WORDS[]; static const int MAX_TRIES = 6; std::string secretWord; char guessedLetters[100]; int tries; public: HangmanGame() { srand(static_cast(time(nullptr))); secretWord = getRandomWord(); tries = 0; for (char &guessedLetter : guessedLetters) { guessedLetter = '\0'; } } void playGame() { std::cout << "Welcome to Hangman! Try to guess the word." << std::endl; while (true) { displayHangman(); displayGuessedWord(); std::cout << "Enter a letter: "; char guess; std::cin >> guess; if (!isLetterValid(guess)) { std::cout << "Invalid input. Please enter a valid letter." << std::endl; continue; } if (isLetterAlreadyGuessed(guess)) { std::cout << "You've already guessed that letter. Try again." << std::endl; continue; } if (secretWord.find(guess) != std::string::npos) { updateGuessedLetters(guess); } else { tries++; } if (isWordGuessed()) { std::cout << "Congratulations! You guessed the word: " << secretWord << std::endl; break; } if (tries == MAX_TRIES) { displayHangman(); std::cout << "Sorry, you ran out of tries. The correct word was: " << secretWord << std::endl; break; } } } private: std::string getRandomWord() { return WORDS[rand() % (sizeof(WORDS) / sizeof(WORDS[0]))]; } void displayHangman() { // ASCII art based on the number of tries std::cout << "Hangman (" << tries << "/" << MAX_TRIES << " tries):" << std::endl; switch (tries) { case 1: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | " << std::endl; std::cout << " | " << std::endl; std::cout << " | " << std::endl; break; case 2: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | O " << std::endl; std::cout << " | " << std::endl; std::cout << " | " << std::endl; break; case 3: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | O " << std::endl; std::cout << " | | " << std::endl; std::cout << " | " << std::endl; break; case 4: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | O " << std::endl; std::cout << " | /| " << std::endl; std::cout << " | " << std::endl; break; case 5: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | O " << std::endl; std::cout << " | /|\ " << std::endl; std::cout << " | / " << std::endl; break; case 6: std::cout << " _________" << std::endl; std::cout << " | " << std::endl; std::cout << " | O " << std::endl; std::cout << " | /|\ " << std::endl; std::cout << " | / \ " << std::endl; break; default: std::cout << " | " << std::endl; std::cout << " | " << std::endl; std::cout << " | " << std::endl; break; } } void displayGuessedWord() { std::cout << "Current word: "; for (char letter : secretWord) { if (std::find(std::begin(guessedLetters), std::end(guessedLetters), letter) != std::end(guessedLetters)) { std::cout << letter << " "; } else { std::cout << "_ "; } } std::cout << std::endl; } bool isLetterValid(char letter) { return isalpha(letter) && islower(letter); } bool isLetterAlreadyGuessed(char letter) { return std::find(std::begin(guessedLetters), std::end(guessedLetters), letter) != std::end(guessedLetters); } void updateGuessedLetters(char letter) { guessedLetters[strlen(guessedLetters)] = letter; } bool isWordGuessed() { for (char letter : secretWord) { if (std::find(std::begin(guessedLetters), std::end(guessedLetters), letter) == std::end(guessedLetters)) { return false; } } return true; } }; const std::string HangmanGame::WORDS[] = {"hangman", "cplusplus", "programming", "developer", "coding"}; int main() { HangmanGame hangman; hangman.playGame(); return 0; }