Hangman Game in C++ The instructions are in the pictures. This is what it is supposed to look like: Here is a sample run of the program and what it should look like screen to screen: computer science programming Do you want to play hangman? (y or n): y Let's PLAY Word to Guess: PROGRAMMING -------| | | | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -| | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | / | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | / \ | | ----- Sorry you lose - the word was: PROGRAMMING Do you want to play hangman? (y or n): i Error - please enter (y or n) Do you want to play hangman? (y or n): Sure Let's PLAY Word to Guess: SCIENCE -------| | | | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | | | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -| | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | / | | ----- Enter a letter to guess: w You entered: W W is NOT in the word to guess. -------| | | O | -|- | / \ | | ----- Sorry you lose - the word was: SCIENCE Do you want to play hangman? (y or n): u Error - please enter (y or n) Do you want to play hangman? (y or n): NO Goodbye This is what I have so far but I can't get it to work or to the final product: #include using namespace std;   int main() { string BOARD1 = " -------|\n  |     |\n        |\n        |\n        |\n        |\n       -----\n"; string BOARD2 = " -------|\n  |     |\n  O     |\n        |\n        |\n        |\n       -----\n "; string BOARD3 = " -------|\n  |      |\n  O      |\n  |      |\n         |\n         |\n        -----\n"; string BOARD4 = "  -------|\n  |      |\n  O      |\n -|      |\n         |\n         |\n       -----\n"; string BOARD5 = "  -------|\n  |      |\n  O      |\n -|-     |\n         |\n         |\n       -----\n"; string BOARD6 = "  -------|\n  |      |\n  O      |\n -|-     |\n /       |\n         |\n       -----\n"; string BOARD7 = "  -------|\n  |      |\n  O      |\n -|-     |\n / \\     |\n         |\n       -----\n"; string word; cout << "Enter a letter to guess: "; cin >> word; int n = word.size(); char c; int ar[n + 1]; for (int i = 0; i < n; i++) { i = 0; } int i = 0; int numberOfWrongAns = 0; while (1) { if (numberOfWrongAns == 0) { cout << BOARD1; } else if (numberOfWrongAns == 1) { cout << BOARD2; } else if (numberOfWrongAns == 2) { cout << BOARD3; } else if (numberOfWrongAns == 3) { cout << BOARD3; } else if (numberOfWrongAns == 4) { cout << BOARD5; } else if (numberOfWrongAns == 5) { cout << BOARD6; } else if (numberOfWrongAns == 6) cout << BOARD7; break; } cout << "Enter a letter to guess: "; cin >> c; cout << "You entered:" << c << "\n\n"; bool flag = false; for (int j = 0; j < n; j++) { if (c == word[j]) { flag = true; ar[j]++; if (ar[j] == 1) { i++; } cout << c << " is in the word to guess\n"; break;

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Hangman Game in C++

The instructions are in the pictures. This is what it is supposed to look like:

Here is a sample run of the program and what it should look like screen to screen:

computer

science

programming

Do you want to play hangman? (y or n): y
Let's PLAY
Word to Guess: PROGRAMMING
-------|
| |
|
|
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
|
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
| |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-| |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
/ |
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
/ \ |
|
-----
Sorry you lose - the word was: PROGRAMMING
Do you want to play hangman? (y or n): i
Error - please enter (y or n)
Do you want to play hangman? (y or n): Sure
Let's PLAY
Word to Guess: SCIENCE
-------|
| |
|
|
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
|
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
| |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-| |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
|
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
/ |
|
-----
Enter a letter to guess: w
You entered: W
W is NOT in the word to guess.
-------|
| |
O |
-|- |
/ \ |
|
-----
Sorry you lose - the word was: SCIENCE
Do you want to play hangman? (y or n): u
Error - please enter (y or n)
Do you want to play hangman? (y or n): NO
Goodbye

This is what I have so far but I can't get it to work or to the final product:

#include<iostream>
using namespace std;
 
int main() {
string BOARD1 = " -------|\n  |     |\n        |\n        |\n        |\n        |\n       -----\n";
string BOARD2 = " -------|\n  |     |\n  O     |\n        |\n        |\n        |\n       -----\n ";
string BOARD3 = " -------|\n  |      |\n  O      |\n  |      |\n         |\n         |\n        -----\n";
string BOARD4 = "  -------|\n  |      |\n  O      |\n -|      |\n         |\n         |\n       -----\n";
string BOARD5 = "  -------|\n  |      |\n  O      |\n -|-     |\n         |\n         |\n       -----\n";
string BOARD6 = "  -------|\n  |      |\n  O      |\n -|-     |\n /       |\n         |\n       -----\n";
string BOARD7 = "  -------|\n  |      |\n  O      |\n -|-     |\n / \\     |\n         |\n       -----\n";
string word;
cout << "Enter a letter to guess: ";
cin >> word;
int n = word.size();
char c;
int ar[n + 1];
for (int i = 0; i < n; i++)
{
i = 0;
}
int i = 0;
int numberOfWrongAns = 0;
while (1)
{
if (numberOfWrongAns == 0)
{
cout << BOARD1;
}
else if (numberOfWrongAns == 1)
{
cout << BOARD2;
}
else if (numberOfWrongAns == 2)
{
cout << BOARD3;
}
else if (numberOfWrongAns == 3)
{
cout << BOARD3;
}
else if (numberOfWrongAns == 4)
{
cout << BOARD5;
}
else if (numberOfWrongAns == 5)
{
cout << BOARD6;
}
else if (numberOfWrongAns == 6)
cout << BOARD7;
break;
}
cout << "Enter a letter to guess: ";
cin >> c;
cout << "You entered:" << c << "\n\n";
bool flag = false;
for (int j = 0; j < n; j++)
{
if (c == word[j])
{
flag = true;
ar[j]++;
if (ar[j] == 1)
{
i++;
}
cout << c << " is in the word to guess\n";
break;
These files must be used in your program, but CANNOT be altered!
• randword.h
• randword.cpp
RandWord files:
o Download and add the randword files given above as existing files to
your project saved locally to the project.
o Using the randword header file and source file:
Call the getWords() function once in the program to get all the
words out of the data file and setup the game play for the
entire program run.
· Then call the getNextWord() function to generate the random
word out of the data file to guess.
. getNextWord() comes back either with a random word
from the list of words in the handman.dat file or
empty string if no new word is available to play with.
The getNextWord() will never return the same word to
play with in a game play, unless the word is duplicated
in the file.
You will still need to take the random word and convert
it to upper case so that the program plays the game
ignoring case.
Remove all code from your int main that was doing anything with
ifstream and ofstream.
MyFuncts Files:
o Create your own separate header file named myfuncts.h (or use the
header file from class myfuncts.h)
Create your own separate source file named myfuncts.cpp (or use
the source file from class myfuncts.cpp)
Declare pre-processor named constants in MyFuncts.h:
. PLAY, STOP and ERROR with the following values as shown
below:
PLAY 1
STOP 0
ERROR -1
• Design of Function: int PromptYN(string reply);
o Input (Pre-condition of function) - the answer to a Yes/No question
as reply from user
o Output (Post-condition of function) - the answer coming out of the
function (PLAY, STOP or ERROR)
The following arguments should return PLAY: YES, yes, Yes,
SURE, sure, Sure, OK, ok, Ok, Y, y
The following argument should return STOP: NO, no, No,
QUIT, quit, Quit, STOP, stop, Stop, TERMINATE, terminate,
Terminate, N, n, Q or q
Process (job of the function) -
the function should check to see if the reply passed in as an
argument to the function is yes, no or error. If yes the function
should return PLAY, else if no the function should return
STOP and anything else the function should return ERROR.
This function does NOT ask the question directly, but takes
the reply and figures out the status.
Transcribed Image Text:These files must be used in your program, but CANNOT be altered! • randword.h • randword.cpp RandWord files: o Download and add the randword files given above as existing files to your project saved locally to the project. o Using the randword header file and source file: Call the getWords() function once in the program to get all the words out of the data file and setup the game play for the entire program run. · Then call the getNextWord() function to generate the random word out of the data file to guess. . getNextWord() comes back either with a random word from the list of words in the handman.dat file or empty string if no new word is available to play with. The getNextWord() will never return the same word to play with in a game play, unless the word is duplicated in the file. You will still need to take the random word and convert it to upper case so that the program plays the game ignoring case. Remove all code from your int main that was doing anything with ifstream and ofstream. MyFuncts Files: o Create your own separate header file named myfuncts.h (or use the header file from class myfuncts.h) Create your own separate source file named myfuncts.cpp (or use the source file from class myfuncts.cpp) Declare pre-processor named constants in MyFuncts.h: . PLAY, STOP and ERROR with the following values as shown below: PLAY 1 STOP 0 ERROR -1 • Design of Function: int PromptYN(string reply); o Input (Pre-condition of function) - the answer to a Yes/No question as reply from user o Output (Post-condition of function) - the answer coming out of the function (PLAY, STOP or ERROR) The following arguments should return PLAY: YES, yes, Yes, SURE, sure, Sure, OK, ok, Ok, Y, y The following argument should return STOP: NO, no, No, QUIT, quit, Quit, STOP, stop, Stop, TERMINATE, terminate, Terminate, N, n, Q or q Process (job of the function) - the function should check to see if the reply passed in as an argument to the function is yes, no or error. If yes the function should return PLAY, else if no the function should return STOP and anything else the function should return ERROR. This function does NOT ask the question directly, but takes the reply and figures out the status.
Terminate, N, h, Q
Process (job of the function) -
the function should check to see if the reply passed in as an
argument to the function is yes, no or error. If yes the function
should return PLAY, else if no the function should return
STOP and anything else the function should return ERROR.
This function does NOT ask the question directly, but takes
the reply and figures out the status.
Declare the function prototype in MyFuncts.h
o Define the function in MyFuncts.cpp
o Call the function in hangman.cpp
Program Game Play:
o The game allows the user to play multiple rounds of hangman until
they run out of words or indicate they want to stop.
• Your main program should prompt the user "Do you want to
play a round of hangman? (Y/N)"
It should pass the reply from the user to the function
(promptYN) and based upon the answer coming out of the
function the round of hangman will either be played or not.
If there is an error, reprompt the user to play the game of
hangman.
• Only give them 3 consectutive error tries. If they cannot
say correctly whether or not they want to play the
game, stop the program.
Also they can only play the game as long as they have a word
to guess and it isn't the empty string.
Once they have played a round of hangman with a word in the
file the word is no longer available to play hangman with
again.
If there are no more words to guess the empty string "" is
returned and the user cannot play the game.
You need to check PLAY, STOP and ERROR the
#defines in your main program when figuring out
whether or not to play a round of hangman, not the
integer values of 1, 0 and -1.
• DrawHangman Functions:
o In the same file as main, create a function named drawHangman.
This function should take the incorrect count number as its
argument and then draw the board based upon that number. You
need to write both a function prototype that goes before main and a
function definition that goes after main and call the function inside
of main.
Modularize Main:
Create additional functions and separate out the details from inside
of main to a separate function that gets called from main.
You may separate out any other tasks into separate functions
that you would like.
If the task is generic and you feel it could be used in other
program, then put it in the myfuncts files.
If the task is specific to playing hangman then put it in the
same file as main.
The program still doesn't handle figuring out if the user has guessed the
entire word yet or not. That is coming in the next assignments.
Transcribed Image Text:Terminate, N, h, Q Process (job of the function) - the function should check to see if the reply passed in as an argument to the function is yes, no or error. If yes the function should return PLAY, else if no the function should return STOP and anything else the function should return ERROR. This function does NOT ask the question directly, but takes the reply and figures out the status. Declare the function prototype in MyFuncts.h o Define the function in MyFuncts.cpp o Call the function in hangman.cpp Program Game Play: o The game allows the user to play multiple rounds of hangman until they run out of words or indicate they want to stop. • Your main program should prompt the user "Do you want to play a round of hangman? (Y/N)" It should pass the reply from the user to the function (promptYN) and based upon the answer coming out of the function the round of hangman will either be played or not. If there is an error, reprompt the user to play the game of hangman. • Only give them 3 consectutive error tries. If they cannot say correctly whether or not they want to play the game, stop the program. Also they can only play the game as long as they have a word to guess and it isn't the empty string. Once they have played a round of hangman with a word in the file the word is no longer available to play hangman with again. If there are no more words to guess the empty string "" is returned and the user cannot play the game. You need to check PLAY, STOP and ERROR the #defines in your main program when figuring out whether or not to play a round of hangman, not the integer values of 1, 0 and -1. • DrawHangman Functions: o In the same file as main, create a function named drawHangman. This function should take the incorrect count number as its argument and then draw the board based upon that number. You need to write both a function prototype that goes before main and a function definition that goes after main and call the function inside of main. Modularize Main: Create additional functions and separate out the details from inside of main to a separate function that gets called from main. You may separate out any other tasks into separate functions that you would like. If the task is generic and you feel it could be used in other program, then put it in the myfuncts files. If the task is specific to playing hangman then put it in the same file as main. The program still doesn't handle figuring out if the user has guessed the entire word yet or not. That is coming in the next assignments.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Concept of pointer parameter
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education