//THIS IS AN EXAMPLE TO HELP YOU GET STARTED WITH THIS ASSIGNMENT /* STUDENT NAME Due Date: Course: C0P3014 Assignment:

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

//THIS IS AN EXAMPLE TO HELP YOU GET STARTED WITH THIS ASSIGNMENT /* STUDENT NAME Due Date: Course: C0P3014 Assignment:

Post by answerhappygod »

This Is An Example To Help You Get Started With This Assignment Student Name Due Date Course C0p3014 Assignment 1
This Is An Example To Help You Get Started With This Assignment Student Name Due Date Course C0p3014 Assignment 1 (154.66 KiB) Viewed 43 times
//THIS IS AN EXAMPLE TO HELP YOU GET STARTED WITH THISASSIGNMENT/*STUDENT NAMEDue Date:Course: C0P3014Assignment: Program 6Professor: SorgenteDescription: hangman game and vector of gameHangman objects*************************************************************/#define _CRT_SECURE_NO_WARNINGS#include <iostream> //standard library for i/o#include <fstream> //files#include <string> //strings#include <vector>#include <cstdlib> //for exit() if the file does notopenusing namespace std;/*********************************************************//Following is the declaration of a round of gameHangman**********************************************************/class 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 (rightorwrong)char letterGuess = ' ';//current letter guessint incorrectCount = 0; //keep track of the number ofincorrectguessesint maxGuesses = 6; //six guesses for hangmanint wordLength = 0;//length of the solution wordbool correctLetter = false;//flag to set if the letter is inthewordstring name;bool wonOrLost = 0; //true or false for the current round ofthegame};//Declaration/ Prototypes for your functions will go here//BE SURE TO ADD COMMENTS TO THE FUNCTION PROTOTYPES AND THEFUNCTIONDEFINITIONS////comments throughout the code making it easier to readvoid HangmanRules();//displays the hangman game rules
void 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 functionCreateStarArray//add the rest of the prototypesvoid PrintPlayerResults(vector <gameHangman> gameList, intgameCount);//prints the list of players onto the screen and into an outputfileint main(){//display the rulesstring yesOrNo = "y";gameHangman currentPlayer; //current playervector <gameHangman> gameList;//declare a vector ofgameHangmanobjectsint gameCount = 0;//count the number of gamesifstream in;//connect to the input fileif (in.fail())//file did not open correctly{cout << "Input file did not open correctly" <<endl;exit(1);}//want to play?while (yesOrNo != "n" && yesOrNo != "N"){//ask for the name here, it may be a different playercout << "\nEnter your first name: ";//into the member variable namecin >> currentPlayer.name;//add the current player to the vector gameListgameList.push_back(currentPlayer);//pass the file, name, and the current game object toPlayOneRoundPlayOneRound(in, gameList[gameCount].name,gameList[gameCount]);//add one to the gamecountgameCount++;
//again?}cout << "\nThank you for playing, have a great day!\n";//print the player listreturn 0;}//*************************************************************************************//Name: HangmanRules//Precondition://Postcondition://Description: Displays the hangman game rules//*************************************************************************************void HangmanRules(){//display the rules}//*************************************************************************************//Name: PlayOneRound//Precondition: The program has connected to the input file, thedatavariable (string)// and object (gameHangman) have been declared// the string has a value before calling this function(const)//Postcondition: One round of the game Hangman has beencompleted//Description: Plays one round of the game Hangman//*************************************************************************************void PlayOneRound(ifstream& in, const string& name,gameHangman&currentGame){//reset everything for the new round of the game//call the setup function//LOOP FOR THE ROUND OF THE GAME//the number of incorrect guesses is less than maxGuesses//and wonOrLost is false{//GetLetterGuess function//UpdateLetterArray function//if (CorrectLetterGuess(currentGame)) //check if theletter is in the word{
//cout << "\nThe letter was in the word!\n";//check if it is the final letter (solutionand star are ==){//cout << "\nCongratulations " <<currentGame.name << " you won that round!\n" <<endl;//currentGame.wonOrLost = true;}}//else //the letter was not in the word{//let the user know//cout << endl << currentGame.name << ",yourletter was not in the word " << endl;//cout << "You have added a body part! " <<endl;//add one to incorrect count//currentGame.incorrectCount++;//draw the hangman}}//display the solutioncout <<"\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";}//*************************************************************************************//Name: SetUpOneRound//Precondition://Postcondition://Description: Sets up each round of the game, calls thefunctionCreateStarArray//*************************************************************************************void SetUpOneRound(ifstream& in, gameHangman&currentGame){currentGame.solutionWord = "";//clear the solution wordstrcpy(currentGame.starWord, "");strcpy(currentGame.letters, "");currentGame.letterCount = 0; //letter count starts at 0currentGame.letterGuess = ' ';currentGame.incorrectCount = 0;currentGame.maxGuesses = 6;currentGame.wordLength = 0;currentGame.correctLetter = false; //reset to falsecurrentGame.wonOrLost = false; //reset to falsein >> currentGame.solutionWord;//create the star array}
//*************************************************************************************//Name: CreateStarArray//Precondition://Postcondition://Description: Gets the length of the solution word and create thehintword of "*"//*************************************************************************************void CreateStarArray(gameHangman& currentGame){//get the length of the solution word//currentGame.wordLength =(int)currentGame.solutionWord.length();//use a loop to create the array of *//add the null ('\0') character after the loop}//*************************************************************************************//Name: GetLetterGuess//Precondition://Postcondition://Description: Displays the hint (star array) and the lettersguessed sofar//and gets a character guess from the user//*************************************************************************************void GetLetterGuess(gameHangman& currentGame){//display the star (hint) array//display the letters that have been guessed so far//get a letter guess from the user}//*************************************************************************************//Name: UpdateLettersArray//Precondition://Postcondition://Description: Add the guessed letter (correct and incorrect) totheletters guessed array//*************************************************************************************void UpdateLettersArray(gameHangman& currentGame){//Add the letter guessed to the letters array
//currentGame.letters[currentGame.letterCount] =currentGame.letterGuess;//currentGame.letterCount++;//currentGame.letters[currentGame.letterCount] = '\0'; //addthenull character}//*************************************************************************************//Name: CorrectLetterGuess//Precondition://Postcondition://Description: if the letter guessed is in the solution word,// adds the letter to the hint (starword) and returns true//*************************************************************************************bool CorrectLetterGuess(gameHangman& currentGame){bool rv = false;//use a loop to search the solution word//if the letter is found//replace the letters in the starword and//set the rv to truereturn rv;}//*************************************************************************************//Name: DrawHangman//Precondition://Postcondition://Description: Draw or describe the hangman drawing//*************************************************************************************void DrawHangman(gameHangman& currentGame){cout <<"\n___________________________________________________\n";if (currentGame.incorrectCount == 0){cout << "\nYou have no incorrect guesses yet!O-|--<";}else if (currentGame.incorrectCount == 1){cout << "\nHEAD\n (1 of 6 incorrect guesses)!";}//add the rest of the conditionselse cout << "\ncount is not recognized";}
//*************************************************************************************//Name: PrintPlayerResults//Precondition://Postcondition://Description: Connects to an output file//Prints the list of players onto the screen and into an outputfile//*************************************************************************************void PrintPlayerResults(vector <gameHangman> gameList, intgameCount){ofstream out;char 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);cout << "\n***********************************\n";cout << "Name\t" << "won or lost\t" << "incorrectcount\n";//use a loop to print the vector (array) (name, WonOrLost,incorrectcount) onto the screenout << "\n***********************************\n";out << "Name\t" << "won or lost\t" << "incorrectcount\n";//use a loop to print the vector (array) (name, WonOrLost,incorrectcount) into the file//close the fileout.close();}
COP 3014-program 6- hangman game Assignment purpose: programmer defined functions, call by reference and call by value, i/o in C++, simple loop, data types string, character arrays, int, and bool, class declaration, vector practice You will write a program that will play a hangman game and allow multiple players to play the game by storing each game object (one round of the game) in a vector. • . . The hint word (or starword) will be created after reading the solution word from the file The letters array will store all the letters that have been guessed 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) . . You will use Isolution string and 2 character arrays as described below in the gameHangman class The solution word will be read from an input file called "words.txt" (all lowercase letters) • If the player enters an uppercase letter, number or other character, 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 by entering (y or Y) OR quit by entering (n or N) Store all the information about each round of the game in a vector ● . cout<<"Enter your first name: "; //into the member variable name cin >> currentPlayer.name; . gameList.push_back (currentPlayer); //add the current player to the vector gameList Ask the player if they want to play before getting started, see the sample output All the words in the file will have all lowercase letters HOW TO PLAY THE GAME When the player(s) wish to close the program, display the game results from the array both on the screen and In an output file, the player will enter the name of the output file char filename [40]; //user should enter filename with .txt extension cout << "Enter the filename: "; cin >> filename; //add to the file if the user enters the same file name out.open(filename, ios::app); Allow the player to guess letters one character at a time At every stage of the game, the player should be able to view the current state of the word in progress (starword array) and the list of guessed letters (letters array) If a letter is not correct, the incorrect count is increased. In ALL cases, the guessed letter should be added to the array of letters If a letter is correctly guessed the player is notified and the letter 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 the six Incorrect guesses are used up. When the game is over, the next player should be allowed to play without 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 letters correctly CS Scanned with CamScanner
Use the class below to store the data for each round of gameHangman class gameHangman ( public: }; string solutionWord; //solution word. char starword [20]=;//hint word made of * char letters [20] = ";//letters guessed so far (right or wrong) int letterCount = 0;//number of letters guessed so far (right or wrong) char letterGuess = ';//current letter guess int incorrect Count = 0; //keep track of the number of incorrect guesses int maxGuesses = 6; //six guesses for hangman int wordLength = 0;//length of the solution word bool correctLetter = false; //flag to set if the letter is in the word string name; bool wonOrLost = 0; //true or false for the current round of the game //Functions void HangmanRules(); //displays the hangman game rules void PlayOneRound(ifstream& in, const string& name, gameHangman& currentGame); //plays one round of the game, calls the following functions: //SetUpOneRound, GetLetterGuess, UpdateLettersArray, CorrectLetterGuess, and DrawHangman void SetUpOneRound (ifstream& in, gameHangman& currentGame); //sets up each round of the game, calls the function CreateStarArray void CreateStarArray (gameHangman& current Game); //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 and gets a character guess from the user void UpdateLettersArray(gameHangman& current Game); //adds the guessed letter (correct and incorrect) to the letters guessed array bool CorrectLetterGuess(gameHangman& current Game); //if the letter guessed is in the solution word, adds the letter to the hint (starword) and returns true void DrawHangman (gameHangman&); //Draw or describe the hangman drawing void PrintPlayerResults(vector <gameHangman> gameList, int gameCount); //prints the list of players onto the screen and into an output file Additional instructions: ● ● . You may add additional functions, but do not modify the functions provided Be sure to comment your code Include a program header with the following information: o Name, due date, course, assignment number, professor name, and a brief description of the assignment see the sample skeleton provided with the assignment. Read all comments in the sample code provided before getting started Read the problem and determine what to do ● Write the algorithm (you DO NOT need to submit the algorithm) CS Scanned with CamScanner
. Implement one component at a time in your code and do not move onto the next component until you are sure it is correct. Test your program before submitting One source code file (unformatted text) will be submitted The file name must match the assignment ● ● . The code should be tested and run on an IDE before it is uploaded onto Canvas The code must be submitted on time in order to receive credit (11:59 PM on the due date) Late submissions will not be accepted or graded ● All programming assignments are individual work, sharing code is considered cheating Here is sample output: . . WELCOME TO HANGMAN! Please read the following instructions before you play. -You will be presented with a word to be quessed (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) OR when you have guessed letters incorrectly SIX times (LOSS). HAVE FUN! Do you want to play the hangman game? (y (Y) or n (N)): y Enter your first name: Tami Here is the word so far: ***** Here are the letters guessed so far: Enter a letter: t The letter was in the word! Here is the word so far: ***** Here are the letters quessed so far: t Enter a letter: 1 Tami, your letter was not in the word You have added a body part! HEAD (1 of 6 incorrect quesses)! Here is the word so far: ***** Here are the letters guessed so far: ti Eersterned with CamScanner
Tami, your letter was not in the word You have added a body part! HEAD BODY (2 of 6 incorrect quesses)! Here is the word so far: Here are the letters guessed so far: ti$ Enter a letter: w Tami, your letter was not in the word You have added a body part! HEAD BODY ARM 1 (3 of 6 incorrect quesses)! Here is the word so far: t.... Here are the letters guessed so far: ti$w Enter a letter: G Tami, your letter was not in the word You have added a body part! HEAD BODY ARM 1 ARM 2 (4 of 6 incorrect quesses)! Here is the word so far: t... Here are the letters quessed so far: tiswG Enter a letter: g The letter was in the word! Here is the word so far: t*g** Here are the letters guessed so far: t15wGg Enter a letter: w Tami, your letter was not in the word You have added a body part! CS Scanned with CamScanner
HEAD BODY ARM 1 ARM 2 LEG 1 (5 of 6 incorrect quesses)! Here is the word so far: t*g** Here are the letters quessed so far: t1wGgw Enter a letteri z Tami, your letter was not in the word You have added a body part! HEAD BODY ARM 1 ARM 2 LEG 1 LEG 2 (6 of 6 incorrect quesses)! GAME OVER The word was tiger ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! Do you want to play the hangman game? (y (Y) Y Enter your first name: Gio or n (N)) : Here is the word so far: *********** Here are the letters guessed so far: Enter a letter: m The letter was in the word! Here is the word so far: ********* Here are the letters quessed so far: m Enter a letter: R Glo, your letter was not in the word. You have added a body part! HEAD (1 of 6 incorrect quesses)! Here is the word so far: ********* Here are the letters guessed so far: mR letter: Scanned with CamScanner
The letter was in the word! Here is the word so far: *r**r*mm*** Here are the letters guessed so far: mRr Enter a letter: g The letter was in the word! Here is the word so far: *r*gr*mm**g Here are the letters guessed so far: mRrg Enter a letter: p The letter was in the word! Here is the word so far: pr gr mm g Here are the letters quessed so far: mRrgp Enter a letter: o The letter was in the word! Here is the word so far: progr*mm*g Here are the letters quessed so far: mRrgpo Enter a letter: n The letter was in the word! Here is the word so far: progr*mm*ng Here are the letters guessed so far: mRrgpon Enter a letter: i The letter was in the word! Here is the word so far: programming Here are the letters quessed so far: mRrgponi Enter a letter: z Gio, your letter was not in the word You CS Scanned with CamScanner
HEAD BODY (2 of 6 incorrect quesses)! Here is the word so far: programming Here are the letters guessed so far: mRrgponiz Enter a letter: a The 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)): Enter your first name: Nia Here is the word so far: Here are the letters guessed so far: Enter a letter: c The letter was in the word! Here is the word so far: C*********** Here are the letters guessed so far: Enter a letter: mi The letter was in the word! Here is the word so far: C*mm******** Here are the letters quessed so far: Enter a letter: o The letter was in the word! Here is the word so far: commc**0* Here are the letters quessed so far: cmo Enter a letter: u The letter was in the word! CS Scanned with CamScanner Y
Here is the word so far: commu**c***0* Here are the letters guessed so far: cmou Enter a letter: n The letter was in the word! Here is the word so far: commun* c**on Here are the letters quessed so far: cmoun Enter a letter: t The letter was in the word! Here is the word so far: commun*c*t*on Here are the letters guessed so far: cmount Enter a letter: u The letter was in the word! Here is the word so far: commun c*t*on Here are the letters quessed so far: cmountu Enter a letter: k Nia, your letter was not in the word. You have added a body part! HEAD (1 of 6 incorrect quesses)! Here is the word so far: commun c*t*on Here are the letters quessed so far: cmountuk Enter a letter: i The letter was in the word! Here is the word so far: communic'tion Here are the letters guessed so far: cmountuki Enter a letter: a The letter was in the word! CS with CamScanner gratulations Nia you won that round!
The word was communication. ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! !!!!!! Do you want to play the hangman game? (y (Y) or n (N)): n Thank you for playing, have a great day! Enter the filename: hangmanresults.txt Name Iwon or lost Tami Gio Nia 0 1 1 ***** incorrect count 6 2 1 CS Scanned with CamScanner
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply