1. Engineering
  2. Computer Science
  3. i need to implement a class or classes in this...

Question: i need to implement a class or classes in this...

Question details

I need to implement a CLASS or CLASSES in this C++ code, or transform the whole code with the use of classes.

#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",
"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;
}

/*
OUTPUT:


Welcome to hangman!! Guess a country that comes into your mind.

Each letter is represented by an asterisk.

You have to type only one letter in one try.

You have 5 tries to try and guess the country.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*****

Guess a letter: q

Whoops! That letter isn't in there!
You have 4 guesses left.


*****

Guess a letter: w

Whoops! That letter isn't in there!
You have 3 guesses left.


*****

Guess a letter: e

Whoops! That letter isn't in there!
You have 2 guesses left.


*****

Guess a letter: r

Whoops! That letter isn't in there!
You have 1 guesses left.


*****

Guess a letter: t

Whoops! That letter isn't in there!
You have 0 guesses left.

Sorry, you lose...you've been hanged.
The word was : japan
*/

&#include #include #include #include <iostream» <CStdlib> くctime> <string> 2 4 using namespace std; 6 7 const int MAX_TRIES 5; 9int letterFill(char, string, string&) 10 11 Eint main) 12 13 14 15 16 17 18 19 20 string name; char letter int num of wrong guesses string word; india, nepal, string words[] japan china madagascar, azerbaijan, kyrgyzstan, turkmenistan, 23 french guiana, 25 26 27 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 << Innelcome to hangman!! Guess a country that comes into your mind.; cout << nnEach letter is represented by an asterisk. 29 30 35 37 coutnInYou have to type only one letter in one try.

37 38 39 40 41 42 43 cout <<InnYou have to type only one letter in one try. cout < nnYou have <MAX TRIES << tries to try and guess the country.; cout < // Loop until the guesses are used up while (num_of_ wrong_guessesMAX_TRIES) cout<nln < unknown; cout << nnGuess a letter: ; : cin > letter; : I Fill secret word with letter if the guess is correct, // otherwise increment the number of wrong guesses. if (letterFill(letter, word, unknown)) :cout << endl << Whoops! That letter isnt in there! << endl; 45 46 48 49 50 51 52 53 num_of_wrong_guesses++ else t cout << endl <<You found a letter! Isnt that exciting?<< endl; I/ Tell user how many guesses has left cout <<You have<< MAX TRIES -numof wrong guesses : cout<guesses left. << endl; : II Check if user guessed the word 56 57 58 59 60 61 62 63 64 65 if (word-unknown) { : cout << word << endl; : cout <<Yeah! You got it!; : break; if (num_of_wrong_guessesMAX_TRIES) cout <Insorry, you lose...you ve been hanged. << endl; cout << The word was word << endl; 67 68 69 70 71 72 73 74 cin.ignore(); cin.get(O; return e; /* Take a one character guess and the secret word, and fill in the unfinished guessword. Returns number of characters matched

73 74 Take a one character guess and the secret word, and fill in the unfinished guessword. Returns number of characters matched. 75 LAso, returns zero if the character is already guessed. 76 int letterFill(char guess, string secretword, string &guessword) int i; int matches - int len -secretword.length); for (i-o; i< len; i++) { 78 79 80 le: 81 82 83 84 85 : 86 87 // Did we already match this letter in a previous guess? :if (guess -guessword[i]) return e :// Is the guess in the secret word? if (guess-secretword[i]) guessword[i] - guess; :matches++; 89 90 91 92 93 /* 94 OUTPUT: 95 96 return matches; 97 Welcome to hangman!! Guess a country that comes into your mind. 98 99 Each letter is represented by an asterisk 108 101 You have to type only one letter in one try 102 103 You have 5 tries to try and guess the country 104 105 106 107 108 109 Guess a letter:q

109 110 Whoops! That letter isnt in there! 111 You have 4 guesses left. 112 113 114 115 116 Guess a letter: w 118 Whoops! That letter isnt in there! 119 You have 3 guesses left. 128 121 122 123 124 Guess a letter: e 125 126 whoops! That letter isnt in there! 127 You have 2 guesses left. 128 129 130 131 132 Guess a letter:r 133 134 Whoops! That letter isnt in there! 135 136 137 138 139 140 Guess a letter:t 141 142 Whoops! That letter isnt in there! 143 You have guesses left. You have 1 guesses left.

143 You have e guesses left. 144 145 Sorry, you lose...you ve been hanged. The word was japan s/ 147

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution