我是靠谱客的博主 瘦瘦小馒头,这篇文章主要介绍2048C++,现在分享给大家,希望可以做个参考。

学习记录,使用图形库编程2048。效果展示如下:

通过整形二维数组存放棋盘内16个值,并且通过sprintf函数转换为字符型再显示于棋盘上。

核心在于用键盘操作时,棋盘内数字移动的问题。使用了嵌套循环,利用双层for循环依次遍历每行每列,并将每行或每列的每一个值对前一个值进行覆盖,如此循环三次即可。代码如下: 

复制代码
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
void Up(int arr[4][4], int* max, int* score)//向上移动函数 { int tmp = 3; int x = 0, y = 0; while (tmp--)//按下W,遍历整个数组,使每列每行的元素根据前列的值向上移动或停止一行,重复3次 { for (x = 0; x < 4; x++) { for (y = 1; y < 4; y++) { while (1) { if (arr[x][y - 1] == 0) { arr[x][y - 1] = arr[x][y]; arr[x][y] = 0; break; } else if (arr[x][y - 1] == arr[x][y] && arr[x][y - 1] != 2048) { *score += arr[x][y - 1]; arr[x][y - 1] *= 2; arr[x][y] = 0; if (arr[x][y - 1] > *max) { *max = arr[x][y - 1]; } break; } else { break; } } } } } }

之后是每移动一次后会随机产生一个随机值,显示在任意位置。而在产生随机值需要寻找当前二维数组中的最小数,来确保产生的随机值不能小于最小数。由于知识储备少,没有想到好的方法,便采用了先将二维数组转换为一维数组,再对一维数组进行冒泡排序的方法找到最小值。完整代码如下:

