#include "bugs.h"


/**********************************************************/
/*		Fonction Principale			  */
/**********************************************************/

main() {

init();

lire_info();
test_armee();
test_ennemis();
test_proximite();
deplacement();
ecrire_info();
}


/**********************************************************/
/* Fontion qui initialise la matrice			  */
/**********************************************************/

int init()
    {
     int i,j;
	for (i=0;i<MAXX;i++)
	{
	 for (j=0;j<MAXY;j++) 
             {
              MATRICE[i][j]=0;
	      ENNEMIS[i][j]=0;
             }
        }
	for (i=0;i<MAX;i++)
	    {
	     TAB_GEL[i].etat=0;
	     TAB_GEL[i].but[0]=0;
	     TAB_GEL[i].but[1]=0;
	     TAB_GEL[i].tueur=1;
	     TAB_GEL[i].direction=1;
            }
    }
	  
/***********************************************************/
/* Fonction qui lit des donnees sur le port d'entree       */
/* standard						   */
/***********************************************************/

int lire_info() {

/* la taille du buffer doit pouvoir contenir les lignes d'informations
   (qui ont une taille de 11 octet avec le caractere return) 
   comme mon algo n'est pas axer sur la fission, la taille est emplement
   suffisante. Je multiplie seulement par 10 car je ne sauve pas le 
   caractere return dans le buffer */

int i=0,j,test;
char tampon[1];

 
test=0;

 while(test != 2)
     {
    fread(&TAB_GEL[j].proprietes.y,2,1,stdin);
    fread(&TAB_GEL[j].proprietes.x,2,1,stdin);
    fread(&TAB_GEL[j].proprietes.charge,2,1,stdin);
    fread(&TAB_GEL[j].proprietes.proche[0],1,1,stdin);
    fread(&TAB_GEL[j].proprietes.proche[1],1,1,stdin);
    fread(&TAB_GEL[j].proprietes.proche[2],1,1,stdin);
    fread(&TAB_GEL[j].proprietes.proche[3],1,1,stdin);
    fread(&tampon[0],1,1,stdin);
    fread(&tampon[0],1,1,stdin);
    if (tampon[0]=='\n')
	{
         test=2;
	}
     else
         ungetc(tampon[0],stdin);

    /* Je marque les cases que j'occupe */

    MATRICE[TAB_GEL[j].proprietes.x % MAXX][TAB_GEL[j].proprietes.y % MAXY]=1;
     j++;
    }





NB_GEL=j;  /* Nombre REEL de gellule que je vais traiter */

for (i=0;i<NB_GEL;i++) 
    {
    TAB_GELANC[i].proprietes.x=TAB_GEL[i].proprietes.x;
    TAB_GELANC[i].proprietes.y=TAB_GEL[i].proprietes.y;
    TAB_GELANC[i].proprietes.charge=TAB_GEL[i].proprietes.charge;
    TAB_GELANC[i].proprietes.proche[0]=TAB_GEL[i].proprietes.proche[0];
    TAB_GELANC[i].proprietes.proche[1]=TAB_GEL[i].proprietes.proche[1];
    TAB_GELANC[i].proprietes.proche[2]=TAB_GEL[i].proprietes.proche[2];
    TAB_GELANC[i].proprietes.proche[3]=TAB_GEL[i].proprietes.proche[3];
}
}



/*************************************************************/
/* Fonction qui permet d'etablir les armees		     */
/* Je vais donner certaines directives (direction a prendre) */ 
/* a certaines gellules (Il n'y a que 4 armees de 10 gellules*/
/* Sur les 50 du depart, 10 ne seront n'apartiendront a      */
/* aucun corps ... ce seront des gellules tueuses, des       */
/* mercenaires.						     */
/*************************************************************/

int test_armee()
{

/* Beaucoup de variables ... beaucoup de test ... trop ? ;) */

short x,y;
int i,j,k;
int tueur[MAX*2],armee[4];
int ax1,ax2,ax3,ax4,ay1,ay2,ay3,ay4;
int X1,X2,X3,X4,Y1,Y2,Y3,Y4;
int col1=0,col2=0,col3=0,col4=0;

/* Ici je vais determiner le centre de chacune des 4 armees */
/* Je partage l'air de jeu en quatre partie, j'ordonne aux  */
/* gellules de former une armee dans chacune des 4 zones    */
/* REM : en fait ce n'est pas le centre, mais l'extrimite   */
/* de l'armee ... ceci pour faciliter la tache de l'algo    */
/* cf schema de la doc 					    */


X1=((MAXX/4)-5);
X2=((MAXX/2)+(MAXX/4)+5);
X3=((MAXX/2)+(MAXX/4)+5);
X4=((MAXX/4)-5);

Y1=((MAXY/4)+5);
Y2=((MAXY/4)+5);
Y3=((MAXY/2)+(MAXY/4)-5);
Y4=((MAXY/2)+(MAXY/4)-5);


for (i=0;i<4;i++) armee[i]=0; /* initialisation */	
			      /* lorsque armee[x]=1 cela voudra dire */
			      /* que l'armee est formee */


for(i=0;i<NB_GEL;i++)
{
x=TAB_GEL[i].proprietes.x;
y=TAB_GEL[i].proprietes.y;

if ( (armee[0]==0) && ((x <= (MAXX/2)) && (y <= (MAXY/2))) )
   {
    TAB_GEL[i].but[0]=X1+ax1;
    TAB_GEL[i].but[1]=Y1+ay1;
    TAB_GEL[i].tueur=0;
    ax1++;
    ay1++;

/* une armee est composee de 2 colonnes */
/* si colx=1 cela veut dire qu'une colonne a ete formee */

    if ((ax1 >= 5) && (col1==0)) {
       col1=1;
       ax1=0;
       ay1=0;
       Y1++;
      }
    if ((ax1 >= 5) && (col1==1)) armee[0]=1;
   }

else if ( (armee[1]==0) && ((x > (MAXX/2)) && (y <= (MAXY/2))) )
   	{
   	TAB_GEL[i].but[0]=X2+ax2;
    	TAB_GEL[i].but[1]=Y2+ay2;
    	TAB_GEL[i].tueur=0;
    	ax2++;
    	ay2++;

    	if ((ax2 <= -5) && (col2==0)) {
       	col2=1;
       	ax2=0;
       	ay2=0;
       	Y2++;
     	 }
    	if ((ax2 <= -5) && (col2==1)) armee[1]=1;
  	 }

else if ( (armee[2]==0) && ((x > (MAXX/2)) && (y > (MAXY/2))) )
   	{
    	TAB_GEL[i].but[0]=X3+ax3;
    	TAB_GEL[i].but[1]=Y3+ay3;
	TAB_GEL[i].tueur=0;
    	ax3++;
    	ay3++;

    	if ((ax3 >= 5) && (col3==0)) {
       	col3=1;
       	ax3=0;
       	ay3=0;
       	Y3++;
    	  }
    	if ((ax3 >= 5) && (col3==1)) armee[2]=1;
   	}
 
else if ( (armee[3]==0) && ((x <= (MAXX/2)) && (y > (MAXY/2))) )
  	 {
    	TAB_GEL[i].but[0]=X4+ax4;
    	TAB_GEL[i].but[1]=Y4+ay4;
	TAB_GEL[i].tueur=0;
    	ax4++;
    	ay4++;

    	if ((ax4 >= 5) && (col4==0)) {
       	col4=1;
       	ax4=0;
       	ay4=0;
       	Y4++;
     	 }
    	if ((ax4 >= 5) && (col4==1)) armee[3]=1;
   	}
else {
	tueur[j]=i;
	j++;
     }


/* test pour savoir si les 4 armees ont ete constituees */
/* Si les armees n'ont pas ete toutes constituees cela veut dire */
/* que les gellules n'etaient pas reparties equitablement dans */
/* l'espace de jeu. */

j=0;

if (armee[0]!=1)
   {
    for(k=ax1;k<5;k++,j++)
	{
	TAB_GEL[tueur[j]].but[0]=X1+k;
	TAB_GEL[tueur[j]].but[1]=Y1+k;
	TAB_GEL[tueur[j]].tueur=0;
  	}
   }
if (armee[1]!=1)
   {
    for(k=ax2;k<5;k++,j++)
        {
        TAB_GEL[tueur[j]].but[0]=X2+k;
        TAB_GEL[tueur[j]].but[1]=Y2+k;
       TAB_GEL[tueur[j]].tueur=0; 
	}
   }
	
}
if (armee[2]!=1)
   {
    for(k=ax3;k<5;k++,j++)
        {
        TAB_GEL[tueur[j]].but[0]=X3+k;
        TAB_GEL[tueur[j]].but[1]=Y3+k;
	TAB_GEL[tueur[j]].tueur=0;
        }
   }
if (armee[3]!=1)
   {
    for(k=ax4;k<5;k++,j++)
        {
        TAB_GEL[tueur[j]].but[0]=X4+k;
        TAB_GEL[tueur[j]].but[1]=Y4+k;
	TAB_GEL[tueur[j]].tueur=0;
        }
   }

}


