游戏人工智能之有限状态机初探(一)
来源:互联网 发布:查看linux snmp信息 编辑:程序博客网 时间:2024/06/05 22:31
游戏人工智能之有限状态机初探(一)
原创文章,转载请注明原文地址。
一、什么是有限状态机
有限状态机,(英语:Finite-state machine, FSM),又称有限状态自动机,简称状态机,是表示有限个状态以及在这些状态之间的转移和动作等行为的数学模型。
一个有限状态机是一个设备,或是一个设备模型,具有有限数量的状态,它可以在任何给定的时间根据输入进行操作,使得从一个状态变换到另一个状态,或者促使一个输出或者一种行为的发生。一个有限状态机在任何瞬间只能处在一种状态。
二、有限状态机初探
先模拟编译器中一个简单的词法分析状态机:
#include <stdio.h>#include <string.h>#include <ctype.h>#define BSIZE 1024 enum StateType{ Alpha, Digit, Symbol };char *key_list[] = { "int", "main", "if", "return", "while", "do", "then" };char *border_list[] = { ";", ",", "{", "}", ")", "(" };char *operator_list[] = { "=", "+", "-", "*", "/", ">", "<" };char buffer[1024];int start;char headch;FILE *fp;void deal_alpha(){ int flag, i; char word[100]; word[0] = headch; for (i = start; isdigit(buffer[i]) || isalpha(buffer[i]); i++){ word[i - start] = buffer[i]; } word[i - start] = '\0'; start = i; headch = buffer[start]; flag = is_key_word(word); if (flag == -1){ printf("( 2: “%s”)\n", word); } else{ printf("( 1: “%s”)\n", word); }}void deal_dight(){ char word[100]; int i; word[0] = headch; for (i = start; isdigit(buffer[i]) || buffer[i] == '.'; i++){ word[i - start] = buffer[i]; } word[i - start] = '\0'; start = i; headch = buffer[start]; printf("( 3:“%s”)\n", word);}void deal_symbol(){ char s[3]; int i; s[0] = buffer[start]; s[1] = '\0'; for (i = 0; operator_list[i][0]; i++) { if (strcmp(s, operator_list[i]) == 0) { start++; headch = buffer[start]; printf("( 4: “%s”)\n", s); return; } } printf("( 5: “%c”)\n", headch); start++; headch = buffer[start];}void updateState(StateType state){ switch (state) { case Alpha: Deal_alpha(); break; case Digit: Deal_dight(); break; case Symbol: Deal_symbol(); break; }}int main(){ fp = fopen("test.txt", "r"); if (fp == NULL){ printf("无法打开文件!"); return -1; } while (fgets(buffer, BSIZE, fp) != NULL) { int len = strlen(buffer); buffer[len - 1] = '\0'; start = 0; headch = buffer[start]; while (headch != '\0') { while (buffer[start] == ' '){ start++; } headch = buffer[start]; if (headch == '\0'){ break; } else if (isalpha(headch)){ updateState(Alpha); } else if (isdigit(headch)){ updateState(Digit); } else{ updateState(Symbol); } } } return 0;}
分析的程序:
int main() { int a,b;a = 10;b = a + 20;return 0;}
输出结果:
这种方法使用了一系列的if-else语句和一个具有枚举类型的switch语句来表达状态。虽然初看之下这种方法是合理的,但当实际应用到任何一个复杂情况时,随着更多的状态和条件被加入,程序变得越来越庞大,几乎难以扩展。
三、使用面向对象的思想和设计模式
Singleton设计模式:确保一个对象只能实例化一次
FSM中的每一个状态都是singleton的。
直接上代码:
class Myclass{private: int num;//成员属性 MyClass(){};//构造器是私有的public: ~MyClass(){}; int GetNum() const {return num;} static Myclass* Instance(); };Myclass* Myclass::Instance(){ static Myclass instance; return &instance;}
访问成员属性时:
int num = Myclass::Instance()->GetNum();
Singleton模式的实现方式多种多样,具体大家可以参考《设计模式》一书。
使用OOAP设计FSM我准备放到状态驱动智能体设计中去,如果大家感兴趣可以在www.jblearning.com/catalog/9781556220784下载相关源代码进行学习。
参考文献:
1.游戏人工智能编程案例精粹 【美】Mat Buckland 著
如果大家看完觉得有帮助,请给我点个赞,我希望能和大家一起学习一起进步。>_<
阅读全文
1 0
- 游戏人工智能之有限状态机初探(一)
- 游戏开发中的人工智能(九):有限状态机
- 有限状态机(FSM)初探
- 游戏AI之有限状态机(1)
- cocos2d-x 游戏开发之有限状态机(FSM) (一)
- 游戏中的人工智能(一)
- 游戏开发中的人工智能(一):游戏人工智能简介
- 游戏人工智能 状态驱动智能体设计——有限状态机(FSM)
- 游戏开发中的人工智能——有限状态机和神经网络
- unity 有限状态机(一)
- 游戏人工智能(AI)开发(一)
- 游戏人工智能(AI)开发(一)
- Unity3D架构系列之-FSM有限状态机设计(一)
- 《游戏人工智能编程》感想一
- WebRTC之初探(一)
- 有限状态机的编写(游戏AI学习)
- 游戏编程中的人工智能技术-神经网络入门(一)
- [视频讲解+附下载云盘地址]游戏人工智能—有限状态机FSM
- 异常
- 实现后台管理登陆
- python中event事件应用举例
- jsp内置对象out
- 238. Product of Array Except Self
- 游戏人工智能之有限状态机初探(一)
- centos配置ssh
- POJ 1935 Journey 笔记
- mac下WebStrom + node.js +jdk环境变量配置
- git学习------>如何汉化GitLab?
- Netty User guide
- Ubuntu14配置OpenCV2.4.11
- Effective Java 笔记1
- OkHttp3简单的使用说明和Cookie自动化管理管理