1、。实验四 有限状态机实验实验报告一、实验目的通过蚂蚁世界实验掌握游戏中追有限状态机算法二、实验仪器Windows7系统 Microsoft Visual Studio2015三、实验原理及过程1) 制作菜单设置参数:点击会弹出对话框,设置一些参数,红、黑蚂蚁的家会在地图上标记出来运行:设置好参数后点击运行,毒药、食物、水会在地图上随机显示下一步:2只红蚂蚁和2只黑蚂蚁会随机出现在地图上,窗口右方还会出现红、黑蚂蚁当前数量的统计不断按下一步,有限状态机就会不断运行,使蚁群产生变化2)添加加速键资源视图中下方选择ID和键值3) 新建头文件def.h 在AntView.cpp中加入#include
2、def.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 Thi
3、rsty();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 kWa
4、ter 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 theCo
5、l)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(ne
6、wCol=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);terrainfoodRowfoodCo
7、l=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;int
8、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=kM
9、axRows)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=home
10、Row)&(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 foodC
11、ol;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=newRow
12、;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,
13、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 m_BlackHomeRow;int m_BlackHomeCol;int m_foo
14、d;int m_water;int m_poison;在AntView.cpp中加入#include InputDlg.h5) 对菜单项添加事件给CAntView类添加一些成员变量没做说明都是添加到CAntView类,没做说明都是COMMAND事件。设置参数:点击出现对话框,输入值,确定后赋给CAntView的成员变量,刷新窗口void CAntView:OnData()/ TODO: 在此添加命令处理程序代码InputDlg dlg;if(dlg.DoModal()=IDOK)RedHomeRow=kRedHomeRow=dlg.m_RedHomeRow;RedHomeCol=kRedHom
15、eCol=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|k
16、RedHomeCol0|kBlackHomeCol0|kRedHomeRow=kBlackHomeRow&kRedHomeCol=kBlackHomeCol)AfxMessageBox(L输入坐标不合法,请重新输入!);return;flag=1;Invalidate();运行:初始化数组,随机生成毒药、事物、水,刷新窗口void CAntView:OnRun()/ TODO: 在此添加命令处理程序代码if(flag=0)AfxMessageBox(L还未设置蚂蚁家的位置!);return ;if(flag!=1)return ;int i,j;for(i=0;ikMaxRows;i+)for
17、(j=0;jkMaxCols;j+)terrainij=kGround;terrainkRedHomeRowkRedHomeCol=kRedHome;terrainkBlackHomeRowkBlackHomeCol=kBlackHome;srand(time(NULL); /取系统时间为随机种子int r,c;for(i=0;ikMaxWater;i+)r=Rnd(-1,kMaxRows);c=Rnd(-1,kMaxCols);if(terrainrc=kGround)terrainrc=kWater;elsei-;for(i=0;ikMaxPoison;i+)r=Rnd(-1,kMaxRo
18、ws);c=Rnd(-1,kMaxCols);if(terrainrc=kGround)terrainrc=kPoison;elsei-;for(i=0;ikMaxFood;i+)r=Rnd(-1,kMaxRows);c=Rnd(-1,kMaxCols);if(terrainrc=kGround)terrainrc=kFood;elsei-;flag=2;Invalidate();下一步:有限状态机的核心代码,刷新窗口void CAntView:OnNext()/ TODO: 在此添加命令处理程序代码if(flag=2)/kGround才能新建int x,y;for(int i=0;i2;i+
19、)x=Rnd(-1,kMaxCols);y=Rnd(-1,kMaxRows);if(terrainyx=kGround)entityListi.New(kRedAnt,kForage,y,x);elsei-;for(int i=2;i4;i+)x=Rnd(-1,kMaxCols);y=Rnd(-1,kMaxRows);if(terrainyx=kGround)entityListi.New(kBlackAnt,kForage,y,x);elsei-;flag=3;elsefor(int i=0;ikMaxEntities;i+)switch(entityListi.state)case kFo
20、rage:entityListi.Forage();break;case kGoHome:entityListi.GoHome();break;case kThirsty:entityListi.Thirsty();break;case kDead:entityListi.Dead();break;default:break;Invalidate();6) OnDraw函数flag是标志变量,便于控制绘图void CAntView:OnDraw(CDC* pDC)CAntDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);if (!pDoc)return;
21、/ TODO: 在此处为本机数据添加绘制代码int i,j;/绘制网格for(i=0;iMoveTo(0,i);pDC-LineTo(LENGTH*kMaxCols,i);for(i=0;iMoveTo(i,0);pDC-LineTo(i,LENGTH*kMaxRows);/画出红黑房子if(flag=1)pDC-TextOutW(kBlackHomeCol*LENGTH+1,kBlackHomeRow*LENGTH+1,LH);pDC-SetTextColor(RGB(255,0,0);pDC-TextOutW(kRedHomeCol*LENGTH+1,kRedHomeRow*LENGTH+
22、1,LH);pDC-SetTextColor(RGB(0,0,0);if(flag=2)/遍历数组在相应位置显示图片(水滴、骷髅、苹果)CDC dcMemory;dcMemory.CreateCompatibleDC(pDC);CBitmap bmp1,bmp2,bmp3;BITMAP bmpInfo1,bmpInfo2,bmpInfo3;bmp1.LoadBitmapW(IDB_BITMAP1);bmp1.GetBitmap(&bmpInfo1);bmp2.LoadBitmapW(IDB_BITMAP2);bmp2.GetBitmap(&bmpInfo2);bmp3.LoadBitmapW(
23、IDB_BITMAP3);bmp3.GetBitmap(&bmpInfo3);CBitmap *pOldBitmap=dcMemory.SelectObject(&bmp1);for(i=0;ikMaxRows;i+)for(j=0;jStretchBlt(j*LENGTH+1,i*LENGTH+1,19,19,&dcMemory,0,0,bmpInfo1.bmWidth,bmpInfo1.bmHeight,SRCCOPY);else if(terrainij=kPoison)dcMemory.SelectObject(&bmp2);pDC-StretchBlt(j*LENGTH+1,i*LE
24、NGTH+1,19,19,&dcMemory,0,0,bmpInfo2.bmWidth,bmpInfo2.bmHeight,SRCCOPY);else if(terrainij=kFood)dcMemory.SelectObject(&bmp3);pDC-StretchBlt(j*LENGTH+1,i*LENGTH+1,18,18,&dcMemory,0,0,bmpInfo3.bmWidth,bmpInfo3.bmHeight,SRCCOPY);dcMemory.SelectObject(pOldBitmap);if(flag=3) /绘制蚂蚁CDC dcMemory;dcMemory.Cre
25、ateCompatibleDC(pDC);CBitmap bmp1,bmp2;BITMAP bmpInfo1,bmpInfo2;bmp1.LoadBitmapW(IDB_BITMAP4);bmp1.GetBitmap(&bmpInfo1);bmp2.LoadBitmapW(IDB_BITMAP5);bmp2.GetBitmap(&bmpInfo2);CBitmap *pOldBitmap=dcMemory.SelectObject(&bmp1);for(i=0;iStretchBlt(entityListi.col*LENGTH+1,entityListi.row*LENGTH+1,19,19
26、,&dcMemory,0,0,bmpInfo1.bmWidth,bmpInfo1.bmHeight,SRCCOPY);if(entityListi.type=kBlackAnt)dcMemory.SelectObject(&bmp2);pDC-StretchBlt(entityListi.col*LENGTH+1,entityListi.row*LENGTH+1,19,19,&dcMemory,0,0,bmpInfo2.bmWidth,bmpInfo2.bmHeight,SRCCOPY);dcMemory.SelectObject(pOldBitmap);CString s;s.Format(
27、_T(红蚂蚁数:%d 黑蚂蚁数:%d),RedNum,BlackNum);/输出当前蚂蚁数pDC-TextOutW(43*LENGTH,0,s);四、实验结果运行:点击菜单项,设置参数,弹出对话框如果设置参数不对:(房子坐标越界、红黑房子坐标相等、食物毒物水数为负数)参数设置好后,房子出现:点击运行或F5,食物毒物水出现:点击下一步或F10,蚂蚁和文字出现:多次点击下一步,蚂蚁数改变:可以多次点击下一步设置的参数不同蚂蚁的繁殖情况不同。比如,食物多,繁殖快,毒物多,蚂蚁可能很快就灭绝了五、实验心得通过有限状态机实现蚂蚁的繁殖,很有趣的一个实验,通过此次实验能增加AI和MFC的知识,程序还有很多
28、需要改进的地方。比如,有时候不按指定的顺序点击菜单将会出现小错误;def.h里的东西太杂,可以新建cpp文件,整理进去。六、主要代码实验过程中已经列出主要代码,这里再列出与有限状态机算法有关的代码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
29、 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(newRow0)return;if(newCol=kMaxRows)return
30、;if(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);terrainfoodRow
31、foodCol=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 newCo
32、l;int 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(new
33、Col=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(newRo
34、w=homeRow)&(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
35、 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(newCol=kMaxCols)return;if(terrainnewRownewCol=kGround)|(terrainnewRownewCol=kFood)row=newRow;col=newCol;if(terrainnewRownewCol=kWater)row=
36、newRow;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;void CAntView:OnNext()/ TODO: 在此