-
Notifications
You must be signed in to change notification settings - Fork 24
/
2048.cpp
344 lines (319 loc) · 8.58 KB
/
2048.cpp
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
#include <iostream>
#include <windows.h>
#include <ctime>
using namespace std;
int const ROW = 4;
int const COL = 4;
int game[ROW][COL] = {0};
//上下左右
int const UP = 1;
int const DOWN = 2;
int const LEFT = 3;
int const RIGHT = 4;
//游戏所处的状态
int const GAME_OVER = 1;
int const GAME_WIN = 2;
int const GAME_CONTINUE = 3;
enum GameNum
{
Game_2 = 2,
Game_4 = 4,
Game_8 = 8,
Game_16 = 16,
Game_32 = 32,
Game_64 = 64,
Game_128 = 128,
Game_256 = 256,
Game_512 = 512,
Game_1024 = 1024,
Game_2048 = 2048,
};
//打印所得的数组
void Print()
{
system("cls");
cout << "***************** 2048 控 制 台 版 ******************" << endl;
cout << "***************** By Tanzf (Intern) ******************" << endl << endl;
for (int i = 0; i < ROW; ++i)
{
cout << "---------------------------------"<< endl;
for (int j = 0; j < COL; ++j)
{
if (game[i][j] == 0)
{
cout <<"| \t";
}
else
{
cout <<"| " << game[i][j] << "\t";
}
}
cout << "|" << endl;
}
cout << "---------------------------------"<< endl;
}
//界面设置
bool CreateNumber()
{
int x = -1;
int y = -1;
int times = 0;
int maxTimes = ROW * COL;
//三分之二的概率生成2,三分之一的概率生成4
int whitch = rand() % 3;
do
{
x = rand() % ROW;
y = rand() % COL;
++times;
} while (game[x][y] != 0 && times <= maxTimes);
//说明格子已经满了
if(times >= maxTimes)
{
return false;
}
else
{
GameNum num;
if(whitch == 0)
{
num = Game_4;
}
else if(whitch)
{
num = Game_2;
}
game[x][y] = num;
}
return true;
}
void Process(int direction)
{
switch (direction)
{
case UP:
//最上面一行不动
for(int row = 1; row < ROW; ++row)
{
for(int crow = row; crow >= 1; --crow)
{
for(int col = 0; col < COL; ++col)
{
//上一个格子为空
if(game[crow-1][col] == 0)
{
game[crow-1][col] = game[crow][col];
game[crow][col] = 0;
}
else
{
//合并
if(game[crow-1][col] == game[crow][col])
{
game[crow - 1][col] *= 2;
game[crow][col] = 0;
}
}
}
}
}
break;
case DOWN:
//最下面一行不动
for(int row = ROW - 2; row >= 0; --row)
{
for(int crow = row; crow < ROW - 1; ++crow)
{
for(int col = 0; col < COL; ++col)
{
//上一个格子为空
if(game[crow + 1][col] == 0)
{
game[crow + 1][col] = game[crow][col];
game[crow][col] = 0;
}
else
{
//合并
if(game[crow + 1][col] == game[crow][col])
{
game[crow + 1][col] *= 2;
game[crow][col] = 0;
}
}
}
}
}
break;
case LEFT:
//最左边一列不动
for(int col = 1; col < COL; ++col)
{
for(int ccol = col; ccol >= 1; --ccol)
{
for(int row = 0; row < ROW; ++row)
{
//上一个格子为空
if(game[row][ccol-1] == 0)
{
game[row][ccol - 1] = game[row][ccol];
game[row][ccol] = 0;
}
else
{
//合并
if(game[row][ccol - 1] == game[row][ccol])
{
game[row][ccol - 1] *= 2;
game[row][ccol] = 0;
}
}
}
}
}
break;
case RIGHT:
//最右边一列不动
for(int col = COL - 2; col >= 0; --col)
{
for(int ccol = col; ccol <= COL - 2; ++ccol)
{
for(int row = 0; row < ROW; ++row)
{
//上一个格子为空
if(game[row][ccol + 1] == 0)
{
game[row][ccol + 1] = game[row][ccol];
game[row][ccol] = 0;
}
else
{
//合并
if(game[row][ccol + 1] == game[row][ccol])
{
game[row][ccol + 1] *= 2;
game[row][ccol] = 0;
}
}
}
}
}
break;
}
}
//处理输入输出,返回上下左右
int Input()
{
//读取上下左右四个方向键
int upArrow = 0;
int downArrow = 0;
int leftArrow = 0;
int rightArrow = 0;
int direction = 0;
while (true)
{
upArrow = GetAsyncKeyState(VK_UP);
downArrow = GetAsyncKeyState(VK_DOWN);
leftArrow = GetAsyncKeyState(VK_LEFT);
rightArrow = GetAsyncKeyState(VK_RIGHT);
if(upArrow)
{
direction = UP;
break;
}
else if(downArrow)
{
direction = DOWN;
break;
}
else if(leftArrow)
{
direction = LEFT;
break;
}
else if(rightArrow)
{
direction = RIGHT;
break;
}
Sleep(100);
}
return direction;
}
//判断游戏状态
int Judge()
{
//赢得游戏
for(int i = 0; i < ROW; ++i)
{
for(int j = 0; j < COL; ++j)
{
if(game[i][j] == 2048)
{
return GAME_WIN;
break;
}
}
}
//横向检查
for(int i = 0 ; i < ROW; ++i)
{
for(int j = 0; j < COL - 1; ++j)
{
if(!game[i][j] || (game[i][j] == game[i][j+1]))
{
return GAME_CONTINUE;
break;
}
}
}
//纵向检查
for(int j = 0; j< COL; ++j)
{
for(int i = 0; i < ROW -1; ++i)
{
if(!game[i][j] || (game[i][j] == game[i+1][j]))
{
return GAME_CONTINUE;
break;
}
}
}
//不符合上述两种状况,游戏结束
return GAME_OVER;
}
int main()
{
//设置一个随机数种子
srand((unsigned int)time(0));
CreateNumber();
CreateNumber();
Print();
int direction = 0;
int gameState = -1;
while(true)
{
direction = Input();
gameState = Judge();
if(direction && gameState == GAME_CONTINUE)
{
Process(direction);
CreateNumber();
Print();
Sleep(100);
}
else if(gameState == GAME_WIN)
{
Print();
cout << "You Win!" << endl;
break;
}
else if(gameState == GAME_OVER)
{
Print();
cout <<"You lose!" << endl;
break;
}
}
return 0;
}