现在的位置: 首页 > 综合 > 正文

连连看游戏核心代码(C++实现)

2018年01月11日 ⁄ 综合 ⁄ 共 2672字 ⁄ 字号 评论关闭

         这两天研究了一下连连看游戏的源代码,感觉它挺简单的,主要就是判断选中的两张图片能否消去。我参考了网上的源代码(抱歉的是,不记得当时下载的网址了,在此对原作者表示深深的歉意!),然后自己把核心代码整理如下,与大家共享。需要说明的是,这只是核心算法的代码,界面设计和操作的代码均已略去。

         完整源代码下载地址:http://download.csdn.net/detail/u013149325/6714345

#include <stdlib.h>
#include <time.h>
//图片类
class picture
{
public:
	int type;//图片的编号,共有n种,从0到n-1
	bool visible;//图片是否可见
	int x;//图片位置的横坐标
	int y;//图片位置的综坐标
};
//整个图由8行10列组成,每个单元格是一张小图片
const int pNum = 10;
const int pType = 8;
static picture p[pType][pNum];
//进入新一关
void newStage()
{
	srand(time(0));
	int i,j;
	for(i = 0;i < pType;++i)
		for(j = 0;j < pNum;j++)
			p[i][j].visible = false;
	int x,y;
	for (i = 0;i < pType - 1;++i)
		for(j = 0;j < pNum;++j)
		{
			bool re = true;
			while (re)
			{
				x = rand() % pType;
				y = rand() % pNum;
				if (p[x][y].visible == false)
				{
					p[x][y].type = i;
					p[x][y].visible = true;
					p[x][y].x = x;
					p[x][y].y = y;
					re = false;
				}
			}
		}
		//处理剩余的最后一种图片
		for (i = 0;i < pType;++i)
			for(j = 0;j < pNum;++j)
			{
				if (p[i][j].visible == false)
				{
					p[i][j].type = pType - 1;
					p[i][j].visible = true;
					p[i][j].x = i;
					p[i][j].y = j;
				}
			}
}

//在a、b两点之间画线
void drawLine(picture a,picture b)
{

}
//判断图片a和b之间能否通过一条直线相连(a和b之间有0个转角)
bool matchDirect(picture a,picture b)
{
	if(!(a.x == b.x || a.y == b.y))
		return false;
	//a、b的横坐标相同时
	bool yMatch = true;
	if(a.x == b.x)
	{
		if(a.y > b.y)
		{
			for(int i = b.y + 1;i < a.y;++i)
			{
				if(p[a.x][i].visible == true)
					yMatch = false;
			}
		}
		if(b.y > a.y)
		{
			for(int i = a.y + 1;i < b.y;++i)
			{
				if(p[a.x][i].visible == true)
					yMatch = false;
			}
		}
	}
	//a、b的纵坐标相同时
	bool xMatch = true;
	if(a.y == b.y)
	{
		if(a.x > b.x)
		{
			for(int i = b.x + 1;i < a.x;++i)
			{
				if(p[i][a.y].visible == true)
					xMatch = false;
			}
		}
		if(b.x > a.x)
		{
			for(int i = a.x + 1;i < b.x;++i)
			{
				if(p[i][a.y].visible == true)
					xMatch = false;
			}
		}
	}
	return (xMatch && yMatch);
}
//判断图片a和b之间是否可以通过一个转角的折线相连
bool matchOneCorner(picture a,picture b)
{
	if (p[a.x][b.y].visible == false && matchDirect(a,p[a.x][b.y]) && matchDirect(p[a.x][b.y],b))
	{
		drawLine(a,p[a.x][b.y]);
		drawLine(p[a.x][b.y],b);
		return true;
	}
	if (p[b.x][a.y].visible == false && matchDirect(a,p[b.x][a.y]) && matchDirect(p[b.x][a.y],b))
	{
		drawLine(a,p[b.x][a.y]);
		drawLine(p[b.x][a.y],b);
		return true;
	}
	return false;
}
//判断图片a和b之间是否可以通过两个转角的折线相连
bool matchTwoCorner(picture a,picture b)
{
	int i,j;
	for(i = a.x - 1,j = a.y;i >= 0;--i)
	{
		if(p[i][j].visible == true)
			break;
		else if(matchOneCorner(b,p[i][j]))
		{
			drawLine(a,p[i][j]);
			return true;
		}
	}
	for (i = a.x + 1,j = a.y;i < pNum;++i)
	{
		if(p[i][j].visible == true)
			break;
		else if(matchOneCorner(b,p[i][j]))
		{
			drawLine(a,p[i][j]);
			return true;
		}
	}
	for(i = a.x,j = a.y - 1;j >= 0;--j)
	{
		if(p[i][j].visible == true)
			break;
		else if(matchOneCorner(b,p[i][j]))
		{
			drawLine(a,p[i][j]);
			return true;
		}
	}
	for(i = b.x,j = b.y + 1;j < pType;++j)
	{
		if(p[i][j].visible == true)
			break;
		else if(matchOneCorner(b,p[i][j]))
		{
			drawLine(a,p[i][j]);
			return true;
		}
	}
	return false;
}
//判断a和b能否相连,条件是a和b的类型相同,且a和b之间的连线拐角数<=2个
bool match(picture a,picture b)
{
	if(a.type != b.type)
		return false;
	if(matchDirect(a,b))
	{
		drawLine(a,b);
		return true;
	}
	else if(matchOneCorner(a,b))
		return true;
	else if(matchTwoCorner(a,b))
		return true;
	return false;
}

抱歉!评论已关闭.