/****************************************************************/
/* Fonction qui permet d'avoir une vision globale des positions */ 
/* des enemis que les gellules ont repere 			*/
/****************************************************************/

int test_ennemis()
{

int i;
short indice;

for(i=0;i<NB_GEL;i++)
   {
    if ((TAB_GEL[i].proprietes.proche[0]!=0) && 
(MATRICE[TAB_GEL[i].proprietes.x % MAXX] 
  [(TAB_GEL[i].proprietes.y + TAB_GEL[i].proprietes.proche[0])% MAXY])==0)
 { 
  ENNEMIS[TAB_GEL[i].proprietes.x % MAXX]
   [(TAB_GEL[i].proprietes.y + TAB_GEL[i].proprietes.proche[0])%MAXY]=1;
 }

    if (TAB_GEL[i].proprietes.proche[1]!=0) 
       {
   indice= (TAB_GEL[i].proprietes.y - TAB_GEL[i].proprietes.proche[1]);
   if (indice<0) indice=indice + MAXY;

       if (MATRICE[TAB_GEL[i].proprietes.x % MAXX][indice % MAXY]==0)
      {
        ENNEMIS[TAB_GEL[i].proprietes.x % MAXX][indice % MAXY]=1;
      }

      }

    if ((TAB_GEL[i].proprietes.proche[2]!=0) &&
(MATRICE[(TAB_GEL[i].proprietes.x + TAB_GEL[i].proprietes.proche[2])%MAXX]
  [TAB_GEL[i].proprietes.y % MAXY])==0)
 {
  ENNEMIS[(TAB_GEL[i].proprietes.x + TAB_GEL[i].proprietes.proche[2])% MAXX]
  [TAB_GEL[i].proprietes.y % MAXY]=1;
 }

    if (TAB_GEL[i].proprietes.proche[3]!=0)
	{
	indice = (TAB_GEL[i].proprietes.x - TAB_GEL[i].proprietes.proche[3]);
	if (indice<0) indice=indice + MAXX;
 
	if  (MATRICE[indice %MAXX][TAB_GEL[i].proprietes.y % MAXY]==0)
 	{
  	ENNEMIS[indice % MAXX][TAB_GEL[i].proprietes.y % MAXY]=1;
 	} 
	}
}

}

/*********************************************************************/
/* Fonction determinant la STRATEGIE des gelules		     */
/*********************************************************************/

