0% found this document useful (0 votes)
32 views3 pages

p1 Hangman

This document contains a C++ implementation of the Hangman game. The game randomly selects a word from a predefined list, allows the player to guess letters, and tracks the number of remaining guesses. The player wins by guessing the word before running out of guesses, with visual feedback provided through a hangman display.

Uploaded by

survivor000111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

p1 Hangman

This document contains a C++ implementation of the Hangman game. The game randomly selects a word from a predefined list, allows the player to guess letters, and tracks the number of remaining guesses. The player wins by guessing the word before running out of guesses, with visual feedback provided through a hangman display.

Uploaded by

survivor000111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

p1 hangman

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>

class Hangman {
private:
std::string targetWord;
std::string guessedWord;
std::vector<char> guessedLetters;
int maxGuesses;
int remainingGuesses;

public:
Hangman() : maxGuesses(6), remainingGuesses(6) {
srand(time(nullptr));
std::vector<std::string> words = {"apple", "banana", "cherry", "durian",
"elderberry", "fig", "grape", "honeydew"};
int randomIndex = rand() % words.size();
targetWord = words[randomIndex];
guessedWord = std::string(targetWord.length(), '_');
}

void play() {
std::cout << "Welcome to Hangman!\n";
std::cout << "Guess the letters of the word to save the hangman.\n";
std::cout << "You have " << maxGuesses << " guesses.\n";

while (remainingGuesses > 0 && guessedWord != targetWord) {


std::cout << "\n";
displayHangman();
std::cout << "Guessed word: " << guessedWord << "\n";
std::cout << "Guessed letters: ";
displayGuessedLetters();
std::cout << "\n";

char guess;
std::cout << "Enter your guess: ";
std::cin >> guess;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "\n";

if (std::isalpha(guess)) {
guess = std::tolower(guess);
if (std::find(guessedLetters.begin(), guessedLetters.end(),
guess) != guessedLetters.end()) {
std::cout << "You already guessed that letter. Try again.\n";
} else {
guessedLetters.push_back(guess);
if (isLetterInWord(guess)) {
std::cout << "Correct guess!\n";
updateGuessedWord(guess);
} else {
std::cout << "Wrong guess!\n";
remainingGuesses--;
}
}
} else {
std::cout << "Invalid input. Please enter a letter.\n";
}
}

if (guessedWord == targetWord) {
std::cout << "Congratulations! You guessed the word: " << targetWord <<
"\n";
} else {
std::cout << "Game over! You couldn't guess the word. The word was: "
<< targetWord << "\n";
}
}

private:
void displayHangman() {
std::cout << " ________ \n";
std::cout << "| | \n";
std::cout << "| ";
if (remainingGuesses < maxGuesses)
std::cout << "O";
std::cout << " \n";
std::cout << "| ";
if (remainingGuesses < maxGuesses - 1)
std::cout << "/";
if (remainingGuesses < maxGuesses - 2)
std::cout << "|";
if (remainingGuesses < maxGuesses - 3)
std::cout << "\\";
std::cout << " \n";
std::cout << "| ";
if (remainingGuesses < maxGuesses - 4)
std::cout << "/";
std::cout << " \\";
std::cout << " \n";
std::cout << "| \n";
std::cout << "| \n";
std::cout << "| \n";
}

void displayGuessedLetters() {
for (char letter : guessedLetters)
std::cout << letter << " ";
std::cout << "\n";
}

bool isLetterInWord(char letter) {


bool found = false;
for (int i = 0; i < targetWord.length(); i++) {
if (targetWord[i] == letter) {
guessedWord[i] = letter;
found = true;
}
}
return found;
}
void updateGuessedWord(char letter) {
for (int i = 0; i < targetWord.length(); i++) {
if (targetWord[i] == letter)
guessedWord[i] = letter;
}
}
};

int main() {
Hangman game;
game.play();

return 0;
}

You might also like