游戏人工智能实验报告四

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验四有限状态机实验

实验报告

一、实验目的

通过蚂蚁世界实验掌握游戏中追有限状态机算法

二、实验仪器

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 200

class 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 2

int RedHomeRow;

int RedHomeCol;

int BlackHomeRow;

int BlackHomeCol;

int RedNum=2;

int BlackNum=2;

//地图大小,可改变

#define kMaxRows 30

#define kMaxCols 40

#define LENGTH 20

int 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()

相关文档
最新文档