Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5.  
  6. typedef struct{
  7. char name[33];
  8. }Film;
  9.  
  10. FILE *titles;
  11. Film film;
  12.  
  13. char film_name[33];
  14. char *str1 = film_name;
  15.  
  16. int guess_count = 0;
  17.  
  18. void play_game();
  19. void random_film();
  20. void guess_character();
  21. void guess_film();
  22. char* lower_char(char*);
  23.  
  24. int main() {
  25. srand(time(NULL));
  26. play_game();
  27. return 0;
  28. }
  29.  
  30. void play_game(){
  31. char play_ans = 'y';
  32. char film_ans;
  33.  
  34. printf("\t*********Welcome to the Film Genie!*********\n");
  35. fflush(stdout);
  36.  
  37. do{
  38. random_film();
  39.  
  40. printf("\nYour Film Title to guess is:\n");
  41. fflush(stdout);
  42.  
  43. int length;
  44. length = strlen(film.name) - 1;
  45.  
  46. for(int i = 0; i < length; i++){
  47. if (film.name[i] != ' ' && film.name[i] != '\0'){
  48. printf("*");
  49. fflush(stdout);
  50. }
  51. else{
  52. printf(" ");
  53. fflush(stdout);
  54. }
  55. }
  56.  
  57. strcpy(film_name, film.name);
  58. str1 = lower_char(str1);
  59.  
  60. do{
  61. printf("\nWould you like to guess a character? (Enter c);\nOR guess the film? (Enter f); ");
  62. fflush(stdout);
  63.  
  64. scanf("%c", &film_ans);
  65. fflush(stdin);
  66.  
  67. if(film_ans == 'c'){
  68. guess_character();
  69. }
  70. else if(film_ans == 'f'){
  71. guess_film();
  72. }
  73. else{
  74. printf("Invalid answer,\nPlease try again: ");
  75. fflush(stdout);
  76.  
  77. scanf("%c", &film_ans);
  78. fflush(stdin);
  79. }
  80. } while(guess_count < 5);
  81.  
  82. printf("\nDo you want to play again? ");
  83. fflush(stdout);
  84.  
  85. scanf("%c", &play_ans);
  86. fflush(stdin);
  87.  
  88. } while (play_ans == 'y' || play_ans == 'Y');
  89.  
  90. printf("Thank you for playing!");
  91. fflush(stdout);
  92. }
  93. void random_film(){
  94.  
  95. titles = fopen("C:\\Documents and Settings\\Owner\\Desktop\\FilmGenie\\filmtitles.txt", "rt");
  96.  
  97. int count = 0;
  98. char line[33];
  99.  
  100. while (fgets(line, 33, titles) != NULL)
  101. {
  102. count++;
  103. if (((rand() % 45) * count) / 45 == 0)
  104. strcpy(film.name, line);
  105. }
  106.  
  107. fclose(titles);
  108. }
  109.  
  110. void guess_character(){
  111. char guess;
  112.  
  113. printf("Please Enter a Character: ");
  114. fflush(stdout);
  115.  
  116. scanf("%c", &guess);
  117. fflush(stdin);
  118.  
  119. if(strchr(film.name, guess) != NULL){
  120. printf("Your character exists! Well done. Please continue playing.");
  121. fflush(stdout);
  122. }
  123. else{
  124. printf("Your character does not exist! Please continue playing.");
  125. fflush(stdout);
  126. }
  127. }
  128.  
  129. void guess_film(){
  130. printf("\nPlease Enter your Guess: ");
  131. fflush(stdout);
  132.  
  133. char guess[33];
  134.  
  135. gets(guess);
  136.  
  137. char *str2 = guess;
  138.  
  139. guess_count++;
  140.  
  141. str2 = lower_char(str2);
  142.  
  143. if (strcmp(film_name, guess) == 1){
  144. switch(guess_count){
  145. case 1:
  146. printf("\n1st guess! You got it right!");
  147. fflush(stdout);
  148. guess_count = 5;
  149. break;
  150. case 2:
  151. printf("\n2nd guess! You got it right!");
  152. fflush(stdout);
  153. guess_count = 5;
  154. break;
  155. case 3:
  156. printf("\n3rd guess! You got it right!");
  157. fflush(stdout);
  158. guess_count = 5;
  159. break;
  160. case 4:
  161. printf("\n4th guess! You got it right!");
  162. fflush(stdout);
  163. guess_count = 5;
  164. break;
  165. case 5:
  166. printf("\n5th guess! You got it right!");
  167. fflush(stdout);
  168. break;
  169. }
  170. }
  171. else{
  172. switch(guess_count){
  173. case 1:
  174. printf("\n1st guess! You got it wrong!");
  175. fflush(stdout);
  176. break;
  177. case 2:
  178. printf("\n2nd guess! You got it wrong!");
  179. fflush(stdout);
  180. break;
  181. case 3:
  182. printf("\n3rd guess! You got it wrong!");
  183. fflush(stdout);
  184. break;
  185. case 4:
  186. printf("\n4th guess! You got it wrong!");
  187. fflush(stdout);
  188. break;
  189. case 5:
  190. printf("\n5th guess! You got it wrong!");
  191. fflush(stdout);
  192. break;
  193. }
  194. }
  195. }
  196. char* lower_char(char *str){
  197. for(int i = 0; i < 33; i++){
  198. if(*(str + i) >= 'A' && *(str + i) <= 'Z'){
  199. *(str + i) = *(str + i) -'A' + 'a';
  200. }
  201. }
  202. return str;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement