Advertisement
Koalaazz

hangman 0.1

Dec 16th, 2020
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <ctime>
  6. #include <cctype>
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. const int MAX_WRONG = 7;
  12. vector<string> WordList = { "large", "booger" };
  13.  
  14. srand(static_cast<unsigned int>(time(0)));
  15. random_shuffle(WordList.begin(), WordList.end());
  16. const string TheWord = WordList[0];
  17. int wrong = 0;
  18. string soFar(TheWord.size(), '-');
  19. string used = " ";
  20.  
  21.  
  22. cout << "Welcome To Hangman\n";
  23.  
  24. while ((wrong < MAX_WRONG) && (soFar != TheWord))
  25. {
  26. cout << "\n\nYou Have " << (MAX_WRONG - wrong);
  27. cout << " incorrect guesses left.\n";
  28. cout << "\nYou've used the following letters: \n" << used << endl;
  29.  
  30. char guess;
  31. cout << "\n\n Enter you guess: ";
  32. cin >> guess;
  33. guess = toupper(guess);
  34. while (used.find(guess) != string::npos)
  35. {
  36. cout << "You have already guessed " << guess << endl;
  37. cout << "Enter your guess: ";
  38. cin >> guess;
  39. cout << "\n";
  40. guess = toupper(guess);
  41. }
  42.  
  43. used += guess;
  44.  
  45. if (TheWord.find(guess) != string::npos)
  46. {
  47. cout << "Correct, " << guess << " is the word\n";
  48.  
  49. for (int i = 0; i < TheWord.length(); ++i)
  50. {
  51. if (TheWord[i] == guess)
  52. {
  53. soFar[i] = guess;
  54. }
  55. }
  56. }
  57. else
  58. {
  59. cout << "Sorry, " << guess << " isn't the word.\n";
  60. ++wrong;
  61. }
  62. }
  63. if (wrong == MAX_WRONG)
  64. {
  65. cout << "You've been hanged\n";
  66. }
  67. cout << "The word was " << TheWord << endl;
  68.  
  69. return 0;
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement