Question

In: Computer Science

Write a hangman program in c++.The game is played as follows: 1. The program selects a...

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!

Solutions

Expert Solution

****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:


Related Solutions

Write a program to allow a user to play the game Hangman. DO NOT USE AN...
Write a program to allow a user to play the game Hangman. DO NOT USE AN ARRAY The program will generate a random number (between 1 and 4581) to pick a word from the file - this is the word you then have to guess. Note: if the random number you generate is 42, you need the 42nd word - so loop 41 times picking up the word from the file and not doing anything with it, it is the...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and puts that into g->hidden_word. It sets...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly...
Hangman We're going to write a game of hangman. Don't worry, this assignment is not nearly as difficult as it may appear. The way hangman works (for this assignment - we are doing a simplified game) is as follows: the computer will choose a word. (For this version, a word is selected from a static list encoded into the program -- so the words are pretty limited). Let's say the word is "cocoa". the computer shows the user how many...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
C Program and pseudocode for this problem. Write a C program that plays the game of...
C Program and pseudocode for this problem. Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or...
A game is played as follows. There are 2 goats and 1 car, each hidden behind...
A game is played as follows. There are 2 goats and 1 car, each hidden behind a numbered door. You choose a door (for simplicity you always choose door 1) and then Monty Hall opens one of doors 2 or 3.The door that he opens always reveals a goat. Now Monty offers you to switch yourchoice of doors (a) What is the probability that you win the car by switching your choice? Hint: Consider the events {Ci= car is behind...
Write a C program that selects and displays the maximum value of five numbers to be...
Write a C program that selects and displays the maximum value of five numbers to be entered when the program is executed. (Hint : Use a for loop with both a scan_s and if statement inside the loop.)
Write python code so that in a game of hangman the word ‘apple’ is displayed as...
Write python code so that in a game of hangman the word ‘apple’ is displayed as “ _ _ _ _ _ “ and is usable code for any random words. Include in this code how to after the player has guessed a correct letter replace the _ with the correct guess shown as “ _ p _ _ _”
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3...
Write a program that plays tic-tac-toe. The tic-tac-toe game is played on a 3 × 3 grid as shown below: The game is played by two players, who take turns. The first player marks moves with a circle, the second with a cross. The player who has formed a horizontal, vertical, or diagonal sequence of three marks wins. Your program should draw the game board, ask the user for the coordinates of the next mark (their move), change the players...
Please create a Hangman game in Java language. For this game a random word will be...
Please create a Hangman game in Java language. For this game a random word will be selected from the following list (abstract, cemetery, nurse, pharmacy, climbing). You should keep a running tab of the user’s score and display it on screen; the user begins with 100 points possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 points should be taken away from what is left of their total possible points. For...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT