有限状态机设计与实现.docx

上传人:小飞机 文档编号:5335143 上传时间:2023-06-27 格式:DOCX 页数:26 大小:123.33KB
返回 下载 相关 举报
有限状态机设计与实现.docx_第1页
第1页 / 共26页
有限状态机设计与实现.docx_第2页
第2页 / 共26页
有限状态机设计与实现.docx_第3页
第3页 / 共26页
有限状态机设计与实现.docx_第4页
第4页 / 共26页
有限状态机设计与实现.docx_第5页
第5页 / 共26页
亲,该文档总共26页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《有限状态机设计与实现.docx》由会员分享,可在线阅读,更多相关《有限状态机设计与实现.docx(26页珍藏版)》请在三一办公上搜索。

1、游戏引擎目录一、实验目的二、实验要求三、实验内容及原理四、实验设计思路五、状态转换表六、静态类图七、关键代码八、调试截图一、实验目的通过深入研究分析、设计、实现及测试有限状态机架构,掌握常规的FSM架构设 计与实现。二、实验要求1)FSM状态装换图。2)设计算法的静态类图3)采用基于数据结构的方法、或STL/JDK标准类库编写代码4)可选择C+、Java其中一种语言5)程序测试用例:设定某种初始状态,之后智能体在各状态间正常随机转化即可。三、实验原理及内容有限状态机(FSM),简称状态机,是表示有限个状态以及在这些状态之间的 转移和动作等行为的数学模型。可以这样理解,系统的行为如果在不同的时间

2、(环 境)下,其工作不同,并且行为可以分成所谓的有限的状态以及不重叠的程序块 时,系统显现出了状态行为。有限状态机的基本原理图(ChairMan Mao的):总之,“有限状态机”是一个有限的状态组成的,其中的一个状态是“当前状态”。 “有限状态机”可以接受一个“输入”,这个“输入”的结果将导致一个“状态 转换”的发生(即从“当前状态”转换到“输出”状态)。这个“状态转换”是 基于“状态转换函数”的。状态转换完成之后,“输出状态”即变成了 “当前状 态”。根据这些输入输出,可以画出状态转移表(ChairMan Mao的状态转移表)当前状态输入条件输出状态Walk群众向主席握手ShakeHand群

3、众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkShakeHand群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkWaveHand群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkDownHat群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路Walk智能体隔一定时间查询这个表格,以使得它能够从游戏环境接收消息,来进 行必需的状态转换。每一个状态都能够实现为彼此分离的与智能体不耦合的对象 或函数,以提供清晰和可

4、伸缩的架构。这一设计不像if . then/Switch架构那 样成为意大利面条。这种方式也就是将状态迁移的所有可能固化,甚至在游戏中作为脚本提供。这样的话,一方面易于理解,因为逻辑集中;另一方面维护容易,要增加新的状 态与控制逻辑,在迁移表中修改即可,甚至可以动态修改。C+的指针完美地支 持了这一点。在状态变化比较简单的情况下,这是一种比较可取的方法。但是如 果状态变化比较复杂或者控制逻辑比较复杂的话,查表过程可能会花费较多时 间。四、实验设计思路在ChairMan这个项目当中一共设计了三个有限状态机,分别是毛主席ChairMan, 群众化身Audience和情报员Intelligencer

5、。其中毛主席有限状态机与群众化身 之间通过消息系统进行交流互动。这两个有限状态机有同样的状态,分别是:走 路、握手、挥手、敬礼。为了做到毛主席根据群众化身状态翻转做相应的翻转, 设计了一个拥有4个消息种类的消息系统。这四个消息分别是:Msg_waveHand, Msg_shakeHand,Msg_downHat,Msg_walk;群众化身Audience向毛主席ChairMan 发送消息,毛主席状态机就会发生相应的状态转移。而群众化身Audience的状态 转移是随机分配的。由RandFloat()的大小来决定状态翻转。情报员有限状态机则是在自己的状态机里翻转,它有三个状态,分别是:全局状 态

6、GlobalState、收集情报Investigation和汇报情报GetReport。由于技术还不 够成熟,没有增加毛主席听报告的状态和消息设置,这也是将要扩展的功能。五、状态转换表A、ChairMan状态转移表当前状态输入条件输出状态Walk群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkShakeHand群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkWaveHand群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkD

7、ownHat群众向主席握手ShakeHand群众向主席挥手WaveHand群众向主席敬礼DownHat群众走路WalkB、Audience状态转移表当前状态输入条件输出状态DoWalkRandFloat() 0.4)&(RandFloat() 0.6)&(RandFloat() 1)DoDownHatDoShakeHandRandFloat() 0.4)&(RandFloat() 0.6)&(RandFloat() 1)DoDownHatDoWaveHandRandFloat() 0.4)&(RandFloat() 0.6)&(RandFloat() 1)DoDownHatDoDownHatR

8、andFloat() 0.4)&(RandFloat() 0.6)&(RandFloat() 1)DoWalk六、静态类图A、ChairMan静态类图(部分)群众向主席挥手群众走路群众向 王席散礼WaveHandShakeHand群众向主席握手DownHatWalkC、Intelligencer 静态类图返回前一状态七、关键代码1)项目头文件(3-miscE ConsoleUtils.h屈 CrudeTimer.cppE CrudeTimer.hE utils.hI白“曰头文件E Audience.hE AudienceOwnedStates.hE BaseGameEntity.hE Chai

9、rh/lan.hE ChairManOwnedStates.hE EntityManager.hE EntityNames.hE Intelligencer.hE IntelligencerOwnedStates.hE MessageDispatcher.hE MessageTypes.hE State.hE Stateh/lachine.hE Telegram.h2)项目源文件e - n般件色I Audience.cpp同 AudienceOwnedStates.cpp同 BaseGameEntity.cpp同 Chairh/lan.cpp羽 Chairh/lanOwnedStates.cp

10、p羽 EntityManager.cpp羽 Intelligencer.cpp羽 IntelligencerOwnedStates.cpp对 main.cpp羽 MessageDispatcher.cppi白资敬件3)关键代码ChairManOwnedStates.cpp #include utils.h #include ChairManOwnedStates.h#include State.h #include ChairMan.h #include Telegram.h#include MessageDispatcher.h#include MessageTypes.h#include

11、CrudeTimer.h#include EntityNames.h #include using std:cout;#ifdef TEXTOUTPUT#include extern std:ofstream os; #define cout os#endif/methods for WalkWalk* Walk:Instance()(static Walk instance;return instance;void Walk:Enter(ChairMan* pChairMan)(void Walk:Execute(ChairMan* pChairMan)(cout n ID() : 毛主席在

12、路上继续走着。;void Walk:Exit(ChairMan* pChairMan)(bool Walk:OnMessage(ChairMan* pChairMan, const Telegram& msg)(SetTextColor(BACKGROUND_RED|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUN D_BLUE);switch (msg.Msg)(case Msg_waveHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-Chang

13、eState(WaveHand:Instance();return true;case Msg_shakeHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(ShakeHand:Instance();return true;case Msg_downHat :cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(DownHat:Instanc

14、e();return true;/end switchreturn false; /send message to global message handler/methods for ShakeHandShakeHand* ShakeHand:Instance()(static ShakeHand instance;return &instance;void ShakeHand:Enter(ChairMan* pChairMan)(void ShakeHand:Execute(ChairMan* pChairMan)(/wealthy enough to have a well earned

15、 rest?cout n ID() :GetFSM()-RevertToPreviousState();void ShakeHand:Exit(ChairMan* pChairMan)(bool ShakeHand:OnMessage(ChairMan* pChairMan, const Telegram& msg)(SetTextColor(BACKGROUND_RED|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUN D_BLUE);switch (msg.Msg)(case Msg_waveHand:cout n Message handled by I

16、D() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(WaveHand:Instance();return true;case Msg_shakeHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(ShakeHand:Instance();return true;case Msg_downHat :cout n Message handled by ID() at time: GetCurren

17、tTime();pChairMan-GetFSM()-ChangeState(DownHat:Instance();return true;/end switchreturn false; /send message to global message handler/methods for DownHatDownHat* DownHat:Instance()(static DownHat instance;return instance;void DownHat:Enter(ChairMan* pChairMan)(void DownHat:Execute(ChairMan* pChairM

18、an)(cout n ID() : 毛主席向群众敬礼;void DownHat:Exit(ChairMan* pChairMan)(bool DownHat:OnMessage(ChairMan* pChairMan, const Telegram& msg)(SetTextColor(BACKGROUND_RED|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUN D_BLUE);switch (msg.Msg)(case Msg_waveHand:cout n Message handled by ID() at time: GetCurrentTime()

19、;pChairMan-GetFSM()-ChangeState(WaveHand:Instance();return true;case Msg_shakeHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(ShakeHand:Instance();return true;case Msg_downHat :cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-Ch

20、angeState(DownHat:Instance();return true;/end switchreturn false; /send message to global message handler/WaveHandWaveHand* WaveHand:Instance()(static WaveHand instance;return instance;void WaveHand:Enter(ChairMan* pChairMan)(void WaveHand:Execute(ChairMan* pChairMan)(cout n ID() : 毛主席 与小男孩挥手:再见,细伢子

21、!;void WaveHand:Exit(ChairMan* pChairMan)(bool WaveHand:OnMessage(ChairMan* pChairMan, const Telegram& msg)(SetTextColor(BACKGROUND_RED|FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUN D_BLUE);switch (msg.Msg)(case Msg_waveHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-Cha

22、ngeState(WaveHand:Instance();return true;case Msg_shakeHand:cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(ShakeHand:Instance();return true;case Msg_downHat :cout n Message handled by ID() at time: GetCurrentTime();pChairMan-GetFSM()-ChangeState(DownHat:Insta

23、nce();return true;/end switchreturn false; /send message to global message handlerAudienceOwnedStates.cpp:#include AudienceOwnedStates.h #include ChairManOwnedStates.h#include Audience.h #include CrudeTimer.h#include MessageDispatcher.h#include MessageTypes.h#include EntityNames.h#include utils.h #i

24、nclude using std:cout;#ifdef TEXTOUTPUT#include extern std:ofstream os;#define cout os#endif/DoWalkDoWalk* DoWalk:Instance()(static DoWalk instance;return instance;void DoWalk:Enter(Audience* audience)(void DoWalk:Execute(Audience* audience)(cout n ID() :群众在路上走着!;if (RandFloat() GetFSM()-ChangeState

25、(DoWaveHand:Instance();if (RandFloat() 0.4)&(RandFloat() GetFSM()-ChangeState(DoShakeHand:Instance();if (RandFloat() 0.6)&(RandFloat() GetFSM()-ChangeState(DoDownHat:Instance();void DoWalk:Exit(Audience* audience)(bool DoWalk:OnMessage(Audience* audience, const Telegram& msg)(return false;DoDownHat敬

26、礼/DoDownHat* DoDownHat:Instance() (static DoDownHat instance; return &instance; void DoDownHat:Enter(Audience* audience) ( void DoDownHat:Execute(Audience* audience) (:群众向主席敬cout n ID() 礼!;/time delay/sender ID/receiver IDDispatch-DispatchMessage(SEND_MSG_IMMEDIATELY, audience-ID(), ent_ChairMan, Ms

27、g_downHat, NO_ADDITIONAL_INFO);/msgif (RandFloat() GetFSM()-ChangeState(DoWaveHand:Instance(); if (RandFloat() 0.4)&(RandFloat() GetFSM()-ChangeState(DoShakeHand:Instance();if (RandFloat() 0.6)&(RandFloat() GetFSM()-ChangeState(DoWalk:Instance();void DoDownHat:Exit(Audience* audience)(bool DoDownHat

28、:OnMessage(Audience* audience, const Telegram& msg)(return false;/DoShakeHand/握手DoShakeHand* DoShakeHand:Instance()(static DoShakeHand instance;return &instance;void DoShakeHand:Enter(Audience* audience)(void DoShakeHand:Execute(Audience* audience)(cout n ID() DispatchMessage(SEND_MSG_IMMEDIATELY, /

29、time delayaudience-ID(),/sender IDent_ChairMan,/receiver IDMsg_shakeHand,NO_ADDITIONAL_INFO);/msgif (RandFloat() GetFSM()-ChangeState(DoWaveHand:Instance();if (RandFloat() 0.4)&(RandFloat() GetFSM()-ChangeState(DoWalk:Instance();if(RandFloat() 0.6)&(RandFloat() GetFSM()-ChangeState(DoDownHat:Instanc

30、e();void DoShakeHand:Exit(Audience* audience)(bool DoShakeHand:OnMessage(Audience* audience, const Telegram& msg)(return false;/DoWaveHand/挥手DoWaveHand* DoWaveHand:Instance()(static DoWaveHand instance;return &instance;void DoWaveHand:Enter(Audience* audience)(void DoWaveHand:Execute(Audience* audie

31、nce)(cout n ID() DispatchMessage(SEND_MSG_IMMEDIATELY,/time delayaudience-ID(),/sender IDent_ChairMan,/receiver IDMsg_waveHand,/msgNO_ADDITIONAL_INFO);if (RandFloat() GetFSM()-ChangeState(DoWalk:Instance();if (RandFloat() 0.4)&(RandFloat() GetFSM()-ChangeState(DoShakeHand:Instance();if(RandFloat() 0

32、.6)&(RandFloat() GetFSM()-ChangeState(DoDownHat:Instance();void DoWaveHand:Exit(Audience* audience)(bool DoWaveHand:OnMessage(Audience* audience, const Telegram& msg)(return false;IntelligencerOwnedStates.cpp: #include IntelligencerOwnedStates.h#include ChairManOwnedStates.h” #include Intelligencer.

33、h#include CrudeTimer.h”#include MessageDispatcher.h#include MessageTypes.h”#include EntityNames.h”#include using std:cout;#ifdef TEXTOUTPUT#include extern std:ofstream os;#define cout os#endif/GlobalStateIntelligencerGlobalState* IntelligencerGlobalState:Instance() (static IntelligencerGlobalState i

34、nstance;return instance;void IntelligencerGlobalState:Execute(Intelligencer* intelligencer)(/1 in 10 chance of needing the bathroom (provided she is not already /in the bathroom)if ( (RandFloat() GetFSM()-isInState(*GetReport:Instance()(intelligencer-GetFSM()-ChangeState(GetReport:Instance();bool In

35、telligencerGlobalState:OnMessage(Intelligencer* intelligencer, const Telegram& msg)(return false;/InvestigationInvestigation * Investigation:Instance() (static Investigation instance;return &instance;void Investigation:Enter(Intelligencer* intelligencer) (void Investigation:Execute(Intelligencer* in

36、telligencer)(cout n ID() :侦查员在 前沿侦查情报。;void Investigation:Exit(Intelligencer* intelligencer)(bool Investigation:OnMessage(Intelligencer* intelligencer, const Telegram& msg)(return false;/GetReportGetReport* GetReport:Instance()(static GetReport instance;return &instance;void GetReport:Enter(Intellig

37、encer* intelligencer)(cout n ID() ”:收集到重 要情报!;void GetReport:Execute(Intelligencer* intelligencer)(cout n ID() GetFSM()-RevertToPreviousState();void GetReport:Exit(Intelligencer* intelligencer)(cout n ID() ”:向主席回 报紧急情报!; bool GetReport:OnMessage(Intelligencer* intelligencer, const Telegram& msg)(ret

38、urn false;Main.cpp :#include #include #include ChairMan.h”#include Audience.h#include Intelligencer.h”#include EntityManager.h”#include MessageDispatcher.h”#include ConsoleUtils.h”#include EntityNames.h”std:ofstream os;int main()(/define this to send output to a text file (see locations.h)#ifdef TEXTOUTPUTos.open(output.txt);#endif/seed random number generator srand(unsigned) time(NULL);Audience* pAudience = new Audience(ent_Audience);ChairMan* pChairMan =

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 生活休闲 > 在线阅读


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号