Untitled
user_9243973
plain_text
3 years ago
4.1 kB
9
Indexable
#include <stdlib.h>
#include <time.h>
#include <windows.h>
#include <dos.h>
#include <dir.h>
#include "LibPileFileInt.c"
typedef struct Liste*Pliste;
typedef struct Liste{int x;int y;Pliste Suiv;}Cellule;
int T[100][100];
void ExtraireObjet(int T[][100],int x,int y,int n,int m,int nature,Pliste Tete)
{
Pliste P,Q;
int A,B;
if (T[x][y]==nature) {
//printf("(%d %d)\n",i,j);
T[x][y]=9;
if (y<m-1){ExtraireObjet(T,x,y+1,n,m,nature,Tete);}//droite
if (x<n-1){ExtraireObjet(T,x+1,y,n,m,nature,Tete);}//bas
if (y>0){ ExtraireObjet(T,x,y-1,n,m,nature,Tete);} //case gauche
if (x>0){ ExtraireObjet(T,x-1,y,n,m,nature,Tete);}// case haut
P=malloc(sizeof(Cellule));
P->x=x; P->y=y;
P->Suiv=Tete; Tete=P;
afficheliste(Tete);
}
}
void afficheliste(Pliste Tete)
{
Pliste P;
P=Tete;
triliste(P);
while(P!=NULL)
{
printf("(%d %d)",P->x,P->y);P=P->Suiv;
}
}
Pliste triliste(Pliste Tete)
{
Pliste Q,P;
int A,B;
for (int i =0; i<100; i++)
{
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;
}
Tete=P;
}
}
//Creation
void creercarte(int n,int m, int T[][100])
{
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 voisins 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 n,int m,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;
}
int main()
{ int n,m,i,j,nature;
Pliste P,*Tete,R;
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(n,m,T);
affichecarte(n,m,T);
printf("\nentrez les coordonnees i et j: ");
scanf("%d",&i); scanf("%d",&j);
printf("entrez la nature de la parcelle: ");
scanf("%d",&nature);
Tete=NULL;
ExtraireObjet(T,i,j,n,m,nature,Tete);
}
Editor is loading...