1) I need to make the game ask the player if they would like to play again and repeat if they choose yes. I have tried to add a loop in a few different ways but have not made any progress so far.
2) When a letter is guessed that is repeated in the hidden word only 1 letter is added. EX: Hidden word: difficult Guess letter: f Output: **f******
srand(time(0)); //seed for the random function.
string words[5] = { "programming", "technical", "computer", "language", "difficult" };//Words I chose.
string word; //string for the word with asterisks.
word = words[rand() % 5]; //Randomly chooses from the lists of words I provided.
//Create a temporary holder for the word without asterisks.
string temporary;//string for the original word without the asterisks. Used for searching for characters.
temporary = word;//Makes the temporary string equal to the word before the asterisks are placed there.
//Change the letters in the word to asterisks.
int length = word.length();
for (int i = 0; i < (int)word.length(); i++)
{
word[i] = '*';
}
//Prompt the user to guess a character in the word.
cout << "(Guess) Enter a letter in word " << word << " > ";
string letter;
cin >> letter;
//See if the letter entered matches any of the letters in the chosen word. If so the asterisks need to be replaced with
//the actual letter.
//I will use the temporary string to compare my answers and then correct the word as the game progresses.
//A wrong guess counter needs to start here and be incremented every time a wrong guess is made.
//Placeholder is simply to keep the loop running.
int wrongGuess = 0;
string placeholder;
placeholder = word;
do
{
//Checks to make sure that the letter is part of the word.
while (temporary.find(letter) != string::npos)
{
//If the letter is part of the word the letter is put in the place of the asterisk in the correct location.
word.replace(temporary.find(letter), 1, letter);
//Then part of the placeholder is erased.
placeholder.erase(1, 1);
//The user is then promted to enter another word.
cout << "(Guess) Enter a letter in the word " << word << " > " << letter << " ENTER ";
cin >> letter;
}
//If the letter entered is not part of the word...
if (temporary.find(letter) == string::npos)
{
//The wrong guess counter is incremented.
wrongGuess++;
//A message is displayed.
cout << letter << " is not part of the word!" << endl;
//And the user is prompted to enter another word.
cout << "(Guess) Enter a letter in the word " << word << " > ";
cin >> letter;
}
//If the letter has already been guessed...
if (word.find(letter) != string::npos)
{
//A message is displayed
cout << letter << " is already part of the word." << endl;
}
}
//All of this is done until the placeholder is empty.
while (placeholder.empty() == false);
//When the placeholder word becomes empty the game is over and the word has been guessed.
//Display a message to the user to show that the word has been guess and how many missed guesses there were.
if (placeholder.empty() == true)
cout << "You guessed the word!!! The word is " << word << ".";
cout << "You missed " << wrongGuess << " time(s)." << endl;
return 0;
}