转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/24308701
源码下载:http://download.csdn.net/detail/oyangyufu/7272177
连连看两个图片是否可消除条件:
1、两个图片是同一类型的图片(在程序中表现为图片id一样)
2、两个图片之间存在通路且此路是由不多于2个拐点
两个图片是同一类型的图片(在程序中表现为图片id一样) 代码:
判断两个是否可以消除
bool MapLayer::canClearTwo(cocos2d::CCPoint pointpre, cocos2d::CCPoint pointcurrent)
{
bool bMatch = false;
int pre = this->indexFromPoint(pointpre);
int current = this->indexFromPoint(pointcurrent);
//是否为同类图片(imaid相同),并开始寻路
int p = ((MapNode *)(mapArray->objectAtIndex(pre)))->imgid;
int c = ((MapNode *)(mapArray->objectAtIndex(current)))->imgid;
if (p == c && this->match(pointcurrent, pointpre)) {
bMatch = true;
}
return bMatch;
}
两个图片之间存在通路且此路是由不多于2个拐点,算法:
2图片在一直线上,且中间无障碍
bool MapLayer::match_direct(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
if (!(a.x == b.x || a.y == b.y)) { //同一坐标,retrun
return false;
}
CCLOG("a.x: %f, a.y: %f", a.x, a.y);
CCLOG("b.x: %f, b.y: %f", b.x, b.y);
int i;
bool match_x = false; //初始x,y 坐标都不相同
if(a.x == b.x) {//a,b图x轴相同
match_x = true;
if(a.y > b.y) { //图b在a图片上面
for(i = a.y - 1; i > b.y; --i) {
CCPoint point = CCPointMake(a.x, i);
if(!this->isValiableNode(point) || !this->isEmptyNode(point)){
match_x = false;
}
}
}
if(b.y > a.y) { //图b在a图片下面
for(i = b.y - 1; i > a.y; --i) {
CCPoint point = CCPointMake(a.x, i);
if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
match_x = false;
}
}
}
}
bool match_y = false;
if(a.y == b.y) {//a,b图y轴相同
match_y = true;
if(a.x > b.x) { //图b在a图片左面
for(i = a.x - 1; i > b.x; --i) {
CCPoint point = CCPointMake(i, a.y);
if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
match_y = false;
}
}
}
if(b.x > a.x) { //图b在a图片右面
for(i = b.x - 1; i > a.x; --i) {
CCPoint point = CCPointMake(i, a.y);
if(!this->isValiableNode(point) || !this->isEmptyNode(point)) {
match_y = false;
}
}
}
}
return match_x || match_y;
}
2图片无障碍通路出现一个拐点
bool MapLayer::match_one_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
CCPoint point = CCPointMake(b.x, a.y);//取b.x,a.y组合成p的坐标,从a至p是否无阻碍通路, 从b至p是否无阻碍通路
if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
return true;
}
point = CCPointMake(a.x, b.y);
if( this->isValiableNode(point) && this->isEmptyNode(point) && this->match_direct(a, point) && this->match_direct(b, point)){
return true;
}
return false;
}
2图片无障碍通路出现两个拐点
bool MapLayer::match_two_corner(cocos2d::CCPoint a, cocos2d::CCPoint b)
{
for(int i = a.x - 1; i >= 0; --i) { //往a的左边找一个point,若无效中止,否则以point.x与b.y组合成新的p,进行通路判断
CCPoint point = CCPointMake(i, a.y);
if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
break;
} else {
if (this->match_one_corner(point, b)) {
return true;
}
}
}
for(int i = a.x + 1; i < total_x; ++i) { //往a的右边找
CCPoint point = CCPointMake(i, a.y);
if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
break;
} else {
if (this->match_one_corner(point, b)) {
return true;
}
}
}
for(int i = a.y - 1; i >= 0; --i) { //往a的上边找
CCPoint point = CCPointMake(a.x ,i);
if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
break;
} else {
if (this->match_one_corner(point, b)) {
return true;
}
}
}
for(int i = a.y + 1; i < total_y; ++i) { //往a的下边找
CCPoint point = CCPointMake(a.x ,i);
if (!this->isValiableNode(point) || !this->isEmptyNode(point)) {
break;
} else {
if (this->match_one_corner(point, b)) {
return true;
}
}
}
return false;
}