1 Overview project, I will supply you with starter must add your own function(s)
ID: 3765689 • Letter: 1
Question
1 Overview project, I will supply you with starter must add your own function(s) to this code to make it work. The project will rely program, you will play a game of hangman. In this In this code, and on you to utilize string functions, as well as some simple array functionality, to accomplish your goal of playing hangman. If you do not know how to play hangman, ASK You must you ou must play a game of hangman to completion (either the player guesses all of the letters correctly or runs out of "lives"), and ask the player if she wants to play again. Continue to do so until she says "no". 2 Implementation Details The starter code provided does a few things we have not yet seen in this class. A list of English words is located in the fle "wordsEn.txt". A copy of this fle must be file. You will use these saved in the same directory on your computer as your epp file. You will use t words to play hangman with the player. . These words are read in from the file and stored in the string array vords in main. This is already done for you, and you do not have to (and should not) change any of this code. We will discuss reading data in from files later in the semester. Whenever you need to get a word from the list at random, you should use the other function provided in the starter code, getRandomWord. This function returns a random word from the list. This function uses the built-in rand) function, which returns a random integer. Your portion of the code should play the game of hangman. .You must get a random string from the list (using getRandomWord) to use as the word that the player is trying to guess. You must display asterisk characters ) or some other placeholder character in each space that the player has not yet successfully gu The player starts with word.length)+ 4 lives. When the player guesses a character, the program must find every instance of that character in the word and replace the asteristk with the guessed character. You must continue to ask the player for letters until either the player has run out of lives (remember to subtract one life every time the player guesses wrong) or the player has correctly filled in every character of the word. Do not assume al guesses are lower case. All words in the list are lower ease, however, so you o not assume all guesses are lower case. All words in the list are lower case, however, so you must convert every character to be lower case when the player guesses them.Explanation / Answer
As you have not given starter code I have written starter code also.
#include <bits/stdc++.h>
using namespace std;
string getRandomword(string arr[],int n){
int r=rand()%n;
return arr[r];
}
void printcarr(char carr[],int n){
cout<<"-----------------"<<endl;
for(int i=0;i<n;i++){
cout<<carr[i];
}
cout<<endl;
}
int main(){
srand (time(NULL));
ifstream inp;
inp.open("wordsEn.txt");
int n;
inp>>n;
string arr[n];
for(int i=0;i<n;i++){
inp>>arr[i];
}
cout<<n<<" words read in successfully."<<endl;
bool gameend=false;
char c;
while(!gameend){
string x=getRandomword(arr,n);
int lives=x.length()+4;
char carr[x.length()];
for(int i=0;i<x.length();i++){
carr[i]='*';
}
int charleft=x.length();
while(charleft>0 && lives>0){
printcarr(carr,x.length());
cout<<"You have "<<lives<<" lives left."<<endl;
cout<<"What is your guess?"<<endl;
cin>>c;
lives-=1;
c=tolower(c);
bool correct=false;
for(int i=0;i<x.length();i++){
if(x[i]==c){
x[i]='*';
carr[i]=c;
charleft-=1;
correct=true;
}
}
if(correct){
cout<<"Correct! "<<charleft<<endl;
}
else{
cout<<"Wrong! "<<charleft<<endl;
}
}
if(charleft==0){
cout<<"You win! The word was "<<x<<endl;
cout<<"Would you like to play again? 'Y' or 'N'."<<endl;
cin>>c;
if(c=='N' or c=='n'){
gameend=true;
}
}
else{
cout<<"You lost! The word was "<<x<<endl;
cout<<"Would you like to play again? 'Y' or 'N'."<<endl;
cin>>c;
if(c=='N' or c=='n'){
gameend=true;
}
}
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.