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

This programming projects asks you to develop a hangman game which has the follo

ID: 3570437 • Letter: T

Question

This programming projects asks you to develop a hangman game which has the following characteristics:

1. It is driven by a while loop from main that allows the user to play the game as many times as they wish.

2. Validates ALL user input. (each character input for the word guess must be an alpha, 'a' is the same as 'A', all blank spaces ignored, 'y' = 'Y' = ' y ', etc)

3. A list of at least 20 words for the game are kept in a file that is read in at the time the program starts. These words are then stored in an array. Use a random number generator to access into the array to select the secret word. Don't repeat words during one run of the program. (What type of C++ structure would best hold this? Parallel arrays? An array of structures?)

4. Once each game begins, show the gallows, and indicate the number of characters in the word, like this:

______

|   

|

|

____________ Word : _ _ _ _ _ _ _

That's just a suggestion as to how it may look. Actually, I am sure that you can do better than that.

For each wrong guess, draw a body part on the gallows and show it in a list of incorrect guesses. For each correct guess, fill in one of the blanks in the word.

______

| ( )

|

|

____________ Word : a _ _ _ _ _ Incorrect guesses: p

(this pictures shows 1 wrong guess and 1 correct guess).

5. The maximum number of moves before the person is hung would be 8 or 9, depending on your ability to draw head, neck, body, 2 legs, 2 arms...when the man is hung, the game is over. Or, the game is over when the user correctly guesses each letter in the word.

6. This program must have good modular design - using proper functions.

7. Pseudocode is absolutely required and I hope that you might even use it to design the code.

8. Use of constructs that we have not studied is not allowed.  I'll just take off the points for that.

Explanation / Answer

Indented code can be found at: http://ideone.com/zvLf9r

---------------------------------------------------------------------------------------------

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

const int MAX_TRIES=8;

int letterFill (char, string, string&);

int main ()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[] =
{
"india",
"pakistan",
"nepal",
"malaysia",
"philippines",
"australia",
"iran",
"ethiopia",
"oman",
"indonesia"
};

//choose and copy a word from array of words randomly
srand(time(NULL));
int n=rand()% 10;
word=words[n];

// Initialize the secret word with the * character.
string unknown(word.length(),'*');

// welcome the user
cout << " Welcome to hangman...Guess a country Name";
cout << " Each letter is represented by a star.";
cout << " You have to type only one letter in one try";
cout << " You have " << MAX_TRIES << " tries to try and guess the word.";
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << " " << unknown;
cout << " Guess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting!" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}

}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << " Sorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}

/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */

int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;

// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

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