forked from chuang02/2048_cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.c
366 lines (355 loc) · 8 KB
/
2048.c
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
// 2048 GAME V5
// BY BTS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#ifdef _WIN32 //Linux platform
#include <conio.h>
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
#endif
int num[16];
int num_v[16];//Compairation
int num_old[16]//Secure Bak
char direct;
int check();
int signal;
int score;
int lose;
char get1char(void)
{
#ifdef _WIN32
// Do nothing
#else
struct termios stored_settings;
struct termios new_settings;
tcgetattr (0, &stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr (0, TCSANOW, &new_settings);
#endif
int ret = 0;
char c;
int express;
#ifdef _WIN32
c = getch();
#else
c = getchar();
putchar('\b');
#endif
printf("[%c]\n", c);
//system("sleep 0.2");
#ifdef _WIN32
// Do nothing
#else
tcsetattr (0, TCSANOW, &stored_settings);
#endif
return c;
}
int rnd(int x)
{
int judge;
judge = rand() % 10 + 1;
if(judge < 8) num[x] = 2;
if(judge >= 8) num[x] = 4;
return 0;
}
void draw_canvas()
{
#ifdef _WIN32
system("cls");//clear sreen
#else
system("clear");//clear screen
#endif
int express;
express = 0;
printf("2048 CLI V5\n");
printf("Built by Coder-BTS\n\n");
printf("Use <HJKL> | E-Exit | R-Reset\n\n");
printf("-----------------------------\n");
printf("| | | | |\n");
printf("|");
do
{
if(num[express] != 0) printf(" %4d |",num[express]);
if(num[express] == 0) printf(" |",num[express]);
express++;
}while(express < 4);
printf("\n| | | | |\n");
printf("-----------------------------\n");
printf("| | | | |\n");
printf("|");
do
{
if(num[express] != 0) printf(" %4d |",num[express]);
if(num[express] == 0) printf(" |",num[express]);
express++;
}while(express < 8);
printf("\n| | | | |\n");
printf("-----------------------------\n");
printf("| | | | |\n");
printf("|");
do
{
if(num[express] != 0) printf(" %4d |",num[express]);
if(num[express] == 0) printf(" |",num[express]);
express++;
}while(express < 12);
printf("\n| | | | |\n");
printf("-----------------------------\n");
printf("| | | | |\n");
printf("|");
do
{
if(num[express] != 0) printf(" %4d |",num[express]);
if(num[express] == 0) printf(" |",num[express]);
express++;
}while(express < 16);
printf("\n| | | | |\n");
printf("-----------------------------\n");
printf(">>> Score: %d <<<\n",score);
if(check() == 1)
{
printf("\n\nCan you make a forward move?\n");
}
}
void bakup()
{
int k;
for(k = 0;k < 16;k++)
{
num_v[k] = num[k];//bakup .old data
}
}
int compair()
{
int k;
for(k = 0;k < 16;k++)
{
if(num_v[k] != num[k]) return 1;
}
return 0;
}
void sec_bakup()
{
for(k = 0;k < 16;k++)
{
num_old[k] = num[k];//Secure Bak ***IMPORTANT
}
}
int sec_restore()
{
for(k = 0;k < 16;k++)
{
num[k] = num_old[k];
}
}
int check()
{
int k;
for(k = 0;k < 16;k++)
{
if(num[k] == 0) return 0;
}
for(k = 0;k < 16;k++)
{
num_old[k] = num[k];//Secure Bak ***IMPORTANT
}
signal = 2;
goto to_right;
check_1:
if(compair() == 1) goto end_check;
sec_restore();
goto to_left;
check_2:
if(compair() == 1) goto end_check;
sec_restore();
goto to_up;
check_3:
if(compair() == 1) goto end_check;
sec_restore();
goto to_down;
check_4:
if(compair() == 1) goto end_check;
lose = 1;
goto get_direct;
end_check:
sec_restore();
goto get_direct;
return 1;
}
int main()
{
int k;
int j;
int new_block;
start:
for(k = 0;k < 16;k++)//init void
{
num[k] = 0;
}
srand(time(NULL));
lose = 0;
score = 0;
rnd(11);//init
rnd(14);
rnd(15);
for(k = 0;k < 16;k++)
{
num_v[k] = num[k];//bakup .old data
}
get_direct:
//system("clear");//clear screen
draw_canvas();
//system("stty -icanon");
direct = get1char();
//printf("%c",direct);
signal = 0;
bakup();
if(direct == 'h' || direct == 'H' || direct == 68) goto to_left;
if(direct == 'j' || direct == 'J' || direct == 66) goto to_down;
if(direct == 'k' || direct == 'K' || direct == 65) goto to_up;
if(direct == 'l' || direct == 'L' || direct == 67) goto to_right;
if(direct == 'r' || direct == 'R') goto start;
if(direct == 'e' || direct == 'E')
{
system("clear");
exit(1);
}
goto get_direct;
to_right:
for(k = 0;k <= 12;k += 4)
{
for(j = k + 3;j >= k + 1;j--)
{
if(num[j] == 0)
{
num[j] = num[j-1];
num[j-1] = 0;
}
if(num[j] != 0)
{
if(num[j] == num[j-1])
{
num[j] = num[j] * 2;
score = score + num[j];
num[j-1] = 0;
}
}
}
}
if(compair() == 0)
{
if(signal != 2) goto get_wait;
if(signal == 2) goto check_1;
}
bakup();
signal = 1;
goto to_right;
to_left:
for(k = 0;k <= 12;k += 4)
{
for(j = k;j <= k + 2;j++)
{
if(num[j] == 0)
{
num[j] = num[j+1];
num[j+1] = 0;
}
if(num[j] != 0)
{
if(num[j] == num[j+1])
{
num[j] = num[j] * 2;
score = score + num[j];
num[j+1] = 0;
}
}
}
}
if(compair() == 0)
{
if(signal != 2) goto get_wait;
if(signal == 2) goto check_2;
}
bakup();
signal = 1;
goto to_left;
to_up:
for(k = 0; k <= 3;k++)
{
for(j = k;j <= 11;j += 4)
{
if(num[j] == 0)
{
num[j] = num[j+4];
num[j+4] = 0;
}
if(num[j] != 0)
{
if(num[j] == num[j+4])
{
num[j] = num[j] * 2;
score = score + num[j];
num[j+4] = 0;
}
}
}
}
if(compair() == 0)
{
if(signal != 2) goto get_wait;
if(signal == 2) goto check_3;
}
bakup();
signal = 1;
goto to_up;
to_down:
for(k = 12;k <= 15;k++)
{
for(j = k;j >= 4;j -= 4)
{
if(num[j] == 0)
{
num[j] = num[j-4];
num[j-4] = 0;
}
if(num[j] != 0)
{
if(num[j] == num[j-4])
{
num[j] = num[j] * 2;
score = score + num[j];
num[j-4] = 0;
}
}
}
}
if(compair() == 0)
{
if(signal != 2) goto get_wait;
if(signal == 2) goto check_4;
bakup();
signal = 1;
goto to_down;
get_wait:
draw_canvas();
if(signal == 0) goto get_direct;
if(check() == 1) goto get_direct;
new_block = rand() % 16;
if(num[new_block] != 0) goto get_wait;
#ifdef _WIN32
sleep(100);
#else
system("sleep 0.1");
#endif
rnd(new_block);
goto get_direct;
quit_g:
exit(1);
return 0;
}