cocos2d 消除类游戏简单的算法 (一)
1. 游戏视频演示
2.三消游戏我的理解
市场上三消游戏已经超级多了。主流的是地图型的。差点儿是无尽模式。各种消除特效。各种各样的过关方式,玩起来还是不错的,就是遇到比較难的关卡,要多试几次,运气很好的时候就过了,不然卡死。
3.消除涉及到的简单算法
3.1 生成随机地图算法
每次先推断下它的左边两个是否已经同色。还有上面两个是否已经同色,假设同色了,要去掉这个颜色。
所以X的结果仅仅能是0,1,4中随机取一个了。
enum MatchItemType{
kRedItem = 0, //0
kGreenItem, //1
kBlueItem, //2
kWhiteItem, //3
kOrangeItem //4
};
MatchItemType getOneRandomTypeExceptParameter(const MatchItemType& type){
MatchItemType allType[5] = {kRedItem, kGreenItem, kBlueItem, kWhiteItem, kOrangeItem};
std::vector restType;
for(int i = 0; i < 5; ++i){
if(allType[i] != type){
restType.push_back(allType[i]);
}
}
int restSize = restType.size();
int randomIndex = rand() % restSize;
return restType[randomIndex];
}
Array2D<MatchItemType> getOneRandomMapArray(){
Array2D<MatchItemType> map = Array2D<MatchItemType>(7, 7);
bool findThreeSameInX = false;
bool findThreeSameInY = false;
for(int y = 0; y < 7; ++y){
for(int x = 0; x < 7; ++x){
MatchItemType randomType = (MatchItemType)(rand() % 5);
if(x >= 2){
//左边两个是同色
if( map.Get(x - 1, y) == map.Get(x - 2, y)){
//need find a new type
findThreeSameInX = true;
}else{
findThreeSameInX = false;
}
}else{
findThreeSameInX = false;
}
if(y >= 2){
//上面两个是同色
if(map.Get(x, y - 1) == map.Get(x, y -2)){
//need find a new type;
findThreeSameInY = true;
}else{
findThreeSameInY = false;
}
}else{
findThreeSameInY = false;
}
if(findThreeSameInX == false && findThreeSameInY == false){
//do nothing
}else if(findThreeSameInX == true && findThreeSameInY == false){
randomType = getOneRandomTypeExceptParameter(map.Get(x - 1, y));
}else if(findThreeSameInX == false && findThreeSameInY == true){
randomType = getOneRandomTypeExceptParameter(map.Get(x, y - 1));
}else{
randomType = getOneRandomTypeExceptParameter(map.Get(x - 1, y),
map.Get(x, y - 1));
}
map.Set(x, y, randomType);
}
}
return map;
}
3.2 推断地图是否是死地图
//case 1
/////[x]//////[x]////////
//[x][o][x][x][o][x]/////
/////[x]//////[x]////////
///////////////////////// //case 2
////////[x]//////////////
/////[x][o][x]///////////
////////[x]//////////////
////////[x]//////////////
/////[x][o][x]///////////
////////[x]//////////////
这里用凝视画了简单的两种情况,注意x的位置。
case1 是横着有两个同色的情况,移动一步能消除仅仅有6种可能,左边3种,右边3种。以下是竖着有两个同色的情况。移动一步能消除也是6种情况。上面3种。以下3种。
知道了这个,代码就easy了。记得找到一个就直接return。
vector<MatchItem*> getThreeMatchItemCanRemoveByOneStep(const Array2D<MatchItem*> & map){
vector<MatchItem*> result;
int maxX = 7;
int maxY = 7;
for(int y = 0; y < maxY; ++y){
for(int x = 0; x < maxX; ++x){
if(x + 1 < maxX){
//case 1
if(map.Get(x, y)->getType() == map.Get(x + 1, y)->getType()){
MatchItemType currentType = map.Get(x, y)->getType();
//check 6 item, one move one step can combine three same item
if(x - 2 >= 0){
if(map.Get(x - 2, y)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x - 2, y));
return result;
}
}
if(x - 1 >= 0 && y - 1 >= 0){
if(map.Get(x - 1, y - 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x - 1, y - 1));
return result;
}
}
if(x + 2 < maxX && y - 1 >= 0){
if(map.Get(x + 2, y - 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x + 2, y - 1));
return result;
}
}
if(x + 3 < maxX){
if(map.Get(x + 3, y)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x + 3, y));
return result;
}
}
if(x + 2 < maxX && y + 1 < maxY){
if(map.Get(x + 2, y + 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x + 2, y + 1));
return result;
}
}
if(x - 1 >= 0 && y + 1 < maxY){
if(map.Get(x - 1, y + 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x + 1, y));
result.push_back(map.Get(x - 1, y + 1));
return result;
}
}
}
}
if(y + 1 < maxY){
MatchItemType currentType = map.Get(x, y)->getType();
//case 2
if(map.Get(x, y)->getType() == map.Get(x, y + 1)->getType()){
if(y - 2 >= 0){
if(map.Get(x, y - 2)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x, y - 2));
return result;
}
}
if(x + 1 < maxX && y - 1 >= 0){
if(map.Get(x + 1, y - 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x + 1, y - 1));
return result;
}
}
if(x + 1 < maxX && y + 2 < maxY){
if(map.Get(x + 1, y + 2)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x + 1, y + 2));
return result;
}
}
if(y + 3 < GameGlobal::xMapCount){
if(map.Get(x, y + 3)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x, y + 3));
return result;
}
}
if(x - 1 >= 0 && y + 2 < maxY){
if(map.Get(x - 1, y + 2)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x - 1, y + 2));
return result;
}
}
if(x - 1 >= 0 && y - 1 >= 0){
if(map.Get(x - 1, y + 1)->getType() == currentType){
//find one
result.push_back(map.Get(x, y));
result.push_back(map.Get(x, y + 1));
result.push_back(map.Get(x - 1, y - 1));
return result;
}
}
}
}
}
}
return result;
}
看起来是有点复杂。穷举了12种情况,这个算法应该速度很快的。还有个地方要用到这个算法。就是在消除游戏中。用户很久时间没有进行消除了,要给提示。就用这个算法找到哪3个能够移动一步进行消除。
算法先到这里... 兴许有时间再更新...
提交给苹果,审核竟然能通过
版权声明:本文博主原创文章,博客,未经同意不得转载。
cocos2d 消除类游戏简单的算法 (一)的更多相关文章
- ccf题库中2015年12月2号消除类游戏
题目如下: 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这 ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(1)——系统主菜单
系统主菜单如下图所示: 首先,介绍下这个主菜单,它包含了一个动画logo以及一个按钮选项,动画logo每隔1秒钟切换一张图片,点击相应的按钮选项会切换不同的游戏场景. 下面看下这个界面的源码: /** ...
- 消除类游戏(js版)
最近一直在玩一款消灭星星的消除类游戏,周末无聊就用js也写了一遍,感觉玩比写还困难一直玩不到10000分.废话不多说直接上源码. 效果图(ps 页面有点难看木有美工) 代码总共456行,未经过严格测试 ...
- CCF2015122消除类游戏(C语言版)
问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消 ...
- ccf消除类游戏
问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行m列的游戏棋盘上进行,棋盘的每一行每一列的方格上放着一个有颜色的棋子,当一行或一列上有连续三个或更多的相同颜色的棋子时,这些棋子都被消 ...
- CCF CSP 201512-2 消除类游戏
CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201512-2 消除类游戏 问题描述 消除类游戏是深受大众欢迎的一种游戏,游戏在一个包含有n行 ...
- CSP201512-2:消除类游戏
引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的"计算机职业资格认证"考试, ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(3)——游戏主界面
游戏主界面,同时也是主程序,包括sprite的生成加入以及游戏状态的控制. 下面同样贴下源码再讲解; /** * Power by html5中文网(html5china.com) * author: ...
- 用cocos2d-html5做的消除类游戏《英雄爱消除》(2)——Block设计实现
Block可以说是这个游戏的核心类,它除了包含自身的一些属性和方法外还添加了对触摸事件的响应. 我们先来看下源码吧 /** * Power by html5中文网(html5china.com) * ...
随机推荐
- Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试)
Cocos2d-x 3.2 Lua演示样例 ClickAndMoveTest(点击移动測试) 本篇博客介绍Cocos2d-x 3.2Lua演示样例中点击移动的样例,在这个样例你能够得到怎样创建单点触 ...
- 编译gRPC
编译gRPC 目录 一.概述 二.编译gRPC 三.C#中使用gRPC 四.C++中使用gRPC 无论通过哪种语言调用gRPC,都必须要编译gRPC,因为生成proto访问类时,除了产生标准的数据定义 ...
- KMP求字符串最小循环节
证明1: 对于一个字符串S,长度为L,如果由长度为len的字符串s(字符串s的最小循环节是其本身)循环k次构成,那么字符串s就是字符串S的最小循环节 那么字符串有个很重要的性质和KMP挂钩,即 i ...
- yum 简介及使用 安装、删除
使用yum装软件很方便,这里简单介绍一下. Yum简介 Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE.CentOS中的She ...
- virtio-blk分析
和virtio-network相同,virtio-blk驱动程序使用Virtio机制Guest它提供了一个高性能的设备I/O方法.我们期待在这里virtio-blk实现. [点击查看全文] http: ...
- 构建自己的Java并发模型框架
Java的多线程特性为构建高性能的应用提供了极大的方便,可是也带来了不少的麻烦.线程间同步.数据一致性等烦琐的问题须要细心的考虑,一不小心就会出现一些微妙的,难以调试的错误. 另外.应用逻辑和线程逻辑 ...
- HDU 1394 Minimum Inversion Number (数据结构-段树)
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- 查看文章strncpy()功能更好的文章
strncpy()功能 原型:extern char *strncpy(char *dest, char *src, int n); 使用方法:#include <string.h> ...
- hdu-2814-Interesting Fibonacci-斐波那契周期节
哇,其实我2A该....否1A纯脑损伤.. 乞讨:F(a^b)^(F(a^b) ^ (n-1))%c 既是求F(a^b)^(F(a^b) ^ (n-1)%phi[c]+phi[c])%c 先求x=F ...
- sql查询 数据库 表 字段 等
1.查询数据库中的所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.查询某个数据库中所有的表名: SELECT Name FR ...

