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 {
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);[Page]
} /* 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\');[Page]
}
}
}
} /* 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);[Page]
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 /* 1. if testdir = NULL, means can not find the max smell or the dir to max smell can not go
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);[Page]
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);
}
[Page]
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 {
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))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break; [Page]
case DOWN: for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
for(j=yyy+1;j<=yyy+ANT_EYESHOT;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))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
case LEFT: for(i=xxx-ANT_EYESHOT;i for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)[Page]
{
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))
{
ms = MAX_SMELL;
break;
} if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
{
if(!JudgeCanGo(i,j)) continue;[Page]
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
(i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
ms = MAX_SMELL;
break;
}
if(IsTrace(i,j)) continue;
if(Smell[type][i][j]>ms) ms = Smell[type][i][j];
}
break;
default: break;
}
return(ms);
}
int IsTrace(int xxx,int yyy)
{
int i;
for(i=0;i if(ant[AntNow].tracex[i]==xxx&&ant[AntNow].tracey[i]==yyy) return(1);
return(0);
}
int MaxLocation(int num1,int num2,int num3)
{
int maxnum;
if(num1==0&&num2==0&&num3==0) return(0);
maxnum = num1;
if(num2>maxnum) maxnum = num2;
if(num3>maxnum) maxnum = num3;
if(maxnum==num1) return(1);[Page]
if(maxnum==num2) return(2);
if(maxnum==num3) return(3);
}
int CanGo(int xxx,int yyy,int ddir)
/* input: xxx,yyy - location of ant
ddir - now dir
output: 0 - forward and left and right can go
1 - forward can not go
2 - left can not go
3 - right can not go
4 - forward and left can not go
5 - forward and right can not go
6 - left and right can not go
7 - forward and left and right all can not go
*/
{
int tx,ty,tdir;
int okf,okl,okr;
/* forward can go ? */
tdir = ddir;
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break;
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okf = 1;
else okf = 0;
/* turn left can go ? */
tdir = TurnLeft(ddir);
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break; [Page]
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okl = 1;
else okl = 0;
/* turn right can go ? */
tdir = TurnRight(ddir);
tx = xxx;
ty = yyy;
switch(tdir)
{
case UP: ty--;
break;
case DOWN: ty++;
break;
case LEFT: tx--;
break;
case RIGHT: tx++;
break;
default: break;
} /* of switch dir */
if(JudgeCanGo(tx,ty)) okr = 1;
else okr = 0;
if(okf&&okl&&okr) return(0);
if(!okf&&okl&&okr) return(1);
if(okf&&!okl&&okr) return(2);
if(okf&&okl&&!okr) return(3);
if(!okf&&!okl&&okr) return(4);
if(!okf&&okl&&!okr) return(5);
if(okf&&!okl&&!okr) return(6);
if(!okf&&!okl&&!okr) return(7);
return(7);[Page]
}
int JudgeCanGo(int xxx,int yyy)
/* input: location to judeg
output: 0 -- can not go
1 -- can go
*/
{
int i,j;
if(xxx<=0||xxx>MAXX) return(0);
if(yyy<=0||yyy>MAXY) return(0);
if(block[xxx][yyy]) return(0);
return(1);
}
int TurnLeft(int ddir)
{
switch(ddir)
{
case UP: return(LEFT);
case DOWN: return(RIGHT);
case LEFT: return(DOWN);
case RIGHT: return(UP);
default: break;
} /* of switch dir */
}
int TurnRight(int ddir)
{
switch(ddir)
{
case UP: return(RIGHT);
case DOWN: return(LEFT);
case LEFT: return(UP);
case RIGHT: return(DOWN);
default: break;
} /* of switch dir */
}
int TurnBack(int ddir)
{
switch(ddir)
{
case UP: return(DOWN);
case DOWN: return(UP);
case LEFT: return(RIGHT);
case RIGHT: return(LEFT);
default: break;
} /* of switch dir */
}
本文作者:佚名 来源:http://www.zxbc.cn/
CIO之家 www.ciozj.com 微信公众号:imciow