Question

Create a simple commented C++ HANGMAN game using CLASSES using the following specifications: - The game...

Create a simple commented C++ HANGMAN game using CLASSES using the following specifications:

- The game must keep track of the player misses
- It must ONLY use 10 three letter words
- It does NOT need a graphical implementation

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

/*main.cpp*/

#include "hangman.h"

using namespace std;

// declaring constants
typedef char String[MAX_WORD_SIZE];

// including implementation of cpp file


//Driver function
int main()
{
   const string fileName = "words.txt";
//Declaring object of Hangman class
Hangman obj;

char ch;

//Calling function to load words from file
obj.loadFile(fileName);
  
do
{
system("CLS");
cout<<"Hangman Game ";

//Function to start game
obj.runGame();

//Asking user for new game
cout<<"Do you want to play again (Y/N)? ";
cin>>ch;

}while(ch == 'Y' || ch =='y');
cout<<"Thanks for playing"<<endl;
return 0;
}

/*End of main.cpp*/

/*hangman.h*/

#ifndef HANGMAN_H
#define HANGMAN_H

#include<iostream>
#include<string>
#include<fstream>
#include <ctime>
#include <cstdlib>

#define MAX_WORD_SIZE 3
#define MAX_WORDS 10

using namespace std;

class Hangman
{
private:
string words[MAX_WORDS];
int count;
   public:
Hangman() : count(0) {}

void loadFile(string fileName);
void runGame();
};

#endif

/*End of hangman.h*/

/*hangman.cpp*/

#include "hangman.h"

using namespace std;

//Function to read words from a file and store them into array of words
void Hangman::loadFile(string fileName)
{
   char ch;
  
   // opening file in read mode
   ifstream file(fileName);
  
   while(file.bad())
   {
       cout<<"Error in opening file. Enter new file name: ";
       cin>>fileName;
       file.open(fileName);
   }
  
   //Read MAX_WORDS words from the file
   for(; count < MAX_WORDS; count++)
   file>>words[count];
  
   //Close file   
   file.close();
}

//Function to start the game
void Hangman::runGame()
{
  
   // declaring varibales
   int word;
   int tries = 1;//To keep track of number of tries
   string guess = "---";//Guess initially
   string copy;
   char letter;//User's guess
   bool correct = false;
  
   //Calling random function to get a random word
   srand(time( NULL ));
   word = rand() % count;//Get a word number
  
   copy = words[word];//Make a copy of selected word
  
   //Main game block
   while(tries != 6)
   {
       cout<<"Current guess: "<<guess<<endl;
      
       cout<<"Guess a letter: ";
       cin>>letter;
      
       letter = tolower(letter);//Convert any capital letter to lower case
      
       for(int i = 0; i < MAX_WORD_SIZE; i++)
       {
           if(copy[i] == letter)//Check if letter matches with any letter in the correct word
           {
               guess[i] = letter;
               correct = true;
           }
       }
      
       if(correct)//If guess was correct
       {
           cout<<"Good Guess! ";
           //If all letters are matched, player won!
           if(copy == guess)
           {
           cout<<"Congratulations, You Won!";
           cout<<"Correct word is: "<<copy<<endl<<endl;
           return;
           }
       }
       else//If letter was not a correct guess
       {
           cout<<"Sorry, bad guess! ";
           tries++;//One more incorrect try
       }
       correct = false;
   }
   cout<<"The word was : "<<copy<<endl;
}

/*End of hangman.cpp*/

/*words.txt*/

OUTPUTS:

Add a comment
Know the answer?
Add Answer to:
Create a simple commented C++ HANGMAN game using CLASSES using the following specifications: - The game...
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 a Hangman Game in C++ using Classes. The program doesn't need to have the hangman...

    Create a Hangman Game in C++ using Classes. The program doesn't need to have the hangman drawing, it just needs the following: - Ten 3-letter words - Keep track of everytime the player misses a letter - Simple and commented code

  • 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...

  • Create a Program in C++ We need commentaries in the program setter and getter we need...

    Create a Program in C++ We need commentaries in the program setter and getter we need the Output of the program and the code compiling and use a file. You will design a program that plays hangman. The rules are explained here. The program must at least include one class and client code. It does not require graphical representation but to keep track of the player misses. The host is the computer. The user has eight chances for error (otherwise,...

  • 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...

  • 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...

  • Create a modification to the Hangman game to provide enhancements for the user experience. Share the...

    Create a modification to the Hangman game to provide enhancements for the user experience. Share the code in text format in this thread. Could someone please assist me coming up with a modification in Python? 4. EXERCISE 4 The following code implements a very simple Hangman game: word = 'underutilise guesses = [] user_input = " while user_input == '0': user_input = input ("Enter a letter, or 0 to give up:') guesses.append(user_input) output = '' for letter in range(1, len...

  • 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...

  • 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...

  • I need help with this. I need to create the hangman game using char arrays. I've...

    I need help with this. I need to create the hangman game using char arrays. I've been provided with the three following classes to complete it. I have no idea where to start. HELP!! 1. /** * This class contains all of the logic of the hangman game. * Carefully review the comments to see where you must insert * code. * */ public class HangmanGame { private final Integer MAX_GUESSES = 8; private static HangmanLexicon lexicon = new HangmanLexicon();...

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