1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
| #include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef char* String; // une chaîne de caractères
typedef String* Vecteur; // une ligne (tableau 1d) de strings
typedef Vecteur* Matrice; // un tableau 2d de strings
void wait(int millisecondes);
main(){
/*----------initialisation MATRICE--------------*/
int n = 40;
int m = 100;
int i, j, k; /*variable de boucles, pas besoin de definir dautres variables*/
int rando;
system("clear");
Matrice mat=(Matrice) malloc(n*sizeof(Vecteur));
for(i=0;i<n;i++){
mat[i]=(Vecteur) malloc(m*sizeof(String));
}
for(i=0; i < n; i++){
for(j=0; j < m; j++){
mat[i][j] =" ";
}
}
for(i=0;i<40;i++){ /*NORD*/
mat[i][40]="|";
mat[i][42]="|";
mat[i][44]="|";
mat[i][45]="/";
mat[i][46]="|";
mat[i][48]="|";
mat[i][50]="|";
}
/*animation route*/
for(k=0; k<3000; k++){
/*on efface les voitures en bout de route*/
mat[39][41] = " ";
mat[39][43] = " ";
/*APPARITION ALEATOIRE DES VOITURES */
int nombre_aleatoire = 0;
int rando = 0;
while(rando==nombre_aleatoire){ /*force le programme random a choisir un nouveau nombre, sinon le meme ressort à l'infini*/
nombre_aleatoire = rand()%5;
}
rando=nombre_aleatoire;
switch(rando){
case 0:
mat[0][41] = "V";
mat[0][43] = "V";
break;
case 1:
mat[0][43] = "V";
break;
case 2:
mat[0][41] = "V";
break;
}
/*on dessine le rond point à l'état k*/
for(i=0; i < n; i++){
for(j=0; j < m; j++){
printf("%s", mat[i][j]);
}
printf("\n");
}
/*ENTREE NORD*/
for(i=39; i > 0; --i){
if(mat[i-1][41] != " " && mat[i][41]==" "){
mat[i][41] = mat[i-1][41];
mat[i-1][41] = " ";
}
}
for(i=39; i > 0; --i){
if(mat[i-1][43] != " " && mat[i][43]==" "){
mat[i][43] = mat[i-1][43];
mat[i-1][43] = " ";
}
}
wait(100);
system("clear");
}
}
void wait(int millisecondes)
{
clock_t start = clock();
while ((clock() - start) * 1000 < millisecondes * CLOCKS_PER_SEC) ;
} |
Partager