Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This program will be written in C++ FILES TO TURN IN Usually, a C++ program consists of many header files (with names ending in .h),

This program will be written in C++

FILES TO TURN IN Usually, a C++ program consists of many header files (with names ending in .h), source files (with names ending in .cpp) and libraries. Header files include information for the compiler and source files include program elements that cause the compiler to generate something in the executable function. In this program you are going to have several files to turn in (NOT JUST ONE!!) 1. hangman.h this is your header file I will give you a partially complete header file to start with. 2. hangman.cpp this is your source file that contains your main function 3. functions.cpp this is your source file that contains all your other functions 4. wordBank.txt this is the file with words for the game to use. You should put 10 words in this text file. COMPILE THIS WAY: g++ -I ./ hangman.cpp functions.cpp o hangman

ARRAYS You will have four arrays. Three of the arrays will be C-Strings: 1. Character array of size 21 (20 characters plus one null-terminator) that will hold the word read in from the word bank. You may assume that the game only plays with words, not phrases. (No spaces). char word[21]; 2. Character array of size 21 (20 characters plus one null-terminator) that will at first hold all underscores. Then, as the user makes correct guesses, the underscores will be replaced with the correct letters. This means you will have to initialize this array to all underscores before the user makes any guesses. char underScores[21]; 3. Character array of size 27 (26 characters plus one null-terminator). Initialize this array when you define it to all the uppercase letters in the alphabet. ABCDEFGHIJKLMNOPQRSTUVWXYZ. char alphabet[27] = ABCDEFGHIJKLMNOPQRSTUVWXYZ; 4. Boolean array of size 26 (26 boolean values). This array will have a true/false toggle for each letter of the alphabet. If the user has guessed the letter B, then this array at element 1 will have the value of true. This array will also need to be initialized to all false before the user makes any guesses. bool userGuesses[26];

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

Word Bank You will have an input file called wordBank.txt. You need to manually type in TEN or more words to this file. Your program will not be outputting to this file. wordBank.txt will be used as an input file only. ------------------------------------------------------------------------------------------------------------------------------------------

Function: int main()

Function parameter: none

How the function should work: 1. Define all your variables including your four arrays. 2. Open the input file wordBank.txt. If the file does not open properly (or does not exist), print out a message that a word could not be found in the word bank. 3. Use a do/while statement to allow the user to play this game (run the program) multiple times. Do this after you open the file so that the user will get different words each time from the file. 4. Using a for loop, set all the elements of the userGuesses array to false. At this point in the program, the user has not guessed any letters yet. 5. Using a for loop, set all elements of the underScores array to the underscore character _. 6. Set the stage variable to zero (the starting stage) 7. Read in a word from the input file (get the word) 8. While the game has not ended (user is not at stage 6 yet and they have not guessed the word), then do the following: o Call the printWord function sending the word array and the underScores array as arguments. o Call the printStage function sending the stage variable as an argument. o Call the printLettersGuessed function sending the userGuesses array and alphabet array as arguments. o Ask the user for their guess and read it into a char. If it is a lowercase letter, then you need to make it an uppercase letter.

cout

cin >> letter;

letter = toupper(letter); //the toupper function will make letter uppercase.

Make sure the letter is not a letter already guessed. If it is a letter they have already guessed, then force the user to enter in a different letter. - I would put this bit of code in a do-while loop and use a Boolean variable (called alreadyGuessed) to keep track of if the letter is already guessed. - You can use a for loop like this one:

for(x=0; x26;>

{

if(letter = alphabet[x])

{

if(userGuesses[x] == true)

{

alreadyGuessed = true; cout

}

}

}

- Take the usersGuesses array and make the letter they just guessed true instead of false. You will need to do this with a for loop. For example, if they guessed the letter C then you will need to make usersGuesses[2] = true; - Find out if the letter that they guessed is inside the chosen word from the word bank. You will need a for loop for this as well to parse through the word character array.

If the letter guessed is found in the word array, then print out CORRECT! And then put the letter in the under

- Scores array in the same location as the wordFromFile array.

underScores[x] = word[x]; // where x is an integer incremented by the for loop. - If there were no letters found, then print out WRONG!, increment the stage number. If the stage number is already 6, then call the printStage() function. - Tell the user to press enter to continue.

- Find out if the user has guessed every letter in the word, because if they have, then the game is done and the while loop will stop.

- Find out the length of the chosen word

wordLength = strlen(word);

-Do a special string comparison that will compare two words up to a given length. The function is strncmp() that you will need to use. If the chosen word (word array) and the underScores array match, then the user has guessed the word and the game is over. Print out a message that the user won the game.

if(strncmp(word, underScores, wordLenght) == 0)

{

wordGuessed = true; cout cout ') \" \"

}

Function: void printWord(char[], char[])

Function parameters: char word[] & char underScores[]

This function will print out the letters in the word that have already been correctly guessed. At the beginning of the game, this function should just print out underscores to represent each letter. This lets the user know how many letters is in the word they are trying to guess.

Word: _ _ _ _ _ _ _ Then, as the user guesses a letter, replace the underscore with the letter:

Word: W _ _ _ _ _ _

How the function should work:

- Figure out the length of the character array word

int wordLength;

wordLength = strlen(word); - Use a for loop to go through as many iterations as the length of word - Print out each character of the underScores[] array. It should be the same length as word. - For example, if the word array is RAVEN, then the string length is 5. Write a for loop that will iterate five times printing out the characters in underScores[] array. The underScores array may have more than 5 characters in it, but you will only print out 5.

---------------------------------------------------------------------------------------------------------------------------------- Function: void printLettersGuessed(bool[], char[])

Function parameters: bool usersGuesses[], char alphabet[]

This function will print out all the letters the user has guessed so far (correct & incorrect).

How the function should work: - Starting at 0, for every element up until 25, if the current element of usersGuesses is true, then output the letter of the same element. For example, if the user has guessed the letter B, then usersGuesses[1] == true and so I need to print out alphabet[1], which will print out B to the screen. Go through the whole usersGuesses array checking if each letter is true, and then print out the letter if it is. - Make sure you print out: LETTERS YOU HAVE ALREADY GUESSED: before you print out the letters.

I did the function that draws the Hangman stage!!!

Here is a output picture:

I really need help for this one

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Law Business and Society

Authors: Tony McAdams

11th edition

78023866, 978-9814577311, 9814577316, 978-0078023866

More Books

Students also viewed these Programming questions