当前位置:文档之家› 游戏人工智能实验报告四

游戏人工智能实验报告四

实验四有限状态机实验实验报告一、实验目的通过蚂蚁世界实验掌握游戏中追有限状态机算法二、实验仪器Windows7系统Microsoft Visual Studio2015三、实验原理及过程1)制作菜单设置参数:点击会弹出对话框,设置一些参数,红、黑蚂蚁的家会在地图上标记出来运行:设置好参数后点击运行,毒药、食物、水会在地图上随机显示下一步:2只红蚂蚁和2只黑蚂蚁会随机出现在地图上,窗口右方还会出现红、黑蚂蚁当前数量的统计不断按下一步,有限状态机就会不断运行,使蚁群产生变化2)添加加速键资源视图中下方选择ID和键值3)新建头文件def.h在AntView.cpp中加入#include"def.h"与本实验有关的数据大都是在这里定义的int flag=0;#define kForage 1#define kGoHome 2#define kThirsty 3#define kDead 4#define kMaxEntities 200class ai_Entity{public: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 Thirsty();void Dead();};ai_Entity entityList[kMaxEntities];#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 terrain[kMaxRows][kMaxCols];#define kGround 1#define kWater 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;do{result=rand()%max;}while(result<=min);return result;}void ai_Entity::New (int theType,int theState,int theRow,int theCol) {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(newRow<0)return;if(newCol<0)return;if(newRow>=kMaxRows)return;if(newCol>=kMaxCols)return;if((terrain[newRow][newCol]==kGround)||(terrain[newRow][newCol]==kWater)) {row=newRow;col=newCol;}if(terrain[newRow][newCol]==kFood){row=newRow;col=newCol;terrain[row][col]=kGround;state=kGoHome;do{foodRow=Rnd(-1,kMaxRows);foodCol=Rnd(-1,kMaxCols);}while(terrain[foodRow][foodCol]!=kGround);terrain[foodRow][foodCol]=kFood;}if(terrain[newRow][newCol]==kPoison){row=newRow;col=newCol;terrain[row][col]=kGround;state=kDead;do{poisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);}while(terrain[poisonRow][poisonCol]!=kGround);terrain[poisonRow][poisonCol]=kPoison;}}void ai_Entity::GoHome(){int rowMove;int colMove;int newRow;int newCol;int homeRow;int homeCol;int poisonRow;int poisonCol;int i;if(type==kRedAnt){homeRow=RedHomeRow;homeCol=RedHomeCol;}else{homeRow=BlackHomeRow;homeCol=BlackHomeCol;}if(row<homeRow)rowMove=1;else if(row>homeRow)rowMove=-1;elserowMove=0;if(col<homeCol)colMove=1;else if(col>homeCol)colMove=-1;elsecolMove=0;newRow=row+rowMove;newCol=col+colMove;if(newRow<0)return;if(newCol<0)return;if(newRow>=kMaxRows)return;if(newCol>=kMaxCols)return;if(terrain[newRow][newCol]!=kPoison){row=newRow;col=newCol;}else{row=newRow;col=newCol;terrain[row][col]=kGround;state=kDead;do{poisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);}while(terrain[poisonRow][poisonCol]!=kGround);terrain[poisonRow][poisonCol]=kPoison;}if((newRow==homeRow)&&(newCol==homeCol)){row=newRow;col=newCol;state=kThirsty;for(i=0;i<kMaxEntities;i++)if(entityList[i].type==0){entityList[i].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 foodCol;int poisonRow;int poisonCol;rowMove=Rnd(-1,3)-1;colMove=Rnd(-1,3)-1;newRow=row+rowMove;newCol=col+colMove;if(newRow<0)return;if(newCol<0)return;if(newRow>=kMaxRows)return;if(newCol>=kMaxCols)return;if((terrain[newRow][newCol]==kGround)||(terrain[newRow][newCol]==kFood)) {row=newRow;col=newCol;}if(terrain[newRow][newCol]==kWater){row=newRow;col=newCol;terrain[row][col]=kGround;state=kForage;do{foodRow=Rnd(-1,kMaxRows);foodCol=Rnd(-1,kMaxCols);}while(terrain[foodRow][foodCol]!=kGround);terrain[foodRow][foodCol]=kWater;}if(terrain[newRow][newCol]==kPoison){row=newRow;col=newCol;terrain[row][col]=kGround;state=kDead;do{poisonRow=Rnd(-1,kMaxRows);poisonCol=Rnd(-1,kMaxCols);}while(terrain[poisonRow][poisonCol]!=kGround);terrain[poisonRow][poisonCol]=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 m_BlackHomeRow;int m_BlackHomeCol;int m_food;int m_water;int m_poison;在AntView.cpp中加入#include"InputDlg.h"5)对菜单项添加事件给CAntView类添加一些成员变量没做说明都是添加到CAntView类,没做说明都是COMMAND事件。

相关主题