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) * ...
随机推荐
- Mvc后台接收 参数
@Html.TextAreaFor(m => m.Emps, new { @class = "easyui-validatebox", @style = "heig ...
- Android TextView里直接显示图片的三种方法
方法一:重写TextView的onDraw方法,也挺直观就是不太好控制显示完图片后再显示字体所占空间的位置关系.一般假设字体是在图片上重叠的推荐这样写.时间关系,这个不付源代码了. 方法二:利用Tex ...
- NPOI+ExcelReport
分享我基于NPOI+ExcelReport实现的导入与导出EXCEL类库:ExcelUtility (续2篇-模板导出综合示例) 自ExcelUtility类推出以来,经过项目中的实际使用与不断完 ...
- ASP.NET Core 1.0 部署 HTTPS
ASP.NET Core 1.0 部署 HTTPS ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1) 提示 更新时间:2016年01月23日. 在目前介 ...
- 解决编译时出错提示: 'error: array must be initialized with a brace-enclosed initializer' 的错误
编译出现这个错误的原因非常简单编译的标准不相同.如果用stdc90,这个就可以直接编译通过了. 下面是代码例子: ...... ] = NULL;或者 :char cmd[256] = '\0'; . ...
- Cocos发展Visual Studio下一个libcurl图书馆开发环境的搭建
我们解释win32在Visual Studio下一个libcurl图书馆开发环境的搭建.Cocos2d-x发动机实际上与Win32在访问libcurl库.Cocos2d-x 3.x在libcurl库文 ...
- Java模式(适配器型号)
今天阅读Java该适配器模式,这里有一个小的总结和下谈感受.对于将来使用. 首先.让我们有关适配器先说说. 适应是“来源”至“目标”适应.其中连接这两个的关系是适配器.它负责“源”过度到“目标”. 举 ...
- MVC Code First (代码优先)
首先配置web.config <connectionStrings> <add name="BookDbContext" connectionString=&qu ...
- Python学习笔记20:server先进
我们不依赖于一个框架,CGI如果是,只能使用socket介面.他完成了一个可以处理HTTP要求Pythonserver. 基于,不管是什么的计算机的操作系统(推荐Linux)和Python该计算机可被 ...
- Project_2007关键
本人今天成功用这个密钥,安装project2007. 分享给着急的小伙伴们. W2JJW-4KYDP-2YMKW-FX36H-QYVD8 版权声明:本文博客原创文章.博客,未经同意,不得转载.

