【梦幻连连连】源码分析(完)-寻路消除算法

本文详细分析了《梦幻连连看》游戏的源码,重点探讨了两个图片可消除的条件:图片类型相同及通路不超过2个拐点的判断算法。提供了源码下载链接。
摘要由CSDN通过智能技术生成

转载请注明出处: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;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值