Linux环境下连连看游戏代码,C++实现连连看游戏核心代码

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

#include

#include

//图片类

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;

}

关于C++小游戏的更多精彩内容请点击专题: 《C++经典小游戏》 学习了解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值