Correct Answers in Hangman

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 4
Reputation: Cromarte888 is an unknown quantity at this point 
Solved Threads: 0
Cromarte888 Cromarte888 is offline Offline
Newbie Poster

Correct Answers in Hangman

 
0
  #1
Jul 19th, 2009
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:

  1. guess(); //call member function of letter chosen
  2. ltrpos = gameWord.find(gameGuess);
  3. cout << "You have chosen the letter " << gameGuess << '.' << endl;
  4. if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
  5. {
  6. for (i = 0; i < gameWord.length(); i++)
  7. {
  8. if (gameGuess == gameWord[i])
  9. {
  10. cout << gameGuess;
  11. }
  12. else
  13. cout << '_';
  14. }
  15. cout << endl;
  16. 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?????
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Correct Answers in Hangman

 
0
  #2
Jul 19th, 2009
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!

  1. if (gameGuess == gameWord[i]) // matching letter found?
  2. {
  3. myWord[i] = gameGuess; // <---- Stuff new letter
  4. cout << gameGuess;
  5. }
  6. else if (myWord[i] )
  7. cout << myWord[i];
  8. else
  9. cout << '_';
  10. }
Last edited by wildgoose; Jul 19th, 2009 at 5:20 am. Reason: alteration
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Correct Answers in Hangman

 
0
  #3
Jul 19th, 2009
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.

  1. guess(); //call member function of letter chosen
  2. ltrpos = gameWord.find(gameGuess);
  3. cout << "You have chosen the letter " << gameGuess << '.' << endl;
  4. if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
  5. {
  6. for (i = 0; i < gameWord.length(); i++)
  7. {
  8. if (gameGuess == gameWord[i])
  9. {
  10. //Instead of cout << gameGuess . I hope gameGuess is a
  11. // a single character.
  12. userGuess[i] = gameGuess;
  13. }
  14.  
  15. } //After the for loop display the changed string altogether
  16. cout << userGuess;
  17. //This will retain changes
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.
Last edited by zalezog; Jul 19th, 2009 at 5:17 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Correct Answers in Hangman

 
0
  #4
Jul 19th, 2009
Yes, simultaneous post. It's late here, and I noticed that after the post and editted then saw your post!

Cromarte88, you need either to store the characters you picked correctly, or store a flag for each correctly chosen character position so that letters previously chosen will be displayed!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: Cromarte888 is an unknown quantity at this point 
Solved Threads: 0
Cromarte888 Cromarte888 is offline Offline
Newbie Poster

Re: Correct Answers in Hangman

 
0
  #5
Jul 19th, 2009
Originally Posted by zalezog View Post
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.

  1. guess(); //call member function of letter chosen
  2. ltrpos = gameWord.find(gameGuess);
  3. cout << "You have chosen the letter " << gameGuess << '.' << endl;
  4. if (ltrpos < gameWord.length()) //checks letter existance in chosen random word (correct choice)
  5. {
  6. for (i = 0; i < gameWord.length(); i++)
  7. {
  8. if (gameGuess == gameWord[i])
  9. {
  10. //Instead of cout << gameGuess . I hope gameGuess is a
  11. // a single character.
  12. userGuess[i] = gameGuess;
  13. }
  14.  
  15. } //After the for loop display the changed string altogether
  16. cout << userGuess;
  17. //This will retain changes
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.

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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Correct Answers in Hangman

 
0
  #6
Jul 19th, 2009
Method of using a double character buffer!

['H']['a']['n']['g']['T']['e']['n'] <-- gameWord[]
['_']['_']['_']['_']['_']['_']['_'] <-- gameGuess[]

As each letter guessed, the letter is copied over to same index

Only display gameGuess, not gameWord gets displayed.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 4
Reputation: Cromarte888 is an unknown quantity at this point 
Solved Threads: 0
Cromarte888 Cromarte888 is offline Offline
Newbie Poster

Re: Correct Answers in Hangman

 
0
  #7
Jul 19th, 2009
Originally Posted by zalezog View Post
Another approach could be
1.Make another userGuess string.
2. First , fill all the characters in userGuess string
with underscores( _ ).
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
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Correct Answers in Hangman

 
0
  #8
Jul 19th, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 45
Reputation: zalezog is an unknown quantity at this point 
Solved Threads: 11
zalezog zalezog is offline Offline
Light Poster

Re: Correct Answers in Hangman

 
0
  #9
Jul 20th, 2009
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.
Then..
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.
  1. #include<iostream>
  2. #include<string>
  3.  
  4. int main()
  5. {
  6. std::string word = "Hangman";
  7. std::string userGuess(word.length() , '+'); //<<
  8. std::cout <<"The original string is" << word;
  9. std::cout <<"\nThe contents of the userGuess string is" << userGuess;
  10. userGuess.erase(); //<<
  11. std::cout <<"\nAfter erasing" <<userGuess;
  12.  
  13. word = "daniweb";
  14. std::cout <<"The original string is " << word;
  15. //And then again
  16. userGuess.resize(word.length(), '_'); //<<
  17. std::cout <<"The original string is" << word;
  18. std::cout <<"\nThe contents of the userGuess string is" << userGuess;
  19. return 0;
  20. }
Last edited by zalezog; Jul 20th, 2009 at 11:24 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Correct Answers in Hangman

 
0
  #10
Jul 20th, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC