您的位置:首页 > 移动开发 > Unity3D

Unity2D游戏入门教程(一)宝石迷阵(3)如何判断地图是死图

2017-09-28 20:54 302 查看

算法讲解

当一个宝石地图没有可以消除的元素时,我们就叫这张图为死图。那么反过来,如果一张地图还有可消除元素时,那么这张地图就不是一张死图。那么如何判断一张地图有可消除元素呢?一共分为以下三种情况:



第一种情况,任意两个相邻的红色块与蓝色块颜色相同,则不是死图。

第二种,当前块是1号,2号块颜色与1号相同,如果不相同则不必继续判断了,然后任意一个红色块和蓝色块颜色相同,则不是死图。

第三种和第二种类似。

代码实现

/*
* O O
*  X
* O O
*/
private bool IsFirstLineCast(int x, int y)
{
int lx = x - 1;
int ly = y - 1;
int tx = x + 1;
int ty = y + 1;
bool isLeftBottomSame = (lx >= 0 && ly >= 0 && MapNum[lx, ly] == MapNum[x, y]);
bool isLeftTopSame = (lx >= 0 && ty < Height && MapNum[lx, ty] == MapNum[x, y]);
bool isRightBottomSame = (tx < Width && ly >= 0 && MapNum[tx, ly] == MapNum[x, y]);
bool isRightTopSame = (tx < Width && ty < Height && MapNum[tx, ty] == MapNum[x, y]);

// 左下角与右下角
if (isLeftBottomSame && isRightBottomSame)
return true;
// 左下角与左上角
if (isLeftBottomSame && isLeftTopSame)
return true;
// 左上角与右上角
if (isLeftTopSame && isRightTopSame)
return true;
// 右上角与右下角
if (isRightTopSame && isRightBottomSame)
return true;

return false;
}

/*
* O O
*  X
*  X
* O O
*/
private bool IsSecondLineCast(int x, int y)
{
int lx = x - 1;
int ly = y - 2;
int tx = x + 1;
int ty = y + 1;
bool isLeftBottomSame = (lx >= 0 && ly >= 0 && MapNum[lx, ly] == MapNum[x, y]);
bool isLeftTopSame = (lx >= 0 && ty < Height && MapNum[lx, ty] == MapNum[x, y]);
bool isRightBottomSame = (tx < Width && ly >= 0 && MapNum[tx, ly] == MapNum[x, y]);
bool isRightTopSame = (tx < Width && ty < Height && MapNum[tx, ty] == MapNum[x, y]);

if (y - 1 >= 0 && MapNum[x, y - 1] == MapNum[x, y])
{
// 左上角
if (isLeftTopSame)
return true;
// 右上角
if (isRightTopSame)
return true;
// 左下角
if (isLeftBottomSame)
return true;
// 右下角
if (isRightBottomSame)
return true;
}
return false;
}

/*
* O  O
*  XX
* O  O
*/
private bool IsThirdLineCast(int x, int y)
{
int lx = x - 1;
int ly = y - 1;
int tx = x + 2;
int ty = y + 1;
bool isLeftBottomSame = (lx >= 0 && ly >= 0 && MapNum[lx, ly] == MapNum[x, y]);
bool isLeftTopSame = (lx >= 0 && ty < Height && MapNum[lx, ty] == MapNum[x, y]);
bool isRightBottomSame = (tx < Width && ly >= 0 && MapNum[tx, ly] == MapNum[x, y]);
bool isRightTopSame = (tx < Width && ty < Height && MapNum[tx, ty] == MapNum[x, y]);

if (x + 1 < Width && MapNum[x + 1, y] == MapNum[x, y])
{
// 左上角
if (isLeftTopSame)
return true;
// 右上角
if (isRightTopSame)
return true;
// 左下角
if (isLeftBottomSame)
return true;
// 右下角
if (isRightBottomSame)
return true;
}
return false;
}


有了上面三种方法,我们就可以判断一张图是否是死图:

// 遍历地图判断是否是死图
public bool IsDead()
{
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
if (IsFirstLineCast(i, j))
return false;
if (IsSecondLineCast(i, j))
return false;
if (IsThirdLineCast(i, j))
return false;
}
}
return true;
}


最后,在随机生成地图后,需要判断生成的地图是否是死图,如果是,则要重新生成:

private void InitMapNum()
{
for (int i = 0; i < Width; i++)
{
for (int j = 0; j < Height; j++)
{
int num = Random.Range(0, Blocks.Length);
MapNum[i, j] = num;
while(IsLine(i, j))
{
num = Random.Range(0, Blocks.Length);
MapNum[i, j] = num;
}
}
}

if (IsDead())
{
InitMapNum();
}
}


下一节教程中,我们将学习如何选择宝石,并交换两颗宝石。

参考

消消乐游戏算法实现(三消乐)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: