Hangman issue
Mar 9, 2013 at 5:01am UTC
Hi everyone,
I am nearly finished writing a good version of hangman, the issue that I am having is that if the user enters a correct letter twice, it messing with a counter for valid guesses, that I have compared to the number of unique letters in a game word. I do not want the counter to increment or this causes my output for complete word guessed to output prematurely. I am only posting one function from the program, I am only using string functions on this one, so please if you are going to help, please keep it in the realm of string functions. thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
void getLetterFromUser(string playWord,int occurance)
{
int tryCount = 0;
int goodGuess = 0;
int x;
do
{
char getLetter;
gotoxy(40,6);
cout << "Choose a letter: \b" ;
cin >> getLetter;
getLetter = toupper(getLetter);
int good = 0;
unsigned int gotIt = alphabet1.find(getLetter);
gotoxy(11 + (gotIt),3);
cout << "*" ;
unsigned correct = playWord.find(tolower(getLetter),0);
if (correct != -1)//string::npos)
{
gotoxy(40 + (correct*2),15);
cout << getLetter;
good = 1;
}
else
{
good = 0;
}
for (x = 1;x < playWord.length();++x)
{
correct = playWord.find(tolower(getLetter),correct + 1);
if (correct != -1)//string::npos)
{
gotoxy(40 + (correct*2),15);
cout << getLetter;
}
}
if (good < 1)
{
tryCount++;
gotoxy(30,18);
cout <<"Sorry, you have guessed wrong, press [enter]" ;
cin.get();
cin.get();
displayHangman(tryCount,playWord);
clrscr(30,18);
}
if (good == 1)
{
goodGuess++;
gotoxy(22,20);
if (goodGuess == occurance)
{
congrats();
}
}
}while (tryCount != 6);
}
Mar 10, 2013 at 3:44pm UTC
I solved my own issue, i decided to save guessed letters into a new string and if the user guessed the same right letter twice not to penalize them with a try.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
void getLetterFromUser(string playWord)
{
int tryCount = 0;
int goodGuess = 0;
string findSingle;
int singleLetter;
int x;
string guessedSoFar;
do
{
char getLetter;
gotoxy(22,19);
cout << playWord;
gotoxy(40,6);
cout << "Choose a letter: \b" ;
cin >> getLetter;
getLetter = toupper(getLetter);
int good = 0;
unsigned int gotIt = alphabet1.find(getLetter);
gotoxy(11 + (gotIt),3);
cout << "*" ;
for (int i = 0; i < playWord.length(); i++)
{
singleLetter= findSingle.find(playWord[i]);
if ( singleLetter < 0)
{
findSingle += playWord[i];
}
}
for (int j=0;j<guessedSoFar.length();j++)
{
unsigned guessed = guessedSoFar.find(getLetter);
if (guessed != -1)
{
goodGuess--;
break ;
}
}
unsigned correct = playWord.find(tolower(getLetter),0);
if (correct != -1)//string::npos)
{
gotoxy(40 + (correct*2),15);
cout << getLetter;
good = 1;
}
else
{
good = 0;
}
for (x = 1;x < playWord.length();++x)
{
correct = playWord.find(tolower(getLetter),correct + 1);
if (correct != -1)//string::npos)
{
gotoxy(40 + (correct*2),15);
cout << getLetter;
}
}
if (good < 1)
{
tryCount++;
gotoxy(30,17);
cout <<"Sorry, you have guessed wrong, press [enter]" ;
cin.get();
cin.get();
displayHangman(tryCount,playWord);
clrscr(30,17);
}
if (good == 1)
{
goodGuess++;
guessedSoFar += getLetter;
if (goodGuess == findSingle.length())
{
congrats();
}
}
}while (tryCount != 6);
}
Topic archived. No new replies allowed.