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

时间:2021-11-01 04:07:35

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#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;
}

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

原文链接:https://blog.csdn.net/u013149325/article/details/17359087