Guest User

Untitled

a guest
Nov 23rd, 2015
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5.  
  6. #define NB_L 43
  7. #define NB_C 90
  8.  
  9. typedef struct array ARRAY;
  10. struct array {
  11. int matrice1 [NB_L][NB_C];
  12. int matrice2 [NB_L][NB_C];
  13. };
  14.  
  15. // Les prototypes des fonctions
  16. void myDelay (float i);
  17. void roadMap1 (ARRAY *vehicule);
  18. void roadMap2 (ARRAY *vehicule);
  19. void initCarsFromWest (ARRAY *vehicule);
  20. void deplacement1 (ARRAY *vehicule);
  21. void deplacement2 (ARRAY *vehicule);
  22. void movingCarsFromWest (ARRAY *vehicule);
  23.  
  24. // Fais une pause de l'application durant i seconds
  25. void myDelay (float i) {
  26.  
  27. clock_t start, end;
  28.  
  29. start = clock ();
  30. while (((end = clock ()) - start) <= i * CLOCKS_PER_SEC);
  31. }
  32.  
  33. // Fonction qui initialise chaque case du giratoire dans un tableau
  34. void roadMap1 (ARRAY *vehicule) {
  35.  
  36. int i = 0, j = 0;
  37.  
  38. // NB_L de NB_C
  39. for (i = 0; i < NB_L; i++) {
  40. for (j = 0; j < NB_C; j++) {
  41. vehicule -> matrice1 [i][j] = 0;
  42. }
  43. }
  44.  
  45. // La partie nord de notre giratoire.
  46. for (i = 1; i < 14; i++) {
  47. vehicule -> matrice1 [i][40] = 20;
  48. vehicule -> matrice1 [i][42] = 13;
  49. vehicule -> matrice1 [i][44] = 20;
  50. vehicule -> matrice1 [i][46] = 13;
  51. vehicule -> matrice1 [i][48] = 20;
  52. }
  53.  
  54. //La partie est du giratoire.
  55. for (j = 61; j < 89; j++) {
  56. vehicule -> matrice1 [17][j] = 16;
  57. vehicule -> matrice1 [19][j] = 15;
  58. vehicule -> matrice1 [21][j] = 12;
  59. vehicule -> matrice1 [23][j] = 15;
  60. vehicule -> matrice1 [25][j] = 16;
  61. }
  62.  
  63. // La partie sud du giratoire.
  64. for (i = 29; i < 42; i++) {
  65. vehicule -> matrice1 [i][40] = 20;
  66. vehicule -> matrice1 [i][42] = 13;
  67. vehicule -> matrice1 [i][44] = 20;
  68. vehicule -> matrice1 [i][46] = 13;
  69. vehicule -> matrice1 [i][48] = 20;
  70. }
  71.  
  72. //La partie ouest du giratoire.
  73. for (j = 0; j < 28; j++) {
  74. vehicule -> matrice1 [17][j] = 16;
  75. vehicule -> matrice1 [19][j] = 15;
  76. vehicule -> matrice1 [21][j] = 12;
  77. vehicule -> matrice1 [23][j] = 15;
  78. vehicule -> matrice1 [25][j] = 16;
  79. }
  80.  
  81. // Le giratoire.
  82. for (i = 15; i < 17; i++) {
  83. vehicule -> matrice1 [i][28] = 20;
  84. }
  85. for (i = 26; i < 28; i++) {
  86. vehicule -> matrice1 [i][28] = 20;
  87. }
  88.  
  89. for (j = 29; j < 40; j++) {
  90. vehicule -> matrice1 [14][j] = 16;
  91. vehicule -> matrice1 [28][j] = 16;
  92. }
  93.  
  94. for (j = 49; j < 60; j++) {
  95. vehicule -> matrice1 [14][j] = 16;
  96. vehicule -> matrice1 [28][j] = 16;
  97. }
  98.  
  99. for (i = 15; i < 17; i++) {
  100. vehicule -> matrice1 [i][60] = 20;
  101. }
  102.  
  103. for (i = 26; i < 28; i++) {
  104. vehicule -> matrice1 [i][60] = 20;
  105. }
  106.  
  107. // Le centre de notre giratoire.
  108. for (j = 35; j < 54; j++) {
  109. vehicule -> matrice1 [17][j] = 11;
  110. vehicule -> matrice1 [18][j] = 11;
  111. vehicule -> matrice1 [19][j] = 11;
  112. vehicule -> matrice1 [20][j] = 11;
  113. vehicule -> matrice1 [21][j] = 11;
  114. vehicule -> matrice1 [22][j] = 11;
  115. vehicule -> matrice1 [23][j] = 11;
  116. vehicule -> matrice1 [24][j] = 11;
  117. vehicule -> matrice1 [25][j] = 11;
  118. }
  119.  
  120. // Les "coins" du centre de notre giratoire.
  121. vehicule -> matrice1 [17][28] = 21;
  122. vehicule -> matrice1 [25][28] = 18;
  123.  
  124. vehicule -> matrice1 [14][28] = 19;
  125. vehicule -> matrice1 [28][28] = 17;
  126.  
  127. vehicule -> matrice1 [14][40] = 21;
  128. vehicule -> matrice1 [28][40] = 18;
  129.  
  130. vehicule -> matrice1 [14][48] = 17;
  131. vehicule -> matrice1 [28][48] = 19;
  132.  
  133. vehicule -> matrice1 [14][60] = 18;
  134. vehicule -> matrice1 [28][60] = 21;
  135.  
  136. vehicule -> matrice1 [17][60] = 17;
  137. vehicule -> matrice1 [25][60] = 19;
  138. }
  139.  
  140. // Fonction qui sert à afficher notre giratoire
  141. void roadMap2 (ARRAY *vehicule) {
  142.  
  143. int i = 0, j = 0;
  144.  
  145. // NB_L de NB_C
  146. for (i = 0; i < NB_L; i++) {
  147. for (j = 0; j < NB_C; j++) {
  148. if (vehicule -> matrice1 [i][j] == 0) {
  149. printf(" ");
  150. }
  151. if (vehicule -> matrice1 [i][j] == 1) {
  152. printf("N");
  153. }
  154. if (vehicule -> matrice1 [i][j] == 2) {
  155. printf("E");
  156. }
  157. if (vehicule -> matrice1 [i][j] == 3) {
  158. printf("S");
  159. }
  160. if (vehicule -> matrice1 [i][j] == 4) {
  161. printf("W");
  162. }
  163. if (vehicule -> matrice1 [i][j] == 11) {
  164. printf("█");
  165. }
  166. if (vehicule -> matrice1 [i][j] == 12) {
  167. printf("■");
  168. }
  169. if (vehicule -> matrice1 [i][j] == 13) {
  170. printf("|");
  171. }
  172. if (vehicule -> matrice1 [i][j] == 14) {
  173. printf("╬");
  174. }
  175. if (vehicule -> matrice1 [i][j] == 15) {
  176. printf("-");
  177. }
  178. if (vehicule -> matrice1 [i][j] == 16) {
  179. printf("═");
  180. }
  181. if (vehicule -> matrice1 [i][j] == 17) {
  182. printf("╚");
  183. }
  184. if (vehicule -> matrice1 [i][j] == 18) {
  185. printf("╗");
  186. }
  187. if (vehicule -> matrice1 [i][j] == 19) {
  188. printf("╔");
  189. }
  190. if (vehicule -> matrice1 [i][j] == 20) {
  191. printf("║");
  192. }
  193. if (vehicule -> matrice1 [i][j] == 21) {
  194. printf("╝");
  195. }
  196. } printf("\n");
  197. }
  198. }
  199.  
  200. // Fonction qui initialise les deux voitures partant de l'ouest
  201. void initCarsFromWest (ARRAY *vehicule) {
  202.  
  203. // char direction [] = {'N', 'S', 'W', 'E'};
  204. vehicule -> matrice1 [22][0] = rand () % 5;
  205. vehicule -> matrice1 [24][0] = rand () % 5;
  206. }
  207.  
  208. // Déplacement sur la ligne 24
  209. void deplacement1 (ARRAY *vehicule) {
  210.  
  211. int i = 0, j = 0;
  212.  
  213. for (j = 0; j < 30; j++) {
  214. if (vehicule -> matrice1 [24][j] == 1 || vehicule -> matrice1 [24][j] == 2 || vehicule -> matrice1 [24][j] == 3 || vehicule -> matrice1 [24][j] == 4) {
  215. vehicule -> matrice1 [24][j+1] = vehicule -> matrice1 [24][j];
  216. vehicule -> matrice1 [24][j] = 0;
  217. }
  218.  
  219. myDelay (0.1);
  220. system ("clear");
  221. roadMap2 (vehicule);
  222. }
  223. }
  224.  
  225. // Déplacement sur la ligne 22
  226. void deplacement2 (ARRAY *vehicule) {
  227.  
  228. int i = 0, j = 0;
  229.  
  230. for (j = 0; j < 32; j++) {
  231. if (vehicule -> matrice1 [22][j] == 1 || vehicule -> matrice1 [22][j] == 2 || vehicule -> matrice1 [22][j] == 3 || vehicule -> matrice1 [22][j] == 4) {
  232. vehicule -> matrice1 [22][j+1] = vehicule -> matrice1 [22][j];
  233. vehicule -> matrice1 [22][j] = 0;
  234. }
  235.  
  236. myDelay (0.1);
  237. system ("clear");
  238. roadMap2 (vehicule);
  239. }
  240. }
  241.  
  242. void movingCarsFromWest (ARRAY *vehicule) {
  243.  
  244. roadMap1 (vehicule);
  245. initCarsFromWest (vehicule);
  246.  
  247. int i = 0, j = 0;
  248.  
  249. // Nord
  250. if (vehicule -> matrice1 [24][0] == 1) {
  251. deplacement1 (vehicule);
  252. }
  253. // Est
  254. if (vehicule -> matrice1 [24][0] == 2) {
  255. deplacement1 (vehicule);
  256. }
  257. // Sud
  258. if (vehicule -> matrice1 [24][0] == 3) {
  259. deplacement1 (vehicule);
  260. }
  261. // Ouest
  262. if (vehicule -> matrice1 [24][0] == 4) {
  263. deplacement1 (vehicule);
  264. }
  265.  
  266. roadMap2 (vehicule);
  267. }
  268.  
  269. int main (int argc, int **argv) {
  270.  
  271. srand (time (NULL));
  272.  
  273. while (1) {
  274. movingCarsFromWest (vehicule);
  275. system ("clear");
  276. }
  277. return 0;
  278. }
Advertisement
Add Comment
Please, Sign In to add comment