Ok, so I've got a lil' two player word guessing game where the first player enters a 5 letter word into the program and then the second player attempts to figure out the word by guessing letters. The second player has only 10 incorrect guesses before losing the game. I need to change this program so that if the second player guesses a letter that they've ALREADY guessed an error message will display and they are NOT penalized for it, (the numIncorrect should not raise +1)
And here's the code...
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace std;
int main()
{
string origWord = "";
string letter = "";
char dashReplaced = 'N';
char gameOver = 'N';
int numIncorrect = 0;
string displayWord = "-----";
//get original word
while (origWord.length() != 5)
{
cout << "Enter a 5-letter word in uppercase: ";
getline(cin, origWord);
} //end while
system("cls"); //clear the screen
//start guessing
cout << "Guess this word: " << displayWord << endl;
while (gameOver == 'N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
//search for the letter in the original word
for (int x = 0; x < 5; x += 1)
{
//if the current character matches the letter, replace the corresponding dash in the displayWord variable and then set the dashReplaced variable to 'Y'
if (origWord.substr(x, 1) == letter)
{
displayWord.replace(x, 1, letter);
dashReplaced = 'Y';
} //end if
} //end for
// if a dash was replaced, check whether the displayWord variable contains another dash
if (dashReplaced == 'Y')
{
//if the displayWord variable does not contain any dashed, the game is over
if (displayWord.find("-", 0) == -1)
{
gameOver = 'Y';
cout << endl << "Yes, the word is " << origWord << endl;
cout << "Great guessing!" <<endl;
}
else //otherwise, continue guessing
{
cout << endl << "Guess this word: " << displayWord << endl;
dashReplaced = 'N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
//add 1 to the number of incorrect guesses
numIncorrect += 1;
//if the number of incorrect guesses is 10, the game is over
if (numIncorrect == 10)
{
gameOver = 'Y';
cout << endl << "Sorry the word is " << origWord << endl;
} //end if
} //end if
} //end while
return 0;
} //end of main function
The way I see it, you could store all of the guesses in the vector. Then, every iteration of the while loop, check f the inputted guess matches any element in the vector.
Below is an edited version of your code.
Added function on starts on line 36.
#include <iostream>
#include <string>
#include <cstdlib>
#include <vector>
usingnamespace std;
int main()
{
string origWord = "";
string letter = "";
char dashReplaced = 'N';
char gameOver = 'N';
int numIncorrect = 0;
string displayWord = "-----";
vector <string> previousGuesses;
//get original word
while (origWord.length() != 5)
{
cout << "Enter a 5-letter word in uppercase: ";
getline(cin, origWord);
} //end while
system("cls"); //clear the screen
//start guessing
cout << "Guess this word: " << displayWord << endl;
while (gameOver == 'N')
{
cout << "Enter an uppercase letter: ";
cin >> letter;
/* Loops through every element in the vector.
* previousGuesses.size() denotes how many elements are in the vector
*/
for (int i = 0; i < previousGuesses.size(); i++)
{
// Check if the guess is in the list of
if (letter == previousGuesses[i])
{
cout << "\nYou already entered that letter! " << endl;
cout << "Please enter a different letter: " << endl;
cin >> letter;
}
}
// Add the guess to the list of guesses
previousGuesses.push_back(letter);
//search for the letter in the original word
for (int x = 0; x < 5; x += 1)
{
//if the current character matches the letter, replace the corresponding dash in the displayWord variable and then set the dashReplaced variable to 'Y'
if (origWord.substr(x, 1) == letter)
{
displayWord.replace(x, 1, letter);
dashReplaced = 'Y';
} //end if
} //end for
// if a dash was replaced, check whether the displayWord variable contains another dash
if (dashReplaced == 'Y')
{
//if the displayWord variable does not contain any dashed, the game is over
if (displayWord.find("-", 0) == -1)
{
gameOver = 'Y';
cout << endl << "Yes, the word is " << origWord << endl;
cout << "Great guessing!" <<endl;
}
else //otherwise, continue guessing
{
cout << endl << "Guess this word: " << displayWord << endl;
dashReplaced = 'N';
} //end if
}
else //processed when dashReplaced contains 'N'
{
//add 1 to the number of incorrect guesses
numIncorrect += 1;
//if the number of incorrect guesses is 10, the game is over
if (numIncorrect == 10)
{
gameOver = 'Y';
cout << endl << "Sorry the word is " << origWord << endl;
} //end if
} //end if
} //end while
return 0;
} //end of main function