Question

In: Computer Science

Hey, please write in c++. This a two-part lab but I did not put them on...

Hey, please write in c++. This a two-part lab but I did not put them on a separate question then it would be confusing to the who is answering it. The first part is already done and the code is down below but I wanted to provide the instructions for it just in case.

Please follow the instructions and provide comments helps me understand if you could, make sure the code meets all the criteria, It should be an easy assignment it should be creating a hangman game.

Please Help and thank you.
use the same code for part 2 use the code I provide, don't make separate codes for each part.

If you have any questions please let me know

-----------------------------------------------------------------

First part

  • 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 already have the file don't worry about it

  • 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

This the code for this first part, use it to do the second part, please.

#include <iostream>
#include <fstream>

using namespace std;

int main()
{

int hangman_level = 0;
int game_no;
int guess_char;
int word_len;
int 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
*/

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;
}
}

/*
* Getting a word from file
*/
string getWordFromFile(int pos)
{

string word;
ifstream ifstr;
ifstr.open("word.txt");
for (int i = 0; i < pos - 1; i++)
{
ifstr >> word;
}
ifstr >> word;
ifstr.close();
return word;
}

/*
* Getting game number from user
*/
int getGameNumberFromUser()
{

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;
}

if (hangman_level == 6)
{
cout << "You lost !!!" << endl;
cout << "Correct word: " << word << endl;
break; // break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop
}
  
/*
* checking if whole word guessed correctly
* if so, user win
*/
if (match_count == word_len)
{
cout << "Congrats you won !!!" << endl;

break; // break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop
}
}
  
cout << "Game Over" << endl;
return 0;
}

-------------------------------------------------------

Part 2

  • This program is part 2 of the Hangman program. keep building on last week’s Hangman.cpp
  • For this part, the program will need to:
    • If the user enters a number less than 1 for the game, then randomly select a game between 1 and 5
    • Add a function to make a copy of the word. Replace all characters with a ‘*’. You can use this to display the current state of the hangman game to the user
      • You can get the number of characters in string s, by calling s.length()
      • You can access a single character in the string with s.at(i) where i is a number between 0 and the length-1. For example, s.at(i) = ‘*’ will change the first character of s to ‘*’
      • And here’s an example to print each character in the string, one per line:
        • for (int i = 0; i < s.length(); i++)
        • {
          • cout << s.at(i) << endl;
        • }
    • Add a function to let the user make the next guess.
      • It should update any of the *’s to the guess letter if it matches
      • It should update the number of wrong guesses if the guess doesn’t match any letter in the word AND print a message to the user
    • main() will need to call your 2 new functions, stop when the user has solved the puzzle or exhausted all of its wrong guesses, and it must print a congratulations message instead of the lost message if needed
    • You may add additional functions if you want.
    • Make sure that you display the current state of the game before each guess

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

Game? 3

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

*******
? a

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

*****a*
? b

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

*****a*
? c

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

*****a*
? p

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

p****a*
? r

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

pr**ra*
? o

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

pro*ra*
? g

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

progra*
? m

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

Congrats!
Answer: program

Sample Run #2 – user loses (bold, underlined text is what the user types):

Game? 3

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

*******
? a

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

*****a*
? b

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

*****a*
? c

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

*****a*
? d

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

*****a*
? e

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

*****a*
? f

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

*****a*
? g

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

***g*a*
? h

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

You lost
Answer: program

Solutions

Expert Solution

screenshot

-------------------------------------------------------------------

Program

//Header files
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
//Function prototypes
void drawHangman(int);
string getWordFromFile(int);
int getGameNumberFromUser();
int getGussedCharFromUser();
string makeWord(string);
string changeMakeWord(string, string, char);
int checkCorrectness(string, string, char);
int main()
{

   //Variable declaration
   int hangman_level = 0;
   int game_no;
   int guess_char;
   int word_len;
   int match_count = 0;
   string word,secretWord;
   /*
   *
   * Getting initial inputs from user
   *
   */
   game_no = getGameNumberFromUser();
   //Game number less tham 1 then select random
   if (game_no < 1) {
       game_no = rand() % 5 + 1;
   }
   word = getWordFromFile(game_no);
   word_len = word.length();
   secretWord = makeWord(word);
   /*
   * Drawing initial hangman figure
   */
   drawHangman(hangman_level);

   while (true)
   {
       cout << secretWord << endl;
       /*
       * Iterate until win or loose
       */
       guess_char = getGussedCharFromUser();
       secretWord = changeMakeWord(word, secretWord, guess_char);
       /*
       * game repetition check
       */
       if (checkCorrectness(word, secretWord, guess_char) == 2) {
           match_count++;
       }
       else if (checkCorrectness(word, secretWord, guess_char) == 1) {
           /*
       * Draw next level hangman figure
       */
           drawHangman(hangman_level);
           cout << "Congrats you won !!!" << endl;
           break;
       }
       else
       {
           cout << "\noops" << endl;
           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; // break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop
       }
   }

   cout << "Game Over" << endl;
   return 0;
}

       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;
           }
       }

       /*
       * Getting a word from file
       */
       string getWordFromFile(int pos)
       {

           string word;
           ifstream ifstr;
           ifstr.open("words.txt");
           for (int i = 0; i < pos - 1; i++)
           {
               ifstr>>word;
           }
           ifstr>>word;
           ifstr.close();
           return word;
       }

       /*
       * Getting game number from user
       */
       int getGameNumberFromUser()
       {

           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 << "? ";
           cin >> guess_char;
           return guess_char;
       }
       /*
       Function to create guess word corresponding * word
       */
       string makeWord(string word) {
           string secretWord = "";
           for (int i = 0; i < word.length(); i++) {
               secretWord += '*';
           }
           return secretWord;
       }
       /*
       Function to change into correct letter instead of *
       When the guess char correct
       */
       string changeMakeWord(string word, string secretWord, char guess) {
           for (int i = 0; i < word.length(); i++) {
               if (word[i] == guess) {
                   secretWord[i] = guess;
               }
           }
           return secretWord;
       }
       /*
       Function to check the word correct or not
       If word and secret word same then win and return 1
       If match found
       then return 2
       else return 0
       */
       int checkCorrectness(string word, string secretWord, char guess) {
           if (word == secretWord) {
               return 1;
           }
           else {
               for (int i = 0; i < word.length(); i++) {
                   if (word[i] == guess) {
                       return 2;
                   }
               }
           }
           return 0;
       }

