| 12
 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
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 
 | #include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void generer_map(int,int,int*);
void generation_chemin(int,int,int*);
 
//execution de la fonction 
 
// ./prog taille_x taille_y nom_du_fichier_de_sortie
 
int main(int argc,char ** argv){
 
 
    FILE * carte;
 
    int  i,j,taille_x,taille_y,pourcentage=70;
    int  *print;
    char *fic;
    char *nom;
 
    printf("Veuillez indiquer le nombre de lignes : ");
    scanf("%i",&taille_x);
    printf("Veuillez indiquer le nombre de colonnes : ");
    scanf("%i",&taille_y);
 
    print = (int*) malloc ((taille_x*taille_y+1) * sizeof(int));
    fic   = (char*) malloc (100*sizeof(char));
    srand((int)time(NULL));
 
    generer_map(taille_x*taille_y,pourcentage,print);
 
    generation_chemin(taille_x,taille_y,print);
 
    strncpy(fic,"../",7);
    printf("Veuillez indiquer le nom de fichier : ");
    scanf("%s",&nom);
    strcat(fic,nom);
 
    carte=fopen(fic,"w+");
 
    fprintf(carte,"TAILLE ");
    fprintf(carte,"%i\n",taille_x*taille_y);
 
    for (i=1;i<=taille_y;i++){
	for (j=1;j<=taille_x;j++){
	    fprintf(carte,"%i",print[j+(i-1)*taille_x]);
            fprintf(carte," ");
	    }
	fprintf(carte,"\n");
	}
    printf("le fichier %s a bien été créé \n ",nom);
 
    fclose(carte);
    free(print);
    free(fic);	
 
 
return 0;
}
 
 
void generer_map(int taille,int remplissage,int * map){
 
int i;
 
for (i=1;i<=taille;i++){
    if (rand()%100>=remplissage)
	map[i]=0;
    else
	map[i]=-1;
    }
}
 
void generation_chemin(int taille_x,int taille_y, int * print){
 
int reste=1,i,position,exit=0,compte,a,nbr_boucle=0;
int *temp;
temp=(int*)malloc(100*sizeof(int));
 
while(nbr_boucle<=(taille_x*taille_y)){
    if( reste == 1 ){
	do{position=1+rand()%(taille_x*taille_y);
	  }while (print[position]!=-1);
	a=0;
	compte=1;
	exit=0;
	while ( exit == 0){
		a++;
		temp[compte] = position; // sauvegarde tout les position ou est passé le programe
		print[position] = 0; // empèche de repasser deux fois sur la mème case
		switch (rand()%5){
		    case 0:
			  if(a >= 15)
			      exit=1;
			  break;
		    case 1://test droite
			  if ( position % taille_x != 0 && print[ position + 1 ] == -1 ){
				position++;
				compte++;
				}
			  break;
		    case 2://test gauche
			  if ( ( position - 1 ) % taille_x != 0 && print[ position -1 ] == -1 ){
				position--;
				compte++;
				}
			  break;
		    case 3://test haut
			  if ( position - taille_x > 0 && print [position -taille_x] == -1){
				position = position -taille_x;
				compte++;
				}
			  break;
		    case 4://test bas
			  if ( position + taille_x > taille_x*taille_y && print [position + taille_x] == -1){
				position = position + taille_x;
				compte++;
				}
			  break;
		    }
	}
	//inscrit les chemins dans letableau de int
	for(i=1;i<=compte;i++){
		if (i==1 || i== compte){
			print[temp[i]] = compte;}
		else {
			print[temp[i]] = 0 ;}
 
	}
	reste=0;
	for(i=1;i<= taille_x*taille_y;i++){
		if (print[i] == -1){
			reste=1;}
		}
}
nbr_boucle++;    
}
free(temp);
} | 
Partager