Question

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 start playing this game.


- The number of letters in the selected work is determined by the difficulty level chosen by the user (i.e., easy (<=5 letters), medium (5 to 8 letters), or difficult (more than 8 letters)). If you want, the difficulty level can also affect the number of attempts that the user can have.


- The user/player can input a letter at a time to try to guess the word. Based on the user input letter, the program will show the positions of the input letter in the word if this letter is in the word. Otherwise, a failure attempt is marked (and part of the hangman will be drawn). If the allowed attempts have been used and the player still cannot guess the word correctly, the hangman is completely drawn, and the game is over. The player loses and the program wins.


- The user can also input the complete word rather than just a letter if he/she already guesses the word. If the word is correct, then the player wins. Otherwise, it is marked as a failure attempt (and part of the hangman will be drawn).


- The program should allow the player to store the current status of the game (using a file), and reload the game later to continue to play. The format of the file that is used to store this status information is up to you.

Mandatory Criteria:
The program as a whole:
- Must contain 2-3 classes (programs containing less than 2 classes will receive a 50% penalty)
- Must use file I/O; (programs with no file I/O will receive a 20% penalty)

Bonus Points:
(up to 5 points) If inheritance is used
(up to 5 points) If polymorphism is used
(up to 5 point) If the graphical representation of the hangman (see the example provided in the wikipedia link above) is drawn by the program using ASCII art


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

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

// Defines a class Word
class Word
{
public:
// vector to store words
vector <string> secretWords;
// Counter for how many success guess tries made
int successTries;
// Counter for current guess letter
int currentLetters;
// To store success or failure
int success;
// Counter for number of attempts made
int totalAttempt;
};// End of class Word

// Defines a class HangmanGame
class HangmanGame
{

public:

// Function to read the secret words from the file and stores it in matrix
bool readFile(Word &secretWord, int difficultLevel)
{
string word;
// File pointer declared
ifstream fRead;
// To store number of words
int len = 0;

// Checks the difficultLevel
switch(difficultLevel)
{
case 1:
// Opens the file for reading with <=5 letters
fRead.open("secretWord1.txt");
break;
case 2:
// Opens the file for reading with 5 to 8 letters
fRead.open("secretWord2.txt");
break;
case 3:
// Opens the file for reading with more than 8 letters
fRead.open("secretWord3.txt");
break;
default:
cout<<"\n Invalid Level...";
// Returns false for un success
return false;
}// End of switch case


// Checks if the file cannot be opened display error message and stop
if(!fRead)
{
printf("ERROR: The file cannot be opened for reading");
exit(0);
}// End of if condition

// Loops till not end of the file
// eof() function returns true if the stream's eof bit error state flag is set (which signals
// that the End-of-File has been reached
// by the last input operation). Otherwise returns false.
while(!fRead.eof())
{
fRead>>word;
secretWord.secretWords.push_back(word);
}// End of while loop

// Close the file
fRead.close();
// Returns true for success
return true;
}// End of function

// Function to generate a random number between 0 and number of words and returns it
int randomSecretWord(int numberOfWords)
{
return (rand() % (numberOfWords - 0)) + 0;
}// End of function

string randomWord(Word secretWord, int index)
{
return secretWord.secretWords.at(index);
}

// Function to play the game
// Returns 1 for win 0 for lose
int playGame(Word &secretWord, string word)
{
// Creates user guess word of
char userGuess [word.length()];
// To store guess letter entered by the user
char guess;
// Counter for number of attempts limit word length plus one time
int counter = word.length() + 1;
// Loops variable
int c;
// Loops till length of the secret word and assigns '-'
for(c = 0; c < word.length(); c++)
userGuess[c] = '-';

// Assigns null character at the end
userGuess[c] = '\0';

// Loops till guessed all letters of the word
do
{
// Displays user guess current status
cout<<"\n ----------------------------------------------------";
cout<<"\n Let's begin, you will be entering "<<word.length()<<" letters one by one";
cout<<"\n\t "<<userGuess;

// Initializes success to 0 for not success for each letter guessed
secretWord.success = 0;

// Increase the number of attempts by one
secretWord.totalAttempt++;

cout<<"\n Number of attempts left: "<<counter--;

// Displays which letter number
cout<<"\n (This is letter "<<secretWord.currentLetters<<")";
cout<<"\n Enter a letter you think is in the word: ";
fflush(stdin);

// Accepts a guess character
cin>>guess;
cout<<"\n ******************************************** \n";

// Loops till length of the word times
for (c = 0; c < word.length(); c++)
{
// Checks if the guess character is equals to current character of secret word
if (tolower(guess) == word[c])
{
// Assigns the guess character at c index position of user guess array
userGuess[c] = tolower(guess);
// Set the success to 1
secretWord.success = 1;
// Increase the success tries by one
secretWord.successTries++;
// Increase the current letters by one
secretWord.currentLetters++;
}// End of if condition

}// End of for loop

// Checks if success value is one then rightly guessed the character
if(secretWord.success == 1)
cout<<"\n The letter "<<guess<<" is in the word! ";

// Otherwise guess character is wrong
else
cout<<"\n The letter "<<guess<<" is not in the word! ";
cout<<"\n ******************************************** \n";

// Displays current user guess status array
cout<<"\n Here are the letters guessed so far: \t"<<userGuess<<endl;

// Checks if success tries is equals to secret word length then return success
if(secretWord.successTries == word.length())
return 1;
}while(counter >= 1); // Loops till 6 attempts

// Checks if success tries is less than the number of letters in the word then return 0 for looser
if(secretWord.successTries < word.length())
return 0;
// Otherwise return 1 for winner
else
return 1;
}// End of function
};// End of class HangmanGame

