Advertisement
Guest User

Untitled

a guest
Apr 27th, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. #include <string>
  2. #include <fstream>
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <ctime>
  7. #include <random>
  8. #include <set>
  9.  
  10. using namespace std;
  11.  
  12. // Global variable.
  13. string rand_word;
  14.  
  15. // This function picks a random word from a text file, then returns it.
  16. string pick_word(const string picked_word)
  17. {
  18. vector<string> words;
  19. ifstream in("dictionary.txt");
  20.  
  21. // Checks to see if the file was successfully read.
  22. if (!in){
  23. cout << "file not found." << endl;
  24. return 0;
  25. }
  26. while (in) {
  27. string word;
  28. in >> word;
  29. words.push_back(word);
  30. }
  31.  
  32. //srand(time(0));
  33. string r = words[rand() % words.size()];
  34.  
  35. return r;
  36. }
  37.  
  38. // This function calls pick_word then starts
  39. // a hangman game with it.
  40. int hang_man(string word)
  41. {
  42. int misses = 0;
  43. int max_misses = 10;
  44. int exposed = 0;
  45. string display = word;
  46. int word_size = word.size();
  47.  
  48. cout << "There are " << word_size << " letters." << endl;
  49.  
  50. for (int i = 0; i < word_size; ++i)
  51. display[i] = '-';
  52.  
  53.  
  54. set<char> guessed;
  55. char guess;
  56.  
  57. while (display != word)
  58. {
  59.  
  60. // Determines wether or not the user has used all his/her guesses.
  61. if (misses >= max_misses)
  62. {
  63. cout << endl;
  64. cout << " Uh oh! You're out of guesses!n";
  65. cout << " The word was " << word << "!" << endl;
  66. cout << endl;
  67.  
  68. return 0;
  69. }
  70.  
  71. // Gives the user details on their current game.
  72. cout << "Already guessed: ";
  73. copy(guessed.begin(), guessed.end(), ostream_iterator<char>(cout, " "));
  74. cout << "nMisses: " << misses << endl;
  75. cout << "Remaining guesses " << max_misses - misses << endl;
  76. cout << "Guess a letter.nn ";
  77. cout << display << "nn";
  78. cin >> guess;
  79. cout << endl;
  80.  
  81. // Checks input for errors such as capitalization
  82. // of if it's even a number.
  83. guess = tolower(guess);
  84.  
  85. if (!isalpha(guess))
  86. {
  87. cout << "That is not a letter!n";
  88. continue;
  89. }
  90.  
  91. bool right = false;
  92.  
  93. //Checks if input has already been entered.
  94. if (guessed.find(guess) != guessed.end())
  95. {
  96. cout << guess << " has already been guessed.n";
  97. right = true;
  98. }
  99.  
  100. guessed.insert(guess);
  101.  
  102. // Compares guess with each char of word and
  103. // updates display accordingly.
  104. for (int i = 0; i < word_size; ++i)
  105. {
  106. if (guess == word[i])
  107. {
  108. display[i] = word[i];
  109. exposed++;
  110. right = true;
  111. }
  112. }
  113.  
  114. // Checks the boolian value to determine
  115. // whether or not to subtract a guess.
  116. if (!right)
  117. {
  118. misses++;
  119. cout << guess << " is not in the word." << endl;
  120. }
  121. }
  122.  
  123. // Displays final stat of game
  124. // and prompts the user for another try.
  125. cout << "That's right! The word was " << word << endl;
  126. cout << "You missed " << misses << " times!n";
  127.  
  128.  
  129. return 0;
  130. }
  131.  
  132.  
  133. int main()
  134. {
  135. // Calls the hangman function which functions as the hangman game itself
  136. cout << "---------- Welcome to Hangman! ----------" << endl;
  137. hang_man(pick_word(rand_word));
  138.  
  139. char answer;
  140. char yes = 'y';
  141.  
  142. // Prompts the user for another game.
  143. cout << " Would you like to play again?" << endl;
  144. cout << " Press y to play again or anything else to exit. " << endl;
  145. cin >> answer;
  146. if (answer != yes)
  147. {
  148. cout << "Okay! See you next time!" << endl;
  149.  
  150. return 0;
  151. }
  152. // Calls the hangman function for another game.
  153. else
  154. while (answer = yes)
  155. {
  156. hang_man(pick_word(rand_word));
  157.  
  158. cout << " Would you like to play again?" << endl;
  159. cout << " Press y to play again or anything else to exit. " << endl;
  160. cin >> answer;
  161. if (answer != yes)
  162. {
  163. cout << "Okay! See you next time!" << endl;
  164. return 0;
  165. }
  166.  
  167. }
  168. return 0;
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement