Eh, you've never played Hangman (or Hangaroo)?
Anyhow, that's actually how most programming is done. Test it on small sample, if it works for 10 words, it'll most probably work for 10,000 words as well. When you're beginning the program, you could have ONE word for testing purposes, just to see if the placement is correct, or are the number of guesses working correctly or not?
When that's done, it's simple for more words. This was an assignment for us as well (although we were given a difficult cheating version of Hangman to make). What I did was somewhat this.
Make 2 strings, one for the answer, and the other is your game string consisting of hyphens (-). For example,
String1 = Blizzard
String2 = --------
For the number of dashes, it's simple, count the number of letters in the answer(string1), and 'concatenate'/add the appropriate number of dashes. And yes, when you take a guess, it searches if it's in the solution, if it is, it will simply place at THE same index as the solution i.e. if you put 'A':
guessed
= solution;
This kind of thing. The 'i' is where it found the 'a' (if any).
I'll put my code here (it's not exactly cheating since you don't know C++ :looking. You can use the code to get some kind of idea IF you're stuck.
Code:
void Hangman::prompt()
{
string buffer;
cout << "Enter word length: ";
cin >> wordLength;
cout << "Enter number of guesses: ";
cin >> guesses;
cout << endl;
ifstream dictionary("dictionary.txt");
if(!dictionary) {cout<<"Error opening Dictionary.\n"; return;}
//Read all required words into vector
while(getline(dictionary,buffer))
{
if(buffer.length()==wordLength)
wordList.push_back(buffer);
}
if(wordList.size()==0){
cout << "No words of size " <<wordLength <<" present";
return;
}
//Choose a solution word
srand(time(NULL));
int randomIndex = rand() % wordList.size();
solutionWord = wordList[randomIndex];
//Initialize playing word to hyphen
for(size_t i=0; i<solutionWord.length(); i++)
{
if(i==solutionWord.length())
playWord[solutionWord.length()]='*';
playWord = playWord + "-";
}
}
void Hangman::run()
{
prompt();
char inputChar;
int charIndex;
cout << "(For Checking)SOLUTION: " <<solutionWord <<endl;//TESTING PURPOSES
while((playWord!=solutionWord) || guesses==0)
{
cout << "\nLetters used: " <<usedCharacters;
cout<< "\nRemaining tries: " <<guesses <<endl;
cout<< playWord;
cout<< "\n\nEnter a guess: ";
cin.ignore(100,'\n'); //For buffer problems
inputChar = getchar();
charIndex = solutionWord.find_first_of(inputChar);
if(charIndex == -1){
cout<< inputChar <<" is not present" <<endl;
if(usedCharacters.find(inputChar) == -1){ //Only decrement if letter wasn't used before and add it
guesses--;
usedCharacters+=inputChar;
}
if(guesses==0)
break;
}
else{
for(size_t i=0; i<solutionWord.length(); i++){
if(inputChar==solutionWord[i])
playWord[i]=inputChar;
}
}
}
if(guesses==0)
cout << "Game Over";
else
cout << "\n************************************************************"
<<"\nCongratulations! You found the solution i.e. "<<solutionWord
<< "\n************************************************************" <<endl;
}
This is just a PART of the code, it won't compile, of course