Question: can someone help me fix this program so it does what the instructions say Write a C++ program which will simulate the Hangman game. Create

can someone help me fix this program so it does what the instructions say

Write a C++ program which will simulate the Hangman game. Create an Array of Strings to hold the words to guess in the game and have the program randomly choose a word. In fact, let's have three groups of words to choose from - Easy, Medium and Hard. Allow the use to choose the difficulty level at the beginning of the game and let them change the level between games. Have around 10 words for each group. Give the user 6 attempts to guess the letters or word.

Make sure you prompt the user for their name and address them by their name. Once the game begins generate a random number to pick the word. Show underscores ( _ ) for each letter (with spacing between each underscore ) AND tell the user how many letters are in the word.

You can count the number of letters by using the length() method.

string word = "Monkey"; int len = word.length(); cout<

Then allow the user to guess a letter (upper or lower case ) and search the word to determine if it has the letter(s). You will need to loop through the word and use the substr() method. Reveal the letters in the word by replacing the underscores with the proper letter.

If the user correctly guesses a letter add 1 point to their score. We will have two scores, one will be the current game score which will be reset back to zero at the start of each new round and the other will be a total score for all the rounds being played.

If the user makes a guess of a letter which is not in the word, tell them and subtract 1 for the total number of attempts. Do not deduct from their score. The round ends when the player has six failed attempts. Reveal the word and give them their scores. Ask them if they want to play another game and allow them to change the level of difficulty. Don't allow any previously used words to be used again.

Here is how to initialize an Array of strings

// Initialize array of string string myArray[] = { "dog", "cat", "rat", "monkey"};

This will give us a string array with four items.

code:

#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; int currentScore = 0;

int randRange(int low, int high){ return rand() % (high - low + 1) + low; }

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[randRange(0, 11) % EASY_WORDS]; }else if (choice == "M" or choice == "m"){ word = mediumWords[randRange(0, 11) % MEDIUM_WORDS]; }else if (choice == "H" or choice == "h"){ word = hardWords[randRange(0, 11) % HARD_WORDS]; }else{ cout << "Invalid Choice. "; 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; currentScore++; } } return correctGuess; }

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

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

cout << "Welcome to Hangman! "; cout << "You have " << MAX_ATTEMPTS << " attempts to guess the word. "; cout << "The word has " << wordLength << " letters. "; showProgress(word, progress);

while (attempts != 0) { cout << "Enter a letter: "; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!