Question & Answer: This programming projects asks you to develop a hangman game which has the following characteristics: IN C++…..

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

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.

My code isnt generating random words for me to guess, when i run it, there is no word, just a blank space. How do i fix this?

HERE IS MY CODE:

//Headers file section
#include <iostream>
#include <fstream>
#include <string.h>
#include <time.h>
#include<ctime>
#include <stdlib.h>

//Declare sizes
#define SIZE 15
#define WORDS_LENGTH 255
typedef char String[SIZE];
String Words[WORDS_LENGTH – 1];
int Count;

//Function prototypes
void ReadFile();
void GuessGame();
void ShowGallows(int State);

using namespace std;

//Program begins with a main function
void main()
{
char choice = ‘Y’;
cout<<“Welcome to Hangman Game!”<<endl;
ReadFile();
//Validates ALL user input.
while(choice == ‘Y’)
{
GuessGame();
cout<<endl<<“Want to play again (Yes or No)? “;
cin>>choice;
choice = toupper(choice);
}
system(“PAUSE”);
}

//Function definition of “ReadFile”
void ReadFile()
{
char C;
ifstream input;
Count=0;
// Open the data file
input.open(“inputFileData.dat”);
//Loop till the end of file
while((C=input.peek()) != EOF)
{
input>>Words[Count++];
if(Count > WORDS_LENGTH – 1)
{
cout<<endl<<“Too many words in the file, stopping with ”
<<WORDS_LENGTH<<” Words.”<<endl;
Count = WORDS_LENGTH;
break;
}

}
Count–;

// Close the data file
input.close();

}

//Function definition of “GuessGame”
void GuessGame()
{
int Word,Size;
int State=1,Subscript=0;
char Guess[SIZE];
char Copy[SIZE];
char Letter;
int Correct=0;

// Seed and create a random number
srand((unsigned)time( NULL ));
Word = rand() % Count;

// Make a local copy of the word
strcpy(Copy,Words[Word]);

Size = strlen(Words[Word]);

for(; Subscript < Size; Subscript++)
{
Guess[Subscript] = ‘-‘;
}

// insert the null character
Guess[Subscript] = ‘’;

// Go till the player is hung
while(State!=6)
{
ShowGallows(State);
cout<<Guess<<endl;

cout<<“Guess a letter :”;
cin>>Letter;

Letter = tolower(Letter);

// Loop through the word
for(Subscript = 0; Subscript < Size; Subscript++)
{

//if the guess is good tell the user and update Guess
if(Copy[Subscript] == Letter)
{
Guess[Subscript] = Letter;
Correct = 1;
cout<<endl<<“Good Guess!”;

// If guess equals the word they won so exit
if(strcmp(Words[Word],Guess) == 0)
{
cout<<endl<<“You won the game!”;
return;
}
}
}

// If they didn’t get aletter correct chide the user
if(Correct == 0)
{
cout<<endl<<“Sorry, bad guess!”;
State++;
}

Correct = 0;

}

ShowGallows(State);

cout<<“The word was : “<<Words[Word]<<endl<<endl;

}

// Show the gallows according to the state
void ShowGallows(int State)
{
if(State==6)
{
cout<<endl<<endl
<<”   +—-+     “<<endl
<<”   |    |     “<<endl
<<”   |    O     “<<endl
<<”   |   /|\   “<<endl
<<”   |   / \   “<<endl
<<”   |Your Dead “<<endl
<<” ============”<<endl<<endl;
}
else if(State==5)
{
cout<<endl<<endl
<<”   +—-+ “<<endl
<<”   |    | “<<endl
<<”   |    O “<<endl
<<”   |   /|\ “<<endl
<<”   |     \ “<<endl
<<”   |       “<<endl
<<” ============”<<endl<<endl;
}
else if(State==4)
{
cout<<endl<<endl
<<”   +—-+ “<<endl
<<”   |    | “<<endl
<<”   |    O “<<endl
<<”   |   /|\ “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<” =============”<<endl<<endl;
}
else if(State==3)
{
cout<<endl<<endl
<<”   +—-+ “<<endl
<<”   |    | “<<endl
<<”   |    O “<<endl
<<”   |   /| “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<” =============”<<endl<<endl;
}
else if(State==2)
{
cout<<endl<<endl
<<”   +—-+ “<<endl
<<”   |    | “<<endl
<<”   |    O “<<endl
<<”   |    | “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<” =============”<<endl<<endl;
}
else if(State==1)
{
cout<<endl<<endl
<<”   +—-+ “<<endl
<<”   |    | “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<”   |       “<<endl
<<” =============”<<endl<<endl;
}

}

Expert Answer

 

The code appears to work fine (running in Dev C++), however had to change the main() signature to make it run:

int main(int argc, char** argv){

..

return 0;

}

Many compilers don’t allow “void” to be its main’s return type. Try changing your main signature to the above code. If it doesn’t works, let me know in the comments, the specifics of how you are executing it.

Also, check if the file is kept in the same directory as of your code. Check if you are able to read the contents of file at all.

Still stressed from student homework?
Get quality assistance from academic writers!