In: Computer Science
Write a hangman program in c++.The game is played as follows:
1. The program selects a random word for the user to guess (Read words for the user to guess into an array of strings from a file. The words are: revolutionary, meaning, recovery, compartment, trainer, pursuit, harm, platform, error, confusion)
2. User guesses one letter at a time until either they guess the word, or they run out of guesses.
3. Select a random word from the array of strings to the word the user has to guess.
Maintain 2 char arrays:
a. availableLetters: a char array of the available letters the user can choose their guess from. Initialize availableLetters as the lowercase alphabet. As the user guesses letters, replace the corresponding guessed letter with a space to denote the letter has already been guessed.
b. visibleLetters: a char array of the not guessed letters in the word. Initialize visibleLetter to all dashes ("-"). As the user correctly guesses a letter, replace the corresponding dash(es) with the correct letter.
4. The main game loop:
a. Display information regarding the status of the game (visibleLetters, availableLetters, and the number of incorrect guesses remaining (the user gets 7 incorrect guesses)).
b. Prompt the user to enter their guess. If the user tries to guess a letter they have already guessed, inform the user and re-prompt.
c. Exit the loop if the users completely guesses the letters of the word or if the user runs out of incorrect guesses.
Note: relevant string methods you will likely need to use for this program are <string variable>.at(i) to get a character at index position and i and <string variable>.size() (or <string variable>.length() to get the number of characters in the string.
Example output:
The word to guess has 5 letters. ----- Available letters: abcdefghijklmnopqrstuvwxyz 7 incorrect guesses remaining. Please enter your guess: s s is not in the word. Too bad. 6 incorrect guesses remaining. ----- Available letters: abcdefghijklmnopqr tuvwxyz 6 incorrect guesses remaining. Please enter your guess: e Nice! e is in the word. -e--- Available letters: abcd fghijklmnopqr tuvwxyz 6 incorrect guesses remaining. Please enter your guess: r r is not in the word. Too bad. 5 incorrect guesses remaining. -e--- Available letters: abcd fghijklmnopq tuvwxyz 5 incorrect guesses remaining. Please enter your guess: r r is not an available letter -e--- Available letters: abcd fghijklmnopq tuvwxyz 5 incorrect guesses remaining. Please enter your guess: l Nice! l is in the word. -ell- Available letters: abcd fghijk mnopq tuvwxyz 5 incorrect guesses remaining. Please enter your guess: h Nice! h is in the word. hell- Available letters: abcd fg ijk mnopq tuvwxyz 5 incorrect guesses remaining. Please enter your guess: o Nice! o is in the word. Congrats, you guessed the word hello!
****This requires a lot of effort so please drop a like if you are satisfied with the solution****
I have satisfied all the requirements of the question and I'm providing the screenshots of code and output for your reference...
Code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <cstdlib>
using namespace std;
void printArray(char array[]){
for(int
i=0;i<sizeof(array)/sizeof(array[0]);i++)
cout<<array[i];
}
int checkAvailableArray(char c,char array[]){
for(int
i=0;i<sizeof(array)/sizeof(array[0]);i++){
if(c==array[i])
return 1;
}
return 0;
}
int checkWord(char c,string word){
for(int i=0;i<word.size();i++){
if(c==word[i])
return 1;
}
return 0;
}
int main() {
ifstream file;
string words[10];
string line;
string word;
int chances=7;
char c;
int flag;
char
availableLetters[]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
cout<<endl;
//this is the path to the input file on my
desktop
file.open("C://Users/THE__INFINITY/Desktop/words.txt");
if (file.is_open())
{
int i=0;
while (getline(file,line))
{
words[i]=line;
i++;
}
int num=rand()%10;
cout<<num+1<<endl;
word=words[num];
char visibleLetters[word.size()];
for(i=0;i<sizeof(visibleLetters)/sizeof(visibleLetters[0]);i++)
visibleLetters[i]='-';
while(true){
flag=0;
int available=0;
printArray(visibleLetters);
cout<<endl;
cout<<"Available letters:
";
for(int
i=0;i<sizeof(availableLetters)/sizeof(availableLetters[0]);i++)
cout<<availableLetters[i];
cout<<"\n"<<chances<<" incorrect guesses
remaining"<<endl;
while(true){
cout<<"Please enter your guess"<<endl;
cin>>c;
for(int
i=0;i<sizeof(availableLetters)/sizeof(availableLetters[0]);i++){
if(c==availableLetters[i])
available=1;
}
if(available==0)
cout<<c<<" is already guessed enter
a different letter\n"<<endl;
else{
if(checkWord(c,word)==0){
chances--;
cout<<c<<" is not
in the word. Too bad "<<chances<<" guesses
left\n"<<endl;
for(int
i=0;i<sizeof(availableLetters)/sizeof(availableLetters[0]);i++){
if(availableLetters[i]==c)
availableLetters[i]='-';
}
break;
}
else{
cout<<"Nice! "<<c<<" is in the
word.\n"<<endl;
for(int
i=0;i<word.size();i++){
if(word[i]==c)
visibleLetters[i]=c;
}
for(int
i=0;i<sizeof(availableLetters)/sizeof(availableLetters[0]);i++){
if(availableLetters[i]==c)
availableLetters[i]='-';
}
break;
}
}
}
for(int
i=0;i<word.size();i++){
if(word[i]!=visibleLetters[i]){
flag=1;
}
}
if(flag==0&&chances>0){
cout<<"Congrats you guessed the word
"<<word<<"!"<<endl;
break;
}
if(chances<=0){
cout<<"\nYou ran out of
guesses"<<endl;
break;
}
}
}
file.close();
return 0;
}
File input ( words.txt):
Output Screenshot:
Sample run 1:
****In the above output the number 2 is just for showing you that the program has picked up the 2nd word from the input file..its just for your understanding so the 2nd word in input file is " meaning " so we are going to test the code for this word now... you can take this out by erasing the line number 46 in the code....
Sample Run 2:
Code Screenshots: