实验三:有限状态机姓名:廖远东班级:数媒2 学号:E10700217一、实验目的掌握有限状态机的原理,并会使用有限状态机,要求能编程序实现一个案例如书上蚁群世界。
.二、实验仪器linux 下的codeblock三、主要代码ant.h:#ifndef ANT_H_INCLUDED#define ANT_H_INCLUDED#define kGround 1#define kWater 2#define kBlackHome 3#define kRedHome 4#define kPoison 5#define kFood 6#define kMaxWater 15#define kMaxPoison 8#define kMaxFood 20#define kRedHomeRow 5#define kRedHomeCol 5#define kBlackHomeRow 5#define kBlackHomeCol 36// entities#define kMaxEntities 30#define kRedAnt 1#define kBlackAnt 2#define kTree 3#define kPlayerSpeed 4// entity states#define kForage 1#define kGoHome 2 #define kGuard 3#define kThirsty 4#define kDead 5//#define kChaseDistance 7//#define kMaxPathLength 200// world constants#define kMaxRows 30 #define kMaxCols 10 //#define kMaxTiles 10// world constants//#define kUpKey 1 //#define kDownKey 2 //#define kLeftKey 3//#define kRightKey 4class a i_Entity{public:int type;int state;int row;int col;//unsigned long timeToMove;ai_Entity();~ai_Entity();void Forage(void);void GoHome(void);void Thirsty(void);void Dead(void);void New(int theType, int theState, int theRow, int theCol);int DistanceFrom(int t);};class a i_World{public:ai_World();~ai_World();void UpdateWorld(void);};#endif // ANT_H_INCLUDEDant.cpp#include "ant.h"//#include<cstdlib.h>#include<stdlib.h>#include<time.h>ai_World MainWorld;ai_Entity entityList[kMaxEntities];int terrainBackup[kMaxRows][kMaxCols]; int terrain[kMaxRows][kMaxCols];ai_Entity::ai_Entity(){int i;for (i=0;i<kMaxEntities;i++){entityList[i].row=0;entityList[i].col=0;entityList[i].type=0;entityList[i].state=0;}/* for (i=0;i<KBegEnt;i++){entityList[i].New(kRedAnt,)}*/entityList[0].New(kRedAnt,kForage,5,5);entityList[1].New(kRedAnt,kForage,8,5);entityList[2].New(kBlackAnt,kForage,5,36);entityList[3].New(kBlackAnt,kForage,8,36);}// ----------------------------------------------------------------- //ai_Entity::~ai_Entity()// ----------------------------------------------------------------- //{}// ----------------------------------------------------------------- //void ai_Entity::Forage(void)// ----------------------------------------------------------------- //{int rowMove;int colMove;int newRow;int newCol;int foodRow;int foodCol;int poisonRow;int poisonCol;rowMove=rand()%3-1;colMove=rand()%3-1;newRow=row+rowMove;newCol=col+colMove;if (newRow<1) return;if (newCol<1) return;if (newRow>=kMaxRows-1) return;if (newCol>=kMaxCols-1) 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 {/////////////////////please makesure about the begin and the end//////////foodRow=rand()%(kMaxRows-3)+1;foodCol=rand()%(kMaxCols-3)+1;} 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=rand()%(kMaxRows-3)+1;poisonCol=rand()%(kMaxCols-3)+1;} while (terrain[poisonRow][poisonCol]!=kGround);terrain[poisonRow][poisonCol]=kPoison;}}// ----------------------------------------------------------------- //void ai_Entity::Dead(void)// ----------------------------------------------------------------- //{type=0;row=0;col=0;state=0;}// ----------------------------------------------------------------- //void ai_Entity::Thirsty(void)// ----------------------------------------------------------------- //int rowMove;int colMove;int newRow;int newCol;int foodRow;int foodCol;int poisonRow;int poisonCol;rowMove=rand()%3-1;colMove=rand()%3-1;newRow=row+rowMove;newCol=col+colMove;if (newRow<1) return;if (newCol<1) return;if (newRow>=kMaxRows-1) return;if (newCol>=kMaxCols-1) 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=rand()%(kMaxRows-3)+1;;foodCol=rand()%(kMaxCols-3)+1;} while (terrain[foodRow][foodCol]!=kGround);terrain[foodRow][foodCol]=kWater;}if (terrain[newRow][newCol]==kPoison){row=newRow;col=newCol;state=kDead;do {poisonRow=rand()%(kMaxRows-3)+1;poisonCol=rand()%(kMaxCols-3)+1;} while (terrain[poisonRow][poisonCol]!=kGround);terrain[poisonRow][poisonCol]=kPoison;}}// ----------------------------------------------------------------- //void ai_Entity::GoHome(void)// ----------------------------------------------------------------- //{int rowMove;int colMove;int newRow;int newCol;int homeRow;int homeCol;int i;int poisonRow;int poisonCol;if (type==kRedAnt){homeRow=kRedHomeRow;homeCol=kRedHomeCol;}else{homeRow=kBlackHomeRow;homeCol=kBlackHomeCol;}if (row<homeRow)rowMove=1;else if (row>homeRow)rowMove=-1;elseif (col<homeCol)colMove=1;else if (col>homeCol)colMove=-1;elsecolMove=0;newRow=row+rowMove;newCol=col+colMove;if (newRow<1) return;if (newCol<1) return;if (newRow>=kMaxRows-1) return;if (newCol>=kMaxCols-1) return;if (terrain[newRow][newCol]!=kPoison){row=newRow;col=newCol;}else{row=newRow;col=newCol;terrain[row][col]=kGround;state=kDead;do {poisonRow=rand()%(kMaxRows-3)+1;poisonCol=rand()%(kMaxCols-3)+1;} 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].row=homeRow;entityList[i].col=homeCol;entityList[i].type=type;break;}}}// ----------------------------------------------------------------- //// ----------------------------------------------------------------- //void ai_Entity::New(int theType, int theState, int theRow, int theCol)// ----------------------------------------------------------------- //{type=theType;row=theRow;col=theCol;state=theState;}// ----------------------------------------------------------------- //ai_World::ai_World()// ----------------------------------------------------------------- //{int i;int j;for (i=0;i<kMaxRows;i++)for (j=0;j<kMaxCols;j++){terrain[i][j]=kGround;}terrain[kRedHomeRow][kRedHomeCol]=kRedHome;terrain[kBlackHomeRow][kBlackHomeCol]=kBlackHome;for (i=0;i<kMaxWater;i++)terrain[rand()%(kMaxRows-3)+1][rand()%(kMaxCols-3)+1]=kWater;for (i=0;i<kMaxPoison;i++)terrain[rand()%(kMaxRows-3)+1][rand()%(kMaxCols-3)+1]=kPoison;terrain[rand()%(kMaxRows-3)+1][rand()%(kMaxCols-3)+1]=kFood;//for (i=0;i<kMaxRows;i++)// for (j=0;j<kMaxCols;j++)// terrainBackup[i][j]=terrain[i][j];}// ----------------------------------------------------------------- //ai_World::~ai_World()// ----------------------------------------------------------------- //{}// ----------------------------------------------------------------- //void ai_World::UpdateWorld(void)// ----------------------------------------------------------------- //{int i;for (i=0;i<kMaxEntities;i++){switch (entityList[i].state){case kForage:entityList[i].Forage();break;case kGoHome:entityList[i].GoHome();break;case kThirsty:entityList[i].Thirsty();break;case kDead:entityList[i].Dead();break;}}main.cpp#include <iostream>#include "ant.cpp"#include "ant.h"using namespace std;int main(){int again;do{int terrain2[kMaxRows][kMaxCols]={0};int row,col;cout<<"this is the blackground,and 1->Ground 2->Water 3->BlackHome 4->redhome 5->poision 6->food"<<endl;for(int i=1;i<kMaxCols;i++){for(int j=1;j<kMaxRows;j++){cout<<terrain[i][j];}cout<<endl;}cout<<"this is the ant location "<<endl;for(int i=0;i<kMaxEntities;i++){if(entityList[i].state!=kDead)row=entityList[i].row;col=entityList[i].col;terrain2[row][col]++;}for(int f=1;f<kMaxCols;f++){for(int j=1;j<kMaxRows;j++){cout<<terrain2[f][j];}cout<<endl;}cout<<"if you want to again ,please input the 1"<<endl;cin>>again;MainWorld.UpdateWorld();}while(again==1);}四、实验结果四、实验心得书上的代码抄死人了。