实验四有限状态机实验实验报告一、实验目的通过蚂蚁世界实验掌握游戏中追有限状态机算法二、实验仪器Windows7系统MicrosoftVisualStudio2015三、实验原理及过程1)制作菜单设置参数:点击会弹出对话框,设置一些参数,红、黑蚂蚁的家会在地图上标记出来运行:设置好参数后点击运行,毒药、食物、水会在地图上随机显示下一步:2只红蚂蚁和2只黑蚂蚁会随机出现在地图上,窗口右方还会出现红、黑蚂蚁当前数量的统计不断按下一步,有限状态机就会不断运行,使蚁群产生变化2)添加加速键资源视图中下方选择ID和键值3)新建头文件def.h在AntView.cpp中加入#includedef.h与本实验有关的数据大都是在这里定义的intflag=0;#definekForage1#definekGoHome2#definekThirsty3#definekDead4#definekMaxEntities200classai_Entity{public:inttype;intstate;introw;intcol;ai_Entity();~ai_Entity(){}voidNew(inttheType,inttheState,inttheRow,inttheCol);voidForage();voidGoHome();voidThirsty();voidDead();};ai_EntityentityList[kMaxEntities];#definekRedAnt1#definekBlackAnt2intRedHomeRow;intRedHomeCol;intBlackHomeRow;intBlackHomeCol;intRedNum=2;intBlackNum=2;//地图大小,可改变#definekMaxRows30#definekMaxCols40#defineLENGTH20intterrain[kMaxRows][kMaxCols];#definekGround1#definekWater2#definekBlackHome3#definekRedHome4#definekPoison5#definekFood6//ai_Entity类中函数的定义ai_Entity::ai_Entity(){type=0;state=0;row=0;col=0;}intRnd(intmin,intmax)//不能产生负数{intresult;do{result=rand()%max;}while(result=min);returnresult;}voidai_Entity::New(inttheType,inttheState,inttheRow,inttheCol){type=theType;row=theRow;col=theCol;state=theState;}voidai_Entity::Forage(){introwMove;intcolMove;intnewRow;intnewCol;intfoodRow;intfoodCol;intpoisonRow;intpoisonCol;rowMove=Rnd(-1,3)-1;colMove=Rnd(-1,3)-1;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol0)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;}}voidai_Entity::GoHome(){introwMove;intcolMove;intnewRow;intnewCol;inthomeRow;inthomeCol;intpoisonRow;intpoisonCol;inti;if(type==kRedAnt){homeRow=RedHomeRow;homeCol=RedHomeCol;}else{homeRow=BlackHomeRow;homeCol=BlackHomeCol;}if(rowhomeRow)rowMove=1;elseif(rowhomeRow)rowMove=-1;elserowMove=0;if(colhomeCol)colMove=1;elseif(colhomeCol)colMove=-1;elsecolMove=0;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol0)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;ikMaxEntities;i++)if(entityList[i].type==0){entityList[i].New(type,kForage,homeRow,homeCol);if(type==kRedAnt)RedNum++;if(type==kBlackAnt)BlackNum++;break;}}}voidai_Entity::Thirsty(){introwMove;intcolMove;intnewRow;intnewCol;intfoodRow;intfoodCol;intpoisonRow;intpoisonCol;rowMove=Rnd(-1,3)-1;colMove=Rnd(-1,3)-1;newRow=row+rowMove;newCol=col+colMove;if(newRow0)return;if(newCol0)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;}}voidai_Entity::Dead(){if(type==kRedAnt)RedNum--;if(type==kBlackAnt)BlackNum--;type=0;}4)制作对话框添加资源,拖动控件9个静态文本框,7个编辑框右键对话框添加类InputDlg,添加成员变量intm_RedHomeRow;intm_RedHomeCol;intm_BlackHomeRow;intm_BlackHomeCol;intm_food;intm_water;intm_poison;在AntView.cpp中加入#includeInputDlg.h5)对菜单项添加事件给CAntView类添加一些成员变量没做说明都是添加到CAntView类,没做说明都是COMMAND事件。设置参数:点击出现对话框,输入值,确定后赋给CAntView的成员变量,刷新窗口voidCAntView::OnData(){//TODO:在此添加命令处理程序代码InputDlgdlg;if(dlg.DoModal()==IDOK){RedHomeRow=kRedHomeRow=dlg.m_RedHomeRow;RedHomeCol=kRedHomeCol=dlg.m_RedHomeCol;BlackHomeRow=kBlackHomeRow=dlg.m_BlackHomeRow;BlackHomeCol=kBlackHomeCol=dlg.m_BlackHomeCol;kMaxWater=dlg.m_water;kMaxFood=dlg.m_food;kMaxPoison=dlg.m_poison;}if(kRedHomeRow=kMaxRows||kBlackHomeRow=kMaxRows||kRedHomeCol=kMaxCols||kBlackHomeCol=kMaxCols||kRedHomeRow0||kBlackHomeRow0||kRedHomeCol0||kBlackHomeCol0||kRedHomeRow==kBlackHomeRow&&kRedHomeCol==kBlackHomeCol){AfxMessageBox(L输入坐标不合法,请重新输入!);return;}flag=1;Invalidate();}运行:初始化数组,随机生成毒药、事物、水,刷新窗口voidCAntView::OnRun(){//TODO:在此添加命令处理程序代码if(flag==0){AfxMessageBox(L还未设置蚂蚁家的位置!);return;}if(flag!=1)re