fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4.  
  5.  
  6. int x, y, z, choice;
  7. clock_t start, end;
  8. FILE * fPointer;
  9. double time_taken;
  10. void easy(void); //NOT YET FINAL, ERASE PRTINF FOR GENERATION y CHANGE A BIT yung variables
  11. void hard(void);
  12. FILE *fptr;
  13.  
  14.  
  15.  
  16. int main()
  17.  
  18. {
  19. srand(time(NULL));
  20.  
  21. start:
  22. printf("\n\nPress 1 for Easy Mode \n Press 2 for Hard Mode \n Choice:");
  23. scanf("%i", &choice);
  24.  
  25. if(choice==1)
  26. {
  27. start=clock();
  28. easy();
  29.  
  30.  
  31. end=clock();
  32. time_taken = (end-start)/CLOCKS_PER_SEC;
  33. printf("\nTime taken: %f\n", time_taken);
  34. fPointer = fopen("save.txt", "w");
  35. fprintf(fPointer, "%f", time_taken);
  36. fclose(fPointer);
  37. }
  38.  
  39. if(choice==2)
  40. {
  41. start=clock();
  42. hard();
  43.  
  44. end=clock();
  45. time_taken = (end-start)/CLOCKS_PER_SEC;
  46. printf("\nTime taken: %f\n", time_taken);
  47. fPointer = fopen("save.txt", "w");
  48. fprintf(fPointer, "%f", time_taken);
  49. fclose(fPointer);
  50.  
  51. }
  52.  
  53.  
  54.  
  55.  
  56. printf("Fastest Time: \n");
  57.  
  58. char filename[100], c;
  59.  
  60.  
  61. // Open file
  62. fptr = fopen("save.txt", "r");
  63. if (fptr == NULL)
  64. {
  65. printf("Cannot open file \n");
  66. exit(0);
  67. }
  68.  
  69. // Read contents from file
  70. c = fgetc(fptr);
  71. while (c != EOF)
  72. {
  73. printf ("%c", c);
  74. c = fgetc(fptr);
  75. }
  76.  
  77. fclose(fptr);
  78.  
  79. return 0;
  80. }
  81.  
  82.  
  83. void easy()
  84. {
  85. int array[20], i;
  86. for ( i = 0; i < 10; i++)
  87. {
  88. array[i] = i;
  89. }
  90.  
  91. for (i = 0; i < 4; i++)
  92. {
  93. int temp = array[i];
  94. int randomIndex = rand() % 10;
  95.  
  96. array[i] = array[randomIndex];
  97. array[randomIndex] = temp;
  98.  
  99. }
  100. for (i = 0; i < 4; i++)
  101. {
  102. printf("%i", array[i]);
  103. }
  104. printf("\n");
  105.  
  106. int almost, right, tries, y, real[100] ;
  107. char input[20];
  108. for(tries=1; tries<=10; tries++)
  109. {
  110. printf("\nTurn %d\n", tries);
  111. printf("Your guess:\n");
  112. scanf("%s", input);
  113. for(y=0; y<=4; y++)
  114. {
  115. real[y]=input[y] -'0';
  116. }
  117.  
  118. printf("Key Sequence:");
  119. if (real[0]==array[0])
  120. {
  121. printf("2");
  122. right++;
  123. }
  124. else if (real[0]==array[1]||real[0]==array[2]||real[0]==array[3])
  125. {
  126. printf("1");
  127. almost++;
  128. }
  129. else
  130. {
  131. printf("0");
  132. }
  133.  
  134. if (real[1]==array[1])
  135. {
  136. printf("2");
  137. right++;
  138. }
  139. else if (real[1]==array[0]||real[1]==array[2]||real[1]==array[3])
  140. {
  141. printf("1");
  142. almost++;
  143. }
  144. else
  145. {
  146. printf("0");
  147. }
  148.  
  149. if (real[2]==array[2])
  150. {
  151. printf("2");
  152. right++;
  153. }
  154. else if (real[2]==array[0]||real[2]==array[1]||real[2]==array[3])
  155. {
  156. printf("1");
  157. almost++;
  158. }
  159. else
  160. {
  161. printf("0");
  162. }
  163.  
  164. if (real[3]==array[3])
  165. {
  166. printf("2");
  167. right++;
  168. }
  169. else if (real[3]==array[0]||real[3]==array[1]||real[3]==array[2])
  170. {
  171. printf("1");
  172. almost++;
  173. }
  174. else
  175. {
  176. printf("0");
  177. }
  178.  
  179. if (right==4)
  180. {
  181. printf("\nYou got it right in %d try/tries\n", tries);
  182. tries = 10;
  183. }
  184. else
  185. {
  186. right = 0;
  187. almost = 0;
  188. }
  189.  
  190.  
  191. if (tries==10 && right!=4)
  192. {
  193. printf("\n\nYou failed!\n");
  194. printf("The Random Number Generated is: %i%i%i%i", array[0], array[1], array[2], array[3]);
  195. }
  196. }
  197. }
  198.  
  199.  
  200. void hard()
  201. {
  202. int x, ran[4];
  203. int almost, right, tries, y, real[100] ;
  204. char input[20];
  205.  
  206. for(x=0;x<4;x++)
  207. {
  208. ran[x]=rand() %10;
  209. }
  210. for(x=0;x<4;x++)
  211. {
  212. printf("%i", ran[x]);
  213. }
  214.  
  215.  
  216. for(tries=1; tries<=10; tries++)
  217. {
  218. printf("\nTurn %d\n", tries);
  219. printf("Your guess:\n");
  220. scanf("%s", input);
  221. for(y=0; y<=4; y++)
  222. {
  223. real[y]=input[y] -'0';
  224. }
  225.  
  226. printf("Key Sequence:");
  227. if (real[0]==ran[0])
  228. {
  229. printf("2");
  230. right++;
  231. }
  232. else if (real[0]==ran[1]||real[0]==ran[2]||real[0]==ran[3])
  233. {
  234. printf("1");
  235. almost++;
  236. }
  237. else
  238. {
  239. printf("0");
  240. }
  241.  
  242. if (real[1]==ran[1])
  243. {
  244. printf("2");
  245. right++;
  246. }
  247. else if (real[1]==ran[0]||real[1]==ran[2]||real[1]==ran[3])
  248. {
  249. printf("1");
  250. almost++;
  251. }
  252. else
  253. {
  254. printf("0");
  255. }
  256.  
  257. if (real[2]==ran[2])
  258. {
  259. printf("2");
  260. right++;
  261. }
  262. else if (real[2]==ran[0]||real[2]==ran[1]||real[2]==ran[3])
  263. {
  264. printf("1");
  265. almost++;
  266. }
  267. else
  268. {
  269. printf("0");
  270. }
  271.  
  272. if (real[3]==ran[3])
  273. {
  274. printf("2");
  275. right++;
  276. }
  277. else if (real[3]==ran[0]||real[3]==ran[1]||real[3]==ran[2])
  278. {
  279. printf("1");
  280. almost++;
  281. }
  282. else
  283. {
  284. printf("0");
  285. }
  286.  
  287. if (right==4)
  288. {
  289. printf("\nYou got it right in %d try/tries\n", tries);
  290. tries = 10;
  291. }
  292. else
  293. {
  294. right = 0;
  295. almost = 0;
  296. }
  297.  
  298.  
  299. if (tries==10 && right!=4)
  300. {
  301. printf("\n\nYou failed!\n");
  302. printf("The Random Number Generated is: %i%i%i%i", ran[0], ran[1], ran[2], ran[3]);
  303. }
  304. }
  305. }
  306.  
  307.  
Success #stdin #stdout 0s 4276KB
stdin
Standard input is empty
stdout
Press 1 for Easy Mode 
 Press 2 for Hard Mode 
 Choice:Fastest Time: 
Cannot open file