三消游戏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 studio - Failed to find target android-18

    看了一下国外的解决方案,好多人也都遇到此类问题.看老外的聊天,由衷觉得着实的可爱,同时外国的月亮也不见得比国内的圆.以下是他们的对话(最后有一个小总结):   I have a problem wit ...

  2. Android app内存管理的16点建议

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiopshared memory(共享内存) Android通过下面几个方式在不同的Process中来共享RAM: 每一个app的proc ...

  3. SpringMVC常用配置(二),最简洁的配置实现文件上传

    Spring.SpringMVC持续介绍中,基础配置前面已经介绍了很多,如果小伙伴们还不熟悉可以参考这几篇文章: 1.Spring基础配置 2.Spring常用配置 3.Spring常用配置(二) 4 ...

  4. Dynamics CRM2016 Web API之更新记录

    本篇继续探索web api,介绍如何通过web api更新记录. 下面是一段简单的更新代码,更新了几个不同类型的字段,entity的赋值和前篇创建时候的一样的. var entity = {}; en ...

  5. Servlet规范总结

    Servlet接口 Servlet规范的核心接口即是Servlet接口,它是所有Servlet类必须实现的接口,在Java Servelt API中已经提供了两个抽象类方便开发者实现Servlet类, ...

  6. JDBC存储和读取二进制数据

    以下JSP文件用common-fileupload组件实现文件上传,并将文件以二进制文件的形式存入数据库 <% if("POST".equalsIgnoreCase(requ ...

  7. linux 定时任务详解 按秒设定

    实现linux定时任务有:cron.anacron.at等,这里主要介绍cron服务. 名词解释: cron是服务名称,crond是后台进程,crontab则是定制好的计划任务表. 软件包安装: 要使 ...

  8. LCD 显示异常定位分析方法

    第一种情况: 进入kernel或android 后,如果LCM图像示异常,可以通过如下步骤来判断问题出现在哪个层面. step1:通过DMMS截图,来判断上面刷到LCM的数据是否有问题. 若DMMS获 ...

  9. 读生产环境下go语言最佳实践有感

    最近看了一篇关于go产品开发最佳实践的文章,go-in-procution.作者总结了他们在用go开发过程中的很多实际经验,我们很多其实也用到了,鉴于此,这里就简单的写写读后感,后续我也争取能将这篇文 ...

  10. Mat, IplImage, CvMat, Cvarr关系及元素获取

    自己目前正打算整理opencv数据结构之间关系,寻寻觅觅之间,发现这篇博文很全面,总结得很好,故转之.红色部分不对,自己已修改! 原文地址:http://blog.csdn.net/abcjennif ...