Introduction
The Hangman game is a popular word guessing game where players try to guess a hidden word by suggesting letters within a certain number of guesses.
The goal of this game is to guess the word correctly before running out of attempts, which are typically represented by drawing parts of a hangman figure.
Objective
The objective of this task is to implement the Hangman game using the C++ programming language. In this game, the program randomly selects a word from a list,
and the player must guess the word by inputting letters. The game will display the number of correct guesses and the remaining attempts. If the player guesses all letters of the word correctly within the given attempts, they win. If they run out of attempts, they lose.
Hangman Game Code in C++
#include #include #include #include using namespace std; // Function to print the current state of the game (word with guessed letters) void displayWord(const string &word, const string &guessedLetters) { for (int i = 0; i < word.length(); ++i) { if (guessedLetters.find(word[i]) != string::npos) cout << word[i] << " "; else cout << "_ "; } cout << endl; } // Function to check if the player has guessed the word correctly bool isWordGuessed(const string &word, const string &guessedLetters) { for (int i = 0; i < word.length(); ++i) { if (guessedLetters.find(word[i]) == string::npos) return false; } return true; } int main() { string words[] = {"programming", "computer", "hangman", "game", "cplusplus"}; int numWords = sizeof(words) / sizeof(words[0]); // Randomly select a word from the list srand(time(0)); string word = words[rand() % numWords]; string guessedLetters = ""; // Stores guessed letters int attempts = 6; // Number of attempts before losing the game cout << "Welcome to the Hangman Game!" << endl; // Game loop while (attempts > 0) { cout << "\nWord to guess: "; displayWord(word, guessedLetters); cout << "Guessed letters: " << guessedLetters << endl; cout << "Remaining attempts: " << attempts << endl; cout << "Enter a letter to guess: "; char guess; cin >> guess; // Check if the letter is already guessed if (guessedLetters.find(guess) != string::npos) { cout << "You've already guessed that letter!" << endl; continue; } // Add the guessed letter to the guessedLetters string guessedLetters += guess; // Check if the guess is correct if (word.find(guess) == string::npos) { attempts--; // Decrease attempts if the guess is incorrect cout << "Incorrect guess!" << endl; } else { cout << "Correct guess!" << endl; } // Check if the player has won if (isWordGuessed(word, guessedLetters)) { cout << "\nCongratulations! You've guessed the word: " << word << endl; break; } } if (attempts == 0) { cout << "\nSorry, you've lost! The word was: " << word << endl; } return 0; }
Explanation of the Program Structure
The Hangman game in C++ is implemented using standard input and output. Here is an explanation of how the program works:
- Word Selection: A predefined list of words is provided, and the program selects a random word using
rand()
function. - Display the Word: The
displayWord()
function shows the current state of the word, replacing unguessed letters with underscores (“_”). - Input Handling: The user is asked to input a letter. The program checks whether the input letter has already been guessed or not.
- Guess Evaluation: If the guessed letter is correct, it is added to the list of guessed letters. If incorrect, the number of remaining attempts is decreased.
- Game Over Conditions: The game ends if the player either guesses all letters of the word correctly (win) or runs out of attempts (lose).
How to Run the Program:
1. Copy the code into a C++ compiler or IDE like Code::Blocks, Visual Studio, or an online compiler.
2. Compile the program. In most IDEs, this can be done by pressing the “Build” or “Compile” button.
3. Run the program. The terminal or console window will display the game and prompt for inputs.
4. Play the game by guessing letters, and try to guess the word before you run out of attempts!