Question

Hangman is a game for two or more players. One player thinks of a word, phrase...

Hangman is a game for two or more players. One player thinks of a word, phrase or sentence and the other tries to guess it by suggesting letters or numbers, within a certain number of guesses. You have to implement this game for Single Player, Where Computer (Your Program) will display a word with all characters hidden, which the player needed to be guessed. You have to maintain a dictionary of Words. One word will be selected by your program. It will hide all characters from the word and will ask the player to guess the hidden characters. Player should be allowed a limited attempt to guess the hidden characters. If the player successfully guessed the word or failed to guess, the player should be asked either he wanted to play more? Once user ends the game you must display its win and lose count.

making project hang man search game using c++ language

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CPP PROGRAM:

#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//using namespace std;
const int MAX_TRIES = 5;
int letterFill(char, string, string&);


int main()
{
   string name;
   char letter;
   int num_of_wrong_guesses = 0;
   string word;

   srand(time(NULL)); // ONLY NEED THIS ONCE!

   // welcome the user
   cout << "\n\nWelcome to hangman!! Guess a country that comes into your mind.";
   // Ask user for for Easy, Average, Hard
   string level;
   cout << "\nChoose a LEVEL(E - Easy, A - Average, H - Hard):" << endl;
   cin >> level;

   // compare level
   if (level == "Easy")
   {
       //put all the string inside the array here
       string easy[] = { "india", "japan", "nepal", "china" };
       string word;

       int n = rand() % 4;
       word = easy[n];

       //call the function here for guessing game
       // Initialize the secret word with the * character.
       string unknown(word.length(), '*');
       cout << "\n\nEach letter is represented by an asterisk.";
       cout << "\n\nYou have to type only one letter in one try.";
       cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the country.";
       cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
       // Loop until the guesses are used up
       while (num_of_wrong_guesses < MAX_TRIES)
       {
           cout << "\n\n" << unknown;
           cout << "\n\nGuess a letter: ";
           cin >> letter;
           // Fill secret word with letter if the guess is correct,
           // otherwise increment the number of wrong guesses.
           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Whoops! That letter isn't in there!" << endl;
               num_of_wrong_guesses++;
           }
           else
           {
               cout << endl << "You found a letter! Isn't that exciting?" << endl;
           }
           // Tell user how many guesses has left.
           cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
           cout << " guesses left." << endl;
           // Check if user guessed the word.
           if (word == unknown)
           {
               cout << word << endl;
               cout << "Yeah! You got it!";
               break;
           }
       }
       if (num_of_wrong_guesses == MAX_TRIES)
       {
           cout << "\nSorry, you lose...you've been hanged." << endl;
           cout << "The word was : " << word << endl;
       }
       cin.ignore();
       cin.get();
       return 0;
   }

   else if (level == "Average")
   {
       //put all the string inside the array here
       string average[] = { "madagascar", "azerbaijan", "kyrgyzstan" };

       int n = rand() % 3;
       word = average[n];

       //call the function here for guessing game
       // Initialize the secret word with the * character.
       string unknown(word.length(), '*');
       cout << "\n\nEach letter is represented by an asterisk.";
       cout << "\n\nYou have to type only one letter in one try.";
       cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the country.";
       cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
       // Loop until the guesses are used up
       while (num_of_wrong_guesses < MAX_TRIES)
       {
           cout << "\n\n" << unknown;
           cout << "\n\nGuess a letter: ";
           cin >> letter;
           // Fill secret word with letter if the guess is correct,
           // otherwise increment the number of wrong guesses.
           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Whoops! That letter isn't in there!" << endl;
               num_of_wrong_guesses++;
           }
           else
           {
               cout << endl << "You found a letter! Isn't that exciting?" << endl;
           }
           // Tell user how many guesses has left.
           cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
           cout << " guesses left." << endl;
           // Check if user guessed the word.
           if (word == unknown)
           {
               cout << word << endl;
               cout << "Yeah! You got it!";
               break;
           }
       }
       if (num_of_wrong_guesses == MAX_TRIES)
       {
           cout << "\nSorry, you lose...you've been hanged." << endl;
           cout << "The word was : " << word << endl;
       }
       cin.ignore();
       cin.get();
       return 0;
   }

   else if (level == "Hard")
   {
       //put all the string inside the array here
       string hard[] = { "computer", "science", "system" };

       int n = rand() % 3;
       word = hard[n];

       //call the function here for guessing game
       // Initialize the secret word with the * character.
       string unknown(word.length(), '*');
       cout << "\n\nEach letter is represented by an asterisk.";
       cout << "\n\nYou have to type only one letter in one try.";
       cout << "\n\nYou have " << MAX_TRIES << " tries to try and guess the country.";
       cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
       // Loop until the guesses are used up
       while (num_of_wrong_guesses < MAX_TRIES)
       {
           cout << "\n\n" << unknown;
           cout << "\n\nGuess a letter: ";
           cin >> letter;
           // Fill secret word with letter if the guess is correct,
           // otherwise increment the number of wrong guesses.
           if (letterFill(letter, word, unknown) == 0)
           {
               cout << endl << "Whoops! That letter isn't in there!" << endl;
               num_of_wrong_guesses++;
           }
           else
           {
               cout << endl << "You found a letter! Isn't that exciting?" << endl;
           }
           // Tell user how many guesses has left.
           cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
           cout << " guesses left." << endl;
           // Check if user guessed the word.
           if (word == unknown)
           {
               cout << word << endl;
               cout << "Yeah! You got it!";
               break;
           }
       }
       if (num_of_wrong_guesses == MAX_TRIES)
       {
           cout << "\nSorry, you lose...you've been hanged." << endl;
           cout << "The word was : " << word << endl;
       }
       cin.ignore();
       cin.get();
       return 0;
   }

}

