Hangman game

I need help writing the .h file for this code. I'm new to c++ and have some scratch code written but it's honestly not even worth posting here. Main program provided by my instructor.

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <list>
#include <string>
#include "hang.h"

/*
* Hangman main program.
*
* Run the program and type a single letter guess when requested. The
* letter prompt also accepts the input !q to quit the program, or !c
* to cancel and forfeit the round. (And get the program to tell you what
* the )(*!#&(&*# that stupid word is.)
*
* The program works from a word list given at the bottom of this file.
* It seems reasonable, but has no certain virtue. Loading a list from a
* file would probably be better.
*/

/*
* Display the current status of the game.
*/
void show_status(hangman &hanger, string missed, string message,
bool show_hang = true);

/*
* Choose at random and return a word to guess from a list. (The list
* is below.
*/
string next_word();

main()
{
// Seed the random number generator with the current time so that
// we don't always get the same words.
srand(time(NULL));

// The hangman game object.
hangman hanger;

// Repeat rounds until stopped by the user.
bool firstword = true; // First word to guess
while(true) {
// Set the word to guess, and display the initial status.
hanger.set_word(next_word());
show_status(hanger, "",
firstword ?
"Welcome to Hangman!" : "Try another!",
firstword);
firstword = false;

// Loop through the guesses for the particular word.
string missed = ""; // String of incorrect letter guesses.
bool done = false; // Termination when the round is done.
while(!done) {
// Prompt for a letter guess.
cout << "Enter guess: ";
string line;
if(!getline(cin, line)) exit(0);

// A couple of special responses.
if(line.substr(0,2) == "!q") exit(0);
if(line.substr(0,2) == "!c") {
// Give up (cancel guess).
cout << "The word was " << hanger.get_word()
<< endl;
break;
}

// Send the guess to the hangman object and
// respond accordingly.
string msg; // Msg describing result to user.
switch (hanger.guess(line[0])) {
case hangman::FOUND:
// Good letter guess, but more to do.
msg = "Found " + line.substr(0,1);
break;
case hangman::MISSED:
// Bad letter guess.
msg = "No, " + line.substr(0,1) + " is wrong";
missed += " " + line.substr(0,1);
break;
case hangman::DUP:
// Repeated a letter guess.
msg = "Already tried " + line.substr(0,1);
break;
case hangman::WIN:
// Successfully guessed the word.
msg = "Congratulations!";
done = true;
break;
case hangman::LOSE:
// Too many guesses. Lost the round.
msg = "Sorry. The word was " +
hanger.get_word();
missed += " " + line.substr(0,1);
done = true;
break;
case hangman::BADCHR:
// Can't type.
msg = line.substr(0,1) +
" is an illegal character";
break;
case hangman::ERROR:
// Shouldn't happen unless there's a bug
// in the object, since we won't call it
// here outside the guessing state.
msg = "Unexpected error.";
break;
}

// Show the status again, giving the result of
// the last guess.
show_status(hanger, missed, msg);
}
}
}


/*
* Draw the hangman figure, according to the number of misses. Parts
* should be in the range 0-6. Offset is just some number of blanks to
* put at the left of the figure.
*/
void draw_hang(int offset, int parts)
{

}

/*
* Display the current status of the game.
*/
void show_status(hangman &hanger, string missed, string message,
bool show_hang)
{
// Build the word with blanks.
string word = hanger.get_word();
string display_word = "";
for(int i = 0; i < word.length(); ++i) {
if(display_word.length() > 0) display_word.push_back(' ');
if(hanger.is_found(i))
display_word.push_back(word[i]);
else
display_word.push_back('_');
}

// Print the sent message, and (optionally) show the gallows picture.
cout << "\n=== " << message << " ===" << endl << endl;
if(show_hang) {
draw_hang(5, hanger.num_letters_incorrect());
cout << endl;
}

// Give the stats.
cout << " Word: " << display_word << " | Missed: " << missed << endl
<< " Tried " << hanger.num_letters_guessed() << " letter(s), "
<< "with " << hanger.num_letters_incorrect() << " incorrect."
<< endl << " " << hanger.num_words_guessed()
<< " word(s) guessed; " << hanger.num_words_correct()
<< " correct." << endl;
}

/*
* This is just a list of words. Would be better to read from a file, but
* this is easier and fits our present purposes.
*/

static vector<string> words = {
"abluent", "absents", "acyclic", "adsorbs", "advises", "aflower",
"afterward", "agencies", "aircrew", "airtight", "alabaster",
"alehouses", "allusions", "alumine", "ambience", "ammoniums",
"amputates", "anchoring", "androgyne", "anteceded", "anywise",
"apiaries", "apostates", "appriser", "arbors", "archer", "arcsine",
"asking", "aspirate", "asserting", "atropine", "backing", "backyard",
"baffled", "baignet", "bakeware", "baluster", "banknotes", "baptizers",
}

// Return a word from the list chosen at random.
string next_word()
{
return words[rand() % words.size()];
}
Line 40: Your program has a hangman class, so you need to define that class in your hang.h file. Go through the code and find all references to the hangman class and the hanger instance. That will tell you what functions you need to support in your class. For example, line 46 calls a set_word() function, so your class will need that function.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.



Last edited on
Topic archived. No new replies allowed.