int test_proximite()
    {

int i,j,k,nb_enemis,nb_amis,nb_libre;
short ex[16],ey[16],amx[16],amy[16],lbx[16],lby[16],bidon;
short *x,*y;
short tempx,tempy,sortir,charge;
short *etat;

     for(i=0;i<NB_GEL;i++)
     {

/* Je simplifie l'utilisation des proprietes des gelules */

        x=&TAB_GEL[i].proprietes.x;
	y=&TAB_GEL[i].proprietes.y;
	etat=&TAB_GEL[i].etat;
	charge=TAB_GEL[i].proprietes.charge;

/* Ici on scanne une partie du terrain de la maniere suivante :
   On regarde l'etat des cases qui sont sur le cercle de rayon
   indice et de centre x,y */

	nb_enemis=scan(*x,*y,1,0,ex,ey);  /* Determine le nombre d'enemis dans la peripherie */
	nb_amis=scan(*x,*y,1,1,amx,amy);  /* Determine le nombre de gelules "amies" */
	nb_libre=scan(*x,*y,1,2,lbx,lby); /* permet de savoir combien de case sont occupees */

/* La premiere phase de test correspond aux cases tout de suite a cote*/

/* s'il y a plus d'ennemis que d'amis autour de la gellule je fais sauter */
/* la gellule */

	if ( (nb_enemis>nb_amis) && (test_energie(3,charge)!=0)) *etat=(short)3;
        else if(nb_enemis != 0)  /* sinon j'analyse les conditions de combat */
		{
		 sortir=0,j=0;
		 do{
		   if ((scan(ex[j],ey[j],1,0,&bidon,&bidon)==0) &&
		      (test_energie(2,charge)!=0)) { 
				tempx=ex[j];
				tempy=ey[j];
 				*etat=(short)2;
				sortir=1;
				}
                    j++;
                    if (j==nb_enemis) sortir=2;
		   }while(sortir==0);

/* Si les conditions sont defavorables, je .... fuis ! :) */			

                   if (sortir==2)
                   {
	           if ((test_energie(1,charge)!=0) && (nb_libre < 8))
			 {
                          for(k=0;k<(8 - nb_libre);k++)
			  {
                          if(scan(lbx[k],lby[k],1,0,&bidon,&bidon)==0)
			  {
			  tempx=lbx[k];
			  tempy=lby[k];
			  *etat=(short)1;
                          k=(8 - nb_libre);
		          }		 
			  }
			 }
		    else {           /* Si je ne peux pas fuir , j'attaque quand meme */
			tempx=ex[0];
			tempy=ey[0];
			*etat=(short)2;
			 }
                    }

		if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
	        if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
		if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
		if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;	

		MATRICE[*x][*y]=0;
		*x=tempx;
		*y=tempy;
		MATRICE[*x][*y]=1;
	         }
			     
/* pour les gellules n'ayant pas d'ennemis directement autour,
   j'elargit le cercle d'une case et je scanne a nouveau */

if (*etat==0)
   {
	nb_enemis=scan(*x,*y,2,0,ex,ey);
        nb_amis=scan(*x,*y,2,1,amx,amy);

        j=0;
        sortir=0;
	tempx=*x;
	tempy=*y;
	do{
	   if(ex[j]>*x) tempx++;
	   else if (ex[j]<*x) tempx--;
	
	   if(ey[j]>*y) tempy=*y + 1;
	   else if (ey[j]<*y) tempy--;

/* Si je vois une gelelule ennemi, je me dirige vers elle en me fusionnant */
/* Cela permet de couvrir les arrieres de la gelule qui attaque */
/* car si elle se fait attaquer, la gellule suiveuse pourra tuer l'ennemi */

	if ((MATRICE[tempx][tempy]==0) && (test_energie(4,charge)!=0))
           {
	    sortir=1;

	     if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
                if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
                if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
                if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;

	    MATRICE[*x][*y]=0;
	    *x=tempx;
	    *y=tempy;
	    MATRICE[*x][*y]=1;
	    *etat=(short)4;
	   }
	j++;
	if(j==nb_enemis) 
	  {
	   sortir=1;
	   *etat=(short)0;
	  }
       }while(sortir==0);
			
	     
/* Dans le cas ou la gellule est une gellule tueuse, des qu'elle voit un ennemi */
/* situe a 3 cases d'elle, elle lance l'attaque meme si elle n'est pas inquiete */
/* Alors que les gellules faisant partie d'une armee se contenteront de rejoindre */
/* leur position */


if ((etat==0) && (TAB_GEL[i].tueur==1))
    {
     nb_enemis=scan(*x,*y,3,0,ex,ey);
     nb_amis=scan(*x,*y,3,1,amx,amy);

        j=0;
        sortir=0;
        tempx=*x;
        tempy=*y;
        do{
           if(ex[j]>*x) tempx++;
           else if (ex[j]<*x) tempx--;

           if(ey[j]>*y) tempy=*y + 1;
           else if (ey[j]<*y) tempy--;
        if ((MATRICE[tempx][tempy]==0) && (test_energie(1,charge)!=0))
           {
            sortir=1;

	     if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
                if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
                if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
                if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;

  	    MATRICE[*x][*y]=0;
            *x=tempx;
            *y=tempy;
            MATRICE[*x][*y]=1;
            *etat=(short)1;
           }
        j++;
        if(j==nb_enemis)
          {
           sortir=1;
           *etat=(short)0;
          }
       }while(sortir==0);
     }

}
}
}



/************************************************************************/
/* Fontion qui regarde si un ennemi,un ami ou les 2 sont a proximite    */
/* d'une gellule.						        */
/* On choisit le test a faire par les parametres.			*/
/************************************************************************/

