2048小游戏

基于easyx,这是我做的第一个游戏

1.2048字符版

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
int main()
{
using namespace std;
int a[6][6], 备份[6][6];
int i, j, k;
srand((int)time(0));
//初始化数组a,并且随机产生两个2
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
//打印初始数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
cout << a[i][j]<<"\t";
}
cout << endl;
}
cout << endl;
//键盘输入,后,随机一个格子生成一个数字
int ch, ch1, flag, tmp = 0, 为了符合规则[6][6];// 4 4 2 2 动过的不再动
while (1)
{
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
为了符合规则[i][j] = 0;
}
}
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j]&&为了符合规则[i][flag+1]==0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
为了符合规则[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <=4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 为了符合规则[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
为了符合规则[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 为了符合规则[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
为了符合规则[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 为了符合规则[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
为了符合规则[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
//空余位置随机生成一个2或者4
int i3, j3, kk;
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;

//打印a数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
cout << a[i][j] << "\t";
}
cout << endl;
}
cout << endl;
}
}

2.2048图像版

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
void 初始化屏幕()
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j;
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i+=100)
{
for (j = 100;j < 500;j+=100)
{
rectangle(i, j, 100+i, j+100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
settextstyle(25, 25, _T("隶书"));
setbkmode(TRANSPARENT);
BeginBatchDraw();
}
int main()
{
using namespace std;
初始化屏幕();
int a[6][6], 备份[6][6];
int i, j, k;
srand((int)time(0));
//初始化数组a,并且随机产生两个2
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
//打印初始数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
//键盘输入,后,随机一个格子生成一个数字
int ch, ch1, flag, tmp = 0, 为了符合规则[6][6],旗子;// 4 4 2 2 动过的不再动
while (1)
{
旗子 = 1;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
为了符合规则[i][j] = 0;
}
}
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 为了符合规则[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
为了符合规则[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 为了符合规则[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
为了符合规则[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 为了符合规则[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
为了符合规则[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 为了符合规则[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
为了符合规则[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
//空余位置随机生成一个2或者4
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
cleardevice();
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
//打印a数组
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
return 0;
}

3.2048最终版

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
void 初始化屏幕()
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j;
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
settextstyle(30, 20, _T("隶书"));
setbkmode(TRANSPARENT);
/*settextcolor(BLUE);*/
BeginBatchDraw();
}
void 初始化数组a(int a[6][6],int 备份[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
}
void 打印a数组(int a[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
void 备份并初始化计数器(int a[6][6],int 计数器[6][6], int 备份[6][6])
{
int i,j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
计数器[i][j] = 0;
}
}
}
void 键盘控制和游戏规则(int a[6][6],int 计数器[6][6])
{
int ch, ch1, flag, tmp = 0,i,j,k;
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 计数器[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
计数器[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 计数器[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
计数器[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 计数器[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
计数器[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 计数器[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
计数器[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
}
int 数据备份并判断有无变化(int a[6][6],int 备份[6][6],int 旗子)
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
return 旗子;
}
void 空余位置随机生成一个2或者4(int a[6][6],int 旗子)
{
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
}
void 重新绘制屏幕()
{
int i, j;
cleardevice();
setcolor(BLACK);
setfillcolor(YELLOW);
for (i = 100;i < 500;i += 100)
{
for (j = 100;j < 500;j += 100)
{
rectangle(i, j, 100 + i, j + 100);
fillrectangle(i, j, 100 + i, j + 100);
}
}
FlushBatchDraw();
}
int main()
{
using namespace std;
初始化屏幕();
int a[6][6], 备份[6][6],计数器[6][6], 旗子;
srand((int)time(0));
初始化数组a(a, 备份);
打印a数组(a);
while (1)
{
旗子 = 1;
备份并初始化计数器(a, 计数器, 备份);
键盘控制和游戏规则(a, 计数器);
旗子 = 数据备份并判断有无变化(a, 备份, 旗子);
空余位置随机生成一个2或者4(a,旗子);
重新绘制屏幕();
打印a数组(a);
}
return 0;
}

4.2048优化版

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
#include<iostream>
#include<cstdlib>
#include<ctime>
#include<conio.h>
#include<graphics.h>
#include<cmath>
int 判断aij是2的几次方(int tt)
{
int i, sum = 1, cnt = 0;
if (tt == 0)
return 0;
for (i = 1;;i++)
{
sum *= 2;
cnt++;
if (sum == tt)
break;
}
return cnt;
}
void 初始化屏幕(int a[6][6])
{
initgraph(1000, 1000);
setbkcolor(WHITE);
cleardevice();
int i, j,tt,cnt;
setcolor(WHITE);
settextcolor(RED);
for (i = 1;i < 5;i ++)
{
for (j = 1;j < 5;j ++)
{
tt = a[i][j];
cnt=判断aij是2的几次方(tt);
setfillcolor(RGB(100, 255, 20 * cnt));
rectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
fillrectangle(j*100, i*100, 100 + j*100, i*100 + 100);
}
}
settextstyle(30, 20, _T("隶书"));
setbkmode(TRANSPARENT);
/*settextcolor(BLUE);*/
BeginBatchDraw();
}
void 初始化数组a(int a[6][6], int 备份[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
a[i][j] = 0;
备份[i][j] = 0;
}
}
for (i = 0;i < 6;i++)
{
a[i][0] = 5;
a[i][5] = 5;
a[0][i] = 5;
a[5][i] = 5;
}
int i1, i2, j1, j2;
do {
i1 = rand() % 4 + 1;
i2 = rand() % 4 + 1;
j1 = rand() % 4 + 1;
j2 = rand() % 4 + 1;
} while (i1 == i2 && j1 == j2);
a[i1][j1] = 2;
a[i2][j2] = 2;
}
void 打印a数组(int a[6][6])
{
int i, j;
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
if (a[i][j] != 0)
{
TCHAR s[5];
_stprintf_s(s, _T("%d"), a[i][j]);
/*outtextxy(j * 100 + 25, i * 100 + 25, s);*/
RECT r = { 100 * j, 100 * i, 100 + 100 * j, 100 + 100 * i };
drawtext(s, &r, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}
}
}
FlushBatchDraw();
}
void 备份并初始化计数器(int a[6][6], int 计数器[6][6], int 备份[6][6])
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
备份[i][j] = a[i][j];
}
}
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
计数器[i][j] = 0;
}
}
}
void 键盘控制和游戏规则(int a[6][6], int 计数器[6][6])
{
int ch, ch1, flag, tmp = 0, i, j, k;
if (_kbhit)
{
ch1 = _getch();
ch = _getch();
//右移
if ((ch1 == 224 && ch == 77) || (ch == 228 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 3;j >= 1;j--)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j + 1;k <= 4;k++)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
//交换aij和aiflag;
if (a[i][flag + 1] == a[i][j] && 计数器[i][flag + 1] == 0)
{
a[i][flag + 1] *= 2;
a[i][j] = 0;
计数器[i][flag + 1] = 1;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
//下移
if ((ch1 == 224 && ch == 80) || (ch == 243 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 3;i >= 1;i--)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i + 1;k <= 4;k++)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag + 1][j] == a[i][j] && 计数器[flag + 1][j] == 0)
{
a[flag + 1][j] *= 2;
计数器[flag + 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//上移
if ((ch1 == 224 && ch == 72) || (ch == 247 && ch1 == 163))
{
for (j = 1;j <= 4;j++)
{
for (i = 2;i <= 4;i++)
{
if (a[i][j] != 0)
{
flag = i;
for (k = i - 1;k >= 1;k--)
{
if (a[k][j] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[flag - 1][j] == a[i][j] && 计数器[flag - 1][j] == 0)
{
a[flag - 1][j] *= 2;
计数器[flag - 1][j] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[flag][j];
a[flag][j] = tmp;
}
}
}
}
}
//左移
if ((ch1 == 224 && ch == 75) || (ch == 225 && ch1 == 163))
{
for (i = 1;i <= 4;i++)
{
for (j = 2;j <= 4;j++)
{
if (a[i][j] != 0)
{
flag = j;
for (k = j - 1;k >= 1;k--)
{
if (a[i][k] == 0)
flag = k;
else
break;
}
/*if (a[i][j] == a[i][j - 1])
{
a[i][j- 1] *= 2;
a[i][j] = 0;
}*/
//交换aij和aiflag;
if (a[i][flag - 1] == a[i][j] && 计数器[i][flag - 1] == 0)
{
a[i][flag - 1] *= 2;
计数器[i][flag - 1] = 1;
a[i][j] = 0;
}
else
{
tmp = a[i][j];
a[i][j] = a[i][flag];
a[i][flag] = tmp;
}
}
}
}
}
}
}
int 数据备份并判断有无变化(int a[6][6], int 备份[6][6], int 旗子)
{
int i, j;
for (i = 0;i < 6;i++)
{
for (j = 0;j < 6;j++)
{
if (备份[i][j] == a[i][j])
旗子 = 1;
else
{
旗子 = 0;
break;
}
}
if (旗子 == 0)
break;
}
return 旗子;
}
void 空余位置随机生成一个2或者4(int a[6][6], int 旗子)
{
int i3, j3, kk;
if (旗子 == 0)
{
do
{
i3 = rand() % 4 + 1;
j3 = rand() % 4 + 1;
} while (a[i3][j3] != 0);
kk = rand() % 2;
if (kk == 1)
a[i3][j3] = 2;
else
a[i3][j3] = 4;
}
}
void 重新绘制屏幕(int a[6][6])
{
int i, j,tt,cnt;
cleardevice();
setcolor(WHITE);
settextcolor(RED);
for (i = 1;i < 5;i++)
{
for (j = 1;j < 5;j++)
{
tt = a[i][j];
cnt = 判断aij是2的几次方(tt);
setfillcolor(RGB(100, 255, 20 * cnt));
rectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
fillrectangle(j * 100, i * 100, 100 + j * 100, i * 100 + 100);
}
}
FlushBatchDraw();
}
int main()
{
using namespace std;
int a[6][6], 备份[6][6], 计数器[6][6], 旗子;
srand((int)time(0));
初始化数组a(a, 备份);
初始化屏幕(a);
打印a数组(a);
while (1)
{
旗子 = 1;
备份并初始化计数器(a, 计数器, 备份);
键盘控制和游戏规则(a, 计数器);
旗子 = 数据备份并判断有无变化(a, 备份, 旗子);
空余位置随机生成一个2或者4(a, 旗子);
重新绘制屏幕(a);
打印a数组(a);
}
return 0;
}

似乎只改动了颜色

作者

我是呆呆啊

发布于

2021-01-02

更新于

2021-01-02

许可协议

评论