Untitled
user_9243973
plain_text
3 years ago
5.8 kB
11
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>
typedef struct Liste*Pliste;
typedef struct Liste{int x;int y;int nature;Pliste Suiv;}Cellule;
int n,m,k1,k2; int T[100][100];
Pliste Tete;
//Creation
void creercarte(int T[][100],int n,int m)
{
int i,j,r;
srand(time(NULL));
for(i=0;i<n;i=i+2) //on avance de deux pas pour travailler que par paire
{
for (j=0;j<m;j=j+2)
{
r=(rand()%4)+1; // rand%4 veut dire qu'on remplie avec des 0 1 2 3 on rajoute
T[i][j]=r; //+1 pour remplir avec 1 2 3 4 seulement
T[i][j+1]=r;
T[i+1][j]=r; //on remplie aleatoirement 4 pixels voisin en meme temps et vu que cest
T[i+1][j+1]=r; //aleatoire on a au minimum des objets de 4 sinon plus que 4
}
}
}
//Affichage
void affichecarte(int T[][100])
{
int i,j;
for (i=0;i<n+2;i++) //pour delimiter la carte : la premiere ligne de_
{
printf("_");
}
for(i=0;i<n;i++)
{
printf("\n");
printf("|");
for (j=0;j<m;j++)
{
if(T[i][j]==1){SetColor(1);}
if(T[i][j]==2){SetColor(4);} //pour que chaque nature ait sa propre couleur
if(T[i][j]==3){SetColor(6);} //on appelle la fonction des couleurs
if(T[i][j]==4){SetColor(2);}
printf("%d",T[i][j]);
}
SetColor(15); //(le 15 la couleur du blanc) pour quil n'y ait que les chiffres en couleurs et non les |
printf("|");
}
printf("\n");
for (i=0;i<n+2;i++) // la derniere ligne de _
{
printf("_");
}
}
//la fonction pour les couleurs
void SetColor(int ForgC)
{
WORD wColor;
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//We use csbi for the wAttributes word.
if(GetConsoleScreenBufferInfo(hStdOut, &csbi))
{
//Mask out all but the background attribute, and add in the forgournd color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
return;
}
//procedure qui affiche liste
void afficheliste(Pliste Tete)
{
Pliste P;
P=Tete;
while(P!=NULL)
{
printf("(%d %d)",P->x,P->y);P=P->Suiv;
}
}
//procedure qui trie la liste
void triliste(Pliste *Tete)
{
Pliste Q,P;
int A,B;
P=*Tete;
while(P->Suiv!=NULL)
{
Q=P->Suiv;
while(Q!=NULL)
{
if (P->x >= Q->x) { A=P->x; B=P->y; P->x=Q->x; P->y=Q->y; Q->x=A; Q->y=B; };
if (P->x == Q->x && P->y >= Q->y) { A=P->x; B=P->y; P->x=Q->x; P->y=Q->y; Q->x=A; Q->y=B; };
Q=Q->Suiv;
}
P=P->Suiv;
}
}
//extraire objet
Pliste ExtraireObjet(int T[][100],int x,int y,int n,int m,int nature)
{
Pliste P,Q;
int A,B;;
if (T[x][y]==nature)
{
T[x][y]=9; k1=k1+1;
if (y>0) { ExtraireObjet(T,x,y-1,n,m,nature);} //case gauche
if (x>0) { ExtraireObjet(T,x-1,y,n,m,nature);}// case haut
if (x<n-1){ExtraireObjet(T,x+1,y,n,m,nature);}//bas
if (y<m-1){ExtraireObjet(T,x,y+1,n,m,nature);}//droite
P=malloc(sizeof(Cellule));
P->x=x; P->y=y;
P->nature = nature;
P->Suiv=Tete; Tete=P;
k2=k2+1;k1=k1-1;
}
if(k1==0 && k2>0){
printf("la liste non triee:");
afficheliste(Tete);triliste(&Tete);
printf("\n");
printf("la liste apres le tri:");
afficheliste(Tete);
}
printf("\n");
return (Tete);
}
//affiche objet
void AfficheObjet(int T[][100],int x,int y,int n,int m,int nature)
{
int i,j;
Pliste P;
P= ExtraireObjet(T,x,y,n,m,nature);
for (i=0;i<n+2;i++) //pour delimiter la carte : la premiere ligne de_
{
printf("_");
}
for(int i=0;i<n;i++)
{
printf("\n");
printf("|");
for(int j=0;j<m;j++)
{
if(P!= NULL)
{
if(P->nature==1){SetColor(1);}
if(P->nature==2){SetColor(4);} //pour que chaque nature ait sa propre couleur
if(P->nature==3){SetColor(6);} //on appelle la fonction des couleurs
if(P->nature==4){SetColor(2);}
if(i==P->x && j==P->y)
{
printf("%d",P->nature);
P=P->Suiv;
}
else
{
SetColor(15);
printf(" ");
}
}
else
{
SetColor(15);
printf(" ");
}
}
SetColor(15);
printf("|");
}
printf("\n");
for (i=0;i<n+2;i++) // la derniere ligne de _
{
printf("_");
}
}
int main()
{ int x,y,nature;
Pliste P;
k1=0;k2=0;
printf("veuillez entrer les dimensions de votre carte \n");
printf("la longueur:");
scanf("%d",&n);
printf("la largeur:");
scanf("%d",&m);
printf("la carte : \n");
creercarte(T,n,m);
affichecarte(T);
printf("\nentrez les coordonnees x et y: ");
scanf("%d",&x); scanf("%d",&y);
printf("entrez la nature de la parcelle: ");
scanf("%d",&nature);
Tete=NULL;
AfficheObjet(T,x,y,n,m,nature);
return 0;
}Editor is loading...