三消游戏FSM状态机设计图

1) 设计FSM图

2) smc配置文件

///////////////////////////////////////////////////////////////////////
// ColorTable.sm
//   -- ColorTable State Map for C++ classes auto-generation
//
// 1) generate c++ classes:
//   $ java -jar Smc.jar -c++ ColorTable.sm
//
// 2) generate graphviz dot graph:
//   $ java -jar Smc.jar -graph -glevel 1 ColorTable.sm
//
// see also:
//   http://graphviz.org/
//
// Author: cheungmine
// Copyright 2015-?, All rights reserved.
//
///////////////////////////////////////////////////////////////////////
%class ColorTable
%header ColorTable.h
%start ColorTableMap::Vacancy
%map ColorTableMap
%%
/**
* State {
*     Transition [Guard Condition]
*		EndState {
*			Action(s)
*		}
*
*     Transition [context.getOwner().is_valid()]
*		EndState {
*			Action(s)
*		}
*     ...
* }
*/

//==> ColorTableMap::Vacancy
Vacancy
Entry {
	enterVacancy();
}
Exit {
	leaveVacancy();
}
{
	// Expose Transitions to other parts if necessary
	shuffle
		ShuffleFilled {
			shuffle();
		}
}

//==> ColorTableMap::ShuffleFilled
// only swappable state
ShuffleFilled
Entry {
	enterShuffleFilled();
}
Exit {
	leaveShuffleFilled();
}
{
	// !!can swap here!!
	swap [ context.getOwner().swap_done() ]
		Filled {
		}
	swap
		ShuffleFilled {
			// set stack top sub sm: SwapMap
		}
}

//==> ColorTableMap::FallenOver
FallenOver
Entry {
	enterFallenOver();
}
Exit {
    leaveFallenOver();
}
{
	// Expose Transitions to other parts if necessary
	fill [ context.getOwner().is_empty() ]
		Vacancy {
		}
	fill
		Filled {
			fill();
		}
}

//==> ColorTableMap::Filled
Filled
Entry {
	enterFilled();
}
Exit {
	leaveFilled();
}
{
	erase [ context.getOwner().can_erase() ]
		Unfilled {
			erase();
		}
	erase
		Vacancy {
			empty();
		}
}

//==> ColorTableMap::Unfilled
Unfilled
Entry {
	enterUnfilled();
}
Exit {
	leaveUnfilled();
}
{
	fall
		Fallen {
			fall();
		}
}

//==> ColorTableMap::Fallen
Fallen
Entry {
    enterFallen();
}
Exit {
	leaveFallen();
}
{
	erase [ context.getOwner().can_erase() ]
		Unfilled {
			erase();
		}

	erase
		FallenOver {
			empty();
		}
}
%%

3) ColorTable.h和ColorTable.cpp

/**
* ColorTable.h
* 2015-02-01
* cheungmine, all rights reserved.
*
*        col->
*      +-------+-------+-------+-------+-------+-------+-------+
*  row |00     |01     |02     |03     |04     |05     |06     |
*   |  |       |       |       |       |       |       |       |
*   v  |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |10     |11     |12     |13     |14     |15     |16     |
*      |       |       |       |       |       |       |       |
*      |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |20     |21     |22     |23     |24     |25     |26     |
*      |       |(xp,yp)|       |       |       |       |       | height
*      |       | .     |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*      |30     |31     |32     |33     |34     |35     |36     |
*      |       |       |       |       |       |       |       |
*      |       |       |       |       |       |       |       |
*      +-------+-------+-------+-------+-------+-------+-------+
*  y   |40     |41     |42     |43     |44     |45     |46     |
*  ^   |       |       |       |       |       |       |       |
*  |   |       |       |       |       |       |       |       |
*  |   o-------+-------+-------+-------+-------+-------+-------+
*  |  origin                     width
*  +------------>x
* (0,0)
*
*/
#ifndef __COLORTABLE_H__
#define __COLORTABLE_H__

#include "cocos2d.h"
USING_NS_CC;

#include "ColorTable_sm.h"

class ColorTable : public Node
{
public:

    // Global Settings:

    static const int ColorTable_NumRows = 5;
    static const int ColorTable_NumCols = 7;
    static const int ColorTable_NumCells = ColorTable_NumRows*ColorTable_NumCols;
    static const int ColorTable_NumColors = 4;
    static const int ColorTable_ColorsPal[ColorTable_NumColors+1];    // colors palette

    // Construct and Deconstruct

    ColorTable() :
       swap_done_(false),
      _fsm(0),
      _DeltaTime(0.1f),
      _NumRows(ColorTable_NumCols),
      _NumCols(ColorTable_NumRows),
      _NumCells(ColorTable_NumCells)
    {
    }

    // Public Types:

    enum EnumColor
    {
        INVALID = 0,
        RED     = 1,
        GREEN   = 2,
        BLUE    = 3,
        YELLOW  = 4
    };

    // Public Inline Methods:

    inline int index(int row, int col) const
    {
        return (col*_NumCols + row);
    }

    inline int color(int row, int col) const
    {
        return table_[index(row, col)];
    }

    inline int result(int row, int col) const
    {
        return results_[index(row, col)];
    }

public:
    // Public Methods:
    CREATE_FUNC(ColorTable);

    virtual bool init();

    bool is_empty();

    bool can_erase();

    bool swap_done();

    void shuffle();    

    void empty();

    void erase();

    void fall();

    void fill();

    void print();

public:
    // Public Callbacks:
    void enterVacancy();
    void leaveVacancy();

    void enterShuffleFilled();
    void leaveShuffleFilled();

    void enterFallenOver();
    void leaveFallenOver();

    void enterFilled();
    void leaveFilled();

    void enterUnfilled();
    void leaveUnfilled();

    void enterFallen();
    void leaveFallen();

    // Public Events:
    void onIdleVacancy(float dt);
    void onIdleShuffleFilled(float dt);
    void onIdleFallenOver(float dt);
    void onIdleFilled(float dt);
    void onIdleUnfilled(float dt);
    void onIdleFallen(float dt);

private:
    // Private Methods:

    void init_table();

    void exit();

private:
    // Private Members:

    ColorTableContext * _fsm;

    const int _NumRows;
    const int _NumCols;
    const int _NumCells;
    const float _DeltaTime;

    bool swap_done_;

    int table_[ColorTable_NumCells];
    int results_[ColorTable_NumCells];
};

#endif // __COLORTABLE_H__

/**
* ColorTable.cpp
* 2015-02-01
* cheungmine, all rights reserved.
*
*/
#include "ColorTable.h"

const int ColorTable::ColorTable_ColorsPal[ColorTable_NumColors+1] = {'O', 'R', 'G', 'B', 'Y'};

/////////////////////////// Private Methods ////////////////////////////

void ColorTable::init_table()
{
    int fixed[] = {
        1,1,3,1,2,
        3,2,1,1,4,
        2,2,4,3,3,
        1,3,3,4,1,
        3,4,4,2,4,
        2,2,4,1,1,
        2,1,1,2,4
    };

    int at = 0;
    int * p = table_;

    while (p - table_ < _NumCells) {
        *p++ = fixed[at++];
    }
}

/////////////////////////// Public Callbacks ///////////////////////////

void ColorTable::enterVacancy()
{
    empty();

    this->schedule(schedule_selector(ColorTable::onIdleVacancy), _DeltaTime);
}

void ColorTable::leaveVacancy()
{
    exit();
}

void ColorTable::enterShuffleFilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleShuffleFilled), _DeltaTime);
}

void ColorTable::leaveShuffleFilled()
{
    exit();
}

void ColorTable::enterFallenOver()
{
    this->schedule(schedule_selector(ColorTable::onIdleFallenOver), _DeltaTime);
}

void ColorTable::leaveFallenOver()
{
    exit();
}

void ColorTable::enterFilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleFilled), _DeltaTime);
}

void ColorTable::leaveFilled()
{
    exit();
}

void ColorTable::enterUnfilled()
{
    this->schedule(schedule_selector(ColorTable::onIdleUnfilled), _DeltaTime);
}

void ColorTable::leaveUnfilled()
{
    exit();
}

void ColorTable::enterFallen()
{
    this->schedule(schedule_selector(ColorTable::onIdleFallen), _DeltaTime);
}

void ColorTable::leaveFallen()
{
    exit();
}

//////////////////////////// Public Events ////////////////////////////

void ColorTable::onIdleVacancy(float dt)
{
    _fsm->shuffle();
}

void ColorTable::onIdleShuffleFilled(float dt)
{
}

void ColorTable::onIdleFallenOver(float dt)
{
}

void ColorTable::onIdleFilled(float dt)
{
}

void ColorTable::onIdleUnfilled(float dt)
{
}

void ColorTable::onIdleFallen(float dt)
{
}

/////////////////////////// Public Methods ////////////////////////////

bool ColorTable::init()
{
    _fsm = new ColorTableContext(*this);
    _fsm->setDebugFlag(true);
    _fsm->enterStartState();

    return true;
}

void ColorTable::exit()
{
    this->unscheduleAllCallbacks();
}

bool ColorTable::is_empty()
{
    for (int index = 0; index < _NumCells; ++index) {
        if (table_[index] != INVALID) {
            return false;
        }
    }
    return true;
}

bool ColorTable::can_erase()
{
    return false;
}

bool ColorTable::swap_done()
{
    return swap_done_;
}

void ColorTable::shuffle()
{
    init_table();
}

void ColorTable::empty()
{
    for (int cell = 0; cell < _NumCells; ++cell) {
        table_[cell] = INVALID;
        results_[cell] = 0;
    }
}

void ColorTable::erase()
{
}

void ColorTable::fall()
{
}

void ColorTable::fill()
{
}

void ColorTable::print()
{
#if defined(COCOS2D_DEBUG) && defined(WIN32)
    FILE * fp = fopen("c:/temp/color-table.prt", "a+");
    if (fp) {
        fprintf(fp, "\n>>>> %s {", _fsm->getState().getName());
        fprintf(fp, "\n---- TABLE ----\n");
        for (int row = 0; row < ColorTable_NumRows; ++row) {
            for (int col = 0; col < ColorTable_NumCols; ++col) {
                int v = color(row, col);
                fprintf(fp, " %c", ColorTable_ColorsPal[v]);
            }
            fprintf(fp, "\n");
        }
        fprintf(fp, "\n---- RESULTS ----\n");
        for (int row = 0; row < ColorTable_NumRows; ++row) {
            for (int col = 0; col < ColorTable_NumCols; ++col) {
                int v = result(row, col);
                fprintf(fp, " %d", v);
            }
            fprintf(fp, "\n");
        }
        fprintf(fp, "}\n");
        fclose(fp);
    }
#endif
}

三消游戏FSM状态机设计图的更多相关文章

  1. C++ 三消游戏基本实现

    最近在研究三消算法,我想试试在完全不借助网络资源的情况下搞定这个东西,所以有些地方可能不是最优的. 代码留此备忘. 1. 3x_desk_event.h 1 #pragma once 2 3 #ifn ...

  2. 消消乐、candy crush类三消游戏程序逻辑分析

    最近在开发一款类似消消乐的三消游戏,在碰到实现斜方向下落的时候卡住了很长时间.好几天没有思路,原本的思路是一次性预判多个宝石的一连串运动路径,运用缓动运动队列来实现宝石运动路径,例如 下落->滑 ...

  3. cocos2d-x 3.2 它 三消游戏——万圣节大作战

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  4. Unity3d开发“类三消”游戏

    新建一个Project,导入图片素材和声音文件,把图片的Texture Type都修改为Sprite(2D and UI)[1].新建一个命名为Background的GameObject,为之添加背景 ...

  5. 最近用unity写三消游戏,mark一个准备用的unity插件,用来控制运动。

    http://www.pixelplacement.com/itween/index.php itween 听说还不错!

  6. [翻译]:Artificial Intelligence for games 5.3 STATE MACHINES:状态机

    目录 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 Chapter 5 Decision Making 5.3 STATE MACHINES:状态机 ...

  7. Creator开源游戏、插件、教程、视频汇总

    Creator开源游戏.插件.教程.视频汇总 来源 http://forum.cocos.com/t/creator/44782 王哲首席客服   17-03-17    4   史上最全,没有之一. ...

  8. UNITY 状态机 + SVN + 码云 下篇

    上篇说到自己写的一个FSM状态机,这篇写怎么把代码和码云联系在一起! 首先,我们应该知道为什么使用码云? 码云是开源中国社区2013年推出的基于 Git 的完全免费的代码托管服务,这个服务是基于 Gi ...

  9. unity 状态机 + svn + 码云 上篇

    最近刚找到在实习,忙于公司一个c++ 项目 ,一直想写博客来着,没时间写今天熬夜打算先献上自己前几天自己封装的一个fsm状态机 话不多说,直接上正题,这篇博客主要是在学校的时候状态机一直使用的是pla ...

随机推荐

  1. Android N(7.0) 被美翻的新特性!

    Tamic 专注移动开发!更多文章请关注 Csdn: http://blog.csdn.net/sk719887916/article/details/52612444 $ http://www.ji ...

  2. 【BAT经典算法面试题系列】求和为n的连续正整数

    马上就要到9月份了,意味着一年一度的秋招就要开始了,相信不论是正在实习的童鞋还是马上就要找工作的童鞋,BAT无疑是国内的"明星企业",是每个学计算机的小伙伴们心之向往的企业,但是呢 ...

  3. Linux 高性能服务器编程——Linux服务器程序规范

    问题聚焦:     除了网络通信外,服务器程序通常还必须考虑许多其他细节问题,这些细节问题涉及面逛且零碎,而且基本上是模板式的,所以称之为服务器程序规范.     工欲善其事,必先利其器,这篇主要来探 ...

  4. Linux下的一些常用命令(一)

    在Linux环境下敲各种命令是再正常不过了,尤其是现在大多少服务器均为Linux系统,但是我又记不住这么多命令,只是偶尔在项目做完发布到服务器上的时候会涉及到,所以在网上找了一些命令,在此记录一下~ ...

  5. pipeline(管道)设计模式

  6. Android简易实战教程--第十四话《模仿金山助手创建桌面Widget小部件》

    打开谷歌api,对widget小部件做如下说明: App Widgets are miniature application views that can be embedded in otherap ...

  7. EBS接口程序调试

    这两天在做采购接收的时候有一个香港的业务实体的采购接不进去,但是其他业务实体能接进去,找错误话费了不少时间,也参考了网上好多资料,虽然最后这个方法没用但觉得还是很有用的,整理一下作为自己的一个总结 错 ...

  8. UNIX网络编程——客户/服务器程序设计示范(八)

        TCP预先创建线程服务器程序,主线程统一accept 最后一个使用线程的服务器程序设计示范是在程序启动阶段创建一个线程池之后只让主线程调用accept并把每个客户连接传递给池中某个可用线程.  ...

  9. Android必知必会--GreenDao缓存

    Github版 CSDN版 本篇文章不是介绍GreenDao的配置和基础使用,记录一下GreenDao缓存的问题,帮助遇到同样问题的朋友找到原因和方法,下面是示例: 场景重现 //第一次查询 List ...

  10. Android程序崩溃异常收集框架

    最近在写Android程序崩溃异常处理,完成之后,稍加封装与大家分享. 我的思路是这样的,在程序崩溃之后,将异常信息保存到一个日志文件中,然后对该文件进行处理,比如发送到邮箱,或发送到服务器. 所以, ...