Using only standard C++ your assignment is to write a computer program which pla
ID: 3767472 • Letter: U
Question
Using only standard C++ your assignment is to write a computer program which plays a game of Hangman
1. Read the file dictionary.txt, which contains the master word list.
2. Prompt the user for a word length, prompting as necessary until she enters a number such that there's at least one word that's exactly that long. That is, if the user wants to play with words of length -42 or 137, since no English words are that long, you should reprompt her.
3. Prompt the user for a number of guesses, which must be an integer greater than zero. Don't worry about unusually large numbers of guesses – after all, having more than 26 guesses is not going to help your opponent!
4. Prompt the user for whether she wants to have a running total of the number of words remaining in the word list. This completely ruins the illusion of a fair game that you'll be cultivating, but it's quite useful for testing (and grading!)
5. Play a game of Hangman using the Evil Hangman algorithm, as described below:
1. Construct a list of all words in the English language whose length matches the input length.
2. Print out how many guesses the user has remaining, along with any letters the player has guessed and the current blanked-out version of the word. If the user chose earlier to see the number of words remaining, print that out too.
3. Prompt the user for a single letter guess, prompting until the user enters a letter that she hasn't guessed yet. Make sure that the input is exactly one character long and that it's a letter of the alphabet.
4. Partition the words in the dictionary into group’s byQword family.
Master word list is here: http://www.mediafire.com/view/79uq03qpkqq7jvb/worddic.txt
Explanation / Answer
#include <iostream>
#include<string>
using namespace std;
int main()
{
string s;
int n,i;
cin>>s;
string ch;
string inl="";
string gs(s.length(),'_');
cout<<s<<endl<<gs<<endl;
cout<<" enter the number of guesses you want ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Number of guesses remaining : "<<(n-i)<<endl;
cin>>ch;
if(ch.length()>1)
{
cout<<"enter a guess of single charecter ";
i--;
continue;
}
if(inl.find(ch)==string::npos)
inl+=ch;
else
{
cout<<"this charecter is already guessed ";
i--;
}
for(int j=0;j<s.length();j++)
{
if(ch[0]==s[j])
gs[j]=ch[0];
}
cout<<gs<<endl;
if(s==gs)
{
cout<<"you have guessed correctly ";
cout<<"The word is :"<<gs<<endl;
break;
}
}
}
the above is the core implementation of the game
the question is little confusing as the hangman game should be played with one word
i did my level best in the following code
#include <iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
string s;
int n,i,l;
vector<string> v,v1;
ifstream in("dictionary.txt")
do{
cout<<"enter the length of word you want to guess ";
cin>>l;
}
while(d<=0 || d>=20);
while(in>>s)
{
if(s.length()==l)
{
string gs(s.length(),'_');
v.push_back(s);
v1.push_back(gs);
}
}
string ch;
string inl="";
cout<<s<<endl<<gs<<endl;
cout<<" enter the number of guesses you want ";
cin>>n;
for(i=0;i<n;i++)
{
cout<<"Number of guesses remaining : "<<(n-i)<<endl;
cin>>ch;
if(ch.length()>1)
{
cout<<"enter a guess of single charecter ";
i--;
continue;
}
if(inl.find(ch)==string::npos)
inl+=ch;
else
{
cout<<"this charecter is already guessed ";
i--;
}
for(int k=0;k<v.size();k++)
{
s =v[k];
gs=v1[k];
for(int j=0;j<s.length();j++)
{
if(ch[0]==s[j])
gs[j]=ch[0];
}
v1[k]=gs;
cout<<gs<<endl;
if(s==gs)
{
cout<<"you have guessed correctly ";
cout<<"The word is :"<<gs<<endl;
break;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.