}/* of have smell to drop * *
}/* of have food * *
Else
/* No food, looking for food * *
{
if (Ant[antnow). Smellamount[smell_type_home])
{
Smellnow = Smell[smell_type_home][antx][anty];
Smelltodrop = Ant[antnow]. Smellamount[smell_type_home]*smell_drop_rate;
if (smelltodrop>smellnow) smell[smell_type_home][antx][anty] = Smelltodrop;
/* Else smell[...] = Smellnow *
Ant[antnow]. smellamount[smell_type_home]-= Smelltodrop;
if (Ant[antnow). smellamount[smell_type_home]<0) Ant[antnow]. Smellamount[smell_type_home] = 0;
}/* of have smell to drop * *
}
}/* of time to go * *
/Else not go * *
}/* of for Antnow * *
TextColor (Food_home_color);
Gotoxy (HOME.XXX,HOME.YYY); Putch (Home_char);
Gotoxy (FOOD.XXX,FOOD.YYY);
if (food.amount>0) Putch (Food_char);
else Putch (FOOD_CHAR2);
TextColor (7);
Gotoxy (1,maxy+1);
printf ("Food%d, home%d", food.amount,home.amount);
}
void Antonestep (void)
{
int ddir,tttx,ttty;
int i;
Ddir = Ant[antnow].dir;
TTTX = ant[antnow].xxx; [Page]
Ttty = ant[antnow].yyy;
Ddir = Antnextdir (Tttx,ttty,ddir);
Switch (DDIR)
{
Case up:ttty--;
Break
Case down:ttty++;
Break
Case left:tttx--;
Break
Case right:tttx++;
Break
Default:break;
}/* of Switch dir * *
Ant[antnow].dir = Ddir;
Ant[antnow].xxx = TTTX;
ant[antnow].yyy = Ttty;
if (Ant[antnow].food)
/* This ant carry and food, search for home * *
{
if (TTTX==HOME.XXX&&TTTY==HOME.YYY)
{
home.amount++;
Antinitial ();
}
if (TTTX==FOOD.XXX&&TTTY==FOOD.YYY)
Ant[antnow]. Smellamount[smell_type_food] = Max_smell;
}/* of search for Home * *
Else
/* This ant are empty, search for food * *
{
if (TTTX==FOOD.XXX&&TTTY==FOOD.YYY)
{
if (food.amount>0)
{
Ant[antnow].food = 1;
food.amount--; [Page]
Ant[antnow]. Smellamount[smell_type_food] = Max_smell;
Ant[antnow]. Smellamount[smell_type_home] = 0;
Ant[antnow].dir = turnback (Ant[antnow].dir);
for (i=0;i<trace_remember;i++)
{
Ant[antnow].tracex[i] = 0;
Ant[antnow].tracey[i] = 0;
}
Ant[antnow]. traceptr = 0;
Canfindfood = 1;
}/* of still have food * *
}
if (TTTX==HOME.XXX&&TTTY==HOME.YYY)
Ant[antnow]. Smellamount[smell_type_home] = Max_smell;
}/* of search for food * *
}
void Dealkey (char key)
{
int i;
Switch (key)
{
Case ' P ': gettime (&endtime);
Dispplaytime ();
Getch ();
Gotoxy (1,maxy+1);
for (i=1;i<=maxx-1;i++) putch (space);
Break
Case ' t ': if (smelldispflag) [Page]
{
smelldispflag=0;
Clearsmelldisp ();
}
else Smelldispflag = 1;
Break
Case ' 1 ': Dispsmell (Smell_type_food);
Getch ();
Clearsmelldisp ();
Break
Case ' 2 ': Dispsmell (Smell_type_home);
Getch ();
Clearsmelldisp ();
Break
Case ' 3 ': Dispsmell (2);
Getch ();
Clearsmelldisp ();
Break
Case ' s ': Saveblock ();
Break
Case ' L ': loadblock ();
Break
Default:gotoxy (1,maxy+1);
for (i=1;i<=maxx-1;i++) putch (space);
}/* of Switch * *
}
void Clearsmelldisp (void)
{
int k,i,j;
for (k=0;k<=1;k++)
/* Smell TYPE FOOD and Home * *
for (i=1;i<=maxx;i++)
for (j=1;j<=maxy;j++)
{
if (Smell[k][i][j])
{
Gotoxy (I,J);
Putch (space);
}
}/* of one location * *
}
void Dispsmell (int type)
/* input:0-only display food smell
1--Only display home smell
2--Display both food and home smell
*/
{
int k,i,j;
int Fromk,tok;
int smelldisp;
Switch (type)
{
Case 0:fromk = 0;
Tok = 0;
Break
Case 1:fromk = 1;
Tok = 1;
Break
Case 2:fromk = 0;
Tok = 1;
Break
Default:fromk = 0;
Tok = 1;
Break [Page]
}
Smellgonetimer = 0;
for (k=fromk;k<=tok;k++)
/* Smell TYPE FOOD and Home * *
for (i=1;i<=maxx;i++)
for (j=1;j<=maxy;j++)
{
if (Smell[k][i][j])
{
Smelldisp = 1+ ((10*smell[k][i][j))/(max_smell*smell_drop_rate));
if (smelldisp>=30000| | smelldisp<0) Smelldisp = 30000;
Gotoxy (I,J);
if (i!=food.xxx| | J!=FOOD.YYY)
{
if ((i==food.xxx&&j==food.yyy) | | (I==HOME.XXX&&J==HOME.YYY))
/* Don t over write Food and home * *;
Else
{
if (smelldisp>9) putch (' # ');
else Putch (smelldisp+ ' 0 ');
}
}
}
}/* of one location * *
}
int antnextdir (int xxx,int yyy,int ddir)
{
int randnum;
int TestDir;
int cangostate;
int Cangof,cangol,cangor;
int msf,msl,msr,maxms;
int type;
Cangostate = Cango (Xxx,yyy,ddir);
if (cangostate==0| | cangostate==2| | cangostate==3| | cangostate==6) Cangof = 1;
else Cangof = 0;
if (cangostate==0| | cangostate==1| | cangostate==3| | cangostate==5) Cangol = 1;
else Cangol = 0;
if (cangostate==0| | cangostate==1| | cangostate==2| | cangostate==4) Cangor = 1;
else Cangor = 0;
if (ant[antnow].food) type = Smell_type_home;
else type = Smell_type_food;
MSF = Getmaxsmell (Type,xxx,yyy,ddir);
MSL = Getmaxsmell (Type,xxx,yyy,turnleft (Ddir));
Msr= Getmaxsmell (Type,xxx,yyy,turnright (Ddir));
MAXMS = Maxlocation (MSF,MSL,MSR);
/* Maxms-1-MSF is MAX
2-MSL is MAX
3-MSR is MAX
0-all 3 # is 0 * *
TestDir = NULL;
Switch (MAXMS)
{
Case 0:/* All is 0, keep TestDir = NULL, Random select dir * *
Break
Case 1:if (CANGOF)
TestDir = Ddir;
Else
if (MSL>MSR) if (cangol) TestDir = TurnLeft (Ddir);
else if (cangor) TestDir = TurnRight (Ddir);
Break
Case 2:if (Cangol)
TestDir = TurnLeft (Ddir);
Else
if (MSF>MSR) if (cangof) TestDir = Ddir;
else if (cangor) TestDir = TurnRight (Ddir);
Break
Case 3:if (Cangor)
TestDir = TurnRight (Ddir);
Else
if (MSF>MSL) if (cangof) TestDir =ddir;
else if (cangol) TestDir = TurnLeft (Ddir);
Break
Default:break;
}/* of MAXMS * *
Randnum = random (1000);
if (randnum<smell_drop_rate*1000| | Testdir==null)
/* 1. If TestDir = NULL, means can not find the max smell or the dir to max smell can
Then random select dir
2. If ant error, don ' t follow the smell, random select dir
*/
{
Randnum = random (100);
Switch (cangostate)
{
Case 0:if (randnum<90) TestDir = Ddir;
else if (randnum>=90&&randnum<95) TestDir = TurnLeft (Ddir);
else TestDir = TurnRight (Ddir);
Break
Case 1:if (randnum<50) TestDir = TurnLeft (Ddir);
else TestDir = TurnRight (Ddir);
Break
Case 2:if (randnum<90) TestDir = Ddir;
else TestDir = TurnRight (Ddir);
Break
Case 3:if (randnum<90) TestDir = Ddir;
else TestDir = TurnLeft (Ddir);
Break
Case 4:testdir = TurnRight (Ddir);
Break
Case 5:testdir = TurnLeft (Ddir);
Break
Case 6:testdir = Ddir;
Break
Case 7:testdir = turnback (Ddir);
Break
Default:testdir = turnback (Ddir);
}/* of can go state */
}
return (TESTDIR);
}
int Getmaxsmell (int type,int xxx,int yyy,int ddir)
{
int i,j;
int MS; * MAX Smell * *
ms = 0;
Switch (DDIR)
{
Case Up:for (i=xxx-ant_eyeshot;i<=xxx+ant_eyeshot;i++)
for (j=yyy-ant_eyeshot;j<yyy;j++)
{
if (! Judgecango (I,J)) continue;
if ((I==food.xxx&&j==food.yyy&&type==smell_type_food) | |
(I==home.xxx&&j==home.yyy&&type==smell_type_home))
{