Sign in to follow this  
JeremyYox

Hangman program question (C++)

Recommended Posts

JeremyYox    105
So, I'm already back with my question of the hour. :)

I've just finished looking over the hangman program from my book, and while the majority of it makes sense, there are two parts I don't seem to get.

[code]// Hangman
// The classic game of hangman

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>

using namespace std;

int main()
{
//setup
const int MAX_WRONG = 8; //maximum number of incorrect guesses allowed

vector<string> words; //collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

srand(static_cast<unsigned int>(time(0)));
random_shuffle(words.begin(), words.end());
const string THE_WORD = words[0]; //word to guess
int wrong = 0; //number of incorrect guesses
string soFar(THE_WORD.size(), '-'); //word guessed so far
string used = ""; //letters already guessed

cout << "Welcome to Hangman. Good luck!\n";

//main loop
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << "\n\nYou have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left.\n";
cout << "\nYou've used the following letters:\n" << used << endl;
cout << "\nSo far, the word is:\n" << soFar << endl;

char guess;
cout << "\n\nEnter your guess: ";
cin >> guess;
guess = toupper(guess); //make uppercase since secret word is in uppercase

while (used.find(guess) != string::npos)
{
cout << "\nYou've already guessed " << guess << endl;
cout << "Enter your guess: ";
cin >> guess;
guess = toupper(guess);
}

used += guess;

if (THE_WORD.find(guess) != string::npos)
{
cout << "That's right! " << guess << " is in the word.\n";

//update soFar to include newly guessed letter
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
}
}
else
{
cout << "Sorry, " << guess << " isn't in the word.\n";
++wrong;
}
}

//shut down

if (wrong == MAX_WRONG)
{
cout << "\nYou've been hanged!";
}
else
{
cout << "\nYou guessed it!";
}
cout << "\nThe word was " << THE_WORD << endl;

return 0;
}[/code]

How does the program replace the secret word's characters as they are guessed, and how does it search for those characters in the word? I think I can make out where it does it, but the process isn't making sense to me. I can see it compare guesses to the word, see that it tells how they're there or not and update MAX_WRONG, but the replacement/update/completion I can't make out.

I appreciate the help and sorry for posting a new thread seemingly on the hour.

Edit: Actually think I may be starting to make some progress on it. May be able to solo this one after all.

I was originally misunderstanding [i], but what I am thinking now is:
[code]
for (unsigned int i = 0; i < THE_WORD.length(); ++i)
{
if (THE_WORD[i] == guess)
{
soFar[i] = guess;
}
[/code]
[i] is incremented to check position by position through the length of THE_WORD for the value of guess, if its there, it updates the same position in soFar, which simply mimics the length of THE_WORD with '-' instead of the position's real value. The value for that position is updated with the value of guess in all positions it applies to. Woo, I think I got it. Sorry again for posting so much. Edited by ebontide

Share this post


Link to post
Share on other sites
ryan20fun    2635
Hidden
here is where the words come from

[code]
vector<string> words; //collection of possible words to guess
words.push_back("GUESS");
words.push_back("HANGMAN");
words.push_back("DIFFICULT");

[/code]

if you want more, just push back more (preferbly from a dictionary)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this