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

发布时间:2020-10-18 22:48:45 作者:遨游网络huster
来源:脚本之家 阅读:161

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

#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;
}

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

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

推荐阅读:
  1. python实现连连看游戏
  2. python递归法实现简易连连看小游戏

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

c++ 连连看 c+

上一篇:python 拼接文件路径的方法

下一篇:Android仿QQ讨论组头像效果

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》