Question

Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...

Help please, write code in c++. The assignment is about making a hangman game.

Instructions:

  • This program is part 1 of a larger program. Eventually, it will be a complete Hangman game.
  • For this part, the program will
    • Prompt the user for a game number,
    • Read a specific word from a file,
    • Loop through and display each stage of the hangman character
      • I recommend using a counter while loop and letting the counter be the number of wrong guesses. This will help you prepare for next week
    • Print the final messages of the game.
    • NOTE: Look at the sample run for the prompt, different hangman pictures, and final messages.
  • You must have at least 3 functions other than main:
    • Function to prompt the user for a game number.
    • Function to read the appropriate word from the file based on the game number
      • If number is 1, then read the first word
      • If number is 2, then read the second word and so on
    • Function to print the correct hangman picture based on the number of wrong guesses

I have the file already downloaded.

  • The file with the game words is called, word.txt. An example is:
moon
racecar
program
cat
word

Sample Run #1 (bold, underlined text is what the user types):

Game? 3

L----+----
|
|
|
X

L----+----
|    O
|
|
X

L----+----
|    O
|    |
|
X

L----+----
|    O
|   /|
|
X

L----+----
|    O
|   /|\
|
X

L----+----
|    O
|   /|\
|   / 
X

L----+----
|    O
|   /|\
|   / \
X

You lost
Answer: program
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution code:

#include <iostream>
#include <fstream>

using namespace std;

void drawHangman(int level){
/*
* Drawing hangman levels
*/
switch(level){
case 0:
cout << 'L' << "----+----" << endl;
cout << '|' << endl << "|" << endl << "|" << endl << "X" << endl;
break;
case 1:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "|" << endl << "|" << endl << "X" << endl;
break;
case 2:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| |" << endl << "|" << endl << "X" << endl;
break;
case 3:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|" << endl << "|" << endl << "X" << endl;
break;
case 4:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl << "|" << endl << "X" << endl;
break;
case 5:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl << "| /" << endl << "X" << endl;
break;
case 6:
cout << 'L' << "----+----" << endl;
cout << "| O" << endl << "| /|\\" << endl << "| / \\" << endl << "X" << endl;
break;
}
}

string getWordFromFile(int pos){
/*
* Getting a word from file
*/
string word;
ifstream ifstr;
ifstr.open("word.txt");
for(int i = 0; i < pos - 1; i++){
ifstr >> word;
}
ifstr >> word;
ifstr.close();
return word;
}

int getGameNumberFromUser(){
/*
* Getting game number from user
*/
int game_no;
cout << "Enter game number: ";
cin>> game_no;
return game_no;
}

int getGussedCharFromUser(){
/*
* Getting a guessed character from user
*/
char guess_char;
cout << "Enter ypur guess: ";
cin >> guess_char;
return guess_char;
}

int main()
{
int hangman_level = 0, game_no, guess_char, word_len, match_count = 0;
string word;
  
/*
*
* Getting initial inputs from user
*
*/
game_no = getGameNumberFromUser();
word = getWordFromFile(game_no);
word_len = word.length();
  
/*
* Drawing initial hangman figure
*/
drawHangman(hangman_level);
  
while(true){
/*
* Iterate until win or loose
*/
guess_char = getGussedCharFromUser();
  
/*
* Compare guessed char with word char
*/
if(guess_char == word[match_count]){
match_count++;
}
else{
hangman_level++;
}
  
/*
* Draw next level hangman figure
*/
drawHangman(hangman_level);
  
/*
* Checking if hangman drwan completely
* if so the user will loose geme
*/
if(hangman_level == 6){
cout << "You lost !!!" << endl;
cout << "Correct word: " << word << endl;
break;
}
  
/*
* checking if whole word guessed correctly
* if so, user win
*/
if(match_count == word_len){
cout << "Congrats you won !!!" << endl;
break;
}
}
  
cout << "Game Over" << endl;
   return 0;
}


Code screenshot:

Output of sample run:

Add a comment
Know the answer?
Add Answer to:
Help please, write code in c++. The assignment is about making a hangman game. Instructions: This...
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
  • Help c++ Description: This program is part 1 of a larger program. Eventually, it will be...

    Help c++ Description: This program is part 1 of a larger program. Eventually, it will be a complete Hangman game. For this part, the program will Prompt the user for a game number, Read a specific word from a file, Loop through and display each stage of the hangman character I recommend using a counter while loop and letting the counter be the number of wrong guesses. This will help you prepare for next week Print the final messages of...

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

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

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

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

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

  • ​ TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM. Learning objectives: The...

    ​ TEXT FILE IS ALREADY GIVEN, JUST NEED TO READ IN THE PROGRAM. Learning objectives: The intent of this programing project is to allow you the opportunity to demonstrate your ability to solve problems using procedural C++ programming. This project HANG MAN will focus on file I/O and string manipulation in the implementation of the game Hangman. Program Description: In this project, you will write a C++ program that simulates playing the game Hangman. This version of the game will...

  • ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file...

    ​​​​​​This program will make Maze game. Please Help in c++ Prompt the user for a file that contains the maze. Read it into a two-dimensional array Remember you can use inputStream.get(c) to read the next character from the inputStream. This will read whitespace and non-whitespace characters Don’t forget to read the newline character at the end of each line Print the maze to the screen from your array You should include a ‘*’ in your maze to indicate where the...

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

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