Creating a Hangman game in C++ is a fun and beginner-friendly project to learn C and C++ programming language and is also considered as on of the good college projects in semesters.
This guide will walk you through the game’s rules, work, and implementation to bring this classic word-guessing puzzle to life. Let’s make this Hangman game.
What is hangman game in C++?
Hangman game is a word-guessing game where you guess a hidden word one letter at a time.
Each wrong guess adds to a stick-figure drawing of a man hanging.
Guess the word in time, and you win!
Features of this Hangman Game
There are 5 features of this game:
Random Word Selection: A random fruit name is chosen from a predefined list as the hidden word.
Player Interaction: The player can guess letters with prompts and feedback provided for each guess.
Tracking Guesses: Correct and incorrect guesses are tracked, ensuring players don’t repeat guesses.
Limited Attempts: Players have a fixed number of attempts to guess the word correctly.
Visual Feedback: A representation of the hangman game figure is displayed with each incorrect guess.
What are the concepts used in hangman game?
1) Randomization: Randomization helps pick a word from a predefined list. By seeding the rand()
function with the current time using <strong>srand()</strong>
, the program ensures a new word is selected each time it runs.
<strong>vector wordPool = { "apple", "banana", "cherry", "grape", "kiwi" };
int randomIndex = rand() % wordPool.size();
return wordPool[randomIndex];</strong>
2) Loops and Conditionals: The loops process player guesses and conditionals to determine if a guess is correct or incorrect.
The game continues until the player wins by guessing the word or loses by exhausting their attempts.
<strong>while (remainingTries > 0) {
// Prompt for user guess
// To Check if the guess is correct or incorrect
// Update game state accordingly
}</strong>
3) String Manipulation: The hidden word is represented as a string, with underscores (_) displaying unguessed letters. Correct guesses replace the underscores with the corresponding letters.
The Complete Source Code: Hangman Game C++
#include <algorithm> #include <cstdlib> #include <ctime> #include <iostream> #include <string> #include <vector> // Define the maximum number of incorrect attempts #define MAX_WRONG_ATTEMPTS 6 using namespace std; // main class class WordGuessGame { public: WordGuessGame() { srand(static_cast<unsigned int>(time(nullptr))); hiddenWord = pickRandomWord(); guessedWord = string(hiddenWord.length(), '_'); remainingTries = MAX_WRONG_ATTEMPTS; } // function to begin the game. void start() { cout << "Welcome to Word Guess!" << endl; cout << "Category: Fruits" << endl; cout << "You have " << remainingTries << " attempts to guess the fruit name." << endl; // main game loop that continues until attempts are exhausted or the word is guessed while (remainingTries > 0) { showGameStatus(); char userGuess; cout << "Guess a letter: "; cin >> userGuess; if (isalpha(userGuess)) { userGuess = tolower(userGuess); if (hasGuessed(userGuess)) { cout << "You've already guessed that letter." << endl; } else { bool isCorrect = modifyGuessedWord(userGuess); // if the guess is correct, update the word and check if it is fully guessed if (isCorrect) { cout << "Nice guess!" << endl; if (guessedWord == hiddenWord) { showGameStatus(); cout << "Congratulations! You guessed the word: " << hiddenWord << endl; return; } } else { cout << "Incorrect guess." << endl; remainingTries--; renderHangman(remainingTries); } } } else { cout << "Please enter a valid letter." << endl; } } if (remainingTries == 0) { showGameStatus(); cout << "You're out of attempts. The word was: " << hiddenWord << endl; } } private: string hiddenWord; string guessedWord; int remainingTries; vector<char> guessedChars; // pick a random word from a predefined list string pickRandomWord() { vector<string> wordPool = { "apple", "banana", "cherry", "grape", "kiwi" }; int randomIndex = rand() % wordPool.size(); return wordPool[randomIndex]; } // check if a letter was already guessed bool hasGuessed(char letter) { return find(guessedChars.begin(), guessedChars.end(), letter) != guessedChars.end(); } // update the word after a correct guess bool modifyGuessedWord(char letter) { bool isCorrect = false; for (int i = 0; i < hiddenWord.length(); i++) { if (hiddenWord[i] == letter) { guessedWord[i] = letter; isCorrect = true; } } guessedChars.push_back(letter); return isCorrect; } // display current game status void showGameStatus() { cout << "Word: " << guessedWord << endl; cout << "Attempts left: " << remainingTries << endl; cout << "Guessed letters: "; for (char letter : guessedChars) { cout << letter << " "; } cout << endl; } // render the hangman figure progressively void renderHangman(int remainingTries) { if (remainingTries == 5) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " |" << endl; cout << " |" << endl; cout << " |" << endl; cout << " |" << endl; } else if (remainingTries == 4) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " | |" << endl; cout << " |" << endl; cout << " |" << endl; cout << " |" << endl; } else if (remainingTries == 3) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " | /|" << endl; cout << " |" << endl; cout << " |" << endl; cout << " |" << endl; } else if (remainingTries == 2) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " | /|\\" << endl; cout << " |" << endl; cout << " |" << endl; cout << " |" << endl; } else if (remainingTries == 1) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " | /|\\" << endl; cout << " | /" << endl; cout << " |" << endl; cout << " |" << endl; } else if (remainingTries == 0) { cout << " _____" << endl; cout << " | |" << endl; cout << " | O" << endl; cout << " | /|\\" << endl; cout << " | / \\" << endl; cout << " |" << endl; cout << " |" << endl; } } }; int main() { WordGuessGame game; game.start(); return 0; }
Conclusion:
The Hangman game in C++ Project has features like random word selection, interactive input, and ASCII art.
You’ll gain experience in essential concepts like loops, conditionals, and string manipulation. For more insights and tips, visit CodeHelping to enhance your learning experience!