-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadDirection.cpp
More file actions
511 lines (437 loc) · 8.47 KB
/
ReadDirection.cpp
File metadata and controls
511 lines (437 loc) · 8.47 KB
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
#include<iostream>
#include<conio.h>
#include"Map.h"
#include"Random.h"
#include"ReadDirection.h"
#include "Fun_Function.h"
char ReadDirection()
{
int key = _getch();
if(key==224)
key = _getch();
switch (key)
{
case 72://上
case 'W':
case 'w':
return 'U'; break;
case 80://下
case 'S':
case 's':
return 'D'; break;
case 75://左
case 'A':
case 'a':
return 'L'; break;
case 77://右
case 'D':
case 'd':
return 'R'; break;
case 'b':
return 'B'; break;
case 'B':
return 'B'; break;
case '27':
return 0; break;
default:
return -1; break;
}
}
void Move(void) //实现方向键导致的移动和合并同类项
{
char key = ReadDirection();
if (score - 1 >= 0)
score--;
int tempmove[4][4];//记录移动前的数组情况
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
tempmove[i][j] = map[i][j];
if (key == 'U')
{
for(int x=0;x<4;++x)//对每一列都要做这个操作
{
int y_temp[4] = {0};
int i = 0;
for (int j = 0; j < 4; j++)
{
if (map[j][x] != 0)
y_temp[i++] = map[j][x];
}
for (int y = 0; y <3 ; ++y)//纵向操作 从上往下
{
if (y_temp[y]==y_temp[y+1])//合并项目
{
y_temp[y] *= 2;
score += y_temp[y+1];
y_temp[y+1] = 0;
}
}
int k = 0;
int temp2[4]={ 0 };
for (int j = 0; j < 4; j++)
{
if (y_temp[j] != 0)
temp2[k++] = y_temp[j];
}
for (int y = 0; y < 4; ++y)//抄回更新后的状态
{
map[y][x] = temp2[y];
}
}
}
else if (key == 'D')
{
for (int x = 0; x < 4; ++x)//对每一列都要做这个操作
{
int y_temp[4] = { 0 };
int i = 3;
for (int j = 3; j >=0; --j)//倒着存下来
{
if (map[j][x] != 0)
y_temp[i--] = map[j][x];
}
for (int y = 3; y >0; --y)//纵向操作 从下往上
{
if (y_temp[y] == y_temp[y - 1])//合并项目
{
y_temp[y] *= 2;
score += y_temp[y-1];
y_temp[y-1] = 0;
}
}
int k = 3;
int tempD[4] = { 0 };
for (int j = 3; j >=0 ; j--)//注意!要倒着检索
{
if (y_temp[j] != 0)
tempD[k--] = y_temp[j];
}
for (int y = 0; y < 4; ++y)//抄回更新后的状态
{
map[y][x] = tempD[y];
}
}
}
else if (key == 'L')
{
for (int y = 0; y < 4; ++y)//对每一行都要做这个操作
{
int x_temp[4] = { 0 };
int i = 0;
for (int j = 0; j < 4; j++)
{
if (map[y][j] != 0)
x_temp[i++] = map[y][j];
}
for (int x = 0; x < 4; ++x)//横向操作 自左向右
{
if (x_temp[x] == x_temp[x + 1])//合并项目
{
x_temp[x] *= 2;
score += x_temp[x+1];
x_temp[x+1] = 0;
}
}
int tempL[4] = { 0 };
int k = 0;
for (int j = 0; j < 4; j++)
{
if (x_temp[j] != 0)
tempL[k++] = x_temp[j];
}
for (int x = 0; x < 4; ++x)//抄回更新后的状态
{
map[y][x] = tempL[x];
}
}
}
else if (key == 'R')
{
for (int y = 0; y < 4; ++y)//对每一行都要做这个操作
{
int x_temp[4] = { 0 };
int i = 3;
for (int j = 3; j >= 0; --j)//倒着存下来
{
if (map[y][j] != 0)
x_temp[i--] = map[y][j];
}
for (int x = 3; x >0; --x)////横向操作 自右向左
{
if (x_temp[x] == x_temp[x - 1])//合并项目
{
x_temp[x] *= 2;
score += x_temp[x-1];
x_temp[x-1] = 0;
}
}
int tempR[4] = { 0 };
int k = 3;
for(int j = 3; j >= 0; j--)
{
if (x_temp[j] != 0)
tempR[k--] = x_temp[j];
}
for (int x = 0; x < 4; ++x)//抄回更新后的状态
{
map[y][x] = tempR[x];
}
}
}
else if(key=='B')
Bomb();
else if(key==0)
exit(0);
bool move_or_not = ActualMove(tempmove, 4);
if(move_or_not)
BirthRandom();
}
void IfMove(char key) //实现方向键导致的移动和合并同类项
{
if (key == 'U')
{
for (int x = 0; x < 4; ++x)//对每一列都要做这个操作
{
int y_temp[4] = { 0 };
int i = 0;
for (int j = 0; j < 4; j++)
{
if (map[j][x] != 0)
y_temp[i++] = map[j][x];
}
for (int y = 0; y < 3; ++y)//纵向操作 从上往下
{
if (y_temp[y] == y_temp[y + 1])//合并项目
{
y_temp[y] *= 2;
score += y_temp[y + 1];
y_temp[y + 1] = 0;
}
}
int k = 0;
int temp2[4] = { 0 };
for (int j = 0; j < 4; j++)
{
if (y_temp[j] != 0)
temp2[k++] = y_temp[j];
}
for (int y = 0; y < 4; ++y)//抄回更新后的状态
{
map[y][x] = temp2[y];
}
}
}
else if (key == 'D')
{
for (int x = 0; x < 4; ++x)//对每一列都要做这个操作
{
int y_temp[4] = { 0 };
int i = 3;
for (int j = 3; j >= 0; --j)//倒着存下来
{
if (map[j][x] != 0)
y_temp[i--] = map[j][x];
}
for (int y = 3; y > 0; --y)//纵向操作 从下往上
{
if (y_temp[y] == y_temp[y - 1])//合并项目
{
y_temp[y] *= 2;
score += y_temp[y - 1];
y_temp[y - 1] = 0;
}
}
int k = 3;
int tempD[4] = { 0 };
for (int j = 3; j >= 0; j--)//注意!要倒着检索
{
if (y_temp[j] != 0)
tempD[k--] = y_temp[j];
}
for (int y = 0; y < 4; ++y)//抄回更新后的状态
{
map[y][x] = tempD[y];
}
}
}
else if (key == 'L')
{
for (int y = 0; y < 4; ++y)//对每一行都要做这个操作
{
int x_temp[4] = { 0 };
int i = 0;
for (int j = 0; j < 4; j++)
{
if (map[y][j] != 0)
x_temp[i++] = map[y][j];
}
for (int x = 0; x < 4; ++x)//横向操作 自左向右
{
if (x_temp[x] == x_temp[x + 1])//合并项目
{
x_temp[x] *= 2;
score += x_temp[x + 1];
x_temp[x + 1] = 0;
}
}
int tempL[4] = { 0 };
int k = 0;
for (int j = 0; j < 4; j++)
{
if (x_temp[j] != 0)
tempL[k++] = x_temp[j];
}
for (int x = 0; x < 4; ++x)//抄回更新后的状态
{
map[y][x] = tempL[x];
}
}
}
else if (key == 'R')
{
for (int y = 0; y < 4; ++y)//对每一行都要做这个操作
{
int x_temp[4] = { 0 };
int i = 3;
for (int j = 3; j >= 0; --j)//倒着存下来
{
if (map[y][j] != 0)
x_temp[i--] = map[y][j];
}
for (int x = 3; x > 0; --x)////横向操作 自右向左
{
if (x_temp[x] == x_temp[x - 1])//合并项目
{
x_temp[x] *= 2;
score += x_temp[x - 1];
x_temp[x - 1] = 0;
}
}
int tempR[4] = { 0 };
int k = 3;
for (int j = 3; j >= 0; j--)
{
if (x_temp[j] != 0)
tempR[k--] = x_temp[j];
}
for (int x = 0; x < 4; ++x)//抄回更新后的状态
{
map[y][x] = tempR[x];
}
}
}
}
//用于计算是否发生了真正的移动的辅助函数。
void BirthRandom() {
int x = 0;
int y = 0;
int emptyCount = 0;
// Count empty positions
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (map[i][j] == 0) {
emptyCount++;
}
}
}
// If there are empty positions, generate a new number
if (emptyCount > 0) {
int randomIndex = Random() % emptyCount + 1;
int currentIndex = 0;
int TwoOrFour = RandomBirth() % 8;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (map[i][j] == 0) {
currentIndex++;
if (currentIndex == randomIndex) {
if (TwoOrFour == 0)
map[i][j] = 4;
else
map[i][j] = 2;
}
}
}
}
}
}
bool WinOrLose()
{
//判断赢
int N = 2048;
for(int i=0;i<4;i++)
for (int j = 0; j < 4; j++)
{
if (map[i][j] == N)
{
settextcolor(RED);
settextstyle(50, 0, _T("黑体"));
TCHAR win[] = _T("你赢了!");
outtextxy(200, 300, win);
settextstyle(20, 0, _T("黑体"));
TCHAR gameagain[] = _T("要再来一局吗?按任意键继续");
outtextxy(20, 400, gameagain);
_getch();
return true;
}
}
//判断输
int tempmove[4][4];//记录移动前的数组情况
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
tempmove[i][j] = map[i][j];
int emptyCount = 0;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (map[i][j] == 0) {
emptyCount++;
}
}
}
if (emptyCount == 0)
{
bool could_U = 0;//UP是不是有效移动
IfMove('U');
could_U = ActualMove(tempmove, 4);
reset_map;
bool could_D = 0;
IfMove('D');//Down是不是有效移动
could_D = ActualMove(tempmove, 4);
reset_map;
bool could_L = 0;
IfMove('L');//Left是不是有效移动
could_L = ActualMove(tempmove, 4);
reset_map;
bool could_R = 0;
IfMove('R');//Right是不是有效移动
could_R = ActualMove(tempmove, 4);
reset_map;
if (!(could_D || could_L || could_R || could_U))
{
settextcolor(RED);
settextstyle(50, 0, _T("黑体"));
TCHAR lose[] = _T("你输了!");
outtextxy(200, 300, lose);
settextstyle(20, 0, _T("黑体"));
TCHAR gameagain[] = _T("要再来一局吗?按任意键继续");
outtextxy(20, 400, gameagain);
_getch();
return true;
}
}
return false;
}
bool ActualMove(int a[][4],int line)
{
bool flag = false;
for (int i = 0; i < line; i++)
for (int j = 0; j < 4; j++)
{
if (a[i][j] != map[i][j])
{
flag = true;
break;
}
}
return flag;
}
//检查map是否发生了变化