// main function definition
int main()
{
// Creates an object of the class HangmanGame
HangmanGame hg;

// Use current time as seed for random generator
srand(time(NULL));

// To store user choice to continue play or not
char choice;

// To store game level
int level;

// Loops till user choice is not 'N' or 'n'
do
{
// Creates an object of the class Word
Word secretWord;
// Initializes the data members for the game
secretWord.successTries = 0;
secretWord.currentLetters = 1;
secretWord.success;
secretWord.totalAttempt = 0;
// Displays levels
cout<<"\n 1) Easy \n 2) Medium \n 3) Difficult";

// Accepts level
cout<<"\n Enter your choice: ";
cin>>level;

// Calls the function to read file and stores the words in matrix
// Return value as number of words stored by reference
// Checks if function reads the file success fully
if(hg.readFile(secretWord, level))
{

// Calls the function to generate random number between 0 and number of words
int pos = hg.randomSecretWord(secretWord.secretWords.size());
string word = hg.randomWord(secretWord, pos);

// Stores the length of the secret word at generated random index position
int len = word.length();

cout<<"\n Secret: "<< word;

// Sets the number of success tries, total attempts and current letter to 0 for each play
secretWord.successTries = 0;
secretWord.totalAttempt = 0;
secretWord.currentLetters = 0;

// Calls the method to check win or loose status
if(hg.playGame(secretWord, word) == 0)
cout<<"\n Loose the game.";
else
cout<<"\n Won the game.";

// Displays total number of attempt made by the user to guess the word
cout<<"\n Your total attempt = "<<secretWord.totalAttempt;

// Clears the console input
fflush(stdin);

// Accepts user choice to play again or not
cout<<"\n\n Would you like to play another game (Y/ N)";
cin>>choice;
}// End of if condition
// Checks if user choice is 'N' or 'n' then stop
if(choice == 'N' || choice == 'n')
break;
}while(1); // End of do - while loop
}// End of main function

Sample Output:

1) Easy
2) Medium
3) Difficult
Enter your choice: 3

Secret: mandatory
----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
---------
Number of attempts left: 10
(This is letter 0)
Enter a letter you think is in the word: a

********************************************

The letter a is in the word!
********************************************

Here are the letters guessed so far: -a--a----

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--a----
Number of attempts left: 9
(This is letter 2)
Enter a letter you think is in the word: s

********************************************

The letter s is not in the word!
********************************************

Here are the letters guessed so far: -a--a----

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--a----
Number of attempts left: 8
(This is letter 2)
Enter a letter you think is in the word: t

********************************************

The letter t is in the word!
********************************************

Here are the letters guessed so far: -a--at---

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--at---
Number of attempts left: 7
(This is letter 3)
Enter a letter you think is in the word: r

********************************************

The letter r is in the word!
********************************************

Here are the letters guessed so far: -a--at-r-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--at-r-
Number of attempts left: 6
(This is letter 4)
Enter a letter you think is in the word: q

********************************************

The letter q is not in the word!
********************************************

Here are the letters guessed so far: -a--at-r-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--at-r-
Number of attempts left: 5
(This is letter 4)
Enter a letter you think is in the word: t

********************************************

The letter t is in the word!
********************************************

Here are the letters guessed so far: -a--at-r-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--at-r-
Number of attempts left: 4
(This is letter 5)
Enter a letter you think is in the word: l

********************************************

The letter l is not in the word!
********************************************

Here are the letters guessed so far: -a--at-r-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--at-r-
Number of attempts left: 3
(This is letter 5)
Enter a letter you think is in the word: o

********************************************

The letter o is in the word!
********************************************

Here are the letters guessed so far: -a--ator-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--ator-
Number of attempts left: 2
(This is letter 6)
Enter a letter you think is in the word: p

********************************************

The letter p is not in the word!
********************************************

