🌿 Smart Garden Manager in C++ with Robotics, UI, Drones, and Sound "A Rainwater Conservation System for Tomorrow’s Farms"

Guys, I'm back with another cool project—the Hangman game! This one is going to be pretty interesting, and I want to warn you beforehand that it's not one of the easiest in the series. However, since we have built many projects by now, we should be able to tackle this one as well. Just focus on the project, and if there's anything you're confused about, feel free to ask. Let's go!
cpp#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>
using namespace std;
// Function to display the current state of the word
void displayWord(const string& word, const vector<bool>& guessed) {
for (size_t i = 0; i < word.size(); ++i) {
if (guessed[i]) {
cout << word[i] << " ";
} else {
cout << "_ ";
}
}
cout << endl;
}
// Function to check if the player has won
bool hasWon(const vector<bool>& guessed) {
for (bool letterGuessed : guessed) {
if (!letterGuessed) return false;
}
return true;
}
int main() {
// List of words for the game
vector<string> wordList = {"programming", "hangman", "cplusplus", "developer", "project"};
// Randomly select a word from the list
srand(static_cast<unsigned int>(time(0)));
string word = wordList[rand() % wordList.size()];
// Vector to track which letters have been guessed correctly
vector<bool> guessed(word.size(), false);
// Variables to track the game state
int lives = 6; // Number of incorrect guesses allowed
vector<char> incorrectGuesses; // Store incorrect guesses
cout << "Welcome to Hangman!" << endl;
// Main game loop
while (lives > 0 && !hasWon(guessed)) {
cout << "\nCurrent word: ";
displayWord(word, guessed);
cout << "Lives remaining: " << lives << endl;
if (!incorrectGuesses.empty()) {
cout << "Incorrect guesses: ";
for (char c : incorrectGuesses) {
cout << c << " ";
}
cout << endl;
}
// Get the player's guess
char guess;
cout << "Enter a letter: ";
cin >> guess;
// Check if the guess is correct
bool found = false;
for (size_t i = 0; i < word.size(); ++i) {
if (tolower(word[i]) == tolower(guess)) {
guessed[i] = true;
found = true;
}
}
// Handle incorrect guesses
if (!found) {
if (find(incorrectGuesses.begin(), incorrectGuesses.end(), guess) == incorrectGuesses.end()) {
incorrectGuesses.push_back(guess);
--lives;
} else {
cout << "You already guessed that letter!" << endl;
}
}
}
// End of the game
if (hasWon(guessed)) {
cout << "\nCongratulations! You guessed the word: " << word << endl;
} else {
cout << "\nGame over! The word was: " << word << endl;
}
return 0;
}
<iostream>
: For input and output functions.<string>
: For handling words used in guessing.<vector>
: For tracking the guessed letters and storing incorrect guesses.<cstdlib>
& <ctime>
: For generating random numbers.vector<string>
:cppvector<string> wordList = {"programming", "hangman", "cplusplus", "developer", "project"};
rand()
function is used to randomly select a word from this list.vector<bool> guessed
: A vector initialized with false
values to track whether each letter of the word has been guessed correctly.lives
: An integer to track the number of incorrect guesses allowed.vector<char> incorrectGuesses
: A vector to store all incorrect guesses.displayWord
prints the word with correctly guessed letters shown and unknown letters represented as underscores.hasWon
returns true
if all letters have been guessed correctly; otherwise, it returns false
.cppwhile (lives > 0 && !hasWon(guessed)) { ... }
runs as long as the player has lives remaining and has not yet guessed the word.guessed
is updated; if not, the guess is added to incorrectGuesses
and lives are decremented.After the loop, a message is displayed indicating whether the player won (guessed the word) or lost (ran out of lives).
Replayability is ensured through random selection of words, dynamic display of underscores and correctly guessed letters, and tracking of wrong guesses.
Educational Games in Schools
Schools use the Hangman game to teach young children spelling and vocabulary skills. A digital version of this game can serve as an engaging and interactive educational tool.
Apps for Learning Languages
This game is often integrated into language learning apps. It helps users practice spelling and improve their vocabulary in a fun and engaging way.
Word and Puzzle Game Apps
Many web and mobile-based puzzle games incorporate Hangman-style systems to enhance user engagement and provide entertaining ways to learn new words.
Case Study 1: Enhancement of Classroom Learning
A high school teacher integrated a C++ based Hangman game into English vocabulary lessons. As a result, students started competing to guess words and improve their spelling. The randomized selection of words made the activity highly engaging.
Case Study 2: Development of a Word Game App
A small startup developed an app for language learning that integrated the Hangman game. Users had the option to choose different difficulty levels and categories such as history, technology, or science. This approach improved both retention and engagement among users.
Case Study 3: Cognitive Training for Seniors
A research team developed a Hangman game as part of a cognitive training project for seniors. The game helped maintain mental agility by challenging participants' memory and word-recognition skills.
Handling Big Lists of Words Efficiently
Rather than hardcoding the words, using databases or external files can provide a larger variety of vocabulary, making the game more scalable.
Improving Search Functionality
Optimized search algorithms, such as trie-based word lookups, can extend the game's features—for example, by suggesting words from a dictionary.
Using Graphics for Enhanced User Experience
Incorporating graphical elements with libraries like SDL or SFML can make the Hangman game more engaging by visually representing the hangman figure and other game elements.
Multiplayer Mode
The game can be extended to support two players, where one player enters a word and the other guesses it. This feature would improve interactivity and add a social element to the game.
I hope you enjoyed this project. See you next time with another cool C++ mini project!
Comments
Post a Comment