复制代码
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <easyx.h> #include <time.h> #include <string.h> #include <conio.h> #include <math.h> #define length 80 #define space 15 #define amount 2 void Randnum(int arr[4][4])//初始随机值 { int count = amount; int x = 0; int y = 0; int z = 0; for (y = 0; y < 4; y++) { for (x = 0; x < 4; x++) { arr[x][y] = 0; } } while (count) { x = rand() % 4; y = rand() % 4; z = rand() % 5; if (z == 0) { if (arr[x][y] == 0) { arr[x][y] = 4; count--; } } else { if (arr[x][y] == 0) { arr[x][y] = 2; count--; } } } } void Map(int arr[4][4])//图形输出 { char tmp[5]; int y = 0, x = 0; for (y = 0; y < 4; y++) { for (x = 0; x < 4; x++) { if (arr[x][y] != 0) { sprintf(tmp, "%d", arr[x][y]); } else { sprintf(tmp, "%c", ' '); } int width = textwidth(tmp); int height = textheight(tmp); setfillcolor(RGB(74, 154, 214)); settextcolor(WHITE); settextstyle(40, 15, "微软雅黑"); setbkmode(TRANSPARENT); solidroundrect(space * (x + 1) + length * x, space * (y + 1) + length * y, space * (x + 1) + length * x + length, space * (y + 1) + length * y + length, 20, 20); outtextxy(space * (x + 1) + length * x + (length - width) / 2, space * (y + 1) + length * y + (length - height) / 2, tmp); } } } void Up(int arr[4][4], int* max, int* score); void Down(int arr[4][4], int* max, int* score); void Left(int arr[4][4], int* max, int* score); void Right(int arr[4][4], int* max, int* score); void AddRandnum(int arr[4][4], int* max); void Keyget(int arr[4][4], int* max, int* score)//截取键盘值 { int key = _getch(); switch (key)//w s a d { case 87:Up(arr, max, score);//max此时就是地址,没必要再对max取地址传参 break; case 83:Down(arr, max, score); break; case 65:Left(arr, max, score); break; case 68:Right(arr, max, score); break; } } void Up(int arr[4][4], int* max, int* score)//向上移动函数 { int tmp = 3; int x = 0, y = 0; while (tmp--)//按下W,遍历整个数组,使每列每行的元素根据前列的值向上移动或停止一行,重复3次 { for (x = 0; x < 4; x++) { for (y = 1; y < 4; y++) { while (1) { if (arr[x][y - 1] == 0) { arr[x][y - 1] = arr[x][y]; arr[x][y] = 0; break; } else if (arr[x][y - 1] == arr[x][y] && arr[x][y - 1] != 2048) { *score += arr[x][y - 1]; arr[x][y - 1] *= 2; arr[x][y] = 0; if (arr[x][y - 1] > *max) { *max = arr[x][y - 1]; } break; } else { break; } } } } } } void Down(int arr[4][4], int* max, int* score) { int tmp = 3; int x = 0, y = 0; while (tmp--)//按下W,遍历整个数组,使每列每行的元素根据前列的值向下移动或停止一行,重复3次 { for (x = 0; x < 4; x++) { for (y = 0; y < 3; y++) { while (1) { if (arr[x][y + 1] == 0) { arr[x][y + 1] = arr[x][y]; arr[x][y] = 0; break; } else if (arr[x][y + 1] == arr[x][y] && arr[x][y + 1] != 2048) { *score += arr[x][y + 1]; arr[x][y + 1] *= 2; arr[x][y] = 0; if (arr[x][y + 1] > *max) { *max = arr[x][y + 1]; } break; } else { break; } } } } } } void Left(int arr[4][4], int* max, int* score) { int tmp = 3; int x = 0, y = 0; while (tmp--)//按下W,遍历整个数组,使每列每行的元素根据前列的值向左移动或停止一行,重复3次 { for (y = 0; y < 4; y++) { for (x = 1; x < 4; x++) { while (1) { if (arr[x - 1][y] == 0) { arr[x - 1][y] = arr[x][y]; arr[x][y] = 0; break; } else if (arr[x - 1][y] == arr[x][y] && arr[x - 1][y] != 2048) { *score += arr[x - 1][y]; arr[x - 1][y] *= 2; arr[x][y] = 0; if (arr[x - 1][y] > *max) { *max = arr[x - 1][y]; } break; } else { break; } } } } } } void Right(int arr[4][4], int* max, int* score) { int tmp = 3; int x = 0, y = 0; while (tmp--)//按下W,遍历整个数组,使每列每行的元素根据前列的值向右移动或停止一行,重复3次 { for (y = 0; y < 4; y++) { for (x = 0; x < 3; x++) { while (1) { if (arr[x + 1][y] == 0) { arr[x + 1][y] = arr[x][y]; arr[x][y] = 0; break; } else if (arr[x + 1][y] == arr[x][y] && arr[x + 1][y] != 2048) { *score += arr[x + 1][y]; arr[x + 1][y] *= 2; arr[x][y] = 0; if (arr[x + 1][y] > *max) { *max = arr[x + 1][y]; } break; } else { break; } } } } } } void AddRandnum(int arr[4][4], int* max, int* min)//每操作一次随机生成一个值,五分之一是当前最大值的一半,五分之一是最大值,五分之三最小值 { int x = 0; int y = 0; int z = 0; while (1) { x = rand() % 4; y = rand() % 4; z = rand() % 4; if (z == 0) { if (arr[x][y] == 0) { arr[x][y] = ((*max) / 2); break; } } else if (z == 2) { if (arr[x][y] == 0) { arr[x][y] = *max; break; } } else { if (arr[x][y] == 0) { arr[x][y] = *min; break; } } } } void Scoreboard(int* score, int* max)//计分板函数 { char str[] = "SCORE :"; char str1[] = "Max:"; char num[5] = { 0 }; char maxc[5] = { 0 }; setfillcolor(RGB(232, 225, 206)); solidroundrect(395, 15, 580, 380, 20, 20); settextcolor(RGB(250, 210, 20)); settextstyle(40, 20, "微软雅黑"); setbkmode(TRANSPARENT); outtextxy(410, 50, str); setfillcolor(RGB(172, 247, 253)); solidroundrect(410, 140, 565, 365, 20, 20); sprintf(num, "%d", *score); sprintf(maxc, "%d", *max); settextcolor(WHITE); settextstyle(40, 20, "微软雅黑"); outtextxy(440, 150, num); outtextxy(440, 310, maxc); outtextxy(440, 250, str1); } int Minfind(int arr[4][4])//寻找最小数 { int str[16]; int tmp = 0; int x = 0, y = 0, i = 0; for (x = 0; x < 4; x++) { for (y = 0; y < 4; y++) { str[i] = arr[x][y]; i++; } } for (x = 15; x > 0; x--) { for (y = 0; y < x; y++) { if (str[y] > str[y + 1]) { tmp = str[y]; str[y] = str[y + 1]; str[y + 1] = tmp; } } } for (x = 0; x < 16; x++) { if (str[x] != 0) { return str[x]; } } } int main() { srand((unsigned int)time(NULL)); int min = 2; int max = 4; int count = 0; int score = 0; int arr[4][4]; Randnum(arr); ExMessage msg; initgraph(length * 4 + space * 5 + 200, length * 4 + space * 5); while (1) { BeginBatchDraw(); setbkcolor(RGB(215, 255, 240)); cleardevice(); Scoreboard(&score, &max); Map(arr); EndBatchDraw(); Keyget(arr, &max, &score); min = Minfind(arr); AddRandnum(arr, &max, &min); printf("%d ", min); } getchar(); return 0; }

存在的BUG:棋盘方块中偶尔出现数字不居中的问题,暂时还不知道如何解决

最后

以上就是瘦瘦小馒头最近收集整理的关于2048C++的全部内容,更多相关2048C++内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(105)

评论列表共有 0 条评论

立即
投稿
返回
顶部