《游戏人工智能化实验报告四》由会员分享,可在线阅读,更多相关《游戏人工智能化实验报告四(19页珍藏版)》请在金锄头文库上搜索。
1、 .wd.实验四 有限状态机实验实验报告一、实验目的通过蚂蚁世界实验掌握游戏中追有限状态机算法二、实验仪器Windows7系统 Microsoft Visual Studio2015三、实验原理及过程1) 制作菜单设置参数:点击会弹出对话框,设置一些参数,红、黑蚂蚁的家会在地图上标记出来运行:设置好参数后点击运行,毒药、食物、水会在地图上随机显示下一步:2只红蚂蚁和2只黑蚂蚁会随机出现在地图上,窗口右方还会出现红、黑蚂蚁当前数量的统计不断按下一步,有限状态机就会不断运行,使蚁群产生变化2添加加速键资源视图中下方选择ID和键值3) 新建头文件def.h 在AntView.cpp中参加#inclu
2、dedef.h与本实验有关的数据大都是在这里定义的int flag=0;#define kForage 1#define kGoHome 2#define kThirsty 3#define kDead 4#define kMaxEntities 200class ai_Entitypublic:int type;int state;int row;int col;ai_Entity();ai_Entity() void New (int theType,int theState,int theRow,int theCol);void Forage();void GoHome();void T
3、hirsty();void Dead();ai_Entity entityListkMaxEntities;#define kRedAnt 1#define kBlackAnt 2int RedHomeRow;int RedHomeCol;int BlackHomeRow;int BlackHomeCol;int RedNum=2;int BlackNum=2;/地图大小,可改变#define kMaxRows 30#define kMaxCols 40#define LENGTH 20int terrainkMaxRowskMaxCols;#define kGround 1#define k
4、Water 2#define kBlackHome 3#define kRedHome 4#define kPoison 5#define kFood 6/ai_Entity类中函数的定义ai_Entity:ai_Entity()type=0;state=0;row=0;col=0;int Rnd(int min, int max)/不能产生负数int result;doresult=rand()%max;while(result=min);return result;void ai_Entity:New (int theType,int theState,int theRow,int the
5、Col)type=theType;row=theRow;col=theCol;state=theState;void ai_Entity:Forage()int rowMove;int colMove;int newRow;int newCol;int foodRow;int foodCol;int poisonRow;int poisonCol;rowMove=Rnd(-1,3)-1;colMove=Rnd(-1,3)-1;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol=kMaxRows)return;if(
6、newCol=kMaxCols)return;if(terrainnewRownewCol=kGround)|(terrainnewRownewCol=kWater)row=newRow;col=newCol;if(terrainnewRownewCol=kFood)row=newRow;col=newCol;terrainrowcol=kGround;state=kGoHome;dofoodRow=Rnd(-1,kMaxRows);foodCol=Rnd(-1,kMaxCols);while(terrainfoodRowfoodCol!=kGround);terrainfoodRowfood
7、Col=kFood;if(terrainnewRownewCol=kPoison)row=newRow;col=newCol;terrainrowcol=kGround;state=kDead;dopoisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);while(terrainpoisonRowpoisonCol!=kGround);terrainpoisonRowpoisonCol=kPoison;void ai_Entity:GoHome()int rowMove;int colMove;int newRow;int newCol;in
8、t homeRow;int homeCol;int poisonRow;int poisonCol;int i;if(type=kRedAnt)homeRow=RedHomeRow;homeCol=RedHomeCol;elsehomeRow=BlackHomeRow;homeCol=BlackHomeCol;if(rowhomeRow)rowMove=-1;elserowMove=0;if(colhomeCol)colMove=-1;elsecolMove=0;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol=
9、kMaxRows)return;if(newCol=kMaxCols)return;if(terrainnewRownewCol!=kPoison)row=newRow;col=newCol;elserow=newRow;col=newCol;terrainrowcol=kGround;state=kDead;dopoisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);while(terrainpoisonRowpoisonCol!=kGround);terrainpoisonRowpoisonCol=kPoison;if(newRow=ho
10、meRow)&(newCol=homeCol)row=newRow;col=newCol;state=kThirsty;for(i=0;ikMaxEntities;i+)if(entityListi.type=0)entityListi.New(type,kForage,homeRow,homeCol);if(type=kRedAnt)RedNum+;if(type=kBlackAnt)BlackNum+;break;void ai_Entity:Thirsty()int rowMove;int colMove;int newRow;int newCol;int foodRow;int foo
11、dCol;int poisonRow;int poisonCol;rowMove=Rnd(-1,3)-1;colMove=Rnd(-1,3)-1;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol=kMaxRows)return;if(newCol=kMaxCols)return;if(terrainnewRownewCol=kGround)|(terrainnewRownewCol=kFood)row=newRow;col=newCol;if(terrainnewRownewCol=kWater)row=newR
12、ow;col=newCol;terrainrowcol=kGround;state=kForage;dofoodRow=Rnd(-1,kMaxRows);foodCol=Rnd(-1,kMaxCols);while(terrainfoodRowfoodCol!=kGround);terrainfoodRowfoodCol=kWater;if(terrainnewRownewCol=kPoison)row=newRow;col=newCol;terrainrowcol=kGround;state=kDead;dopoisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);while(terrainpoisonRowpoisonCol!=kGround);terrainpoisonRowpoisonCol=kPoison;void ai_Entity:Dead()if(type=kRedAnt)RedNum-;if(type=kBlackAnt)BlackNum-;type=0;4) 制作对话框添加资源,拖动控件9个静态文本框,7个编辑框右键对话框添加类InputDlg,添加成员变量int m_RedHomeRow;int m_RedHomeCol;int