C语言实现蚁群.doc

上传人:marr****208 文档编号:156951435 上传时间:2020-12-20 格式:DOC 页数:27 大小:67.50KB
返回 下载 相关 举报
C语言实现蚁群.doc_第1页
第1页 / 共27页
C语言实现蚁群.doc_第2页
第2页 / 共27页
C语言实现蚁群.doc_第3页
第3页 / 共27页
C语言实现蚁群.doc_第4页
第4页 / 共27页
C语言实现蚁群.doc_第5页
第5页 / 共27页
点击查看更多>>
资源描述

《C语言实现蚁群.doc》由会员分享,可在线阅读,更多相关《C语言实现蚁群.doc(27页珍藏版)》请在金锄头文库上搜索。

1、源代码如下: /*ant.c*/ #define SPACE 0x20#define ESC 0x1b#define ANT_CHAR_EMPTY +#define ANT_CHAR_FOOD 153#define HOME_CHAR H#define FOOD_CHAR F#define FOOD_CHAR2 f#define FOOD_HOME_COLOR 12#define BLOCK_CHAR 177 #define MAX_ANT 50#define INI_SPEED 3#define MAXX 80#define MAXY 23#define MAX_FOOD 10000#def

2、ine TARGET_FOOD 200#define MAX_SMELL 5000#define SMELL_DROP_RATE 0.05#define ANT_ERROR_RATE 0.02#define ANT_EYESHOT 3#define SMELL_GONE_SPEED 50#define SMELL_GONE_RATE 0.05#define TRACE_REMEMBER 50#define MAX_BLOCK 100 #define NULL 0#define UP 1#define DOWN 2#define LEFT 3#define RIGHT 4#define SMEL

3、L_TYPE_FOOD 0#define SMELL_TYPE_HOME 1 #include stdio.h#include conio.h#include dos.h#include stdlib.h#include dos.h#include process.h#include ctype.h#include math.h void WorldInitial(void);void BlockInitial(void);void CreatBlock(void);void SaveBlock(void);void LoadBlock(void);void HomeFoodInitial(v

4、oid);void AntInitial(void);void WorldChange(void);void AntMove(void);void AntOneStep(void);void DealKey(char key);void ClearSmellDisp(void);void DispSmell(int type);int AntNextDir(int xxx,int yyy,int ddir);int GetMaxSmell(int type,int xxx,int yyy,int ddir);int IsTrace(int xxx,int yyy);int MaxLocatio

5、n(int num1,int num2,int num3);int CanGo(int xxx,int yyy,int ddir);int JudgeCanGo(int xxx,int yyy);int TurnLeft(int ddir);int TurnRight(int ddir);int TurnBack(int ddir); int MainTimer(void);char WaitForKey(int secnum);void DispPlayTime(void);int TimeUse(void);void HideCur(void);void ResetCur(void); /

6、* - */struct HomeStructint xxx,yyy;int amount;int TargetFood;home; struct FoodStructint xxx,yyy;int amount; food; struct AntStructint xxx,yyy;int dir;int speed;int SpeedTimer;int food;int SmellAmount2;int tracexTRACE_REMEMBER;int traceyTRACE_REMEMBER;int TracePtr;int IQ;antMAX_ANT;int AntNow;int tim

7、er10ms;struct time starttime,endtime;int Smell2MAXX+1MAXY+1;int blockMAXX+1MAXY+1;int SmellGoneTimer;int SmellDispFlag;int CanFindFood;int HardtoFindPath; /* - Main - */void main(void)char KeyPress;int tu;clrscr();HideCur();WorldInitial();dotimer10ms = MainTimer();if(timer10ms) AntMove();if(timer10m

8、s) WorldChange();tu = TimeUse();if(tu=60&!CanFindFood)gotoxy(1,MAXY+1);printf(Can not find food, maybe a block world.);WaitForKey(10);WorldInitial(); if(tu=180&home.amount=home.TargetFood)gettime(&endtime);KeyPress = WaitForKey(60);DispPlayTime();WaitForKey(10);WorldInitial();else if(kbhit()KeyPress

9、 = getch();DealKey(KeyPress);else KeyPress = NULL;while(KeyPress!=ESC);gettime(&endtime);DispPlayTime();WaitForKey(10);clrscr();ResetCur(); /* - general sub process - */int MainTimer(void)/* output: how much 10ms have pass from last time call this process */static int oldhund,oldsec;struct time t;in

10、t timeuse; gettime(&t);timeuse = 0;if(t.ti_hund!=oldhund)if(t.ti_sec!=oldsec)timeuse+=100;oldsec = t.ti_sec;timeuse+=t.ti_hund-oldhund;oldhund = t.ti_hund;else timeuse = 0;return (timeuse); char WaitForKey(int secnum)/* funtion: if have key in, exit immediately, else wait secnum senconds then exit i

11、nput: secnum - wait this senconds, must minin) secuse = (minnow-1-minin) + (secnow+60-secin);else secuse = secnow - secin;/* counting error check */if(secuse0)gotoxy(1,MAXY+1);printf(Time conuting error, any keyto exit.);getch();exit(3);while(secuse=secnum);return (NULL); void DispPlayTime(void)int

12、ph,pm,ps;ph = endtime.ti_hour - starttime.ti_hour;pm = endtime.ti_min - starttime.ti_min;ps = endtime.ti_sec - starttime.ti_sec;if(ph0) ph+=24;if(pm0) ph-; pm+=60; if(ps0) pm-; ps+=60; gotoxy(1,MAXY+1);printf(Time use: %d hour- %d min- %d sec ,ph,pm,ps); int TimeUse(void)int ph,pm,ps;gettime(&endtime);ph = endtime.ti_hour - starttime.ti_hour;pm = endtime.ti_min - starttime.ti_min;ps = endtime.ti_sec - starttime.ti_sec;if(ph0) ph+=24;if(pm0) ph-; pm+=60; if(ps0) pm-; ps+=60; return(ps+(60*(pm+60*ph);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号