| | |
Correct Answers in Hangman
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
Alright, I'm down to the nitty gritty absolute end of a fully functional hangman game. There is just one part where I am absolutely stuck. When you get a wrong guess, the game successfully draws the gallows. I have no issues with wrong guesses. It's the right guesses that has me stumped. Here is what I have for this:
When you guess the correct letter it DOES display ALL the instances of that character in the correct places. The problem I'm having is, when it loops around to guess again, how do I keep the correct guess that was just made and add to that guess with the next correct guess while putting the NEW letters in their correct instances as well.
Example of how it is now:
Loop 1: n_n__n__
Loop 2: _i______
Loop 3: ___t____
The word here should be nintendo and it should ALSO do this:
Loop 1: n_n__n__
Loop 2: nin__n__
Loop 3: nint_n__
Suggestions?????
C++ Syntax (Toggle Plain Text)
guess(); //call member function of letter chosen ltrpos = gameWord.find(gameGuess); cout << "You have chosen the letter " << gameGuess << '.' << endl; if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice) { for (i = 0; i < gameWord.length(); i++) { if (gameGuess == gameWord[i]) { cout << gameGuess; } else cout << '_'; } cout << endl; cout << "Wow, nice guess!!! Guess another letter." << endl;
When you guess the correct letter it DOES display ALL the instances of that character in the correct places. The problem I'm having is, when it loops around to guess again, how do I keep the correct guess that was just made and add to that guess with the next correct guess while putting the NEW letters in their correct instances as well.
Example of how it is now:
Loop 1: n_n__n__
Loop 2: _i______
Loop 3: ___t____
The word here should be nintendo and it should ALSO do this:
Loop 1: n_n__n__
Loop 2: nin__n__
Loop 3: nint_n__
Suggestions?????
You don't seem to be stuffing the letter! To save it!
Or some indication you have that letter within the word that was already tested!
Or some indication you have that letter within the word that was already tested!
C Syntax (Toggle Plain Text)
if (gameGuess == gameWord[i]) // matching letter found? { myWord[i] = gameGuess; // <---- Stuff new letter cout << gameGuess; } else if (myWord[i] ) cout << myWord[i]; else cout << '_'; }
Last edited by wildgoose; Jul 19th, 2009 at 5:20 am. Reason: alteration
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
3.Make changes in the userGuess string.
4. Search for any underscores remaining in the userGuess string.
If there are , the word is stilll incomplete. Loop again.
Edit:
@wildgoose: I think gameWord is the original string.
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
3.Make changes in the userGuess string.
C++ Syntax (Toggle Plain Text)
guess(); //call member function of letter chosen ltrpos = gameWord.find(gameGuess); cout << "You have chosen the letter " << gameGuess << '.' << endl; if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice) { for (i = 0; i < gameWord.length(); i++) { if (gameGuess == gameWord[i]) { //Instead of cout << gameGuess . I hope gameGuess is a // a single character. userGuess[i] = gameGuess; } } //After the for loop display the changed string altogether cout << userGuess; //This will retain changes
If there are , the word is stilll incomplete. Loop again.
Edit:
@wildgoose: I think gameWord is the original string.
Last edited by zalezog; Jul 19th, 2009 at 5:17 am.
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
3.Make changes in the userGuess string.
4. Search for any underscores remaining in the userGuess string.C++ Syntax (Toggle Plain Text)
guess(); //call member function of letter chosen ltrpos = gameWord.find(gameGuess); cout << "You have chosen the letter " << gameGuess << '.' << endl; if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice) { for (i = 0; i < gameWord.length(); i++) { if (gameGuess == gameWord[i]) { //Instead of cout << gameGuess . I hope gameGuess is a // a single character. userGuess[i] = gameGuess; } } //After the for loop display the changed string altogether cout << userGuess; //This will retain changes
If there are , the word is stilll incomplete. Loop again.
Edit:
@wildgoose: I think gameWord is the original string.
I can see how this will print out the word the way I have it now, but I'm not fully understanding how this will add new correctly guessed words to the string. I'll give it a whirl though. Thanks for your thoughts, I greatly appreciate it.
•
•
Join Date: Jul 2009
Posts: 4
Reputation:
Solved Threads: 0
I'm not clear on how this helps me if the word is a different length every time the game is played. That seems like it would be useful if all words were the same length.
Last edited by Cromarte888; Jul 19th, 2009 at 10:39 pm. Reason: needed to block close quote
So, use a big buffer for both. Get string length of actual hangman string, fill guess string with '_' and set the ASCIIz terminator at the element corresponding to the string length.
So now they are the same length!
I said there were several ways of doing this. If you don't fully understand strings then go back and review!
So now they are the same length!
I said there were several ways of doing this. If you don't fully understand strings then go back and review!
•
•
Join Date: Oct 2008
Posts: 44
Reputation:
Solved Threads: 11
•
•
•
•
Originally Posted by Cromarte888
if the word is a different length every time the game is played. That seems like it would be useful if all words were the same length.
•
•
•
•
Originally Posted by wildgoose
So, use a big buffer for both....
I wouldn't agree.
A snippet which might help you if you can see the link.
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<string> int main() { std::string word = "Hangman"; std::string userGuess(word.length() , '+'); //<< std::cout <<"The original string is" << word; std::cout <<"\nThe contents of the userGuess string is" << userGuess; userGuess.erase(); //<< std::cout <<"\nAfter erasing" <<userGuess; word = "daniweb"; std::cout <<"The original string is " << word; //And then again userGuess.resize(word.length(), '_'); //<< std::cout <<"The original string is" << word; std::cout <<"\nThe contents of the userGuess string is" << userGuess; return 0; }
Last edited by zalezog; Jul 20th, 2009 at 11:24 am.
Like I posted. There are several ways of doing this! In my case, it was based upon a fixed length buffer that gets reused. In Zalezog's case, they're promoting the use of a dynamic buffer. You're having trouble with simple ASCII buffers so I felt the fixed length buffer was a good starting place. The method of using variable length buffer's such as using the string class is the more advanced but better solution. You are using C++ in your code!
Last edited by wildgoose; Jul 20th, 2009 at 11:40 am.
![]() |
Similar Threads
- Hangman - GuessLimit (Pascal and Delphi)
- Little help Comparing Strings (C++)
- Array Assignment (Computer Science)
- Keyboard input or "stuck in a loop" (C++)
- Ai (Computer Science)
- Help with Arrays needed! (C++)
- about:blank Please review my HijackThis log (Viruses, Spyware and other Nasties)
- Please help with algorithm (C)
Other Threads in the C++ Forum
- Previous Thread: I have a question about C++
- Next Thread: Simple GUI - How much effort?
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets





