Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <vector>
  5.  
  6. const int dictionarySize = 5;
  7. const char* dictionary[dictionarySize] = {
  8. "programming",
  9. "encouragement",
  10. "possibility",
  11. "graphics",
  12. "activities"
  13. };
  14.  
  15. bool HasCharacter(const char newGuess, const std::vector<char>& guesses)
  16. {
  17. for (auto const& value : guesses)
  18. {
  19. if (value == newGuess) {
  20. return true;
  21. }
  22. }
  23.  
  24. return false;
  25. }
  26.  
  27. int GetRandomWord(const char* levels[], int start, int end)
  28. {
  29. srand((unsigned int)time(NULL));
  30. int randomPick = rand() % end + start;
  31.  
  32. std::cout << "[DEBUG]: Random pick is " << randomPick << std::endl;
  33.  
  34. return randomPick;
  35. }
  36.  
  37. int SizeOfWord(const char* word)
  38. {
  39. int size = 0;
  40.  
  41. while (*word != '\0')
  42. {
  43. ++size;
  44. ++word;
  45. }
  46.  
  47. return size;
  48. }
  49.  
  50. bool CheckIfGuessIsCorrect(const char needle, const char* haystack)
  51. {
  52. while (*haystack != '\0')
  53. {
  54. if (*haystack == needle)
  55. {
  56. return true;
  57. }
  58. ++haystack;
  59. }
  60.  
  61. return false;
  62. }
  63.  
  64. int DisplayHiddenLetters(const char* word, const std::vector<char>& guesses)
  65. {
  66. std::cout << std::endl;
  67. std::cout << "==============================================================" << std::endl;
  68.  
  69. int countHidden = 0;
  70. while (*word != '\0')
  71. {
  72. if (HasCharacter(*word, guesses))
  73. {
  74. std::cout << " " << *word << " ";
  75. }
  76. else
  77. {
  78. std::cout << " _ ";
  79. ++countHidden;
  80. }
  81. ++word;
  82. }
  83. std::cout << std::endl;
  84.  
  85. std::cout << "==============================================================" << std::endl;
  86. std::cout << std::endl;
  87.  
  88. return countHidden;
  89. }
  90.  
  91. int main()
  92. {
  93. const char* currentWord = dictionary[GetRandomWord(dictionary, 0, 5)];
  94. std::cout << "[DEBUG]: The word is " << currentWord << std::endl;
  95.  
  96. const int maxGuesses = SizeOfWord(currentWord) + 3;
  97.  
  98. bool isRunning = true;
  99. int countGuesses = 0;
  100. std::vector<char> guessedLetters;
  101.  
  102. while (isRunning)
  103. {
  104. char guess;
  105.  
  106. std::cout << "[YOU] Your guess is: ";
  107. std::cin >> guess;
  108. std::cin.get();
  109. std::cout << std::endl;
  110.  
  111. ++countGuesses;
  112. bool isGuessCorrect = CheckIfGuessIsCorrect(guess, currentWord);
  113.  
  114. if (isGuessCorrect)
  115. {
  116. guessedLetters.push_back(guess);
  117. std::cout << "CORRECT GUESS!" << std::endl;
  118. }
  119. else
  120. {
  121. std::cout << "NO LUCK! TRY AGAIN!" << std::endl;
  122. }
  123.  
  124. int hiddenLetters = DisplayHiddenLetters(currentWord, guessedLetters);
  125.  
  126. if (countGuesses <= maxGuesses && hiddenLetters == 0)
  127. {
  128. isRunning = false;
  129.  
  130. std::cout << std::endl;
  131. std::cout << "[WON] Congrats, you are a true word doctor!" << std::endl;
  132. std::cout << std::endl;
  133. }
  134.  
  135. else if (countGuesses == maxGuesses && hiddenLetters > 0) {
  136. isRunning = false;
  137.  
  138. std::cout << std::endl;
  139. std::cout << "[LOST] You ran out of guesses, better luck next time!" << std::endl;
  140. std::cout << std::endl;
  141. }
  142. }
  143.  
  144. std::cin.get();
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement