Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ hangman game using at least 5 user defined functions. The secret han

ID: 3757249 • Letter: W

Question

Write a C++ hangman game using at least 5 user defined functions. The secret hangman word will come from an input file. I cannot use any arrays or vectors, this must be accomplished with loops

Function 1 should prompt user for game number. If the user types "3" then the third word in the file will be used.

Function 2 should read the word from an input file. If the game number chosen from function 1 is less than zero then a randomly selected word will be used.

Function 3 will print the correct hangman picture based on the number of wrong guesses. See below for example of hangman picture

Function 4 will make a copy of the secret word and replace all letters with ***** then print

Function 5 will allow the user to make the next guess and change the **** to letters if a correct letter is chosen

These are basic guidelines, some variation is okay

hangman looks somethin like this:

void printHangman(int wrongGuesses )

{

if (wrongGuesses == 6)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | /|\ " << endl

<< " | / \ " << endl

<< " | " << endl;

}

else if (wrongGuesses == 5)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | /|\ " << endl

<< " | / " << endl

<< " | " << endl;

}

else if (wrongGuesses == 4)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | /|\ " << endl

<< " | " << endl

<< " | " << endl;

}

else if (wrongGuesses == 3)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | /| " << endl

<< " | " << endl

<< " | " << endl;

}

else if (wrongGuesses == 2)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | | " << endl

<< " | " << endl

<< " | " << endl;

}

else if (wrongGuesses == 1)

{

cout << endl << endl

<< " -------- " << endl

<< " | O " << endl

<< " | " << endl

<< " | " << endl

<< " | " << endl;

}

}

Explanation / Answer

#include <iostream>

#include <algorithm>

#include <vector>

#include <string>

#include <ctime>

#include <cctype>

using namespace std;

void prompt();

char getguess();

void yesorno(const string, char, string, int);

int main()

{

//set up

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 the word to be guessed

random_shuffle(words.begin(), words.end());

const string THE_WORD = words[0]; /* words is a vector of possible words to guess.

THE_WORD has been assigned the first word in the

word to be guessed */

int wrong = 0; // number of incorrect guesses

string soFar(THE_WORD.size(), '_'); // word guessed so far.. each letter in word rep by '_'

string used = ""; // letters already guessed

cout << "Welcome to Hangman 2.0. Good Luck! ";

// main loop

while ((wrong < MAX_WRONG) && (soFar != THE_WORD))

{

cout << " You have " << (MAX_WRONG - wrong) << " incorrect guesses left. ";

cout << " You've used the following letters: " << used;

cout << " So far the word is: " << soFar;

// Getting the Players' Guess

prompt();

char guess = getguess();

while (used.find(guess) != string::npos)

{

cout << " You've already guessed " << guess;

prompt();

char guess = getguess();

//guess = toupper(guess);

}

used += guess;

yesorno(THE_WORD, guess, soFar, wrong); //calling function yesorno(const, char, string, int);

} //End the Game

//shut down

if (wrong == MAX_WRONG)

{

cout << " You've used you're last guess! GAME OVER";

}

else

{

cout << " You guessed it!";

}

cout << " The word was " << THE_WORD;

return 0;

}

void prompt()

{

cout << "Enter your guess: ";

}

char getguess()

{

char guess;

cin >> guess; //getting character / answer from user

guess = toupper(guess); //make uppercase since secret word is uppercase

return guess; // returning the answer to main;

}

void yesorno(const string THE_WORD, char guess, string soFar, int wrong)

{

if (THE_WORD.find(guess) != string::npos)

{

cout << "That's right! " << guess << " is in the word. ";

//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. ";

++wrong;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote