Guest User

Untitled

a guest
Nov 17th, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7.  
  8. // `getch` is not available on linux. look for an alternative
  9. #ifndef getch
  10. #define getch _getch
  11. #endif
  12.  
  13. // Maximum guesses a player can take
  14. #define MAXTRY 10
  15. // Maximum letters a word may have
  16. #define MAXLTR 128
  17.  
  18. const char * g_Words[] = {
  19. "house",
  20. "car",
  21. "phone",
  22. "brick",
  23. "moon",
  24. "elephant"
  25. };
  26.  
  27. void ClearScreen()
  28. {
  29. #if defined(WIN32) || defined(_WIN32)
  30. system("cls");
  31. #else
  32. system("clear");
  33. #endif
  34. }
  35.  
  36. const char * PickWord()
  37. {
  38. // Generate a random index within the range of the words array
  39. const int i = rand() % (sizeof(g_Words) / sizeof(g_Words[0]));
  40. // Return the word located at the resulted index
  41. return g_Words[i];
  42. }
  43.  
  44. void Render(const char * w, const char * l, int tries)
  45. {
  46. printf("%3i tries left to guess the word: ", (MAXTRY - tries));
  47. // Keep an unaltered letter pointer
  48. const char * lb = l;
  49. // Output the guessed and hidden word letters
  50. while (*w != '\0' && (l - lb) < MAXLTR)
  51. {
  52. // Print the letter with a space to make it more distinguishable
  53. putchar(*l);
  54. putchar(' ');
  55. // Advance to the next character
  56. ++l;
  57. ++w;
  58. }
  59. // End the line
  60. puts("");
  61. }
  62.  
  63. int main()
  64. {
  65. // Feed the random generator with a seed
  66. // Use the generic current time-stamp
  67. srand(time(NULL));
  68. // Pick a random word to start with
  69. const char * word = PickWord();
  70. // Memory for the guessed words
  71. // Assuming they're bellow 128 characters
  72. char letters[MAXLTR];
  73. // Initialize all guessed characters to _
  74. memset(letters, '_', sizeof(letters));
  75. // Try counter and whether to pick a new word
  76. int tries = 0, renew = 0;
  77. // Perform the initial render
  78. Render(word, letters, tries);
  79. // Enter the application loop
  80. for(;;)
  81. {
  82. const char ch = tolower(getch());
  83. // Are we trying the game again?
  84. if (ch == '\r' /* <- ASCII for ENTER */ && renew > 0)
  85. {
  86. // Pick a new random word to start with
  87. // Note: doesn't check if the same word was picked!
  88. word = PickWord();
  89. // Initialize all guessed characters to underscore
  90. memset(letters, '_', sizeof(letters));
  91. // Reset the `try` counter
  92. tries = 0;
  93. // Game was renewed
  94. renew = 0;
  95. // Clear the screen
  96. ClearScreen();
  97. // Perform the initial render
  98. Render(word, letters, tries);
  99. // And start the game
  100. continue;
  101. }
  102. // Are we leaving the game?
  103. if (ch == 27 /* <- ASCII for ESC */)
  104. {
  105. // Clear the screen
  106. ClearScreen();
  107. // Show the answer only if the word wasn't guessed
  108. if (strncmp(word, letters, strlen(word)) == 0)
  109. {
  110. puts("Sad to see you go. And FYI, I let you win!");
  111. }
  112. else
  113. {
  114. printf("Sad to see you go. The word was: %s\n", word);
  115. }
  116. // Stop the game
  117. break;
  118. }
  119. // Is this an accepted letter? (isalnum(ch) alpha-numeric?)
  120. else if (isalpha(ch) == 0)
  121. {
  122. continue; // Invalid input
  123. }
  124. // Get pointers to the word and list of letters to compare
  125. char * l = letters;
  126. const char * w = word;
  127. // See if the given character is correct
  128. while (*w != '\0' && (l - letters) < MAXLTR)
  129. {
  130. // Was this letter already guessed?
  131. // And is this letter a match?
  132. if (*l == '_' && *w == ch)
  133. {
  134. // Unlock this letter
  135. *l = ch;
  136. // We're done here
  137. break;
  138. }
  139. // Advance to the next character
  140. ++l;
  141. ++w;
  142. }
  143. // Was there a match?
  144. if (*w == '\0' || (l - letters) >= MAXLTR)
  145. {
  146. ++tries; // Increment the number of tries
  147. }
  148. else
  149. {
  150. // tries = 0; // Reset the counter whenever the player guesses a letter?
  151. }
  152. // Did we exhaust the number of tries?
  153. if (tries >= MAXTRY)
  154. {
  155. // Clear the screen
  156. ClearScreen();
  157. // Ask the player if he wants to try again
  158. printf("GAME OVER! The word you were looking for was actually '%s'\n", word);
  159. puts("Press ENTER to try again or ESC to leave.");
  160. // Renew the game on next loop
  161. renew = 1;
  162. // Wait for input
  163. continue;
  164. }
  165. // Did the user completed guessing the word?
  166. else if (strncmp(word, letters, strlen(word)) == 0)
  167. {
  168. // Clear the screen
  169. ClearScreen();
  170. // Ask the player if he wants to try again
  171. printf("YOU WON! The word you were looking for was indeed '%s'\n", word);
  172. puts("Press ENTER to try again or ESC to leave.");
  173. // Renew the game on next loop
  174. renew = 1;
  175. // Wait for next input
  176. continue;
  177. }
  178. // Render everything to the screen again
  179. Render(word, letters, tries);
  180. };
  181. }
Add Comment
Please, Sign In to add comment