Advertisement
Koalaazz

hangman final

Dec 16th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. //Hangman
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <ctime>
  8. #include <cctype>
  9. #include <time.h>
  10. using namespace std;
  11.  
  12. void drawing(int position); //declare conjuction, conjunction....what's your function
  13.  
  14.  
  15. int main()
  16. {
  17. const int MAX_WRONG = 6; //how many attempts user has
  18. vector<string> WordList = { "abruptly","absurd","abyss","affix","askew","avenue","awkward","axiom","azure","bagpipes","bandwagon","banjo","bayou","blitz","blizzard","boggle","bookworm" };
  19.  
  20. srand(static_cast<unsigned int>(time(0)));
  21. random_shuffle(WordList.begin(), WordList.end());
  22. const string TheWord = WordList[0]; //picks word to guess
  23. int wrong = 0; //how many incorrect guesses there have been
  24. string soFar(TheWord.size(), '-'); //shows whats in the word thats been guesses
  25. string used = " ";
  26. char guess;
  27.  
  28. cout << "Welcome To Hangman\n";
  29.  
  30. while ((wrong < MAX_WRONG) && (soFar != TheWord)) //the main loop
  31. {
  32. cout << "\n\nYou Have " << (MAX_WRONG - wrong);
  33. cout << " incorrect guesses left.\n";
  34. cout << "\nYou've used the following letters: \n" << used << endl;
  35.  
  36.  
  37. cout << "\nEnter your guess: ";
  38. cin >> guess;
  39. guess = tolower(guess);
  40. while (used.find(guess) != string::npos) //checks if users input is in TheWord
  41. {
  42. cout << "You have already guessed \n" << guess << endl;
  43. cout << "Enter your guess: ";
  44. cin >> guess;
  45. guess = tolower(guess);
  46. }
  47.  
  48. used += guess;
  49.  
  50. if (TheWord.find(guess) != string::npos) //user is correct
  51. {
  52. cout << "Correct, " << guess << " is in the word\n";
  53.  
  54. for (int i = 0; i < TheWord.length(); ++i)
  55. {
  56. if (TheWord[i] == guess)
  57. {
  58. soFar[i] = guess;
  59. }
  60. }
  61. }
  62. else //user is incorrect
  63. {
  64. cout << "Sorry, " << guess << " isn't in the word.\n";
  65. wrong++; //counts up to 6
  66. }
  67.  
  68. drawing(wrong); //draws board after every guess
  69. cout << "\n\n" << soFar;
  70. }
  71. if (wrong == MAX_WRONG)
  72. {
  73. cout << "\nYou've been hanged\n";
  74. cout << "The word was " << TheWord;
  75. }
  76. else
  77. {
  78. cout << "\nCongrats!!! You guessed the word" << endl;
  79. }
  80. return 0;
  81. }
  82.  
  83. void drawing(int position)
  84. {
  85. switch (position)
  86. {
  87. case 1:
  88. cout << " ___________" << endl;
  89. cout << " | }" << endl;
  90. cout << " | \\ " << endl;
  91. cout << " | " << endl;
  92. cout << " | " << endl;
  93. cout << "_|______________" << endl;
  94. break;
  95. case 2: cout << " ___________" << endl;
  96. cout << " | }" << endl;
  97. cout << " | \\ 0 " << endl;
  98. cout << " | " << endl;
  99. cout << " | " << endl;
  100. cout << "_|______________" << endl;
  101. break;
  102. case 3:
  103. cout << " ___________" << endl;
  104. cout << " | }" << endl;
  105. cout << " | \\ 0 /" << endl;
  106. cout << " | " << endl;
  107. cout << " | " << endl;
  108. cout << "_|______________" << endl;
  109. break;
  110. case 4:
  111. cout << " ___________" << endl;
  112. cout << " | }" << endl;
  113. cout << " | \\ 0 /" << endl;
  114. cout << " | |" << endl;
  115. cout << " | " << endl;
  116. cout << "_|______________" << endl;
  117. break;
  118. case 5:
  119. cout << " ___________" << endl;
  120. cout << " | }" << endl;
  121. cout << " | \\ 0 /" << endl;
  122. cout << " | |" << endl;
  123. cout << " | / " << endl;
  124. cout << "_|______________" << endl;
  125. break;
  126. case 6:
  127. cout << " ___________" << endl;
  128. cout << " | }" << endl;
  129. cout << " | \\ 0 /" << endl;
  130. cout << " | |" << endl;
  131. cout << " | / \\ " << endl;
  132. cout << "_|______________" << endl;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement