Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <fstream>
  4. #include <ctime>
  5. #include <string>
  6. #include <vector>
  7.  
  8. using namespace std;
  9. const int TRIES = 5;
  10.  
  11. int letterFill(char, string, string&);
  12.  
  13. int main() {
  14.     string name;
  15.     char letter;
  16.     int num_of_wrong_guesses = 0;
  17.     string line;
  18.  
  19.     ifstream myfile("words.txt");
  20.     vector<string> words;
  21.  
  22.     if (myfile.is_open()) {
  23.         while (getline(myfile, line)) {
  24.             words.push_back(line);
  25.         }
  26.  
  27.         myfile.close();
  28.     } else cout << "Unable to open file";
  29.  
  30.     size_t n = rand() % words.size();
  31.     string word = words[n];
  32.     //choose and copy a word from array of words randomly
  33.     // Initialize the secret word with the * character.
  34.     string unknown(word.length(), '*');
  35.     // welcome the user
  36.     cout << "\n\nWelcome to hangman!! Guess the word";
  37.     cout << "\n\nYou have to type only one letter in one try.";
  38.     cout << "\n\nYou have " << TRIES << " tries to try and guess the word.";
  39.     cout << "\n*****************************************";
  40.     // Loop until the guesses are used up
  41.     while (num_of_wrong_guesses < TRIES) {
  42.         cout << "\n\n" << unknown;
  43.         cout << "\n\nGuess a letter: ";
  44.         cin >> letter;
  45.         if (letterFill(letter, word, unknown) == 0) {
  46.             cout << endl << "Whoops! That letter isn't in there!" << endl;
  47.             num_of_wrong_guesses++;
  48.         } else {
  49.             cout << endl << "You found a letter!" << endl;
  50.         }
  51.         // Tell user how many guesses has left.
  52.         cout << "You have " << TRIES - num_of_wrong_guesses;
  53.         cout << " guesses left." << endl;
  54.         // Check if user guessed the word.
  55.         if (word == unknown) {
  56.             cout << word << endl;
  57.             cout << "Yeah! You got it!";
  58.             break;
  59.         }
  60.     }
  61.  
  62.     if (num_of_wrong_guesses == TRIES) {
  63.         cout << "\nSorry, you lose." << endl;
  64.         cout << "The word was : " << word << endl;
  65.     }
  66.  
  67.     cin.ignore();
  68.     cin.get();
  69.  
  70.     return 0;
  71. }
  72.  
  73. int letterFill(char guess, string secretword, string &guessword) {
  74.     int i;
  75.     int matches = 0;
  76.     int len = secretword.length();
  77.     for (i = 0; i < len; i++) {
  78.         if (guess == guessword[i])
  79.             return 0;
  80.         if (guess == secretword[i]) {
  81.             guessword[i] = guess;
  82.             matches++;
  83.         }
  84.     }
  85.     return matches;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement