943,811 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 863
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 19th, 2009
0

Correct Answers in Hangman

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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?????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cromarte888 is offline Offline
4 posts
since Jul 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

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
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jul 19th, 2009
0

Re: Correct Answers in Hangman

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

Click to Expand / Collapse  Quote originally posted by zalezog ...
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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cromarte888 is offline Offline
4 posts
since Jul 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

Click to Expand / Collapse  Quote originally posted by zalezog ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Cromarte888 is offline Offline
4 posts
since Jul 2009
Jul 19th, 2009
0

Re: Correct Answers in Hangman

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!
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009
Jul 20th, 2009
0

Re: Correct Answers in Hangman

Quote 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..
Quote 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)
  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.
Reputation Points: 53
Solved Threads: 13
Light Poster
zalezog is offline Offline
47 posts
since Oct 2008
Jul 20th, 2009
0

Re: Correct Answers in Hangman

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.
Reputation Points: 546
Solved Threads: 99
Practically a Posting Shark
wildgoose is offline Offline
891 posts
since Jun 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: I have a question about C++
Next Thread in C++ Forum Timeline: Simple GUI - How much effort?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC