Rewrite the Hangman game from Chapter 4, \"The Standard Template Library: Hangma
ID: 3678226 • Letter: R
Question
Rewrite the Hangman game from Chapter 4, "The Standard Template Library: Hangman," using functions. Include a function to get the player's guess and another function to determine whether the player's guess is in the secret word.From the textbook Beginning C++ Through Game Programming 4th edition by Michael Dawson Rewrite the Hangman game from Chapter 4, "The Standard Template Library: Hangman," using functions. Include a function to get the player's guess and another function to determine whether the player's guess is in the secret word.
From the textbook Beginning C++ Through Game Programming 4th edition by Michael Dawson
From the textbook Beginning C++ Through Game Programming 4th edition by Michael Dawson
Explanation / Answer
// Hangman
// The classic game of hangman
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <cctype>
using namespace std;
char guess_function() // function to take input guess
{
char c;
cin >> c;
return c;
}
void check_guess_is_a_secret_word(int wrong, int MAX_WRONG, string soFar, string THE_WORD, string used) // function to check if the guess is a secret word
{
while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
{
cout << " You have " << (MAX_WRONG - wrong);
cout << " incorrect guesses left. ";
cout << " You’ve used the following letters: " << used << endl;
cout << " So far, the word is: " << soFar << endl;
char guess;
cout << " Enter your guess: ";
guess = guess_function();
guess = toupper(guess); //make uppercase since secret word in uppercase
while (used.find(guess) != string::npos)
{
cout << " You’ve already guessed " << guess << endl;
cout << "Enter your guess: ";
guess = guess_function();
guess = toupper(guess);
}
used += guess;
if (THE_WORD.find(guess) != string::npos)
{
cout << "That’s right! " << guess << " is in the word. ";
//update soFar to include newly guessed letter
for (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. ";
++wrong;
}
}
//shut down
if (wrong == MAX_WRONG)
{
cout << " You’ve been hanged!";
}
else
{
cout << " You guessed it!";
}
cout << " The word was " << THE_WORD << endl;
}
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! ";
check_guess_is_a_secret_word(wrong,MAX_WRONG,soFar,THE_WORD,used);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.