c++ program need help. write a program to create a hangman game with the algorit
ID: 3836302 • Letter: C
Question
c++ program need help.
write a program to create a hangman game with the algorithm used below(not with different algorithm).It should use everything as below
{
char guessedletters[26] = { '' }; //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 = ''; //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] == '') //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;
}
Explanation / Answer
Here is the solution :-
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
const int MAX_TRIES=5;
int letterFill (char, string, string&);
int main ()
{
string name;
char letter;
int num_of_wrong_guesses=0;
string word;
string words[] =
{
"india",
"japan",
"nepal",
"china",
"madagascar",
"azerbaijan",
"kyrgyzstan",
"turkmenistan",
"french guiana",
"new caledonia"
};
//choose and copy a word from array of words randomly
int n=rand()% 10;
word=words[n];
// Initialize the secret word with the * character.
string unknown(word.length(),'*');
// welcome the user
cout << " Welcome to hangman!! Guess a country that comes into your mind.";
cout << " Each letter is represented by an asterisk.";
cout << " You have to type only one letter in one try.";
cout << " You have " << MAX_TRIES << " tries to try and guess the country.";
cout << " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
// Loop until the guesses are used up
while (num_of_wrong_guesses < MAX_TRIES)
{
cout << " " << unknown;
cout << " Guess a letter: ";
cin >> letter;
// Fill secret word with letter if the guess is correct,
// otherwise increment the number of wrong guesses.
if (letterFill(letter, word, unknown)==0)
{
cout << endl << "Whoops! That letter isn't in there!" << endl;
num_of_wrong_guesses++;
}
else
{
cout << endl << "You found a letter! Isn't that exciting?" << endl;
}
// Tell user how many guesses has left.
cout << "You have " << MAX_TRIES - num_of_wrong_guesses;
cout << " guesses left." << endl;
// Check if user guessed the word.
if (word==unknown)
{
cout << word << endl;
cout << "Yeah! You got it!";
break;
}
}
if(num_of_wrong_guesses == MAX_TRIES)
{
cout << " Sorry, you lose...you've been hanged." << endl;
cout << "The word was : " << word << endl;
}
cin.ignore();
cin.get();
return 0;
}
/* Take a one character guess and the secret word, and fill in the
unfinished guessword. Returns number of characters matched.
Also, returns zero if the character is already guessed. */
int letterFill (char guess, string secretword, string &guessword)
{
int i;
int matches=0;
int len=secretword.length();
for (i = 0; i< len; i++)
{
// Did we already match this letter in a previous guess?
if (guess == guessword[i])
return 0;
// Is the guess in the secret word?
if (guess == secretword[i])
{
guessword[i] = guess;
matches++;
}
}
return matches;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.