Here are the letters guessed so far: -a--ator-

----------------------------------------------------
Let's begin, you will be entering 9 letters one by one
-a--ator-
Number of attempts left: 1
(This is letter 6)
Enter a letter you think is in the word: r

********************************************

The letter r is in the word!
********************************************

Here are the letters guessed so far: -a--ator-

Loose the game.
Your total attempt = 10

Would you like to play another game (Y/ N)y

1) Easy
2) Medium
3) Difficult
Enter your choice: 1

Secret: likes
----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
-----
Number of attempts left: 6
(This is letter 0)
Enter a letter you think is in the word: l

********************************************

The letter l is in the word!
********************************************

Here are the letters guessed so far: l----

----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
l----
Number of attempts left: 5
(This is letter 1)
Enter a letter you think is in the word: i

********************************************

The letter i is in the word!
********************************************

Here are the letters guessed so far: li---

----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
li---
Number of attempts left: 4
(This is letter 2)
Enter a letter you think is in the word: j

********************************************

The letter j is not in the word!
********************************************

Here are the letters guessed so far: li---

----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
li---
Number of attempts left: 3
(This is letter 2)
Enter a letter you think is in the word: e

********************************************

The letter e is in the word!
********************************************

Here are the letters guessed so far: li-e-

----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
li-e-
Number of attempts left: 2
(This is letter 3)
Enter a letter you think is in the word: s

********************************************

The letter s is in the word!
********************************************

Here are the letters guessed so far: li-es

----------------------------------------------------
Let's begin, you will be entering 5 letters one by one
li-es
Number of attempts left: 1
(This is letter 4)
Enter a letter you think is in the word: k

********************************************

The letter k is in the word!
********************************************

Here are the letters guessed so far: likes

Won the game.
Your total attempt = 6

Would you like to play another game (Y/ N)y

1) Easy
2) Medium
3) Difficult
Enter your choice: 2

Secret: program
----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
-------
Number of attempts left: 8
(This is letter 0)
Enter a letter you think is in the word: o

********************************************

The letter o is in the word!
********************************************

Here are the letters guessed so far: --o----

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
--o----
Number of attempts left: 7
(This is letter 1)
Enter a letter you think is in the word: q

********************************************

The letter q is not in the word!
********************************************

Here are the letters guessed so far: --o----

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
--o----
Number of attempts left: 6
(This is letter 1)
Enter a letter you think is in the word: p

********************************************

The letter p is in the word!
********************************************

Here are the letters guessed so far: p-o----

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
p-o----
Number of attempts left: 5
(This is letter 2)
Enter a letter you think is in the word: r

********************************************

The letter r is in the word!
********************************************

Here are the letters guessed so far: pro-r--

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
pro-r--
Number of attempts left: 4
(This is letter 4)
Enter a letter you think is in the word: g

********************************************

The letter g is in the word!
********************************************

Here are the letters guessed so far: progr--

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
progr--
Number of attempts left: 3
(This is letter 5)
Enter a letter you think is in the word: a

********************************************

The letter a is in the word!
********************************************

Here are the letters guessed so far: progra-

----------------------------------------------------
Let's begin, you will be entering 7 letters one by one
progra-
Number of attempts left: 2
(This is letter 6)
Enter a letter you think is in the word: m

********************************************

The letter m is in the word!
********************************************

Here are the letters guessed so far: program

Won the game.
Your total attempt = 7

Would you like to play another game (Y/ N)n

Add a comment
Know the answer?
Add Answer to:
C++ Hangman (game) In this project, you are required to implement a program that can play...
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...

  • Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program...

    Please help with this Intro to programming in C assignment! Intro to Programming in C-Large Program 3 - Hangman Game Assignment purpose: User defined functions, character arrays, c style string member functions Write an interactive program that will allow a user to play the game of Hangman. You will need to: e You will use four character arrays: o one for the word to be guessed (solution) o one for the word in progress (starword) o one for all of...

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

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

  • javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that...

    javafx assisantance confused on where to start Problem Description: (Game: hangman) Write a JavaFX program that lets a user play the hangman game. The user guesses a word by entering one letter at a time, as shown in Figure followings. If the user misses seven times, a hanging man swings, as shown in Figures. Once a word is finished, the user can press the Enter key to continue to guess another word. Guess a word: ***** God Missed letters Guess...

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

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

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

  • code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte...

    code block and c++ only please on Programs> Prog 4: Twisted Hangman & Sylabus ule nte zza ckboard ss Notes al Reference ference [Corrected sample output and rubric still to come ..] Write a program to play hangman where the computer chooses the word and the human user tries to guess the word. The twist is that the computer starts out with a dictionary of many words. After each human guess, the computer eliminates all words containing the letters guessed...

  • 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