|
本帖发表于 2016-10-17 16:26:36...阅读 141 人...加油,亲爱的楼主:[db:作者]
} /* 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) 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 with 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 is 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[TR]=30000||smelldisp
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 number 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=90&&randnum
|
|