Question
c++ program: write a program to create a hangman game.. { char guessedletters[26] = { '0' }; //sets all guessed letters to null int guessleft
c++ program:
write a program to create a hangman game..
{ char guessedletters[26] = { '\0' }; //sets all guessed letters to null int guessleft = 6; int lenw; //length of actual word int guessed = 0; //if set to 1 then quit int i, x, xflag, winflag = 0; char userguess = '\0'; //the current letter guessed by the user char guessedword[20];
//The guessedword contains the word as the user //knows it at the current time. So if they //guessed one correct letter, it will show the one //letter in the correct location. It is set to //underscores so that it shows the user which //letters are still missing. for (x = 0; x<20; ++x) guessedword[x] = '_';
//The secretword is a global variable defined //at the top of the program. This means you //don't have to pass it to any other functions. //You don't have to use a secretword. You can //have an array with a bunch of secret words and //the computer randomly picks a word (recommended) //or you can have a second user enter a word. //Either way, leave it like this until you get //it working and then change it to one of the //above cases if you like. lenw = secretword.length(); //secretword is defined at top of program
//*ADD a WHILE LOOP here while the user has not guessed the //*word AND (&&) while you still have guesses left (guessleft is above 0) {
drawing(guessleft); //This draws the first hangman picture cout << "Guessed letters: "; //***Insert a for loop to print out the current list //***of guessed letters - that is, print out the array //***guessedletters cout << endl;
cout << "This is what you have: " << endl; for (x = 0; x cout << guessedword[x] << " "; cout << endl << endl;
//***Go through the guessedword array //***to see if it has been completely guessed //*** and if so, tell user they won and break out //*** or else if user did not win, //*** make sure winflag is set back to 0 //*** and continue on. Do it any way you like, //*** but winflag=1 if they guessed word and //*** winflag=0 if they have not.
//***Get user guess and save it in the //***variable userguess
xflag = 0; for (i = 0; i<26; ++i) //User has 26 tries with letters { if (userguess == guessedletters[i]) { cout << endl << "You already guessed this letter." << endl; system("pause"); break; } else if (guessedletters[i] == '\0') //this means the letter wasn't guessed yet { guessedletters[i] = userguess; //so add userguess as a guessed letter
//***go through and see if the userguess is anywhere in //***secretword, and if so, update the guessedword array //***with the letter. You will also need to //***update the guessleft variable (decrement it) if //***they haven't guessed correctly. If they did guess //***a correct letter than don't decrement guessleft since //***they don't lose a try if they guessed correctly.
} }
}
cout << endl << "Game finished..." << endl; if (winflag == 0) { drawing(0); cout << "Sorry. Better luck next time!" << endl; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started