这个有限状态机实现的牛【C++】

[复制链接]
 楼主| keer_zu 发表于 2022-10-11 16:24 | 显示全部楼层 |阅读模式

游客,如果您要查看本帖隐藏内容请回复

 楼主| keer_zu 发表于 2022-10-11 16:32 | 显示全部楼层
再给你看一个使用这个状态机的demo

  1. // basic hfsm sample

  2. #include <iostream>
  3. #include "fsm.hpp"

  4. // custom states (gerunds) and actions (infinitives)

  5. enum {
  6.     walking = 'WALK',
  7.     defending = 'DEFN',

  8.     tick = 'tick',
  9. };

  10. struct ant_t {
  11.     fsm::stack fsm;
  12.     int health, distance, flow;

  13.     ant_t() : health(0), distance(0), flow(1) {
  14.         // define fsm transitions: on(state,trigger) -> do lambda
  15.         fsm.on(walking, 'init') = [&]( const fsm::args &args ) {
  16.             std::cout << "initializing" << std::endl;
  17.         };
  18.         fsm.on(walking, 'quit') = [&]( const fsm::args &args ) {
  19.             std::cout << "exiting" << std::endl;
  20.         };
  21.         fsm.on(walking, 'push') = [&]( const fsm::args &args ) {
  22.             std::cout << "pushing current task." << std::endl;
  23.         };
  24.         fsm.on(walking, 'back') = [&]( const fsm::args &args ) {
  25.             std::cout << "back from another task. remaining distance: " << distance << std::endl;
  26.         };
  27.         fsm.on(walking, tick) = [&]( const fsm::args &args ) {
  28.             std::cout << "\r" << "\\|/-"[ distance % 4 ] << " walking " << (flow > 0 ? "-->" : "<--") << " ";
  29.             distance += flow;
  30.             if( 1000 == distance ) {
  31.                 std::cout << "at food!" << std::endl;
  32.                 flow = -flow;
  33.             }
  34.             if( -1000 == distance ) {
  35.                 std::cout << "at home!" << std::endl;
  36.                 flow = -flow;
  37.             }
  38.         };
  39.         fsm.on(defending, 'init') = [&]( const fsm::args &args ) {
  40.             health = 1000;
  41.             std::cout << "somebody is attacking me! he has " << health << " health points" << std::endl;
  42.         };
  43.         fsm.on(defending, tick) = [&]( const fsm::args &args ) {
  44.             std::cout << "\r" << "\\|/-$"[ health % 4 ] << " health: (" << health << ")   ";
  45.             --health;
  46.             if( health < 0 ) {
  47.                 std::cout << std::endl;
  48.                 fsm.pop();
  49.             }
  50.         };

  51.         // set initial fsm state
  52.         fsm.set( walking );
  53.     }
  54. };

  55. int main() {
  56.     ant_t ant;
  57.     for(int i = 0; i < 12000; ++i) {
  58.         if( 0 == rand() % 10000 ) {
  59.             ant.fsm.push(defending);
  60.         }
  61.         ant.fsm.command(tick);
  62.     }
  63. }
努力搬砖 发表于 2022-10-11 16:34 | 显示全部楼层
LOOK 一下
chailiuqing 发表于 2022-10-11 16:36 | 显示全部楼层
111111
 楼主| keer_zu 发表于 2022-10-11 16:37 | 显示全部楼层
 楼主| keer_zu 发表于 2022-10-11 16:37 | 显示全部楼层
 楼主| keer_zu 发表于 2022-10-11 16:38 | 显示全部楼层
FSM is a lightweight finite-state machine class. Both Hierarchical FSM and simple FSM implementations are provided.
han0097 发表于 2022-10-11 16:50 | 显示全部楼层
不回复还看不了

评论

[url=home.php?mod=space&uid=472461]@keer_zu[/url] :哈哈,没问题  发表于 2022-10-19 12:07
哈哈,随手回复一下,也算咱的劳动成果吧  发表于 2022-10-11 16:53
xl071310 发表于 2022-10-11 17:06 | 显示全部楼层
Thanks
 楼主| keer_zu 发表于 2022-10-11 17:25 来自手机 | 显示全部楼层
xl071310 发表于 2022-10-11 17:06
Thanks

真不错
kejian2000 发表于 2022-12-6 08:33 | 显示全部楼层
这个有限状态机实现的牛

评论

是的  发表于 2022-12-6 08:57
 楼主| keer_zu 发表于 2022-12-6 08:57 | 显示全部楼层
kejian2000 发表于 2022-12-6 08:33
这个有限状态机实现的牛

是的,首先不需要修改之前代码(开闭原则),其次,接口非常简单,很容易添加到自己的应用。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1474

主题

12900

帖子

55

粉丝
快速回复 返回顶部 返回列表