Jacques
2013-05-03 12:03:56 UTC
Sometimes, I wonder whether there's a flaw in the language itself.
The problem is : In the function "initTermite", when I ask to print the "dir" of the termite, it prints a random number between 0 and 7 (just what I wanted)
but then, when I ask to print it again in the main function, I always get "0" or sometimes, weird numbers like "-179375438"...
why can't it just stay the way it was before in the initTermite function ??
Thanks in advance, id be really grateful if someone had the answer.
#include
#include
#include
// #define pour constantes, int x; pour variables globales
typedef struct {
int x;
int y;
} coord;
typedef struct {
coord pos;
int dir;
int brindille;
int TournerSurPlace;
} termite;
typedef struct {
coord c;
int brindille;
termite *termite;
} place;
place terrain[40][40];
int nbTermites;
termite termites[1600];
int proba(float p)
{
int x=rand()%1001;
return((x}
void placeVide(place *p)
{
p->brindille=0;
p->termite=NULL;
}
void poserBrindille(place *p)
{
p->brindille=1;
p->termite=NULL;
}
termite initTermite(termite *t, int a, int b)
{
int k=rand()%8;
t->pos.x=a;
t->pos.y=b;
t->dir=rand()%8;
printf("%d\n\n\n", t->dir);
t->brindille=0;
t->TournerSurPlace=0;
}
void poserTermite(place *p, termite *t)
{
p->termite=t;
p->brindille=0;
}
void affichePlace (place *p)
{
if (p->brindille==1)
printf("0");
else
if (p->termite==NULL)
printf(" ");
else
{
if(p->termite->dir==0 || p->termite->dir==4)
printf("|");
else
if(p->termite->dir==1 || p->termite->dir==5)
printf("/");
else
if(p->termite->dir==2 || p->termite->dir==6)
printf("-");
else
if(p->termite->dir==3 || p->termite->dir==7)
printf("T");
}
}
void afficheTerrain()
{int i;
int j;
int m;
for (j=0; j<=40; j++)
for(i=0; i<=40; i++)
{
affichePlace(&terrain[i][j]);
}
printf("\n");
}
void initTerrain()
{
int i;
int j;
int k;
k=0;
termite t;
for (j=0; j<=40; j++)
for(i=0; i<=40; i++)
{
placeVide(&terrain[i][j]);
if (proba(0.05))
poserBrindille(&terrain[i][j]);
else
if (proba(0.01))
{ termites[k]=initTermite(&t, i, j);
poserTermite(&terrain[i][j], &termites[k]);
k=k+1;}
}
nbTermites=k+1;
}
int main()
{int m;
srand(time(NULL));
initTerrain();
afficheTerrain();
printf("\n%d\n", nbTermites);
for (m=0; m<=nbTermites; m++)
printf("%d\n\n\n", termites[m].dir );
}