int scan(short x,short y,int indice,int nb_tab,short tx[],short ty[])
    {

   int compte=0,i=0;
   int temp=0,temp2=0;

       if(nb_tab!=1) 
	{
	if(ENNEMIS[(x+indice) % MAXX][y % MAXY]==1) 
	   { 
            tx[i]=(x+indice) % MAXX;
	    ty[i]=y % MAXY;
            compte++;
	    i++;
	   }
	 if(ENNEMIS[(x+indice) % MAXX][(y+indice) % MAXY]==1)
           {
            tx[i]=(x+indice) % MAXX;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
         temp=y-indice;
         if (temp<0) temp=temp+MAXY; 
	 if(ENNEMIS[(x+indice) % MAXX][temp]==1)
           {
            tx[i]=(x+indice) % MAXX;
            ty[i]=temp;
            compte++;
            i++;
           }
        temp=y-indice;
        if (temp<0) temp=temp+MAXY; 
 	if(ENNEMIS[x % MAXX][temp]==1)
           {
            tx[i]=x % MAXX;
            ty[i]=temp;
            compte++;
            i++;
           }
	 if(ENNEMIS[x % MAXX][(y+indice) % MAXY]==1)
           {
            tx[i]=x % MAXX;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
         temp=x-indice;
         if (temp<0) temp=temp+MAXX;
	 if(ENNEMIS[temp][y % MAXY]==1)
           {
            tx[i]=temp;
            ty[i]=y % MAXY;
            compte++;
            i++;
           }
	 temp=x-indice;
         if (temp<0) temp=temp+MAXX;
	 if(ENNEMIS[temp][(y+indice) % MAXY]==1)
           {
            tx[i]=temp;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
	 temp=x-indice;
         if (temp<0) temp=temp+MAXX;
         temp2=y-indice;
         if (temp2<0) temp2=temp2+MAXY;  
	 if(ENNEMIS[temp][temp2]==1)
           {
            tx[i]=temp;
            ty[i]=temp2;
            compte++;
            i++;
           }
	}
	
       if(nb_tab!=0)
	{
	if(MATRICE[(x+indice) % MAXX][y % MAXY]==1) 
	   { 
            tx[i]=(x+indice) % MAXX;
	    ty[i]=y % MAXY;
            compte++;
	    i++;
	   }
	 if(MATRICE[(x+indice) % MAXX][(y+indice) % MAXY]==1)
           {
            tx[i]=(x+indice) % MAXX;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
	 temp=y-indice;
         if (temp<0) temp=temp+MAXY;
	 if(MATRICE[(x+indice) % MAXX][temp]==1)
           {
            tx[i]=(x+indice) % MAXX;
            ty[i]=temp;
            compte++;
            i++;
           }
	 temp=y-indice;
         if (temp<0) temp=temp+MAXY;
 	if(MATRICE[x % MAXX][temp]==1)
           {
            tx[i]=x % MAXX;
            ty[i]=temp;
            compte++;
            i++;
           }
	 if(MATRICE[x % MAXX][(y+indice) % MAXY]==1)
           {
            tx[i]=x % MAXX;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
 	 temp=x-indice;
         if (temp<0) temp=temp+MAXX;
	 if(MATRICE[temp][y % MAXY]==1)
           {
            tx[i]=temp;
            ty[i]=y % MAXY;
            compte++;
            i++;
           }
	 temp=x-indice;
         if (temp<0) temp=temp+MAXX;
	 if(MATRICE[temp][(y+indice) % MAXY]==1)
           {
            tx[i]=temp;
            ty[i]=(y+indice) % MAXY;
            compte++;
            i++;
           }
	 temp=x-indice;
         if (temp<0) temp=temp+MAXX;
	 temp2=y-indice;
         if (temp2<0) temp2=temp2+MAXY;
	 if(MATRICE[temp][temp2]==1)
           {
            tx[i]=temp;
            ty[i]=temp2;
            compte++;
            i++;
           }
	}
	

return compte;

    }


/****************************************************************/
/* Fonction qui permet de tester si la charge d'une gellule     */
/* supportera l'action que je veus lui faire faire.		*/
/****************************************************************/

int test_energie(int i, short charge)
    {

	if ( (i==1) && (charge>2) ) return 1;
        if ( (i==2) && (charge>15) ) return 1;
        if ( (i==3) && (charge>42) ) return 1;
	if ( (i==4) && (charge>20) ) return 1;
	if ( (i==5) && (charge>20) ) return 1;
	if ( (i==6) && (charge>42) ) return 1;
    }


/**************************************************************/
/* Fonction qui permet de donner des ordres aux gellules qui  */
/* soit n'en n'ont pas encore recu, soit celles qui n'ont     */
/* pas d'ennemis visibles a proximite			      */
/**************************************************************/

int deplacement()
{

short *x,*y,tempx,tempy,a,b;
int i,sortir=0;

for (i=0;i<NB_GEL;i++)
{
x=&TAB_GEL[i].proprietes.x;
y=&TAB_GEL[i].proprietes.y;


if ((TAB_GEL[i].tueur==0) && (TAB_GEL[i].etat==0))
   {
   if (*x<TAB_GEL[i].but[0]) a=1;
   if (*x>TAB_GEL[i].but[0]) a=-1;
   if (*y>TAB_GEL[i].but[1]) b=-1;
   if (*y<TAB_GEL[1].but[1]) b=1;
   
   tempx=*x + a;
   tempy=*y + b;
  
   if (MATRICE[tempx][tempy]==1 || ENNEMIS[tempx][tempy]==1)
	{
	 if (a!=0 && b==0)
	    {
	     a=0;
	     b=1;
             tempx=*x + a;
             tempy=*y + b;
        	if (MATRICE[tempx][tempy]==1 || ENNEMIS[tempx][tempy]==1)
		   {
		    a=0;b=-1;tempx=*x + a;tempy=*y + b; 
		 if (MATRICE[tempx][tempy]==0 && ENNEMIS[tempx][tempy]==0)
                   {

		 if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
                if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
                if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
                if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;

 		    MATRICE[*x][*y]=0;
	            *x=tempx;
	   	    *y=tempy;
	 	    MATRICE[*x][*y]=1;
		    TAB_GEL[i].etat=(short)1;
		    sortir=1;
		   }
		  }
	    }

       	 if (a==0 && b!=0 && sortir==0)	
	    {
		 b=0;a=1;tempx=*x + a;tempy=*y + b;
                if (MATRICE[tempx][tempy]==1 || ENNEMIS[tempx][tempy]==1)
                   {
		    b=0;a=-1;tempx=*x + a;tempy=*y + b;
                 if (MATRICE[tempx][tempy]==0 && ENNEMIS[tempx][tempy]==0)
                   {	

		 if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
                if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
                if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
                if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;

                    MATRICE[*x][*y]=0;
                    *x=tempx;
                    *y=tempy;
                    MATRICE[*x][*y]=1;
	  	    TAB_GEL[i].etat=(short)1;
                    sortir=1;
                   }
		  }
            }

	  if (a!=0 && b!=0 && sortir==0)
            {
                 a=0;tempx=*x + a;tempy=*y + b;
                if (MATRICE[tempx][tempy]==1 || ENNEMIS[tempx][tempy]==1)
                   {
                    b=0;a=-1;tempx=*x + a;tempy=*y + b;
                 if (MATRICE[tempx][tempy]==0 && ENNEMIS[tempx][tempy]==0)
                   {

		 if (tempx > *x) TAB_GEL[i].direction=TAB_GEL[i].direction+4;
                if (tempx < *x) TAB_GEL[i].direction=TAB_GEL[i].direction+8;
                if (tempy > *y) TAB_GEL[i].direction=TAB_GEL[i].direction+2;
                if (tempy < *y) TAB_GEL[i].direction=TAB_GEL[i].direction+1;

                    MATRICE[*x][*y]=0;
                    *x=tempx;
                    *y=tempy;
                    MATRICE[*x][*y]=1;
		    TAB_GEL[i].etat=(short)1;
                    sortir=1;
                   }
		  }
            }

	}
	}
}
}



int ecrire_info()
{

int i;

for(i=0;i<NB_GEL;i++)
   {
     if(TAB_GEL[i].etat != 0)
     {
write(1,(*(char*)(&TAB_GEL[i].etat)+1),1);
write(1,&TAB_GELANC[i].proprietes.x,2);
write(1,&TAB_GELANC[i].proprietes.y,2);
write(1,(*(char*)(&TAB_GEL[i].direction)+1),1);
write(1,"\n",1);
     }

   }

write(1,"\n",1);
}
