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) * ...
随机推荐
- 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)
ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...
- windows phone (26) ApplicationBar应用程序栏
原文:windows phone (26) ApplicationBar应用程序栏 在应用程序中,如果需要几个按钮或者菜单来执行一些普通的命令,就应该考虑使用ApplicationBar,因为silv ...
- mysql多实例的配置(转)
1.创建多实例的目录: mkdir -p /data/mysql/mysql_3307/{data,tmp,logs} mkdir -p /data/mysql/mysql_3308/{data,tm ...
- 最近跑hadoop遇到的一些问题
一. [#|2013-09-16T18:19:02.663+0800|INFO|glassfish3.1.2|javax.enterprise.system.std.com.sun.enterpris ...
- RabbitMQ与java、Spring结合实例详细讲解(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文介绍了rabbitMq,提供了如何在Ubuntu下安装RabbitMQ 服务的方法. ...
- CentOS6.2安装memcache
一,安装libevent # cd /tmp # wget http://www.monkey.org/~provos/libevent-1.3.tar.gz # tar -zxvf libevent ...
- Unity 捕获IronPython脚本错误
using System; using System.Collections.Generic; using System.IO; using System.Reflection; using Syst ...
- Android开发之Handler和Looper的关系
关于Handler的总结. Message:消息,当中包括了消息ID,消息处理对象以及处理的数据等,由MessageQueue统一列队,终由Handler处理. Handler:处 ...
- Swift String length property
Swift的String居然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements. 没办法.仅仅有扩展String结构体,给它加入一个属性了. i ...
- linux---Vim命令集
Vim命令集 命令历史 以:和/开头的命令都有历史纪录,能够首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗体中输入下面命令就可以 vim 直接启动vim vim filena ...