Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. /*****************************************************************
  2. * AUTHOR : Summer Huang
  3. * Assignment #6 : Hangman
  4. * CLASS : CS 002
  5. * SECTION : MTWR: 7:30a – 12:30p
  6. * Due Date : 1/22/2018
  7. *****************************************************************/
  8. #include <iostream>
  9. #include <string>
  10. #include <cctype>
  11. #include <cstdlib>
  12.  
  13. using namespace std;
  14.  
  15. // function declarations
  16. string setupNotSolved(string);
  17. char getGuess(string);
  18. string updateNotSolved(string phrase,string unsolved,char guess);
  19. string prevGuesses;
  20. string phrase;
  21. void clearscreen();
  22.  
  23. // global variables to maintain the game status
  24. int incorrectGuesses=0;
  25. bool isFinished = false;
  26. bool validGuess = false;
  27.  
  28. int main()
  29. {
  30. string unsolved;
  31. int a;
  32. char ch;
  33.  
  34. // prompt the first player to enter a phrase
  35. cout << "Enter phrase: ";
  36. getline(cin, phrase);
  37.  
  38. unsolved = setupNotSolved(phrase);
  39. system("CLS");
  40.  
  41. // start the game
  42. while (!isFinished && incorrectGuesses < 7)
  43. {
  44. // display the unsolved phrase
  45. cout << endl << "Phrase: ";
  46. cout << unsolved << endl;
  47.  
  48. // display correctly guessed characters up until now
  49. cout << "Guessed so far: " << prevGuesses << endl;
  50.  
  51. // prompt the user to enter a guess
  52. ch = getGuess(prevGuesses);
  53. unsolved = updateNotSolved(phrase, unsolved, ch);
  54. cout << "Wrong guesses left: " << 7 - incorrectGuesses << endl;
  55. }
  56. system("CLS");
  57. // display game result
  58. if (isFinished)
  59. {
  60. cout << "Congratulations!! You won!" << endl;
  61. }
  62. if (incorrectGuesses >= 7)
  63. {
  64. cout << "You lost!" << endl;
  65. }
  66. return 0;
  67. }
  68.  
  69. ///@brief Puts dashes in place of alphabetic characters in the phrase.
  70. ///@param phrase the phrase to be solved
  71. ///@return the phrase with all alphabetic characters converted to dashes
  72.  
  73. string setupNotSolved(string phrase)
  74. {
  75. // creating unsolved phrase
  76. for(int k = 0;k < phrase.length(); k++)
  77. {
  78. if (isalpha(phrase.at(k)))
  79. {
  80. phrase.at(k) = '-';
  81. }
  82. }
  83. return phrase;
  84. }
  85.  
  86. ///@brief Replaces the dashes with the guessed character.
  87. ///@param phrase the phrase to be solved
  88. ///@param unsolved the phrase with dashes for all unsolved characters
  89. ///@param guess the char containing the current guessed character
  90. ///@return the new un solved string with dashes replaced by new guess
  91.  
  92. string updateNotSolved(string phrase,string unsolved,char guess)
  93.  
  94. {
  95. int i;
  96. int j;
  97. int len;
  98.  
  99. len = 0;
  100.  
  101. validGuess=false;
  102. // pre processing the phrase
  103. for(i = 0; i < unsolved.length(); i++)
  104. {
  105. if(unsolved.at(i)!='-')
  106. {
  107. phrase.at(i)='-';
  108. }
  109. }
  110. for(j = 0;j < phrase.length(); j++)
  111. {
  112. //if valid guess
  113. if(guess == phrase.at(j))
  114. {
  115. validGuess = true;
  116. unsolved.at(j) = guess;
  117. phrase.at(j) = '-';
  118. prevGuesses.append(string(1,guess));
  119. break;
  120. }
  121. }
  122. // update incorrect guesses count, if the guess is not a valid one
  123. if(!validGuess)
  124. {
  125. incorrectGuesses++;
  126. }
  127. // update the status of the game as finished,
  128. // if the phrase contains only '_'s
  129. for(i = 0;i < phrase.length(); i++)
  130. {
  131. if(phrase.at(i) == '-')
  132. {
  133. len++;
  134. }
  135. }
  136.  
  137. if(len == phrase.length())
  138. {
  139. isFinished=true;
  140. }
  141.  
  142. return unsolved;
  143. }
  144.  
  145. ///@brief Gets validguess as input.
  146. ///
  147. /// A guess is taken as input as a character. It is valid if
  148. /// 1)it is an alphabetic character; and
  149. /// 2)the character has not already been guessed
  150. ///
  151. ///@param prevGuesses the string containing all characters guessed so far
  152. ///@return a valid guess and only a valid guess as a character
  153.  
  154. char getGuess(string prevGuesses)
  155. {
  156. char ch;
  157. prevGuesses = "";
  158.  
  159. // prompt user to enter a valid guess
  160. cout << "Enter a guess: ";
  161. cin >> ch;
  162. cout << endl;
  163. if(isalpha(ch))
  164. {
  165. return ch;
  166. }
  167. else
  168. {
  169. while(!isalpha(ch))
  170. {
  171. cout << "Invalid guess! Please re-enter a guess: ";
  172. cin >> ch;
  173. cout << endl;
  174. }
  175. }
  176.  
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement