• Your task is to implement a Hangman game in C++ • The words for the game must
ID: 3626023 • Letter: #
Question
• Your task is to implement a Hangman game in C++
• The words for the game must be stored in a file. You must have at least 10 words in the file.
• The "gallows" and figure should be drawn to the screen using ASCII characters
• This game should open the file, read in a word and display underscores for the characters
• The player should then be allowed to guess letters. Do not allow the player to guess a letter that has already been guessed . . . do not penalize the player for guessing duplicate letters.
• This game should proceed as a normal game of hangman, with the player guessing letters until they get the word correct or get hung. Your game should announce if there is a win or a defeat.
• Once a word has been gotten or a player has been hung, the player should be asked if they would like to play again.
• You must use array syntax for the words
• You must modularize this application into functions
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
void drawing(int position);
void guessword(string word);
int main()
{
int number;
int count = 0;
int position;
string word;
char letter;
ifstream infile;
infile.open("words.txt");
srand(time(NULL)); //initializes the random number generator
while (count < 1)
{
number = rand()%10 +1; // rand returns a random integer from 0 to maxInt
count++;
}
for (count=0;count<number;count++)
getline (infile, word);
guessword(word);
cin.ignore(255,' ');
cin.get();
return 0;
}
void guessword(string word)
{
char letter;
int position;
string blankword[4];
blankword[0] = "_ ";
blankword[1] = "_ ";
blankword[2] = "_ ";
blankword[3] = "_ ";
int wrongguess = 0;
for (int i=0;i<6;i++)
{
cout << "What letter would you like to guess?";
cin >>letter;
position = word.find(letter);
if (position > word.length())
cout<<letter<< " is not in the word "<<endl;
else
{
cout<< letter << " is in the word"<<endl;
if(position==0)
{
blankword[0] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if(position==1)
{
blankword[1] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if(position==2)
{
blankword[2] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
else if (position==3)
{
blankword[3] = letter;
cout<<blankword[0]<<blankword[1]<<blankword[2]<<blankword[3];
}
}
}
}
void drawing(int position)
{
switch(position)
{
case 1:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | " <<endl;
cout << "_|______________"<<endl;
break;
case 2:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ " <<endl;
cout << "_|______________"<<endl;
break;
case 3: cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ 0 " <<endl;
cout << "_|______________"<<endl;
break;
case 4:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ 0 /" <<endl;
cout << "_|______________"<<endl;
break;
case 5:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ 0 /" <<endl;
cout << " | |"<<endl;
cout << "_|______________"<<endl;
break;
case 6:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ 0 /" <<endl;
cout << " | |"<<endl;
cout << " | / "<<endl;
cout << "_|______________"<<endl;
break;
case 7:
cout << " ___________"<<endl;
cout << " | }"<<endl;
cout << " | \ 0 /" <<endl;
cout << " | |"<<endl;
cout << " | / \ "<<endl;
cout << "_|______________"<<endl;
}
}
can't seem to get this to work.
Explanation / Answer
#include #include #include using namespace std; class hangclass1 { public:void func1() { coutRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.