For the last part of this problem, if player 2 loses the game of hangman, I need to display the letters they did get right. In order to do that, I believe that I need to traverse vector v for the elements that exist in the string hiddenword, and then print those characters that match.
Can anyone offer further guidance on how to do this? I have had a lot of fun on this problem, and I'd love to be able to sort out the final part.
int main ()
{
string name;
char letter;
char ans;
const int MAX_TRIES=5;
string hiddenword;
string word;
vector<char> v;
do
{
int num_of_wrong_guesses=0;
//Introduction and secret word selection.
cout << "Welcome to the 2-player word guessing game!" << endl;
cout << "\nPlayer 2 please look away from the screen"
<< "\nPlayer 1 please enter any word: " << endl;
cin >> hiddenword;//player 1's entry
bool isAlphabetic = true;
for (unsigned i = 0; i < hiddenword.size(); ++i)
if (!isalpha(hiddenword[i]))
{
isAlphabetic = false;
cout << "\nPlayer 1, Your entry was not alphabetic!!" << endl;
cout << "\nPlease launch the game again and make a fully alphabetic entry." << endl;
return 0;
}
word=hiddenword;
// Initialize the secret word with the * character.
string unknown(word.length(),'*');
// welcome the user
cout << "\n\nPlayer 2, it is now your turn to guess the word!";
cout << "\n\nEach letter is represented by a star.";
cout << "\n\nYou should type only one letter in per guess";
cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the word.";
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << "\n\n" << unknown;
if (v.size()>0)
{
cout << "\n\nYou have used the following letters: " << endl;
for (int i=0; i < v.size(); i++)
cout << v[i] << endl;
}
cout << "\n\nGuess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Sorry, the entry is not in the word. Try again!" << endl;
num_of_wrong_guesses++;
v.push_back (letter);
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;;
v.push_back (letter);
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << "\nSorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;}
cout << "enter Y or y to repeat, "
<< "any other character ends." << endl;
cin >> ans;
} while ('Y' == ans || 'y' == ans);
return 0;
}
/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
cout << matches++ << endl;
}
}
return matches;
}