// as I said in my previous post, read up on functions!
int letterFill(char guess, string secretword, string &guessword)
{
   int i;
   int matches = 0;
   int len = secretword.length();
   for (i = 0; i< len; i++)
   {
       // Did we already match this letter in a previous guess?
       if (guess == guessword[i])
           return 0;
       // Is the guess in the secret word?
       if (guess == secretword[i])
       {
           guessword[i] = guess;
           matches++;
       }
   }
   return matches;
}

Add a comment
Know the answer?
Add Answer to:
Hangman is a game for two or more players. One player thinks of a word, phrase...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create the game hangman using c code: Program description In the game of hangman, one player...

    Create the game hangman using c code: Program description In the game of hangman, one player picks a word, and the other player has to try to guess what the word is by selecting one letter at a time. If they select a correct letter, all occurrences of the letter are shown. If no letter shows up, they use up one of their turns. The player is allowed to use no more than 10 incorrect turns to guess what the...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] 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) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • Overview In this exercise you are going to recreate the classic game of hangman. Your program...

    Overview In this exercise you are going to recreate the classic game of hangman. Your program will randomly pick from a pool of words for the user who will guess letters in order to figure out the word. The user will have a limited number of wrong guesses to complete the puzzle or lose the round. Though if the user answers before running out of wrong answers, they win. Requirements Rules The program will use 10 to 15 words as...

  • JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The...

    JAVA Hangman Your game should have a list of at least ten phrases of your choosing.The game chooses a random phrase from the list. This is the phrase the player tries to guess. The phrase is hidden-- all letters are replaced with asterisks. Spaces and punctuation are left unhidden. So if the phrase is "Joe Programmer", the initial partially hidden phrase is: *** ********** The user guesses a letter. All occurrences of the letter in the phrase are replaced in...

  • Below are expected output examples: In this assignment you will implement hangman in python. If you...

    Below are expected output examples: In this assignment you will implement hangman in python. If you are not famililar with this game, see the wikipedia article linked above This implementation of hangman will be an interactive command-line interface game. Name the script hangman.py. There will be two players (player 1 and player 2). Player 1 will be responsible for choosing the word for player 2 to guess at the beginning of the game. This is the only time that player...

  • C++ Hangman (game) In this project, you are required to implement a program that can play...

    C++ Hangman (game) In this project, you are required to implement a program that can play the hangman game with the user. The hangman game is described in https://en.wikipedia.org/wiki/Hangman_(game) . Your program should support the following functionality: - Randomly select a word from a dictionary -- this dictionary should be stored in an ASCII text file (the format is up to you). The program then provides the information about the number of letters in this word for the user to...

  • This is for C programming: You will be given files in the following format: n word1...

    This is for C programming: You will be given files in the following format: n word1 word2 word3 word4 The first line of the file will be a single integer value n. The integer n will denote the number of words contained within the file. Use this number to initialize an array. Each word will then appear on the next n lines. You can assume that no word is longer than 30 characters. The game will use these words as...

  • Consider a class that could be used to play a game of hangman. The class has...

    Consider a class that could be used to play a game of hangman. The class has the following attributes: The secret word. The disguised word, in which each unknown letter in the secret word is replaced with a question mark (?). For example, if the secret word is abracadabra and the letters a, b, and e have been guessed, the disguised word would be ab?a?a?ab?a. The number of guesses made. The number of incorrect guesses. It will have the following...

  • 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 pts possible if they correctly guess the word without any mistakes. For every incorrect letter which is guessed, 10 pts should be taken away from what is left of their total possible points. For...

  • JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the...

    JAVA PROGRAMMING (Game: hangman) Write a hangman game that randomly generates a word and prompts the user to guess one letter at a time, as shown in the sample run. Each letter in the word is displayed as an asterisk. When the user makes a correct guess, the actual letter is then displayed. When the user finishes a word, display the number of misses and ask the user whether to continue to play with another word. Declare an array to...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT