Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help fixing this program so when the user guesses the right word the program congragulates them and ask if they wanna play again

i need help fixing this program so when the user guesses the right word the program congragulates them and ask if they wanna play again

#include #include #include #include

using namespace std;

const int EASY_WORDS = 10; const int MEDIUM_WORDS = 10; const int HARD_WORDS = 10; const int MAX_ATTEMPTS = 6;

string easyWords[EASY_WORDS] = {"apple", "banana", "cherry", "dog", "elephant", "fish", "guitar", "horse", "ice", "jaguar"}; string mediumWords[MEDIUM_WORDS] = {"keyboard", "lion", "monkey", "night", "octopus", "penguin", "queen", "rabbit", "sugar", "turtle"}; string hardWords[HARD_WORDS] = {"universe", "violet", "whale", "xylophone", "yellow", "zebra", "alcohol", "biology", "computer", "dinosaur"};

int totalScore = 0;

void chooseDifficulty(string &word, int &wordLength) { string choice = ""; cout << "Choose Difficulty: (E)asy, (M)edium, (H)ard: "; cin >> choice; if (choice == "E" or choice == "e"){ word = easyWords[rand() % EASY_WORDS]; }else if (choice == "M" or choice == "m"){ word = mediumWords[rand() % MEDIUM_WORDS]; }else if (choice == "H" or choice == "h"){ word = hardWords[rand() % HARD_WORDS]; }else{ cout << "Invalid choice, please try again. "; chooseDifficulty(word, wordLength); } wordLength = word.length(); }

void showProgress(string word, string progress) { cout << " Word: "; for (int i = 0; i < word.length(); i++) { cout << progress[i] << " "; } cout << endl; }

bool updateProgress(string word, string &progress, char guess) { bool correctGuess = false; for (int i = 0; i < word.length(); i++) { if (word[i] == guess) { progress[i] = guess; correctGuess = true; } } return correctGuess; }

int main(){ string word, progress; int currentScore = 0; int attempts = MAX_ATTEMPTS; int wordLength = 0; char guess; bool correctGuess, won;

chooseDifficulty(word, wordLength); progress = string(wordLength, '_');

cout << "Welcome to Hangman! "; cout << "Enter your name: "; string name; cin >> name; cout << "Hello " << name << "! ";

while (attempts > 0 && currentScore < wordLength){ cout << "You have " << attempts << " attempts left. "; showProgress(word, progress); cout << "Guess a letter: "; cin >> guess; correctGuess = updateProgress(word, progress, guess); if (!correctGuess) { attempts--; } }

if (currentScore == wordLength) { won = true; } else { won = false; }

if (won) { cout << "Congratulations! You won. "; cout << "The word was " << word << endl; } else { cout << "You lost. "; cout << "The word was " << word << endl; }

totalScore += currentScore; cout << "Your total score is " << totalScore << endl;

cout << "Do you want to play another game? (y/n) "; string playAgain; cin >> playAgain;

if (playAgain == "y" or playAgain == "Y") { currentScore = 0; chooseDifficulty(word, wordLength); progress = string(wordLength, '_'); attempts = MAX_ATTEMPTS; } else { cout << "Thanks for playing, " << name << "! "; return 0; }

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

1. Who should participate and how will participants be recruited?

Answered: 1 week ago