Question

c++ program can you please explain how it works and the process? Question: You will design...

c++ program can you please explain how it works and the process?


Question: You will design a program in C++ that plays hangman using classes (polymorphism and inheritance)....

Hangman Game

CODE:

#include <iostream>
#include <cstdlib>
#include<ctime>
#include <string>
using namespace std;

int NUM_TRY=8; //A classic Hangman game has 8 tries. You can change if you want.
int checkGuess (char, string, string&); //function to check the guessed letter
void main_menu();
string message = "Play!"; //it will always display


int main(int argc, char *argv[])
{
string name;
char letter;
string word;
  

string words[] = //These are the list of 10 three lettered words.
{ //You can change them as per your requirement.
"man",
"van",
"tan",
"hop",
"pop",
"two",
"six",
"may",
"low",
"out",
};
  
srand(time(NULL));
int n=rand()% 10; //Random function to genterate random words from the given list
word=words[n];
  
  
string hide_m(word.length(),'X'); // This function is used to hide the actuall word to be guessed.
//The mask selected is 'X' so the word will show as 'XXX' till you guess the correct letters.
  
  
  
while (NUM_TRY!=0)
{
main_menu();
cout << "\n" << hide_m;
cout << "\nGuess a letter: ";
cin >> letter;
  
if (checkGuess(letter, word, hide_m)==0)
{
message = "Wrong letter.";
NUM_TRY = NUM_TRY - 1;
}
else
{
message = "NICE! You guessed a letter";
}
if (word==hide_m)
{
message = "Congratulations! You got it!";
main_menu();
cout << "\nThe word is : " << word << endl;
break;
}
}
if(NUM_TRY == 0)
{
message = "NOOOOOOO!...you've been hanged.";
main_menu();
cout << "\nThe word was : " << word << endl;
}
cin.ignore();// used to discard everything in the input stream
cin.get();
return 0;
}


int checkGuess (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
  
if (guess == guessword[i])
return 0;
  
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}

void main_menu()
{
system("cls");
cout<<"\nHangman Game!";
cout << "\nYou have " << NUM_TRY << " tries to try and guess the word.";
cout<<"\n"+message;
}

//*end of code*

So here is the code of the Hangman guessing game.


Homework Answers

Answer #1

First of lets understand the game

  1. There will be two person one who give the word and second who will guess the word.
  2. The guessing player has number of attempts.
  3. Now guessing player suggest a letter one by one and he has to guess all the letter of the word within the number of attempts.

Now demonstrating the program

  1. Here NUM_TRY is the number of attempts.
  2. The string array words is the collection of words.
  3. With the help of random function (srand) it selected a index number and according to the index number one word is selected.
  4. String hide_m will hide the actual word and replace the letters with X.
  5. main_menu function will present a main screen for the game.
  6. Now the main process is within while loop.
  7. For every attempts you have to provide a letter and then the letter along with secret word and guess word passed to checkGuess function.
  8. checkGuess function will check if the letter is in the actual word or not.
  9. if yes a success message will be displayed else a failure message will be displayed. Attempt will be incremented by 1 in both the cases.
  10. If the word is guessed within the number of attempts success message will be displayed along with the correct word else failure message displayed along with the correct word.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
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) What int setup_game 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...
Write a program that prompts the user to input a string and outputs the string in...
Write a program that prompts the user to input a string and outputs the string in uppercase letters. (Use dynamic arrays to store the string.) my code below: /* Your code from Chapter 8, exercise 5 is below. Rewrite the following code to using dynamic arrays. */ #include <iostream> #include <cstring> #include <cctype> using namespace std; int main() { //char str[81]; //creating memory for str array of size 80 using dynamic memory allocation char *str = new char[80]; int len;...
can someone edit my c++ code where it will output to a file. I am currently...
can someone edit my c++ code where it will output to a file. I am currently using xcode. #include <iostream> #include <cctype> #include <cstring> #include <fstream> using namespace std; bool inputNum(int [],int&,istream&); void multiply(int[],int,int[],int,int[],int&); void print(int[],int,int,int); int main() {ifstream input; int num1[35],num2[35],len1,len2,num3[60],len3=10,i; input.open("multiplyV2.txt"); //open file if(input.fail()) //is it ok? { cout<<"file did not open please check it\n"; system("pause"); return 1; }    while(inputNum(num1,len1,input)) {inputNum(num2,len2,input); multiply(num1,len1,num2,len2,num3,len3); print(num1,len1,len3,1); print(num2,len2,len3,2); for(i=0;i<len3;i++) cout<<"-"; cout<<endl; print(num3,len3,len3,1); //cout<<len1<<" "<<len2<<" "<<len3<<endl; cout<<endl;    } system("pause"); } void...
Write a program that reads a string and outputs the number of lowercase vowels in the...
Write a program that reads a string and outputs the number of lowercase vowels in the string. Your program must contain a function with a parameter of a char variable that returns an int. The function will return a 1 if the char being passed in is a lowercase vowel, and a 0 for any other character. The output for your main program should be: There are XXXX lowercase vowels in string yyyyyyyyyyyyyyyyyyyyyy Where XXXX is the count of lowercase...
use repl.it intro to C-programing no advance code also put good comment that i can know...
use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you use repl.it intro to C-programing no advance code also put good comment that i can know whats this code use for  thank you program for a game of hangman. Store the word to be guessed as individual characters of an array called word. The player must guess the letters belonging to word. If the user enters a valid letter,...
If you cant answer this please dont waste my question. thank you. This cryptographic program run...
If you cant answer this please dont waste my question. thank you. This cryptographic program run and produce text screen output. You are to create a GUI that uses the program. Your program (GUI) must allow a user to input of a message to be coded. It must also have an area to show the plaintext, the ciphertext, and the decrypted text. If required by your choice of cryptographic method, the user should have an area to input a key....
Convert this C++ program exactly as you see it into x86 assembly language: // Use the...
Convert this C++ program exactly as you see it into x86 assembly language: // Use the Irvine library for the print function #include <iostream> // The string that needs to be printed char word[] = "Golf\0"; // Pointer to a specific character in the string char * character = word; //NOTE: This main() function is not portable outside of Visual Studio void main() { // Set up a LOOP - See the while loop's conditional expression below int ecx =...
Please provide answer in the format that I provided, thank you Write a program that prompts...
Please provide answer in the format that I provided, thank you Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
C++ question. Please explain the code and how it resulted in the output. Explain each line...
C++ question. Please explain the code and how it resulted in the output. Explain each line along with the aspects of "buffers" if possible. Everything is listed below. Code below: #include <iostream> #include <string> using namespace std; int main() {    cout << boolalpha;    cout << static_cast<bool>(1) << endl;    cout << static_cast<bool>(0) << endl;    string s = "AAARGH!!!";    if (s.find("AAA")) { cout << 1 << endl; }    if (s.find("RGH")) { cout << 2 << endl;...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT