[backcolor=rgb(244, 244, 244) !important]/*ant.c*/
[backcolor=rgb(244, 244, 244) !important]#define SPACE 0x20 /*按键定义*/
#define ESC 0x1b
[backcolor=rgb(244, 244, 244) !important]#define ANT_CHAR_EMPTY '+'
#define ANT_CHAR_FOOD 153 /*携带食物的蚂蚁*/
[backcolor=rgb(244, 244, 244) !important]#define HOME_CHAR 'H'
#define FOOD_CHAR 'F'
[backcolor=rgb(244, 244, 244) !important]#define FOOD_CHAR2 'f'
#define FOOD_HOME_COLOR 12 /*红色*/
[backcolor=rgb(244, 244, 244) !important]#define BLOCK_CHAR 177 /*障碍物*/
[backcolor=rgb(244, 244, 244) !important]#define MAX_ANT 50 /*蚂蚁数量*/
#define INI_SPEED 3 /*速度半径为3*3*/
[backcolor=rgb(244, 244, 244) !important]#define MAXX 80 /*活动空间为80*23格*/
#define MAXY 23
[backcolor=rgb(244, 244, 244) !important]#define MAX_FOOD 10000 /*最大食物量*/
#define TARGET_FOOD 200 /*需要采集回家的食物量*/
[backcolor=rgb(244, 244, 244) !important]#define MAX_SMELL 5000 /*最大信息素*/
#define SMELL_DROP_RATE 0.05 /*信息素释放率*/
[backcolor=rgb(244, 244, 244) !important]#define ANT_ERROR_RATE 0.02 /*蚂蚁犯错率(创新率)*/
#define ANT_EYESHOT 3 /**/
[backcolor=rgb(244, 244, 244) !important]#define SMELL_GONE_SPEED 50 /*信息素消失速度*/
#define SMELL_GONE_RATE 0.05 /*信息素消失比率*/
[backcolor=rgb(244, 244, 244) !important]#define TRACE_REMEMBER 50 /*蚂蚁记忆力*/
#define MAX_BLOCK 100 /*最大障碍物个数*/
#define NULL 0
[backcolor=rgb(244, 244, 244) !important]#define UP 1 /*方向定义*/
#define DOWN 2
[backcolor=rgb(244, 244, 244) !important]#define LEFT 3
#define RIGHT 4
[backcolor=rgb(244, 244, 244) !important]#define SMELL_TYPE_FOOD 0 /*信息素类型定义*/
#define SMELL_TYPE_HOME 1
#include "stdio.h"
[backcolor=rgb(244, 244, 244) !important]#include "conio.h" /*getch函数需要此头文件*/
#include "dos.h"
[backcolor=rgb(244, 244, 244) !important]#include "stdlib.h"
#include "dos.h"
[backcolor=rgb(244, 244, 244) !important]#include "process.h"
#include "ctype.h"
[backcolor=rgb(244, 244, 244) !important]#include "math.h"
[backcolor=rgb(244, 244, 244) !important]void WorldInitial(void); /*环境初始化函数*/
void BlockInitial(void); /*障碍物地图初始化*/
[backcolor=rgb(244, 244, 244) !important]void CreatBlock(void); /*产生障碍物地图*/
void SaveBlock(void); /*保存障碍物地图*/
[backcolor=rgb(244, 244, 244) !important]void LoadBlock(void); /*载入障碍物地图*/
void HomeFoodInitial(void); /*食物与家的位置初始化*/
[backcolor=rgb(244, 244, 244) !important]void AntInitial(void); /*蚂蚁初始化*/
void WorldChange(void); /*更改环境*/
[backcolor=rgb(244, 244, 244) !important]void AntMove(void); /*蚂蚁移动*/
void AntOneStep(void); /*蚂蚁动作一步*/
[backcolor=rgb(244, 244, 244) !important]void DealKey(char key); /*按键扫描,功能键有p,t,1,2,3,s,l*/
void ClearSmellDisp(void); /*关闭信息素的显示*/
[backcolor=rgb(244, 244, 244) !important]void DispSmell(int type); /*显示当前信息素的情况*/
int AntNextDir(int xxx,int yyy,int ddir); /*得到蚂蚁下一次移动的方向*/
[backcolor=rgb(244, 244, 244) !important]int GetMaxSmell(int type,int xxx,int yyy,int ddir); /*获取最大信息素的值*/
int IsTrace(int xxx,int yyy); /*是否是曾经走过的路径*/
[backcolor=rgb(244, 244, 244) !important]int MaxLocation(int num1,int num2,int num3); /*获得三个值中最大的一个的序号*/
int CanGo(int xxx,int yyy,int ddir); /*返回可以走的路径*/
[backcolor=rgb(244, 244, 244) !important]int JudgeCanGo(int xxx,int yyy); /*判断某方向是否可以通过*/
int TurnLeft(int ddir); /*左传,右转,后退*/
[backcolor=rgb(244, 244, 244) !important]int TurnRight(int ddir);
int TurnBack(int ddir);
int MainTimer(void); /*返回自上次调用后经历了多少个10ms的时间*/
[backcolor=rgb(244, 244, 244) !important]char WaitForKey(int secnum); /*没有键则等待键盘输入,有键则返回键值*/
void DispPlayTime(void); /*显示运行时间*/
[backcolor=rgb(244, 244, 244) !important]int TimeUse(void); /*计算时间花费*/
void HideCur(void); /*隐藏鼠标*/
[backcolor=rgb(244, 244, 244) !important]void ResetCur(void); /*重置鼠标*/
[backcolor=rgb(244, 244, 244) !important]/* --------------- */
struct HomeStruct
[backcolor=rgb(244, 244, 244) !important]{
int xxx,yyy;
[backcolor=rgb(244, 244, 244) !important] int amount; /*已经搬运回家的食物数量*/
int TargetFood; /*需要搬运回家的食物数量*/
[backcolor=rgb(244, 244, 244) !important]}home;
[backcolor=rgb(244, 244, 244) !important]struct FoodStruct
{
[backcolor=rgb(244, 244, 244) !important] int xxx,yyy;
int amount; /*剩余食物数量*/
[backcolor=rgb(244, 244, 244) !important]}food;
[backcolor=rgb(244, 244, 244) !important]struct AntStruct
{
[backcolor=rgb(244, 244, 244) !important] int xxx,yyy; /*蚂蚁当前位置*/
int dir; /*行走方向*/
[backcolor=rgb(244, 244, 244) !important] int speed; /*蚂蚁速度,即计数器计到此则移动一步,所以越小蚂蚁移动越快*/
int SpeedTimer; /*速度计数器,每10ms记一次*/
[backcolor=rgb(244, 244, 244) !important] int food; /*是否携带食物*/
int SmellAmount[2]; /*两种信息素的含量*/
[backcolor=rgb(244, 244, 244) !important] int tracex[TRACE_REMEMBER]; /*所记忆的x坐标*/
int tracey[TRACE_REMEMBER]; /*所记忆的y坐标*/
[backcolor=rgb(244, 244, 244) !important] int TracePtr; /*记录路径所用的指针*/
int IQ; /*好象没有用到。。。。。*/
[backcolor=rgb(244, 244, 244) !important]}ant[MAX_ANT];
[backcolor=rgb(244, 244, 244) !important]/*全局变量定义*/
int AntNow; /*当前蚂蚁*/
[backcolor=rgb(244, 244, 244) !important]int timer10ms; /*记录多少个10ms已经过去*/
struct time starttime,endtime; /*起始结束时间定义*/
[backcolor=rgb(244, 244, 244) !important]int Smell[2][MAXX+1][MAXY+1]; /*信息素数组*/
int block[MAXX+1][MAXY+1]; /*障碍物数组*/
[backcolor=rgb(244, 244, 244) !important]int SmellGoneTimer; /*信息素消失计数器*/
int SmellDispFlag; /*信息素显示标志*/
[backcolor=rgb(244, 244, 244) !important]int CanFindFood; /*可以找到获取食物的路径标志*/
int HardtoFindPath; /*找到路径比较困难的标志*/
/* ----- Main -------- */
[backcolor=rgb(244, 244, 244) !important]void main(void)
{
[backcolor=rgb(244, 244, 244) !important] char KeyPress;
int tu;
clrscr();
[backcolor=rgb(244, 244, 244) !important] HideCur();
WorldInitial();
[backcolor=rgb(244, 244, 244) !important] do
{
[backcolor=rgb(244, 244, 244) !important] timer10ms = MainTimer();
if(timer10ms) AntMove();
[backcolor=rgb(244, 244, 244) !important] if(timer10ms) WorldChange();
tu = TimeUse();
[backcolor=rgb(244, 244, 244) !important] if(tu>=60&&!CanFindFood)
{
[backcolor=rgb(244, 244, 244) !important] gotoxy(1,MAXY+1);
printf("Can not find food, maybe a block world.");
[backcolor=rgb(244, 244, 244) !important] WaitForKey(10);
WorldInitial();
[backcolor=rgb(244, 244, 244) !important] }
if(tu>=180&&home.amount<100&&!HardtoFindPath)
[backcolor=rgb(244, 244, 244) !important] {
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("God! it is so difficult to find a path.");
if(WaitForKey(10)==0x0d) WorldInitial();
[backcolor=rgb(244, 244, 244) !important] else
{
[backcolor=rgb(244, 244, 244) !important] HardtoFindPath = 1;
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf(" ");
}
[backcolor=rgb(244, 244, 244) !important] }
if(home.amount>=home.TargetFood)
[backcolor=rgb(244, 244, 244) !important] {
gettime(&endtime);
[backcolor=rgb(244, 244, 244) !important] KeyPress = WaitForKey(60);
DispPlayTime();
[backcolor=rgb(244, 244, 244) !important] WaitForKey(10);
WorldInitial();
[backcolor=rgb(244, 244, 244) !important] }
else if(kbhit())
[backcolor=rgb(244, 244, 244) !important] {
KeyPress = getch();
[backcolor=rgb(244, 244, 244) !important] DealKey(KeyPress);
}
[backcolor=rgb(244, 244, 244) !important] else KeyPress = NULL;
}
[backcolor=rgb(244, 244, 244) !important] while(KeyPress!=ESC);
gettime(&endtime);
[backcolor=rgb(244, 244, 244) !important] DispPlayTime();
WaitForKey(10);
[backcolor=rgb(244, 244, 244) !important] clrscr();
ResetCur();
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]/* ------ general sub process ----------- */
int MainTimer(void)
[backcolor=rgb(244, 244, 244) !important]/* output: how much 10ms have pass from last time call this process */
{
[backcolor=rgb(244, 244, 244) !important] static int oldhund,oldsec;
struct time t;
[backcolor=rgb(244, 244, 244) !important] int timeuse;
[backcolor=rgb(244, 244, 244) !important] gettime(&t);
timeuse = 0;
[backcolor=rgb(244, 244, 244) !important] if(t.ti_hund!=oldhund)
{
[backcolor=rgb(244, 244, 244) !important] if(t.ti_sec!=oldsec)
{
[backcolor=rgb(244, 244, 244) !important] timeuse+=100;
oldsec = t.ti_sec;
[backcolor=rgb(244, 244, 244) !important] }
timeuse+=t.ti_hund-oldhund;
[backcolor=rgb(244, 244, 244) !important] oldhund = t.ti_hund;
}
[backcolor=rgb(244, 244, 244) !important] else timeuse = 0;
return (timeuse);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]char WaitForKey(int secnum)
/* funtion: if have key in, exit immediately, else wait 'secnum' senconds then exit
[backcolor=rgb(244, 244, 244) !important] input: secnum -- wait this senconds, must < 3600 (1 hour)
output: key char, if no key in(exit when timeout), return NULL */
[backcolor=rgb(244, 244, 244) !important]{
int secin,secnow;
[backcolor=rgb(244, 244, 244) !important] int minin,minnow;
int hourin,hournow;
[backcolor=rgb(244, 244, 244) !important] int secuse;
struct time t;
gettime(&t);
[backcolor=rgb(244, 244, 244) !important] secin = t.ti_sec;
minin = t.ti_min;
[backcolor=rgb(244, 244, 244) !important] hourin = t.ti_hour;
[backcolor=rgb(244, 244, 244) !important] do
{
[backcolor=rgb(244, 244, 244) !important] if(kbhit()) return(getch());
gettime(&t);
[backcolor=rgb(244, 244, 244) !important] secnow = t.ti_sec;
minnow = t.ti_min;
[backcolor=rgb(244, 244, 244) !important] hournow = t.ti_hour;
[backcolor=rgb(244, 244, 244) !important] if(hournow!=hourin) minnow+=60;
if(minnow>minin) secuse = (minnow-1-minin) + (secnow+60-secin);
[backcolor=rgb(244, 244, 244) !important] else secuse = secnow - secin;
[backcolor=rgb(244, 244, 244) !important] /* counting error check */
if(secuse<0)
[backcolor=rgb(244, 244, 244) !important] {
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("Time conuting error, any keyto exit...");
getch();
[backcolor=rgb(244, 244, 244) !important] exit(3);
}
[backcolor=rgb(244, 244, 244) !important] }
while(secuse<=secnum);
[backcolor=rgb(244, 244, 244) !important] return (NULL);
}
void DispPlayTime(void)
[backcolor=rgb(244, 244, 244) !important]{
int ph,pm,ps;
ph = endtime.ti_hour - starttime.ti_hour;
[backcolor=rgb(244, 244, 244) !important] pm = endtime.ti_min - starttime.ti_min;
ps = endtime.ti_sec - starttime.ti_sec;
if(ph<0) ph+=24;
[backcolor=rgb(244, 244, 244) !important] if(pm<0) { ph--; pm+=60; }
if(ps<0) { pm--; ps+=60; }
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("Time use: %d hour- %d min- %d sec ",ph,pm,ps);
}
int TimeUse(void)
[backcolor=rgb(244, 244, 244) !important]{
int ph,pm,ps;
gettime(&endtime);
[backcolor=rgb(244, 244, 244) !important] ph = endtime.ti_hour - starttime.ti_hour;
pm = endtime.ti_min - starttime.ti_min;
[backcolor=rgb(244, 244, 244) !important] ps = endtime.ti_sec - starttime.ti_sec;
[backcolor=rgb(244, 244, 244) !important] if(ph<0) ph+=24;
if(pm<0) { ph--; pm+=60; }
[backcolor=rgb(244, 244, 244) !important] if(ps<0) { pm--; ps+=60; }
[backcolor=rgb(244, 244, 244) !important] return(ps+(60*(pm+60*ph)));
}
void HideCur(void)
[backcolor=rgb(244, 244, 244) !important]{
union REGS regs0;
[backcolor=rgb(244, 244, 244) !important] regs0.h.ah=1;
regs0.h.ch=0x30;
[backcolor=rgb(244, 244, 244) !important] regs0.h.cl=0x31;
int86(0x10,®s0,®s0);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void ResetCur(void)
{
[backcolor=rgb(244, 244, 244) !important] union REGS regs0;
regs0.h.ah=1;
[backcolor=rgb(244, 244, 244) !important] regs0.h.ch=0x06;
regs0.h.cl=0x07;
[backcolor=rgb(244, 244, 244) !important] int86(0x10,®s0,®s0);
}
/* ------------ main ANT programe ------------- */
[backcolor=rgb(244, 244, 244) !important]void WorldInitial(void)
{
[backcolor=rgb(244, 244, 244) !important] int k,i,j;
randomize();
[backcolor=rgb(244, 244, 244) !important] clrscr();
HomeFoodInitial();
[backcolor=rgb(244, 244, 244) !important] for(AntNow=0;AntNow<MAX_ANT;AntNow++)
{
[backcolor=rgb(244, 244, 244) !important] AntInitial();
} /* of for AntNow */;
BlockInitial();
[backcolor=rgb(244, 244, 244) !important] for(k=0;k<=1;k++)
/* SMELL TYPE FOOD and HOME */
[backcolor=rgb(244, 244, 244) !important] for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
[backcolor=rgb(244, 244, 244) !important] Smell[k][j] = 0;
SmellGoneTimer = 0;
[backcolor=rgb(244, 244, 244) !important] gettime(&starttime);
SmellDispFlag = 0;
[backcolor=rgb(244, 244, 244) !important] CanFindFood = 0;
HardtoFindPath = 0;
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void BlockInitial(void)
{
[backcolor=rgb(244, 244, 244) !important] int i,j;
int bn;
for(i=0;i<=MAXX;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=0;j<=MAXY;j++)
block[j] = 0;
bn = 1+ MAX_BLOCK/2 + random(MAX_BLOCK/2);
[backcolor=rgb(244, 244, 244) !important] for(i=0;i<=bn;i++) CreatBlock();
}
void CreatBlock(void)
[backcolor=rgb(244, 244, 244) !important]{
int x1,y1,x2,y2;
[backcolor=rgb(244, 244, 244) !important] int dx,dy;
int i,j;
x1 = random(MAXX)+1;
[backcolor=rgb(244, 244, 244) !important] y1 = random(MAXY)+1;
[backcolor=rgb(244, 244, 244) !important] dx = random(MAXX/10)+1;
dy = random(MAXY/10)+1;
x2 = x1+dx;
[backcolor=rgb(244, 244, 244) !important] y2 = y1+dy;
[backcolor=rgb(244, 244, 244) !important] if(x2>MAXX) x2 = MAXX;
if(y2>MAXY) y2 = MAXY;
if(food.xxx>=x1&&food.xxx<=x2&&food.yyy>=y1&&food.yyy<=y2) return;
[backcolor=rgb(244, 244, 244) !important] if(home.xxx>=x1&&home.xxx<=x2&&home.yyy>=y1&&home.yyy<=y2) return;
[backcolor=rgb(244, 244, 244) !important] for(i=x1;i<=x2;i++)
for(j=y1;j<=y2;j++)
[backcolor=rgb(244, 244, 244) !important] {
block[j] = 1;
[backcolor=rgb(244, 244, 244) !important] gotoxy(i,j);
putch(BLOCK_CHAR);
[backcolor=rgb(244, 244, 244) !important] }
}
void SaveBlock(void)
[backcolor=rgb(244, 244, 244) !important]{
FILE *fp_block;
[backcolor=rgb(244, 244, 244) !important] char FileNameBlock[20];
int i,j;
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf(" ");
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("Save to file...",FileNameBlock);
gets(FileNameBlock);
[backcolor=rgb(244, 244, 244) !important] if(FileNameBlock[0]==0) strcpy(FileNameBlock,"Ant.ant");
else strcat(FileNameBlock,".ant");
if ((fp_block = fopen(FileNameBlock, "wb")) == NULL)
[backcolor=rgb(244, 244, 244) !important] { gotoxy(1,MAXY+1);
printf("Creat file %s fail...",FileNameBlock);
[backcolor=rgb(244, 244, 244) !important] getch();
exit(2);
[backcolor=rgb(244, 244, 244) !important] }
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf(" ");
[backcolor=rgb(244, 244, 244) !important] fputc(home.xxx,fp_block);
fputc(home.yyy,fp_block);
[backcolor=rgb(244, 244, 244) !important] fputc(food.xxx,fp_block);
fputc(food.yyy,fp_block);
for(i=0;i<=MAXX;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=0;j<=MAXY;j++)
fputc(block[j],fp_block);
fclose(fp_block);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void LoadBlock(void)
{
[backcolor=rgb(244, 244, 244) !important] FILE *fp_block;
char FileNameBlock[20];
[backcolor=rgb(244, 244, 244) !important] int i,j,k;
[backcolor=rgb(244, 244, 244) !important] gotoxy(1,MAXY+1);
printf(" ");
[backcolor=rgb(244, 244, 244) !important] gotoxy(1,MAXY+1);
printf("Load file...",FileNameBlock);
[backcolor=rgb(244, 244, 244) !important] gets(FileNameBlock);
if(FileNameBlock[0]==0) strcpy(FileNameBlock,"Ant.ant");
[backcolor=rgb(244, 244, 244) !important] else strcat(FileNameBlock,".ant");
[backcolor=rgb(244, 244, 244) !important] if ((fp_block = fopen(FileNameBlock, "rb")) == NULL)
{ gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("Open file %s fail...",FileNameBlock);
getch();
[backcolor=rgb(244, 244, 244) !important] exit(2);
}
clrscr();
[backcolor=rgb(244, 244, 244) !important] home.xxx = fgetc(fp_block);
home.yyy = fgetc(fp_block);
[backcolor=rgb(244, 244, 244) !important] food.xxx = fgetc(fp_block);
food.yyy = fgetc(fp_block);
[backcolor=rgb(244, 244, 244) !important] gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
gotoxy(food.xxx,food.yyy); putch(FOOD_CHAR);
[backcolor=rgb(244, 244, 244) !important] food.amount = random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
/* food.amount = MAX_FOOD; */
[backcolor=rgb(244, 244, 244) !important] home.amount = 0;
home.TargetFood =
[backcolor=rgb(244, 244, 244) !important] (food.amount<TARGET_FOOD)?food.amount:TARGET_FOOD;
[backcolor=rgb(244, 244, 244) !important] for(AntNow=0;AntNow<MAX_ANT;AntNow++)
{
[backcolor=rgb(244, 244, 244) !important] AntInitial();
} /* of for AntNow */;
for(i=0;i<=MAXX;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=0;j<=MAXY;j++)
{
[backcolor=rgb(244, 244, 244) !important] block[j] = fgetc(fp_block);
if(block[j])
[backcolor=rgb(244, 244, 244) !important] {
gotoxy(i,j);
[backcolor=rgb(244, 244, 244) !important] putch(BLOCK_CHAR);
}
[backcolor=rgb(244, 244, 244) !important] }
[backcolor=rgb(244, 244, 244) !important] for(k=0;k<=1;k++)
/* SMELL TYPE FOOD and HOME */
[backcolor=rgb(244, 244, 244) !important] for(i=0;i<=MAXX;i++)
for(j=0;j<=MAXY;j++)
[backcolor=rgb(244, 244, 244) !important] Smell[k][j] = 0;
SmellGoneTimer = 0;
[backcolor=rgb(244, 244, 244) !important] gettime(&starttime);
SmellDispFlag = 0;
[backcolor=rgb(244, 244, 244) !important] CanFindFood = 0;
HardtoFindPath = 0;
fclose(fp_block);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void HomeFoodInitial(void)
{
[backcolor=rgb(244, 244, 244) !important] int randnum;
int homeplace;
[backcolor=rgb(244, 244, 244) !important] /* 1 -- home at left-up, food at right-down
-- home at left-down, food at right-up
[backcolor=rgb(244, 244, 244) !important] -- home at right-up, food at left-down
-- home at right-down, food at left-up */
randnum = random(100);
[backcolor=rgb(244, 244, 244) !important] if(randnum<25) homeplace = 1;
else if (randnum>=25&&randnum<50) homeplace = 2;
[backcolor=rgb(244, 244, 244) !important] else if (randnum>=50&&randnum<75) homeplace = 3;
else homeplace = 4;
switch(homeplace)
[backcolor=rgb(244, 244, 244) !important] {
case 1: home.xxx = random(MAXX/3)+1;
[backcolor=rgb(244, 244, 244) !important] home.yyy = random(MAXY/3)+1;
food.xxx = random(MAXX/3)+2*MAXX/3+1;
[backcolor=rgb(244, 244, 244) !important] food.yyy = random(MAXY/3)+2*MAXY/3+1;
break;
[backcolor=rgb(244, 244, 244) !important] case 2: home.xxx = random(MAXX/3)+1;
home.yyy = random(MAXY/3)+2*MAXY/3+1;
[backcolor=rgb(244, 244, 244) !important] food.xxx = random(MAXX/3)+2*MAXX/3+1;
food.yyy = random(MAXY/3)+1;
[backcolor=rgb(244, 244, 244) !important] break;
case 3: home.xxx = random(MAXX/3)+2*MAXX/3+1;
[backcolor=rgb(244, 244, 244) !important] home.yyy = random(MAXY/3)+1;
food.xxx = random(MAXX/3)+1;
[backcolor=rgb(244, 244, 244) !important] food.yyy = random(MAXY/3)+2*MAXY/3+1;
break;
[backcolor=rgb(244, 244, 244) !important] case 4: home.xxx = random(MAXX/3)+2*MAXX/3+1;
home.yyy = random(MAXY/3)+2*MAXY/3+1;
[backcolor=rgb(244, 244, 244) !important] food.xxx = random(MAXX/3)+1;
food.yyy = random(MAXY/3)+1;
[backcolor=rgb(244, 244, 244) !important] break;
}
food.amount = random(MAX_FOOD/3)+2*MAX_FOOD/3+1;
[backcolor=rgb(244, 244, 244) !important] /* food.amount = MAX_FOOD; */
home.amount = 0;
[backcolor=rgb(244, 244, 244) !important] home.TargetFood = (food.amount<TARGET_FOOD)?food.amount:TARGET_FOOD;
[backcolor=rgb(244, 244, 244) !important] /* data correctness check */
if(home.xxx<=0||home.xxx>MAXX||home.yyy<=0||home.yyy>MAXY||
[backcolor=rgb(244, 244, 244) !important] food.xxx<=0||food.xxx>MAXX||food.yyy<=0||food.yyy>MAXY||
food.amount<=0)
[backcolor=rgb(244, 244, 244) !important] {
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("World initial fail, any key to exit...");
getch();
[backcolor=rgb(244, 244, 244) !important] exit(2);
}
gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
[backcolor=rgb(244, 244, 244) !important] gotoxy(food.xxx,food.yyy); putch(FOOD_CHAR);
}void AntInitial(void)
[backcolor=rgb(244, 244, 244) !important]/* initial ant[AntNow] */
{
[backcolor=rgb(244, 244, 244) !important] int randnum;
int i;
ant[AntNow].xxx = home.xxx;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].yyy = home.yyy;
[backcolor=rgb(244, 244, 244) !important] randnum = random(100);
if(randnum<25) ant[AntNow].dir = UP;
[backcolor=rgb(244, 244, 244) !important] else if (randnum>=25&&randnum<50) ant[AntNow].dir = DOWN;
else if (randnum>=50&&randnum<75) ant[AntNow].dir = LEFT;
[backcolor=rgb(244, 244, 244) !important] else ant[AntNow].dir = RIGHT;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].speed = 2*(random(INI_SPEED/2)+1);
ant[AntNow].SpeedTimer = 0;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].food = 0;
ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = MAX_SMELL;
ant[AntNow].IQ = 1;
for(i=0;i<TRACE_REMEMBER;i++)
[backcolor=rgb(244, 244, 244) !important] {
ant[AntNow].tracex = 0;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].tracey = 0;
}
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].TracePtr = 0;
[backcolor=rgb(244, 244, 244) !important] /* a sepecail ant */
if(AntNow==0) ant[AntNow].speed = INI_SPEED;
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void WorldChange(void)
{
[backcolor=rgb(244, 244, 244) !important] int k,i,j;
int smelldisp;
SmellGoneTimer+=timer10ms;
[backcolor=rgb(244, 244, 244) !important] if(SmellGoneTimer>=SMELL_GONE_SPEED)
{
[backcolor=rgb(244, 244, 244) !important] SmellGoneTimer = 0;
for(k=0;k<=1;k++)
[backcolor=rgb(244, 244, 244) !important] /* SMELL TYPE FOOD and HOME */
for(i=1;i<=MAXX;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=1;j<=MAXY;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(Smell[k][j])
{
[backcolor=rgb(244, 244, 244) !important] smelldisp = 1+((10*Smell[k][j])/(MAX_SMELL*SMELL_DROP_RATE));
if(smelldisp>=30000||smelldisp<0) smelldisp = 30000;
[backcolor=rgb(244, 244, 244) !important] if(SmellDispFlag)
{
[backcolor=rgb(244, 244, 244) !important] gotoxy(i,j);
if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy))
[backcolor=rgb(244, 244, 244) !important] /* don't over write Food and Home */;
else
[backcolor=rgb(244, 244, 244) !important] {
if(smelldisp>9) putch('#');
[backcolor=rgb(244, 244, 244) !important] else putch(smelldisp+'0');
}
[backcolor=rgb(244, 244, 244) !important] }
Smell[k][j]-= 1+(Smell[k][j]*SMELL_GONE_RATE);
[backcolor=rgb(244, 244, 244) !important] if(Smell[k][j]<0) Smell[k][j] = 0;
if(SmellDispFlag)
[backcolor=rgb(244, 244, 244) !important] {
if(Smell[k][j]<=2)
[backcolor=rgb(244, 244, 244) !important] {
gotoxy(i,j);
[backcolor=rgb(244, 244, 244) !important] putch(SPACE);
}
[backcolor=rgb(244, 244, 244) !important] }
}
[backcolor=rgb(244, 244, 244) !important] } /* of one location */
} /* of time to change the world */
[backcolor=rgb(244, 244, 244) !important]} /* of world change */
[backcolor=rgb(244, 244, 244) !important]void AntMove(void)
{
[backcolor=rgb(244, 244, 244) !important] int antx,anty;
int smelltodrop,smellnow;
for(AntNow=0;AntNow<MAX_ANT;AntNow++)
[backcolor=rgb(244, 244, 244) !important] {
ant[AntNow].SpeedTimer+=timer10ms;
[backcolor=rgb(244, 244, 244) !important] if(ant[AntNow].SpeedTimer>=ant[AntNow].speed)
{
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SpeedTimer = 0;
gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
[backcolor=rgb(244, 244, 244) !important] putch(SPACE);
AntOneStep();
[backcolor=rgb(244, 244, 244) !important] gotoxy(ant[AntNow].xxx,ant[AntNow].yyy);
/* ant0 is a sepecail ant, use different color */
[backcolor=rgb(244, 244, 244) !important] if(AntNow==0) textcolor(0xd);
if(ant[AntNow].food) putch(ANT_CHAR_FOOD);
[backcolor=rgb(244, 244, 244) !important] else putch(ANT_CHAR_EMPTY);
if(AntNow==0) textcolor(0x7);
/* remember trace */
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].tracex[ant[AntNow].TracePtr] = ant[AntNow].xxx;
ant[AntNow].tracey[ant[AntNow].TracePtr] = ant[AntNow].yyy;
[backcolor=rgb(244, 244, 244) !important] if(++(ant[AntNow].TracePtr)>=TRACE_REMEMBER) ant[AntNow].TracePtr = 0;
[backcolor=rgb(244, 244, 244) !important] /* drop smell */
antx = ant[AntNow].xxx;
[backcolor=rgb(244, 244, 244) !important] anty = ant[AntNow].yyy;
[backcolor=rgb(244, 244, 244) !important] if(ant[AntNow].food)
/* have food, looking for home */
[backcolor=rgb(244, 244, 244) !important] {
if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD])
[backcolor=rgb(244, 244, 244) !important] {
smellnow = Smell[SMELL_TYPE_FOOD][antx][anty];
[backcolor=rgb(244, 244, 244) !important] smelltodrop = ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]*SMELL_DROP_RATE;
if(smelltodrop>smellnow) Smell[SMELL_TYPE_FOOD][antx][anty] = smelltodrop;
[backcolor=rgb(244, 244, 244) !important] /* else Smell[...] = smellnow */
ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]-= smelltodrop;
[backcolor=rgb(244, 244, 244) !important] if(ant[AntNow].SmellAmount[SMELL_TYPE_FOOD]<0) ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = 0;
} /* of have smell to drop */
[backcolor=rgb(244, 244, 244) !important] } /* of have food */
else
[backcolor=rgb(244, 244, 244) !important] /* no food, looking for food */
{
[backcolor=rgb(244, 244, 244) !important] if(ant[AntNow].SmellAmount[SMELL_TYPE_HOME])
{
[backcolor=rgb(244, 244, 244) !important] smellnow = Smell[SMELL_TYPE_HOME][antx][anty];
smelltodrop = ant[AntNow].SmellAmount[SMELL_TYPE_HOME]*SMELL_DROP_RATE;
[backcolor=rgb(244, 244, 244) !important] if(smelltodrop>smellnow) Smell[SMELL_TYPE_HOME][antx][anty] = smelltodrop;
/* else Smell[...] = smellnow */
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SmellAmount[SMELL_TYPE_HOME]-= smelltodrop;
if(ant[AntNow].SmellAmount[SMELL_TYPE_HOME]<0) ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = 0;
[backcolor=rgb(244, 244, 244) !important] } /* of have smell to drop */
}
[backcolor=rgb(244, 244, 244) !important] } /* of time to go */
/* else not go */
[backcolor=rgb(244, 244, 244) !important] } /* of for AntNow */
[backcolor=rgb(244, 244, 244) !important] textcolor(FOOD_HOME_COLOR);
gotoxy(home.xxx,home.yyy); putch(HOME_CHAR);
[backcolor=rgb(244, 244, 244) !important] gotoxy(food.xxx,food.yyy);
if(food.amount>0) putch(FOOD_CHAR);
[backcolor=rgb(244, 244, 244) !important] else putch(FOOD_CHAR2);
textcolor(7);
gotoxy(1,MAXY+1);
[backcolor=rgb(244, 244, 244) !important] printf("Food %d, Home %d ",food.amount,home.amount);
}
void AntOneStep(void)
[backcolor=rgb(244, 244, 244) !important]{
int ddir,tttx,ttty;
[backcolor=rgb(244, 244, 244) !important] int i;
[backcolor=rgb(244, 244, 244) !important] ddir = ant[AntNow].dir;
tttx = ant[AntNow].xxx;
[backcolor=rgb(244, 244, 244) !important] ttty = ant[AntNow].yyy;
[backcolor=rgb(244, 244, 244) !important] ddir = AntNextDir(tttx,ttty,ddir);
[backcolor=rgb(244, 244, 244) !important] switch(ddir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: ttty--;
break;
[backcolor=rgb(244, 244, 244) !important] case DOWN: ttty++;
break;
[backcolor=rgb(244, 244, 244) !important] case LEFT: tttx--;
break;
[backcolor=rgb(244, 244, 244) !important] case RIGHT: tttx++;
break;
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
ant[AntNow].dir = ddir;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].xxx = tttx;
ant[AntNow].yyy = ttty;
if(ant[AntNow].food)
[backcolor=rgb(244, 244, 244) !important] /* this ant carry with food, search for home */
{
[backcolor=rgb(244, 244, 244) !important] if(tttx==home.xxx&&ttty==home.yyy)
{
[backcolor=rgb(244, 244, 244) !important] home.amount++;
AntInitial();
[backcolor=rgb(244, 244, 244) !important] }
if(tttx==food.xxx&&ttty==food.yyy)
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = MAX_SMELL;
} /* of search for home */
[backcolor=rgb(244, 244, 244) !important] else
/* this ant is empty, search for food */
[backcolor=rgb(244, 244, 244) !important] {
if(tttx==food.xxx&&ttty==food.yyy)
[backcolor=rgb(244, 244, 244) !important] {
if(food.amount>0)
[backcolor=rgb(244, 244, 244) !important] {
ant[AntNow].food = 1;
[backcolor=rgb(244, 244, 244) !important] food.amount--;
ant[AntNow].SmellAmount[SMELL_TYPE_FOOD] = MAX_SMELL;
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = 0;
ant[AntNow].dir = TurnBack(ant[AntNow].dir);
[backcolor=rgb(244, 244, 244) !important] for(i=0;i<TRACE_REMEMBER;i++)
{
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].tracex = 0;
ant[AntNow].tracey = 0;
[backcolor=rgb(244, 244, 244) !important] }
ant[AntNow].TracePtr = 0;
[backcolor=rgb(244, 244, 244) !important] CanFindFood = 1;
} /* of still have food */
[backcolor=rgb(244, 244, 244) !important] }
if(tttx==home.xxx&&ttty==home.yyy)
[backcolor=rgb(244, 244, 244) !important] ant[AntNow].SmellAmount[SMELL_TYPE_HOME] = MAX_SMELL;
} /* of search for food */
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void DealKey(char key)
{
[backcolor=rgb(244, 244, 244) !important] int i;
switch(key)
[backcolor=rgb(244, 244, 244) !important] {
case 'p': gettime(&endtime);
[backcolor=rgb(244, 244, 244) !important] DispPlayTime();
getch();
[backcolor=rgb(244, 244, 244) !important] gotoxy(1,MAXY+1);
for(i=1;i<=MAXX-1;i++) putch(SPACE);
[backcolor=rgb(244, 244, 244) !important] break;
case 't': if(SmellDispFlag)
[backcolor=rgb(244, 244, 244) !important] {
SmellDispFlag=0;
[backcolor=rgb(244, 244, 244) !important] ClearSmellDisp();
}
[backcolor=rgb(244, 244, 244) !important] else SmellDispFlag = 1;
break;
[backcolor=rgb(244, 244, 244) !important] case '1': DispSmell(SMELL_TYPE_FOOD);
getch();
[backcolor=rgb(244, 244, 244) !important] ClearSmellDisp();
break;
[backcolor=rgb(244, 244, 244) !important] case '2': DispSmell(SMELL_TYPE_HOME);
getch();
[backcolor=rgb(244, 244, 244) !important] ClearSmellDisp();
break;
[backcolor=rgb(244, 244, 244) !important] case '3': DispSmell(2);
getch();
[backcolor=rgb(244, 244, 244) !important] ClearSmellDisp();
break;
[backcolor=rgb(244, 244, 244) !important] case 's': SaveBlock();
break;
[backcolor=rgb(244, 244, 244) !important] case 'l': LoadBlock();
break;
[backcolor=rgb(244, 244, 244) !important] default: gotoxy(1,MAXY+1);
for(i=1;i<=MAXX-1;i++) putch(SPACE);
[backcolor=rgb(244, 244, 244) !important] } /* of switch */
}
void ClearSmellDisp(void)
[backcolor=rgb(244, 244, 244) !important]{
int k,i,j;
for(k=0;k<=1;k++)
[backcolor=rgb(244, 244, 244) !important] /* SMELL TYPE FOOD and HOME */
for(i=1;i<=MAXX;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=1;j<=MAXY;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(Smell[k][j])
{
[backcolor=rgb(244, 244, 244) !important] gotoxy(i,j);
putch(SPACE);
[backcolor=rgb(244, 244, 244) !important] }
} /* of one location */
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]void DispSmell(int type)
/* input: 0 -- Only display food smell
[backcolor=rgb(244, 244, 244) !important] -- Only display home smell
-- Display both food and home smell
[backcolor=rgb(244, 244, 244) !important]*/
{
[backcolor=rgb(244, 244, 244) !important] int k,i,j;
int fromk,tok;
[backcolor=rgb(244, 244, 244) !important] int smelldisp;
[backcolor=rgb(244, 244, 244) !important] switch(type)
{
[backcolor=rgb(244, 244, 244) !important] case 0: fromk = 0;
tok = 0;
[backcolor=rgb(244, 244, 244) !important] break;
case 1: fromk = 1;
[backcolor=rgb(244, 244, 244) !important] tok = 1;
break;
[backcolor=rgb(244, 244, 244) !important] case 2: fromk = 0;
tok = 1;
[backcolor=rgb(244, 244, 244) !important] break;
default:fromk = 0;
[backcolor=rgb(244, 244, 244) !important] tok = 1;
break;
[backcolor=rgb(244, 244, 244) !important] }
SmellGoneTimer = 0;
[backcolor=rgb(244, 244, 244) !important] for(k=fromk;k<=tok;k++)
/* SMELL TYPE FOOD and HOME */
[backcolor=rgb(244, 244, 244) !important] for(i=1;i<=MAXX;i++)
for(j=1;j<=MAXY;j++)
[backcolor=rgb(244, 244, 244) !important] {
if(Smell[k][j])
[backcolor=rgb(244, 244, 244) !important] {
smelldisp = 1+((10*Smell[k][j])/(MAX_SMELL*SMELL_DROP_RATE));
[backcolor=rgb(244, 244, 244) !important] if(smelldisp>=30000||smelldisp<0) smelldisp = 30000;
gotoxy(i,j);
[backcolor=rgb(244, 244, 244) !important] if(i!=food.xxx||j!=food.yyy)
{
[backcolor=rgb(244, 244, 244) !important] if((i==food.xxx&&j==food.yyy)||(i==home.xxx&&j==home.yyy))
/* don't over write Food and Home */;
[backcolor=rgb(244, 244, 244) !important] else
{
[backcolor=rgb(244, 244, 244) !important] if(smelldisp>9) putch('#');
else putch(smelldisp+'0');
[backcolor=rgb(244, 244, 244) !important] }
}
[backcolor=rgb(244, 244, 244) !important] }
} /* of one location */
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int AntNextDir(int xxx,int yyy,int ddir)
{
[backcolor=rgb(244, 244, 244) !important] int randnum;
int testdir;
[backcolor=rgb(244, 244, 244) !important] int CanGoState;
int cangof,cangol,cangor;
[backcolor=rgb(244, 244, 244) !important] int msf,msl,msr,maxms;
int type;
CanGoState = CanGo(xxx,yyy,ddir);
[backcolor=rgb(244, 244, 244) !important] if(CanGoState==0||CanGoState==2||CanGoState==3||CanGoState==6) cangof = 1;
else cangof = 0;
[backcolor=rgb(244, 244, 244) !important] if(CanGoState==0||CanGoState==1||CanGoState==3||CanGoState==5) cangol = 1;
else cangol = 0;
[backcolor=rgb(244, 244, 244) !important] if(CanGoState==0||CanGoState==1||CanGoState==2||CanGoState==4) cangor = 1;
else cangor = 0;
if(ant[AntNow].food) type = SMELL_TYPE_HOME;
[backcolor=rgb(244, 244, 244) !important] else type = SMELL_TYPE_FOOD;
[backcolor=rgb(244, 244, 244) !important] msf = GetMaxSmell(type,xxx,yyy,ddir);
msl = GetMaxSmell(type,xxx,yyy,TurnLeft(ddir));
[backcolor=rgb(244, 244, 244) !important] msr= GetMaxSmell(type,xxx,yyy,TurnRight(ddir));
maxms = MaxLocation(msf,msl,msr);
[backcolor=rgb(244, 244, 244) !important] /* maxms - 1 - msf is MAX
- msl is MAX
[backcolor=rgb(244, 244, 244) !important] - msr is MAX
- all 3 number is 0 */
testdir = NULL;
[backcolor=rgb(244, 244, 244) !important] switch(maxms)
{
[backcolor=rgb(244, 244, 244) !important] case 0: /* all is 0, keep testdir = NULL, random select dir */
break;
[backcolor=rgb(244, 244, 244) !important] case 1: if(cangof)
testdir = ddir;
[backcolor=rgb(244, 244, 244) !important] else
if(msl>msr) if(cangol) testdir = TurnLeft(ddir);
[backcolor=rgb(244, 244, 244) !important] else if(cangor) testdir = TurnRight(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 2: if(cangol)
testdir = TurnLeft(ddir);
[backcolor=rgb(244, 244, 244) !important] else
if(msf>msr) if(cangof) testdir = ddir;
[backcolor=rgb(244, 244, 244) !important] else if(cangor) testdir = TurnRight(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 3: if(cangor)
testdir = TurnRight(ddir);
[backcolor=rgb(244, 244, 244) !important] else
if(msf>msl) if(cangof) testdir =ddir;
[backcolor=rgb(244, 244, 244) !important] else if(cangol) testdir = TurnLeft(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] default:break;
} /* of maxms */
randnum = random(1000);
[backcolor=rgb(244, 244, 244) !important] 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 not go
[backcolor=rgb(244, 244, 244) !important] then random select dir
. if ant error, don't follow the smell, random select dir
[backcolor=rgb(244, 244, 244) !important] */
{
[backcolor=rgb(244, 244, 244) !important] randnum = random(100);
switch(CanGoState)
[backcolor=rgb(244, 244, 244) !important] {
case 0: if(randnum<90) testdir = ddir;
[backcolor=rgb(244, 244, 244) !important] else if (randnum>=90&&randnum<95) testdir = TurnLeft(ddir);
else testdir = TurnRight(ddir);
[backcolor=rgb(244, 244, 244) !important] break;
case 1: if(randnum<50) testdir = TurnLeft(ddir);
[backcolor=rgb(244, 244, 244) !important] else testdir = TurnRight(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 2: if(randnum<90) testdir = ddir;
else testdir = TurnRight(ddir);
[backcolor=rgb(244, 244, 244) !important] break;
case 3: if(randnum<90) testdir = ddir;
[backcolor=rgb(244, 244, 244) !important] else testdir = TurnLeft(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 4: testdir = TurnRight(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 5: testdir = TurnLeft(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] case 6: testdir = ddir;
break;
[backcolor=rgb(244, 244, 244) !important] case 7: testdir = TurnBack(ddir);
break;
[backcolor=rgb(244, 244, 244) !important] default:testdir = TurnBack(ddir);
} /* of can go state */
[backcolor=rgb(244, 244, 244) !important] }
return(testdir);
[backcolor=rgb(244, 244, 244) !important]}
int GetMaxSmell(int type,int xxx,int yyy,int ddir)
[backcolor=rgb(244, 244, 244) !important]{
int i,j;
[backcolor=rgb(244, 244, 244) !important] int ms; /* MAX smell */
[backcolor=rgb(244, 244, 244) !important] ms = 0;
switch(ddir)
[backcolor=rgb(244, 244, 244) !important] {
case UP: for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=yyy-ANT_EYESHOT;j<yyy;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
[backcolor=rgb(244, 244, 244) !important] (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
[backcolor=rgb(244, 244, 244) !important] ms = MAX_SMELL;
break;
[backcolor=rgb(244, 244, 244) !important] }
if(IsTrace(i,j)) continue;
[backcolor=rgb(244, 244, 244) !important] if(Smell[type][j]>ms) ms = Smell[type][j];
}
[backcolor=rgb(244, 244, 244) !important] break;
case DOWN: for(i=xxx-ANT_EYESHOT;i<=xxx+ANT_EYESHOT;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=yyy+1;j<=yyy+ANT_EYESHOT;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
[backcolor=rgb(244, 244, 244) !important] (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
[backcolor=rgb(244, 244, 244) !important] ms = MAX_SMELL;
break;
[backcolor=rgb(244, 244, 244) !important] }
if(IsTrace(i,j)) continue;
[backcolor=rgb(244, 244, 244) !important] if(Smell[type][j]>ms) ms = Smell[type][j];
}
[backcolor=rgb(244, 244, 244) !important] break;
case LEFT: for(i=xxx-ANT_EYESHOT;i<xxx;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
[backcolor=rgb(244, 244, 244) !important] (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
[backcolor=rgb(244, 244, 244) !important] ms = MAX_SMELL;
break;
[backcolor=rgb(244, 244, 244) !important] }
if(IsTrace(i,j)) continue;
[backcolor=rgb(244, 244, 244) !important] if(Smell[type][j]>ms) ms = Smell[type][j];
}
[backcolor=rgb(244, 244, 244) !important] break;
case RIGHT: for(i=xxx+1;i<=xxx+ANT_EYESHOT;i++)
[backcolor=rgb(244, 244, 244) !important] for(j=yyy-ANT_EYESHOT;j<=yyy+ANT_EYESHOT;j++)
{
[backcolor=rgb(244, 244, 244) !important] if(!JudgeCanGo(i,j)) continue;
if((i==food.xxx&&j==food.yyy&&type==SMELL_TYPE_FOOD)||
[backcolor=rgb(244, 244, 244) !important] (i==home.xxx&&j==home.yyy&&type==SMELL_TYPE_HOME))
{
[backcolor=rgb(244, 244, 244) !important] ms = MAX_SMELL;
break;
[backcolor=rgb(244, 244, 244) !important] }
if(IsTrace(i,j)) continue;
[backcolor=rgb(244, 244, 244) !important] if(Smell[type][j]>ms) ms = Smell[type][j];
}
[backcolor=rgb(244, 244, 244) !important] break;
default: break;
[backcolor=rgb(244, 244, 244) !important] }
return(ms);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int IsTrace(int xxx,int yyy)
{
[backcolor=rgb(244, 244, 244) !important] int i;
[backcolor=rgb(244, 244, 244) !important] for(i=0;i<TRACE_REMEMBER;i++)
if(ant[AntNow].tracex==xxx&&ant[AntNow].tracey==yyy) return(1);
[backcolor=rgb(244, 244, 244) !important] return(0);
}
int MaxLocation(int num1,int num2,int num3)
[backcolor=rgb(244, 244, 244) !important]{
int maxnum;
if(num1==0&&num2==0&&num3==0) return(0);
maxnum = num1;
[backcolor=rgb(244, 244, 244) !important] if(num2>maxnum) maxnum = num2;
if(num3>maxnum) maxnum = num3;
if(maxnum==num1) return(1);
[backcolor=rgb(244, 244, 244) !important] if(maxnum==num2) return(2);
if(maxnum==num3) return(3);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int CanGo(int xxx,int yyy,int ddir)
/* input: xxx,yyy - location of ant
[backcolor=rgb(244, 244, 244) !important] ddir - now dir
output: 0 - forward and left and right can go
[backcolor=rgb(244, 244, 244) !important] - forward can not go
- left can not go
[backcolor=rgb(244, 244, 244) !important] - right can not go
- forward and left can not go
[backcolor=rgb(244, 244, 244) !important] - forward and right can not go
- left and right can not go
[backcolor=rgb(244, 244, 244) !important] - forward and left and right all can not go
*/
[backcolor=rgb(244, 244, 244) !important]{
int tx,ty,tdir;
[backcolor=rgb(244, 244, 244) !important] int okf,okl,okr;
[backcolor=rgb(244, 244, 244) !important] /* forward can go ? */
tdir = ddir;
[backcolor=rgb(244, 244, 244) !important] tx = xxx;
ty = yyy;
[backcolor=rgb(244, 244, 244) !important] switch(tdir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: ty--;
break;
[backcolor=rgb(244, 244, 244) !important] case DOWN: ty++;
break;
[backcolor=rgb(244, 244, 244) !important] case LEFT: tx--;
break;
[backcolor=rgb(244, 244, 244) !important] case RIGHT: tx++;
break;
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
[backcolor=rgb(244, 244, 244) !important] if(JudgeCanGo(tx,ty)) okf = 1;
else okf = 0;
/* turn left can go ? */
[backcolor=rgb(244, 244, 244) !important] tdir = TurnLeft(ddir);
tx = xxx;
[backcolor=rgb(244, 244, 244) !important] ty = yyy;
switch(tdir)
[backcolor=rgb(244, 244, 244) !important] {
case UP: ty--;
[backcolor=rgb(244, 244, 244) !important] break;
case DOWN: ty++;
[backcolor=rgb(244, 244, 244) !important] break;
case LEFT: tx--;
[backcolor=rgb(244, 244, 244) !important] break;
case RIGHT: tx++;
[backcolor=rgb(244, 244, 244) !important] break;
default: break;
[backcolor=rgb(244, 244, 244) !important] } /* of switch dir */
if(JudgeCanGo(tx,ty)) okl = 1;
[backcolor=rgb(244, 244, 244) !important] else okl = 0;
[backcolor=rgb(244, 244, 244) !important] /* turn right can go ? */
tdir = TurnRight(ddir);
[backcolor=rgb(244, 244, 244) !important] tx = xxx;
ty = yyy;
[backcolor=rgb(244, 244, 244) !important] switch(tdir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: ty--;
break;
[backcolor=rgb(244, 244, 244) !important] case DOWN: ty++;
break;
[backcolor=rgb(244, 244, 244) !important] case LEFT: tx--;
break;
[backcolor=rgb(244, 244, 244) !important] case RIGHT: tx++;
break;
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
[backcolor=rgb(244, 244, 244) !important] if(JudgeCanGo(tx,ty)) okr = 1;
else okr = 0;
if(okf&&okl&&okr) return(0);
[backcolor=rgb(244, 244, 244) !important] if(!okf&&okl&&okr) return(1);
if(okf&&!okl&&okr) return(2);
[backcolor=rgb(244, 244, 244) !important] if(okf&&okl&&!okr) return(3);
if(!okf&&!okl&&okr) return(4);
[backcolor=rgb(244, 244, 244) !important] if(!okf&&okl&&!okr) return(5);
if(okf&&!okl&&!okr) return(6);
[backcolor=rgb(244, 244, 244) !important] if(!okf&&!okl&&!okr) return(7);
return(7);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int JudgeCanGo(int xxx,int yyy)
/* input: location to judeg
[backcolor=rgb(244, 244, 244) !important] output: 0 -- can not go
-- can go
[backcolor=rgb(244, 244, 244) !important]*/
{
[backcolor=rgb(244, 244, 244) !important] int i,j;
[backcolor=rgb(244, 244, 244) !important] if(xxx<=0||xxx>MAXX) return(0);
if(yyy<=0||yyy>MAXY) return(0);
[backcolor=rgb(244, 244, 244) !important] if(block[xxx][yyy]) return(0);
return(1);
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int TurnLeft(int ddir)
{
[backcolor=rgb(244, 244, 244) !important] switch(ddir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: return(LEFT);
case DOWN: return(RIGHT);
[backcolor=rgb(244, 244, 244) !important] case LEFT: return(DOWN);
case RIGHT: return(UP);
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int TurnRight(int ddir)
{
[backcolor=rgb(244, 244, 244) !important] switch(ddir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: return(RIGHT);
case DOWN: return(LEFT);
[backcolor=rgb(244, 244, 244) !important] case LEFT: return(UP);
case RIGHT: return(DOWN);
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
[backcolor=rgb(244, 244, 244) !important]}
[backcolor=rgb(244, 244, 244) !important]int TurnBack(int ddir)
{
[backcolor=rgb(244, 244, 244) !important] switch(ddir)
{
[backcolor=rgb(244, 244, 244) !important] case UP: return(DOWN);
case DOWN: return(UP);
[backcolor=rgb(244, 244, 244) !important] case LEFT: return(RIGHT);
case RIGHT: return(LEFT);
[backcolor=rgb(244, 244, 244) !important] default: break;
} /* of switch dir */
[backcolor=rgb(244, 244, 244) !important]}\032
|