-------------------------------------------------------------------------------------

Output

Enter game number: 3
L----+----
|
|
|
X
*******
? a
L----+----
|
|
|
X
*****a*
? b

oops
L----+----
| O
|
|
X
*****a*
? c

oops
L----+----
| O
| |
|
X
*****a*
? d

oops
L----+----
| O
| /|
|
X
*****a*
? e

oops
L----+----
| O
| /|\
|
X
*****a*
? f

oops
L----+----
| O
| /|\
| /
X
*****a*
? g
L----+----
| O
| /|\
| /
X
***g*a*
? h

oops
L----+----
| O
| /|\
| / \
X
You lost !!!
Correct word: program
Game Over


Related Solutions

hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
c# Please write unit tests for this code, at least one or two that I can...
c# Please write unit tests for this code, at least one or two that I can understand how to write it. using System; namespace PrimeNumberFactors { class Program { static void Main(string[] args) { Console.Write("Enter a number you want to check: ");    if (int.TryParse(Console.ReadLine(), out int num)) { PrimeFactors(num); } else { Console.WriteLine("Invalid input!!"); } Console.ReadKey(); } public static void PrimeFactors(int num) { Console.Write($"Prime Factors of {num} are: ");    while (num % 2 == 0) { Console.Write("2 ");...
Good morning, Yes I did post part a Someone answered part (a) and (c) already. Here...
Good morning, Yes I did post part a Someone answered part (a) and (c) already. Here is the answered part (A). Can you complete part B please Thank you Here is part (A) again Comprehensive Problem 5 Part A: Note: You must complete part A before completing parts B and C. Genuine Spice Inc. began operations on January 1 of the current year. The company produces 8-ounce bottles of hand and body lotion called Eternal Beauty. The lotion is sold...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them on your stack. Also, print them. Read a character from the keyboard. If the character is an “o” then pop from the stack but don’t print. If the character is a “p”, then pop from the stack and print that number. If the character is an “e” or the stack is empty, end the program.
C++ Funcion For this lab you need to write a program that will read in two...
C++ Funcion For this lab you need to write a program that will read in two values from a user and output the greatest common divisor (using Euclidean algorithm) to a file. You will also need to demonstrate using ostream and ostringstream by creating 2 functions to output your print heading: one that uses ostream and the other uses ostringstream. Using ostream and ostringstream Write two PrintHeader functions that will allow you to output to the screen and to an...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user...
C# PLEASE Lab7B: For this lab, you’re going to write a program that prompts the user for the number of GPAs to enter. The program should then prompt the user to enter the specified number of GPAs. Finally, the program should print out the graduation standing of the students based on their GPAs. Your program should behave like the sample output below. Sample #1: Enter the number of GPAs: 5 GPA #0: 3.97 GPA #1: 3.5 GPA #2: 3.499 GPA...
Files I need to be edited: I wrote these files and I put them in a...
Files I need to be edited: I wrote these files and I put them in a folder labeled Project 7. I am using this as a study tool for a personal project of mine. //main.cpp #include <iostream> #include "staticarray.h" using namespace std; int main( ) {    StaticArray a;    cout << "Printing empty array -- next line should be blank\n";    a.print();    /*    // Loop to append 100 through 110 to a and check return value    // Should print "Couldn't append 110"...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions...
Lab 1 Write a program in the C/C++ programming language to input and add two fractions each represented as a numerator and denominator. Do not use classes or structures. Print your result (which is also represented as a numerator/denominator) to standard out. If you get done early, try to simplify your result with the least common denominator. The following equation can be used to add fractions: a/b + c/d = (a*d + b*c)/(b*d) Example: 1/2 + 1/4 = ( 1(4)...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A...
I HAVE ALREADY CORRECTLY COMPLETED PART A AND B PLEASE COMPLETE PART C ONLY Part A In late 2020, the Nicklaus Corporation was formed. The corporate charter authorizes the issuance of 6,000,000 shares of common stock carrying a $1 par value, and 2,000,000 shares of $5 par value, noncumulative, nonparticipating preferred stock. On January 2, 2021, 4,000,000 shares of the common stock are issued in exchange for cash at an average price of $10 per share. Also on January 2,...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes...
C++ Write a program that creates two rectangular shapes and then animates them. The two shapes should start on opposite ends of the screen and then move toward each other. When they meet in the middle of the screen, each shape reverses course and moves toward the edge of the screen. The two shapes keep oscillating and bouncing off of each other in the middle of the screen. The program terminates when the shapes meet each other in the middle...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT