Page 1 of 1

Assignment purpose: programmer defined functions, call by reference and call by value, i/o in C++, simple loop, data typ

Posted: Sun Jul 03, 2022 11:22 am
by answerhappygod
Assignment purpose: programmer defined functions, call byreference and call by value, i/o in C++, simple loop, datatypes string, character arrays, int, and bool, class declaration,vector practiceYou will write a program that will play a hangman game and allowmultiple players to play the game by storing eachgame object (one round of the game) in a vector.• You will use 1solution string and 2 character arrays as describedbelow in the gameHangman class• The solution word will be read from an input file called“words.txt” (all lowercase letters)• The hint word (or starword) will be created after reading thesolution word from the file• The letters array will store all the letters that have beenguessed so far (correct and incorrect)• Interact with each player by getting the first name(currentPlayer)• Add the current player to the vector (use push_back)cout << "Enter your first name: ";//into the member variable namecin >> currentPlayer.name;gameList.push_back(currentPlayer); //add the current player to thevector gameList• Ask the player if they want to play before getting started, seethe sample output• All the words in the file will have all lowercase letters• If the player enters an uppercase letter, number or othercharacter, it will count as an incorrect guess• You do not need to force the player to enter a lowercase letter,or change their input to a lowercase letter• The players should be able to continue to play another round byentering (y or Y) OR quit by entering (n or N)• Store all the information about each round of the game in avector• When the player(s) wish to close the program, display the gameresults from the array both on the screen and inan output file, the player will enter the name of the outputfilechar filename[40];//user should enter filename with .txt extensioncout << "Enter the filename: ";cin >> filename;//add to the file if the user enters the same file nameout.open(filename, ios::app);HOW TO PLAY THE GAME• Allow the player to guess letters one character at a time• At every stage of the game, the player should be able to view thecurrent state of the word in progress (starwordarray) and the list of guessed letters (letters array)• If a letter is not correct, the incorrect count isincreased.• In ALL cases, the guessed letter should be added to the array ofletters• If a letter is correctly guessed the player is notified and theletter appears in its proper location in the (starword),• The player is allowed up to 6 incorrect letter guesses. (Head,body, arm1, arm2, leg1, leg2)• The game is over when the player guesses all the letters, or thesix incorrect guesses are used up.• When the game is over, the next player should be allowed to playwithout having to execute the program again.(The player will play with the next word in the file)Note: the only way to win is to guess all the letterscorrectly
Use the class below to store the data for each round ofgameHangmanclass gameHangman{public:string solutionWord; //solution wordchar starWord[20] = "";//hint word made of *char letters[20] = "";//letters guessed so far (right orwrong)int letterCount = 0;//number of letters guessed so far (right orwrong)char letterGuess = ' ';//current letter guessint incorrectCount = 0; //keep track of the number of incorrectguessesint maxGuesses = 6; //six guesses for hangmanint wordLength = 0;//length of the solution wordbool correctLetter = false;//flag to set if the letter is in thewordstring name;bool wonOrLost = 0; //true or false for the current round of thegame};//Functionsvoid HangmanRules();//displays the hangman game rulesvoid PlayOneRound(ifstream& in, const string& name,gameHangman& currentGame);//plays one round of the game, calls the following functions://SetUpOneRound, GetLetterGuess, UpdateLettersArray,CorrectLetterGuess, and DrawHangmanvoid SetUpOneRound(ifstream& in, gameHangman&currentGame);//sets up each round of the game, calls the functionCreateStarArrayvoid CreateStarArray(gameHangman& currentGame);//Gets the length of the solution word and create the hint word of"*"void GetLetterGuess(gameHangman& currentGame);//Displays the hint (star array) and the letters guessed so far andgets a character guess fromthe uservoid UpdateLettersArray(gameHangman& currentGame);//adds the guessed letter (correct and incorrect) to the lettersguessed arraybool CorrectLetterGuess(gameHangman& currentGame);//if the letter guessed is in the solution word, adds the letter tothe hint (starword) andreturns truevoid DrawHangman(gameHangman&);//Draw or describe the hangman drawingvoid PrintPlayerResults(vector <gameHangman> gameList, intgameCount);//prints the list of players onto the screen and into an outputfileHere is sample output:WELCOME TO HANGMAN!Please read the following instructions before you play.-You will be presented with a word to be guessed (starword)-Guess the letters one at a time, all lowercase letters!-You can have up to six incorrect letter guesses-The game is over when you guess all letters correctly (WIN)ORwhen you have guessed letters incorrectly SIX times (LOSS).HAVE FUN!Do you want to play the hangman game? (y(Y) or n(N)): yEnter your first name: Tami___________________________________________________Here is the word so far:*****Here are the letters guessed so far:Enter a letter: tThe letter was in the word!___________________________________________________Here is the word so far:t****Here are the letters guessed so far:tEnter a letter: 1Tami, your letter was not in the wordYou have added a body part!___________________________________________________HEAD(1 of 6 incorrect guesses)!___________________________________________________Here is the word so far:t****Here are the letters guessed so far:t1Enter a letter: $
Tami, your letter was not in the wordYou have added a body part!___________________________________________________HEADBODY(2 of 6 incorrect guesses)!___________________________________________________Here is the word so far:t****Here are the letters guessed so far:t1$Enter a letter: wTami, your letter was not in the wordYou have added a body part!___________________________________________________HEADBODYARM 1(3 of 6 incorrect guesses)!___________________________________________________Here is the word so far:t****Here are the letters guessed so far:t1$wEnter a letter: GTami, your letter was not in the wordYou have added a body part!___________________________________________________HEADBODYARM 1ARM 2(4 of 6 incorrect guesses)!___________________________________________________Here is the word so far:t****Here are the letters guessed so far:t1$wGEnter a letter: gThe letter was in the word!___________________________________________________Here is the word so far:t*g**Here are the letters guessed so far:t1$wGgEnter a letter: wTami, your letter was not in the wordYou have added a body part!___________________________________________________
HEADBODYARM 1ARM 2LEG 1(5 of 6 incorrect guesses)!___________________________________________________Here is the word so far:t*g**Here are the letters guessed so far:t1$wGgwEnter a letter: zTami, your letter was not in the wordYou have added a body part!___________________________________________________HEADBODYARM 1ARM 2LEG 1LEG 2(6 of 6 incorrect guesses)!GAME OVERThe word was tiger!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Do you want to play the hangman game? (y(Y) or n(N)): yEnter your first name: Gio___________________________________________________Here is the word so far:***********Here are the letters guessed so far:Enter a letter: mThe letter was in the word!___________________________________________________Here is the word so far:******mm***Here are the letters guessed so far:mEnter a letter: RGio, your letter was not in the wordYou have added a body part!___________________________________________________HEAD(1 of 6 incorrect guesses)!___________________________________________________Here is the word so far:******mm***Here are the letters guessed so far:mREnter a letter: r
The letter was in the word!___________________________________________________Here is the word so far:*r**r*mm***Here are the letters guessed so far:mRrEnter a letter: gThe letter was in the word!___________________________________________________Here is the word so far:*r*gr*mm**gHere are the letters guessed so far:mRrgEnter a letter: pThe letter was in the word!___________________________________________________Here is the word so far:pr*gr*mm**gHere are the letters guessed so far:mRrgpEnter a letter: oThe letter was in the word!___________________________________________________Here is the word so far:progr*mm**gHere are the letters guessed so far:mRrgpoEnter a letter: nThe letter was in the word!___________________________________________________Here is the word so far:progr*mm*ngHere are the letters guessed so far:mRrgponEnter a letter: iThe letter was in the word!___________________________________________________Here is the word so far:progr*mmingHere are the letters guessed so far:mRrgponiEnter a letter: zGio, your letter was not in the wordYou have added a body part!___________________________________________________
HEADBODY(2 of 6 incorrect guesses)!___________________________________________________Here is the word so far:progr*mmingHere are the letters guessed so far:mRrgponizEnter a letter: aThe letter was in the word!Congratulations Gio you won that round!The word was programming!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Do you want to play the hangman game? (y(Y) or n(N)): yEnter your first name: Nia___________________________________________________Here is the word so far:*************Here are the letters guessed so far:Enter a letter: cThe letter was in the word!___________________________________________________Here is the word so far:c******c*****Here are the letters guessed so far:cEnter a letter: mThe letter was in the word!___________________________________________________Here is the word so far:c*mm***c*****Here are the letters guessed so far:cmEnter a letter: oThe letter was in the word!___________________________________________________Here is the word so far:comm***c***o*Here are the letters guessed so far:cmoEnter a letter: uThe letter was in the word!___________________________________________________
Here is the word so far:commu**c***o*Here are the letters guessed so far:cmouEnter a letter: nThe letter was in the word!___________________________________________________Here is the word so far:commun*c***onHere are the letters guessed so far:cmounEnter a letter: tThe letter was in the word!___________________________________________________Here is the word so far:commun*c*t*onHere are the letters guessed so far:cmountEnter a letter: uThe letter was in the word!___________________________________________________Here is the word so far:commun*c*t*onHere are the letters guessed so far:cmountuEnter a letter: kNia, your letter was not in the wordYou have added a body part!___________________________________________________HEAD(1 of 6 incorrect guesses)!___________________________________________________Here is the word so far:commun*c*t*onHere are the letters guessed so far:cmountukEnter a letter: iThe letter was in the word!___________________________________________________Here is the word so far:communic*tionHere are the letters guessed so far:cmountukiEnter a letter: aThe letter was in the word!Congratulations Nia you won that round!
The word was communication!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Do you want to play the hangman game? (y(Y) or n(N)): nThank you for playing, have a great day!Enter the filename: hangmanresults.txt***********************************Name won or lost incorrect countTami 0 6Gio 1 2Nia 1 1