Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #define NB_L 43
- #define NB_C 90
- typedef struct array ARRAY;
- struct array {
- int matrice1 [NB_L][NB_C];
- int matrice2 [NB_L][NB_C];
- };
- // Les prototypes des fonctions
- void myDelay (float i);
- void roadMap1 (ARRAY *vehicule);
- void roadMap2 (ARRAY *vehicule);
- void initCarsFromWest (ARRAY *vehicule);
- void deplacement1 (ARRAY *vehicule);
- void deplacement2 (ARRAY *vehicule);
- void movingCarsFromWest (ARRAY *vehicule);
- // Fais une pause de l'application durant i seconds
- void myDelay (float i) {
- clock_t start, end;
- start = clock ();
- while (((end = clock ()) - start) <= i * CLOCKS_PER_SEC);
- }
- // Fonction qui initialise chaque case du giratoire dans un tableau
- void roadMap1 (ARRAY *vehicule) {
- int i = 0, j = 0;
- // NB_L de NB_C
- for (i = 0; i < NB_L; i++) {
- for (j = 0; j < NB_C; j++) {
- vehicule -> matrice1 [i][j] = 0;
- }
- }
- // La partie nord de notre giratoire.
- for (i = 1; i < 14; i++) {
- vehicule -> matrice1 [i][40] = 20;
- vehicule -> matrice1 [i][42] = 13;
- vehicule -> matrice1 [i][44] = 20;
- vehicule -> matrice1 [i][46] = 13;
- vehicule -> matrice1 [i][48] = 20;
- }
- //La partie est du giratoire.
- for (j = 61; j < 89; j++) {
- vehicule -> matrice1 [17][j] = 16;
- vehicule -> matrice1 [19][j] = 15;
- vehicule -> matrice1 [21][j] = 12;
- vehicule -> matrice1 [23][j] = 15;
- vehicule -> matrice1 [25][j] = 16;
- }
- // La partie sud du giratoire.
- for (i = 29; i < 42; i++) {
- vehicule -> matrice1 [i][40] = 20;
- vehicule -> matrice1 [i][42] = 13;
- vehicule -> matrice1 [i][44] = 20;
- vehicule -> matrice1 [i][46] = 13;
- vehicule -> matrice1 [i][48] = 20;
- }
- //La partie ouest du giratoire.
- for (j = 0; j < 28; j++) {
- vehicule -> matrice1 [17][j] = 16;
- vehicule -> matrice1 [19][j] = 15;
- vehicule -> matrice1 [21][j] = 12;
- vehicule -> matrice1 [23][j] = 15;
- vehicule -> matrice1 [25][j] = 16;
- }
- // Le giratoire.
- for (i = 15; i < 17; i++) {
- vehicule -> matrice1 [i][28] = 20;
- }
- for (i = 26; i < 28; i++) {
- vehicule -> matrice1 [i][28] = 20;
- }
- for (j = 29; j < 40; j++) {
- vehicule -> matrice1 [14][j] = 16;
- vehicule -> matrice1 [28][j] = 16;
- }
- for (j = 49; j < 60; j++) {
- vehicule -> matrice1 [14][j] = 16;
- vehicule -> matrice1 [28][j] = 16;
- }
- for (i = 15; i < 17; i++) {
- vehicule -> matrice1 [i][60] = 20;
- }
- for (i = 26; i < 28; i++) {
- vehicule -> matrice1 [i][60] = 20;
- }
- // Le centre de notre giratoire.
- for (j = 35; j < 54; j++) {
- vehicule -> matrice1 [17][j] = 11;
- vehicule -> matrice1 [18][j] = 11;
- vehicule -> matrice1 [19][j] = 11;
- vehicule -> matrice1 [20][j] = 11;
- vehicule -> matrice1 [21][j] = 11;
- vehicule -> matrice1 [22][j] = 11;
- vehicule -> matrice1 [23][j] = 11;
- vehicule -> matrice1 [24][j] = 11;
- vehicule -> matrice1 [25][j] = 11;
- }
- // Les "coins" du centre de notre giratoire.
- vehicule -> matrice1 [17][28] = 21;
- vehicule -> matrice1 [25][28] = 18;
- vehicule -> matrice1 [14][28] = 19;
- vehicule -> matrice1 [28][28] = 17;
- vehicule -> matrice1 [14][40] = 21;
- vehicule -> matrice1 [28][40] = 18;
- vehicule -> matrice1 [14][48] = 17;
- vehicule -> matrice1 [28][48] = 19;
- vehicule -> matrice1 [14][60] = 18;
- vehicule -> matrice1 [28][60] = 21;
- vehicule -> matrice1 [17][60] = 17;
- vehicule -> matrice1 [25][60] = 19;
- }
- // Fonction qui sert à afficher notre giratoire
- void roadMap2 (ARRAY *vehicule) {
- int i = 0, j = 0;
- // NB_L de NB_C
- for (i = 0; i < NB_L; i++) {
- for (j = 0; j < NB_C; j++) {
- if (vehicule -> matrice1 [i][j] == 0) {
- printf(" ");
- }
- if (vehicule -> matrice1 [i][j] == 1) {
- printf("N");
- }
- if (vehicule -> matrice1 [i][j] == 2) {
- printf("E");
- }
- if (vehicule -> matrice1 [i][j] == 3) {
- printf("S");
- }
- if (vehicule -> matrice1 [i][j] == 4) {
- printf("W");
- }
- if (vehicule -> matrice1 [i][j] == 11) {
- printf("█");
- }
- if (vehicule -> matrice1 [i][j] == 12) {
- printf("■");
- }
- if (vehicule -> matrice1 [i][j] == 13) {
- printf("|");
- }
- if (vehicule -> matrice1 [i][j] == 14) {
- printf("╬");
- }
- if (vehicule -> matrice1 [i][j] == 15) {
- printf("-");
- }
- if (vehicule -> matrice1 [i][j] == 16) {
- printf("═");
- }
- if (vehicule -> matrice1 [i][j] == 17) {
- printf("╚");
- }
- if (vehicule -> matrice1 [i][j] == 18) {
- printf("╗");
- }
- if (vehicule -> matrice1 [i][j] == 19) {
- printf("╔");
- }
- if (vehicule -> matrice1 [i][j] == 20) {
- printf("║");
- }
- if (vehicule -> matrice1 [i][j] == 21) {
- printf("╝");
- }
- } printf("\n");
- }
- }
- // Fonction qui initialise les deux voitures partant de l'ouest
- void initCarsFromWest (ARRAY *vehicule) {
- // char direction [] = {'N', 'S', 'W', 'E'};
- vehicule -> matrice1 [22][0] = rand () % 5;
- vehicule -> matrice1 [24][0] = rand () % 5;
- }
- // Déplacement sur la ligne 24
- void deplacement1 (ARRAY *vehicule) {
- int i = 0, j = 0;
- for (j = 0; j < 30; j++) {
- if (vehicule -> matrice1 [24][j] == 1 || vehicule -> matrice1 [24][j] == 2 || vehicule -> matrice1 [24][j] == 3 || vehicule -> matrice1 [24][j] == 4) {
- vehicule -> matrice1 [24][j+1] = vehicule -> matrice1 [24][j];
- vehicule -> matrice1 [24][j] = 0;
- }
- myDelay (0.1);
- system ("clear");
- roadMap2 (vehicule);
- }
- }
- // Déplacement sur la ligne 22
- void deplacement2 (ARRAY *vehicule) {
- int i = 0, j = 0;
- for (j = 0; j < 32; j++) {
- if (vehicule -> matrice1 [22][j] == 1 || vehicule -> matrice1 [22][j] == 2 || vehicule -> matrice1 [22][j] == 3 || vehicule -> matrice1 [22][j] == 4) {
- vehicule -> matrice1 [22][j+1] = vehicule -> matrice1 [22][j];
- vehicule -> matrice1 [22][j] = 0;
- }
- myDelay (0.1);
- system ("clear");
- roadMap2 (vehicule);
- }
- }
- void movingCarsFromWest (ARRAY *vehicule) {
- roadMap1 (vehicule);
- initCarsFromWest (vehicule);
- int i = 0, j = 0;
- // Nord
- if (vehicule -> matrice1 [24][0] == 1) {
- deplacement1 (vehicule);
- }
- // Est
- if (vehicule -> matrice1 [24][0] == 2) {
- deplacement1 (vehicule);
- }
- // Sud
- if (vehicule -> matrice1 [24][0] == 3) {
- deplacement1 (vehicule);
- }
- // Ouest
- if (vehicule -> matrice1 [24][0] == 4) {
- deplacement1 (vehicule);
- }
- roadMap2 (vehicule);
- }
- int main (int argc, int **argv) {
- srand (time (NULL));
- while (1) {
- movingCarsFromWest (vehicule);
- system ("clear");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment