Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Hangman.cpp #180

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions Hangman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std; // This will remove the need to write std:: each time

// Function to display the word with underscores for unguessed letters
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 display the current state of the hangman
void displayHangman(int attempts) {
switch (attempts) {
case 6: cout << " +---+\n |\n |\n |\n ===\n"; break;
case 5: cout << " +---+\n O |\n |\n |\n ===\n"; break;
case 4: cout << " +---+\n O |\n | |\n |\n ===\n"; break;
case 3: cout << " +---+\n O |\n /| |\n |\n ===\n"; break;
case 2: cout << " +---+\n O |\n /|\\ |\n |\n ===\n"; break;
case 1: cout << " +---+\n O |\n /|\\ |\n / |\n ===\n"; break;
case 0: cout << " +---+\n O |\n /|\\ |\n / \\ |\n ===\n"; break;
}
}

// Function to check if the word has been fully guessed
bool isWordGuessed(const vector<bool>& guessed) {
for (size_t i = 0; i < guessed.size(); ++i) {
if (!guessed[i]) return false;
}
return true;
}

int main() {
const vector<string> words = {"programming", "hangman", "cplusplus", "challenge", "development"};
srand(static_cast<unsigned int>(time(0))); // Seed for random number generation
string chosenWord = words[rand() % words.size()]; // Choose a random word
vector<bool> guessed(chosenWord.length(), false); // Track guessed letters
int attempts = 6;
char guess;

cout << "Welcome to Hangman!" << endl;

while (attempts > 0) {
cout << "\nAttempts left: " << attempts << endl;
displayHangman(attempts);
displayWord(chosenWord, guessed);
cout << "Enter a letter: ";
cin >> guess;

// Check if the guessed letter is in the word
bool correctGuess = false;
for (size_t i = 0; i < chosenWord.length(); ++i) {
if (chosenWord[i] == guess) {
guessed[i] = true;
correctGuess = true;
}
}

if (!correctGuess) {
--attempts;
cout << "Incorrect guess!" << endl;
}

if (isWordGuessed(guessed)) {
cout << "Congratulations! You've guessed the word: " << chosenWord << endl;
break;
}
}

if (attempts == 0) {
cout << "Sorry, you've run out of attempts! The word was: " << chosenWord << endl;
displayHangman(attempts); // Show final hangman state
}

return 0;
}