fork download
  1. //http://stackoverflow.com/questions/26875913/evil-hangman-implementation-in-python
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <fstream>
  6. #include <ctime>
  7. #include <algorithm>
  8.  
  9. const int max_guesses = 8;
  10.  
  11. std::string rawfile;
  12. std::vector<const char*> wordlist;
  13. std::string targetword;
  14. std::string guessedletters;
  15.  
  16. void loadwordlist(int length);
  17. bool filter(int length, char newguess);
  18. void finalize();
  19.  
  20. int main() {
  21. srand((unsigned)time(NULL));
  22. int length=rand()%5+rand()%5+rand()%5+3; //random between 3 and 15, normal-ish distribution
  23. loadwordlist(length);
  24. targetword.assign(length,'?');
  25. int guessno = 1;
  26. int wrongguesses = 0;
  27.  
  28. std::cout << "Welcome to Hangman!\n"
  29. "I have selected a word from an english dictionary.\n"
  30. "I will first show you the length of the secret word\n"
  31. "as a series of dashes.\n"
  32. "Your task is to guess the secret word one letter at a time.\n"
  33. "If you guess a correct letter I will show you the guessed\n"
  34. "letter(s) in the correct position.\n"
  35. "You can only make "<<max_guesses<<" wrong guesses before you are hanged\n"
  36. "\t\tGood luck\n";
  37.  
  38. while(wrongguesses < max_guesses) {
  39. char newguess = 0;
  40. std::cout << "Word: " << targetword;
  41. std::cout << "\nGuessed Letters: " << guessedletters;
  42. std::cout << "\nWrong Guesses: " << wrongguesses;
  43. std::cout << "\nGuess #" << guessno << ":";
  44. std::cin >> newguess;
  45. std::cout << '\n';
  46.  
  47. if (newguess >= 'a' && newguess <= 'z' && guessedletters.find(newguess)==std::string::npos) {
  48. ++guessno;
  49. guessedletters += newguess;
  50. std::sort(guessedletters.begin(), guessedletters.end());
  51.  
  52. if (filter(length, newguess))
  53. ++wrongguesses;
  54.  
  55. if (targetword.find('?') == std::string::npos)
  56. break;
  57. }
  58. }
  59.  
  60. const char* selection = wordlist[rand()%wordlist.size()];
  61. targetword.assign(selection, length);
  62.  
  63. if (wrongguesses == max_guesses)
  64. std::cout << "You ran out of guesses! The word was: " << targetword;
  65. else {
  66. std::cout << "You figured out the word " << targetword << " in " << guessno << " guesses! What a miracle!";
  67. }
  68.  
  69. return 0;
  70. }
  71.  
  72. void loadwordlist(int length) {
  73. std::ifstream file("hangman.txt", std::ios::binary);
  74. std::getline(file, rawfile, '\0'); //read the entire file
  75. wordlist.clear();
  76. wordlist.reserve(rawfile.size()/6);
  77. for(auto it = rawfile.begin(); it != rawfile.end();) {
  78. auto it2 = it+1;
  79. while(it2 != rawfile.end() && *it2 != '\n')
  80. ++it2;
  81. if (it2-it == length)
  82. wordlist.push_back(&*it);
  83. if (it2 == rawfile.end())
  84. return;
  85. it = it2+1;
  86. }
  87. }
  88.  
  89. bool filter(int length, char newguess) {
  90. if (wordlist.size() > 1) {
  91. static std::vector<int> letterpositions;
  92. letterpositions.assign(1<<length, 0);
  93. for(const char* s : wordlist) {
  94. unsigned flag = 0;
  95. for(int i=0; i<length; ++i) {
  96. if (s[i]==newguess)
  97. flag |= (1<<i);
  98. }
  99. letterpositions[flag]++;
  100. }
  101. auto maxit = std::max_element(letterpositions.begin(), letterpositions.end());
  102. unsigned targetflag = maxit-letterpositions.begin();
  103.  
  104. auto lambda = [=](const char* s)->bool {
  105. unsigned flag = 0;
  106. for(int i=0; i<length; ++i) {
  107. if (s[i]==newguess)
  108. flag |= (1<<i);
  109. }
  110. return flag != targetflag;
  111. };
  112. auto it = std::remove_if(wordlist.begin(), wordlist.end(), lambda);
  113. wordlist.erase(it, wordlist.end());
  114.  
  115. if (targetflag) {
  116. for(int i=0; i<length; ++i) {
  117. if ((1<<i) & targetflag)
  118. targetword[i] = newguess;
  119. }
  120. return false;
  121. }
  122. return true;
  123. }
  124. bool retval = true;
  125. for(int i=0; i<length; ++i) {
  126. if (wordlist[0][i] == newguess) {
  127. targetword[i] = newguess;
  128. retval = false;
  129. }
  130. }
  131. return retval;
  132. }
  133.  
Runtime error #stdin #stdout 0s 3440KB
stdin
etaoinshrdlucmfwypvbgkjqxz
stdout
Welcome to Hangman!
I have selected a word from an english dictionary.
I will first show you the length of the secret word
as a series of dashes.
Your task is to guess the secret word one letter at a time.
If you guess a correct letter I will show you the guessed
letter(s) in the correct position.
You can only make 8 wrong guesses before you are hanged
		Good luck
Word: ????????
Guessed Letters: 
Wrong Guesses: 0
Guess #1: