Guest User

Untitled

a guest
Jan 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. while
  2.  
  3. ((wrong < MAX_WRONG) && (soFar != THE_WORD))
  4. {
  5. cout << "nnYou have " << (MAX_WRONG - wrong);
  6. cout << " incorrect guesses left.n";
  7. cout << "nYou've used the following letters:n" << used << endl;
  8. cout << "nSo far, the word is:n" << soFar << endl;
  9.  
  10. char enter_guess(string);
  11. int check_guess(vector<string>words);
  12. }
  13.  
  14. #include "pch.h"
  15. #include <iostream>
  16. #include <string>
  17. #include <vector>
  18. #include <algorithm>
  19. #include <ctime>
  20. #include <cctype>
  21.  
  22. using namespace std;
  23. char guess;
  24. int wrong = 0;
  25. const string THE_WORD;
  26. string soFar;
  27. string used;
  28. char enter_guess();
  29. int check_guess(vector<string>words);
  30. int main()
  31. {
  32.  
  33. const int MAX_WRONG = 8;
  34.  
  35. vector<string> words;
  36. words.push_back("GUESS");
  37. words.push_back("HANGMAN");
  38. words.push_back("DIFFICULT");
  39. string answer;
  40. cout << "Welcome to Hangman. Good luck!n";
  41.  
  42. while ((answer != "No") || (answer != "no"))
  43. {
  44.  
  45.  
  46. while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
  47. {
  48. cout << "nnYou have " << (MAX_WRONG - wrong);
  49. cout << " incorrect guesses left.n";
  50. cout << "nYou've used the following letters:n" << used << endl;
  51. cout << "nSo far, the word is:n" << soFar << endl;
  52.  
  53. char enter_guess(string);
  54. int check_guess(vector<string>words);
  55. }
  56.  
  57.  
  58. if (wrong == MAX_WRONG)
  59. cout << "nYou've been hanged!";
  60. else
  61. cout << "nYou guessed it!";
  62.  
  63. cout << "nThe word was " << THE_WORD << endl;
  64.  
  65.  
  66. cout << "nDo you want to play again?(Enter: Yes or No) " << endl;
  67. cin >> answer;
  68. }
  69. return 0;
  70. }
  71. char enter_guess()
  72. {
  73. cout << "nnEnter your guess: ";
  74. cin >> guess;
  75. guess = toupper(guess);
  76. return guess;
  77. }
  78.  
  79. int check_guess(vector<string>words)
  80. {
  81. srand(static_cast<unsigned int>(time(0)));
  82. random_shuffle(words.begin(), words.end());
  83. const string THE_WORD = words[0];
  84.  
  85. string soFar(THE_WORD.size(), '-');
  86. string used = "";
  87. while (used.find(guess) != string::npos)
  88. {
  89. cout << "nYou've already guessed " << guess << endl;
  90. cout << "Enter your guess: ";
  91. cin >> guess;
  92. guess = toupper(guess);
  93. }
  94.  
  95. used += guess;
  96.  
  97. if (THE_WORD.find(guess) != string::npos)
  98. {
  99. cout << "That's right! " << guess << " is in the word.n";
  100.  
  101. // update soFar to include newly guessed letter
  102. for (unsigned int i = 0; i < THE_WORD.length(); ++i)
  103. {
  104. if (THE_WORD[i] == guess)
  105. {
  106. soFar[i] = guess;
  107. }
  108. }
  109. }
  110. else
  111. {
  112. cout << "Sorry, " << guess << " isn't in the word.n";
  113. ++wrong;
  114. }
  115. return wrong;
  116. }
  117.  
  118. string used = "";
  119. while (used.find(guess) != string::npos) {
  120. //...
  121.  
  122. #include "pch.h"
  123. #include <iostream>
  124. #include <string>
  125. #include <vector>
  126. #include <algorithm>
  127. #include <ctime>
  128. #include <cctype>
  129.  
  130. using namespace std;
  131.  
  132. int main()
  133. {
  134.  
  135. const int MAX_WRONG = 8;
  136.  
  137. vector<string> words;
  138. words.push_back("GUESS");
  139. words.push_back("HANGMAN");
  140. words.push_back("DIFFICULT");
  141. string answer;
  142.  
  143. while ((answer!="No")||(answer!="no"))
  144. {
  145. srand(static_cast<unsigned int>(time(0)));
  146. random_shuffle(words.begin(), words.end());
  147. const string THE_WORD = words[0];
  148. int wrong = 0;
  149. string soFar(THE_WORD.size(), '-');
  150. string used = "";
  151.  
  152. cout << "Welcome to Hangman. Good luck!n";
  153.  
  154.  
  155. while ((wrong < MAX_WRONG) && (soFar != THE_WORD))
  156. {
  157. cout << "nnYou have " << (MAX_WRONG - wrong);
  158. cout << " incorrect guesses left.n";
  159. cout << "nYou've used the following letters:n" << used << endl;
  160. cout << "nSo far, the word is:n" << soFar << endl;
  161.  
  162. char guess;
  163. cout << "nnEnter your guess: ";
  164. cin >> guess;
  165. guess = toupper(guess);
  166. while (used.find(guess) != string::npos)
  167. {
  168. cout << "nYou've already guessed " << guess << endl;
  169. cout << "Enter your guess: ";
  170. cin >> guess;
  171. guess = toupper(guess);
  172. }
  173.  
  174. used += guess;
  175.  
  176. if (THE_WORD.find(guess) != string::npos)
  177. {
  178. cout << "That's right! " << guess << " is in the word.n";
  179.  
  180. // update soFar to include newly guessed letter
  181. for (unsigned int i = 0; i < THE_WORD.length(); ++i)
  182. {
  183. if (THE_WORD[i] == guess)
  184. {
  185. soFar[i] = guess;
  186. }
  187. }
  188. }
  189. else
  190. {
  191. cout << "Sorry, " << guess << " isn't in the word.n";
  192. ++wrong;
  193. }
  194. }
  195.  
  196.  
  197. if (wrong == MAX_WRONG)
  198. cout << "nYou've been hanged!";
  199. else
  200. cout << "nYou guessed it!";
  201.  
  202. cout << "nThe word was " << THE_WORD << endl;
  203.  
  204.  
  205. cout << "nDo you want to play again?(Enter: Yes or No) " << endl;
  206. cin >> answer;
  207. }
  208. return 0;
  209. }
Add Comment
Please, Sign In to add comment