Make an entire C++ hangman game
- User
Make an entire C++ hangman game
- Assistant
Make an entire C++ hangman game
Creating an entire Hangman game in C++ requires implementing several components such as managing the word to guess, handling user input, displaying the current state of guessing, and maintaining the number of attempts.
Below is a simple Hangman console game in C++:
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
const std::vector<std::string> WORDS = {
"programming",
"hangman",
"game",
"cplusplus",
"code"
};
void printWelcome() {
std::cout << "Welcome to Hangman Game!" << std::endl;
std::cout << "Guess the word letter by letter." << std::endl;
}
std::string selectRandomWord() {
srand(static_cast<unsigned int>(time(nullptr))); // Seed for randomness
int randomIndex = rand() % WORDS.size();
return WORDS[randomIndex];
}
void displayStatus(const std::string& guessedWord, int remainingGuesses) {
std::cout << "Current status: " << guessedWord << std::endl;
std::cout << "Remaining guesses: " << remainingGuesses << std::endl;
}
void gameLoop(const std::string& word) {
std::string guessedWord(word.size(), '-'); // Start with blanks
int remainingGuesses = 6; // For example, player has 6 wrong guesses
while (remainingGuesses > 0 && guessedWord != word) {
displayStatus(guessedWord, remainingGuesses);
char guess;
std::cout << "Enter your guess: ";
std::cin >> guess;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
continue;
}
bool correctGuess = false;
for (size_t i = 0; i < word.length(); i++) {
if (word[i] == guess && guessedWord[i] == '-') {
guessedWord[i] = guess;
correctGuess = true;
}
}
if (!correctGuess) {
std::cout << "Incorrect guess!" << std::endl;
remainingGuesses--;
}
}
if (guessedWord == word) {
std::cout << "Congratulations! You've won!" << std::endl;
std::cout << "The word was: " << word << std::endl;
} else {
std::cout << "Game over. The correct word was: " << word << std::endl;
}
}
int main() {
printWelcome();
std::string wordToGuess = selectRandomWord();
gameLoop(wordToGuess);
return 0;
}
This example includes a simple Hangman game where the player guesses a word letter by letter. Here's a brief overview of the functions:
printWelcome() just prints a welcome message and instructions.selectRandomWord() randomly selects a word from a predefined vector of words.displayStatus() prints the current guessed word and the number of remaining guesses.gameLoop() is where the logic of the game takes place. It keeps asking the user for input until the word is guessed or the player runs out of guesses.main() function initializes the game and starts the game loop.To compile and run this code, you need to have a C++ compiler such as g++. You can compile the program using g++ hangman.cpp -o hangman and then run it with ./hangman on Unix-like systems or hangman.exe on Windows.
Please note that this code does not handle uppercase/lowercase distinctions and assumes the user enters single characters as guesses. It can be considered a starting point and may be extended and improved by including input validation, case-insensitivity, a system to draw the hangman, additional word sets, multiple difficulty levels, or even a graphical user interface (GUI).