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 |
#include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; // Function to display the current state of the guessed word void displayWord(const string& word, const vector<bool>& guessed) { for (size_t i = 0; i < word.length(); ++i) { if (guessed[i]) { cout << word[i] << ' '; } else { cout << '_ ' ; } } cout << endl; } // Function to play the Hangman game void playHangman(const string& word) { vector<bool> guessed(word.length(), false); int attempts = 6; string guessedLetters; cout << "Welcome to Hangman!" << endl; cout << "Guess the word: " << endl; while (attempts > 0) { displayWord(word, guessed); cout << "Attempts remaining: " << attempts << endl; cout << "Guess a letter: "; char guess; cin >> guess; if (guessedLetters.find(guess) != string::npos) { cout << "You already guessed that letter!" << endl; continue; } guessedLetters += guess; bool correctGuess = false; for (size_t i = 0; i < word.length(); ++i) { if (word[i] == guess) { guessed[i] = true; correctGuess = true; } } if (!correctGuess) { --attempts; cout << "Incorrect guess!" << endl; } // Check if the player has guessed the entire word if (all_of(guessed.begin(), guessed.end(), [](bool g) { return g; })) { cout << "Congratulations! You guessed the word: " << word << endl; return; } } cout << "Game over! The word was: " << word << endl; } int main() { string word; cout << "Enter the word for the Hangman game: "; cin >> word; transform(word.begin(), word.end(), word.begin(), ::toupper); // Convert to uppercase playHangman(word); return 0; } |
Explanation:
- Display Word (
displayWord
):- Shows the current state of the word with correctly guessed letters and underscores for unguessed letters.
- Play Hangman (
playHangman
):- Initializes the
guessed
vector to track guessed letters andattempts
to the maximum number of wrong guesses allowed. - Loops until the player either guesses the word or runs out of attempts.
- Checks if the guessed letter has been guessed before and updates the state of the word accordingly.
- Deducts an attempt for incorrect guesses and informs the player of their remaining attempts.
- Ends the game if the player has guessed all letters or exhausted attempts, displaying the outcome.
- Initializes the
- Main Function (
main
):- Prompts the user to input the word to be guessed.
- Converts the word to uppercase to standardize the input.
- Calls
playHangman
to start the game.
Possible Enhancements:
- Word Selection: Implement a dictionary of words and allow random selection or user-defined categories.
- Input Validation: Add checks to ensure valid input (single letters only).
- Enhanced UI: Create a graphical user interface or web interface for a more interactive experience.
- Difficulty Levels: Adjust the number of attempts or add different difficulty levels.