Advertisement
Guest User

Untitled

a guest
Feb 18th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.53 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <ctime>
  5. using namespace std;
  6.  
  7. /*int main() <--Old game code.
  8. {
  9. //declare variables
  10. const int NUMBER_OF_GUESSES = 5;
  11. char letter; //store the letter here
  12. int numGames; // store the number of games they want to play
  13. int gussesUsed;
  14. int ran;
  15. char letterGuess;
  16.  
  17. cout << "Are you ready to play a game? (of guessing letters?)" << endl;
  18. cout << "First, how many games do you want to play? (1-4)" << endl;
  19. cin >> numGames;
  20.  
  21. srand(time(NULL));
  22. gussesUsed = 1;
  23. ran = rand() % 26;
  24. letter = 'a' + ran;
  25. while (numGames >= 1)
  26. {
  27.  
  28. cout << "What is your random letter guess? (please use lower case)" << " CHEAT CODE '" << letter << "' " << endl;
  29. cin >> letterGuess;
  30. if (gussesUsed == NUMBER_OF_GUESSES)
  31. {
  32. cout << "You lost - the letter was " << letter << endl;
  33. numGames--;
  34. ran = rand() % 26;
  35. letter = 'a' + ran;
  36. gussesUsed = 1;
  37. }
  38. else if (letterGuess < letter)
  39. {
  40. cout << "The letter you guessed comes after " << letterGuess << endl;
  41. gussesUsed++;
  42. }
  43. else if (letterGuess > letter)
  44. {
  45. cout << "The letter you guess comes before " << letterGuess << endl;
  46. gussesUsed++;
  47. }
  48. else if (letterGuess == letter)
  49. {
  50. cout << "Good work! You guessed it!" << endl;
  51. numGames--;
  52. ran = rand() % 26;
  53. letter = 'a' + ran;
  54. gussesUsed = 1;
  55. }
  56.  
  57.  
  58. }
  59. cout << "This is the end of the games! Hope you had fun =) " << endl;
  60.  
  61. return 0;
  62. }*/
  63. void introduction();
  64. int getNumberOfGame();
  65. char compareTwoCharacters(char num, char letter);
  66. char playOneGame(char cha);
  67. int main()
  68. {
  69. int games;
  70. char guess;
  71. introduction();
  72. games = getNumberOfGame();
  73. games--;
  74. cout << "Let's play game" << endl;
  75. cout << "Input your guess." << endl;
  76. playOneGame(guess);
  77. while (games >= 0)
  78. {
  79. playOneGame(guess);
  80. if (playOneGame(guess) == 2)
  81. {
  82. games--;
  83. }
  84. else if (playOneGame(guess) == 0)
  85. {
  86. cout << "You lost that game!" << endl;
  87. games--;
  88.  
  89. }
  90. else if (games <= 0)
  91. {
  92. cout << "Insert more coins to play again!" << endl;
  93. return 0;
  94. }
  95. }
  96. }
  97. /*The first function’s name is introduction, which will print (show on the screen)
  98. Welcome to the Letter Guessing Game You will enter the number of games you want to play (1 - 4 games) You have 5 chances to guess each letter Let's begin:
  99. */
  100. void introduction()
  101. {
  102. cout << "Welcome to the Letter guessing game!" << endl;
  103. cout << "You will enter the number of games you want to play (1-4 games)" << endl;
  104. cout << "You have 5 chances to guess each letter." << endl;
  105. cout << "Let's begin:" << endl;
  106. }
  107. /*The second function’s name is getNumberOfGame, which asks the user about the number of games to be played and returns the value. This function will validate
  108. the user input to make sure it is in the desired range (1-4) inclusive, if the value is out of range the user is notified and asked again.
  109. How many games do you want to play (1-4) 3*/
  110. int getNumberOfGame()
  111. {
  112.  
  113. int gamesToPlay = 0;
  114.  
  115. cout << "How many games do you want to play? (1-4)" << endl;
  116. incorrectEntry:
  117. cin >> gamesToPlay;
  118.  
  119. if (gamesToPlay <= 0)
  120. {
  121. cout << "You can't do less then zero games!" << endl;
  122. cout << "How many games do you want to play? (1-4)" << endl;
  123. goto incorrectEntry;
  124.  
  125. }
  126. else if (gamesToPlay >= 5)
  127. {
  128. cout << "You can't do more then four games!" << endl;
  129. cout << "How many games do you want to play? (1-4)" << endl;
  130. goto incorrectEntry;
  131.  
  132. }
  133. return gamesToPlay;
  134. }
  135. /* The third function’s name is compareTwoCharacters, which receives two parameters of type character (e.g. the character to be guessed in that game and the character typed by the user).
  136. This functions return 0 if the two characters are the same, returns 2 if the first parameter comes after the second parameter, otherwise it returns -2.
  137. */
  138. char compareTwoCharacters(char random, char guess)
  139. {
  140. if (random == guess)
  141. {
  142. return 0;
  143. }
  144. else if (random < guess) // Random D, Guess C - Your guess is before the letter i've chosen (the game)
  145. {
  146. return 2;
  147. }
  148. else // Random C, Guess D - Your guess is after the letter i've chosen (the game)
  149. return -2;
  150. }
  151. /*The fourth function’s name is playOneGame, which receives one parameter of type character (the character to be guessed in that game).
  152. This function returns true if the user won; otherwise will return false. This function will handle all tasks to play one game (ask the user to enter their guess,
  153. provide up to 5 chances to guess, provide a hint to the user, etc.). This function is going to call the third function (i.e. compareTwoCharacters) to decide
  154. if the user has guessed the correct answer or to provide a hint to the user.
  155. */
  156. char playOneGame(char cha)
  157. {
  158. char letter;
  159. int ran;
  160. const int NUMBER_OF_GUESSES = 5;
  161. int numberOfWrong = 0;
  162. char guess;
  163. srand(time(NULL));
  164. ran = rand() % 26;
  165. letter = 'a' + ran;
  166. cout << "cheat code! " << letter << endl;
  167. while (numberOfWrong > 0)
  168. {
  169. if (compareTwoCharacters(letter, cha) == 0) // 0 = Guess Correct, 2 = Random is before the letter, -2 = random is after the letter
  170. {
  171. cout << "You Guessed it!!!" << endl;
  172. return 2;
  173. }
  174. else if (compareTwoCharacters(letter, cha) == 2)
  175. {
  176. cout << "Your guess is before the letter I've chosen." << endl;
  177. numberOfWrong++;
  178.  
  179. }
  180. else if (compareTwoCharacters(letter, cha) == -2)
  181. {
  182. cout << "Your guess is after the letter I've chosen." << endl;
  183. numberOfWrong++;
  184. }
  185. else if (numberOfWrong == 5)
  186. {
  187. return 0;
  188. }
  189. }
  190. return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement