Hello all! I am making a hangman program and am having an odd error that I can not correct! It runs perfectly, except for 1 major issue! If the person guesses a letter wrong, everything is fine, but if they guess more than one letter wrong, the rest of the letters they enter do not register as correct even if they are!
I will have my .cpp, .h Posted below; I believe the problem is in the main.cpp, where it compares the guess to the word and stores it to a separate string. I'll make that section easier to distinguish for you all (I'll put comment /// all around it)
- Here is a basic pseudo-code for how it works:
Program starts
Pulls word from wordList.txt (A separate list of 200,000+ Words)
Places word into string
Measure word length
Declare word and gamemechanics to use classes
Set number of available tries to 9 (hitpoints)
Set initial guess counter (alphabetCounter) to 0
generate new output string based on how long the word is
Set output string to all "_ _ _ _ _ _ "
++Do the rest while guesses are available
Get user letter guess
check if letter, if not, make the user enter letter
Set letter to lowercase
check if letter was already guessed, else
store guess to a section of alphabet array. Used to store and output all user's guesses
__
Output user guess(es)
___
Compare guess to newWord string
cycle through letters to see if one matches
if match, set the matching letter and location to the output array. "_ _ _ a _ _ "
check if letter was not in word.
If not in word, lower remaining tries by one.
output current guess word e.g. for cat, with an "a" guessed : " _ a _"
display gallows
set all output array values to a temp comparison string.
check if temp string is same as newWord.
if so, tell user they won.
else continue game.
if hitpoints = 0, tell user they lost.
main.cpp
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 89 90
|
int main()
{
//Good console size
HWND console = GetConsoleWindow();
RECT r;
GetWindowRect(console, &r); //stores the console's current dimensions
//MoveWindow(window_handle, x, y, width, height, redraw_window);
MoveWindow(console, r.left, r.top, 800, 600, TRUE);
system("TITLE Hangman!!!");
CurrentWord Word; //Used for any Operations Involving the Word
Attributes GameMechanics; //Used for any operations involving game variables or settings.
do
{
int correct = Word.newWord.length();
GameMechanics.hitpoints = 9; //Initial hitpoint counter
Word.alphabetCounter = 0; // initial counter value
Word.setNewWord(); //randomly pick new word
string output[Word.newWord.length()+1]; // define length of string array to length of the word to be guessed.
for(int a = 0; a < Word.newWord.length(); a++) //generate initial values of outputed guess
output[a] = " _ ";
//cout << Word.newWord <<endl; //tell word for testing purposes. REMOVE BEFORE SUBMISSION
cout << "\n\nStart Guessing!\n\n";
while(GameMechanics.hitpoints !=0)
{
Word.inputGuesses(); //get guess
Word.outputGuesses(); // out guess
///////////////////////////////////////////////////////////////////////
string str2;
for(int b = 0; b < Word.newWord.length() ;b++) //runs for all spaces in newWord. newWord is the word to be guessed.
{
for(int a = 0; a < Word.newWord.length(); a++) //same as above
{
string temp;
temp = Word.newWord.at(a);//
if(Word.alphabet == temp) //if user guess and word match
{
output[a] = Word.newWord.at(a) ; //set output to letters guessed
//str2 = str2+ Word.newWord.at(a);
}
} //for end
}//for end
cout << endl;
int hitpointCounter=0; //check var for whether or not to dock hitpoints
for(int a =0; a < Word.newWord.length(); a++) //loop through word to check
{
string temp;
temp = Word.newWord.at(a); //store word letter
if(Word.userEnteredWord == temp) //if the user's letter matches a letter in the word, raise the counter to more than 0
hitpointCounter = hitpointCounter + 1; //+1
}
if(hitpointCounter == 0) //if the counter is still zero, doc the hitpoint counter by one.
{
GameMechanics.hitpoints = GameMechanics.hitpoints - 1;
cout << "\nSorry, You were wrong\n\n"; //demoralizing statement
}
for(int a = 0; a < Word.newWord.length(); a++) //output the guesses and spaces
cout << output[a];
cout << endl;
//////////////////////////////////////////////////////////////////////
GameMechanics.displayGallows(); //display gallows
bool allLettersMatch = false;
for(int a = 0; (a < Word.newWord.length()); a++)
{
//string str2;
for (int a=0; a < Word.newWord.length();a++)
str2+=output[a];
if(str2 == Word.newWord)
{
allLettersMatch = true;
}//end if
if(allLettersMatch)
{
cout << "You Win!!!\n\n";
GameMechanics.continueQuestion(GameMechanics.recur);
break;
} //end if
}//end for
}
if(GameMechanics.hitpoints == 0)
{
cout << "\nSorry, You Lose!!!\n\n";
cout << "The word was: " << Word.newWord << endl;
}
GameMechanics.continueQuestion(GameMechanics.recur);
}//end do
while(GameMechanics.recur);
return 0;
}
|
declarations.cpp
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
|
CurrentWord::CurrentWord() //constructor
{
}
void CurrentWord::toLowerCase(string &a) //converts any string to lowercase
{
for(int i = 0; a[i] != '\0'; i++)
a[i] = tolower(a[i]);
};
void CurrentWord::setNewWord() //pulls random word from a seperate notepad .txt
{
srand ( time(NULL) ); // random number generator
//int randnum = (rand() % 216555 + 1); //random number to use in second random generator
ifstream in( "wordList.txt" ); //Title of document with possible words
if ( !in ) //if wordList isn't found, output error message.
{
cout << "wordList.txt not found. Breaking. \n\n";
return; // break program
}
int r = rand() % 200000 + 1 * (rand() % 200000); // declare r as a random number based on 216555 words
do
in >> newWord;
while ( --r >= 0 );
for(int a =0; a < sizeof(newWord); a++)
toLowerCase(newWord);
z = newWord.length();
};
void CurrentWord::outputGuesses() //displays all guesses made. guesses stored in an array
{
cout << '\n' << "\nYou have guessed the following: ";
for(int a = 0; a < alphabetCounter; a++)
cout << alphabet[a] << ",";
cout << endl;
};
void CurrentWord::inputGuesses() //input guess. stores to array
{
bool returnTrue = false;
inGuess:
cout << "Enter Letter Guess: ";
cin >> userEnteredWord;
toLowerCase(userEnteredWord);
for(int a = 0; a < userEnteredWord.length(); a++)
{
if(isalpha(userEnteredWord.at(a)))
{
returnTrue = true;
}
else if(!isalpha(userEnteredWord.at(a)))
{
cout << '\n' << "Error, please enter a letter.\n\n";
goto inGuess;
}
for(int a = 0; a < 30; a++)
{
if( userEnteredWord == alphabet[a])
{
cout << "\nYou already guessed that letter! Guess again!\n\n";
goto inGuess;
}
}
if(returnTrue)
{
alphabet[alphabetCounter] = userEnteredWord;
alphabetCounter = alphabetCounter + 1;
}
}
system("cls");
cout << newWord << endl; //REMOVE
};
Attributes::Attributes()
{
};
void Attributes::toLowerCase(string &a)
{
for(int i = 0; a[i] != '\0'; i++)
a[i] = tolower(a[i]);
};
|
Any help would be greatly appreciated! If you do use any of this code for your own project, I don't mind, just give a little credit.