0%

不务正业系列:C语言版2048

C语言版2048

之前我复现了 Actf 的 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
346
#include<stdio.h>
#include<termios.h>
#include<unistd.h>
#include<signal.h>
#include<stdlib.h>
#include<time.h>

struct termios old_config;
struct termios new_config;

const char *str_list[12]={
"39",
"31",
"32",
"33",
"34",
"35",
"36",
"37",
"91",
"92",
"93",
"94",
};

unsigned long int score;
unsigned long int extra;

unsigned int box[4][4];
char num[8];
size_t key[12]={0,2,4,8,0x10,0x20,0x40,0x80,0x100,0x200,0x400,0x800};

int win;
int lose;

void table_show(){
int m;
int k;
int j;
int i;

system("clear");
printf("\x1B[2J\x1B[HScore: %ld", score);
if(extra){
printf(" (+%ld)",extra);
}
putchar('\n');
puts("+------+------+------+------+");
for(i=0;i<=3;i++){
putchar('|');
for(j=0;j<=3;j++){
if(box[i][j]){
printf("\x1B[7m\x1B[%sm%*s \x1B[0m|", str_list[box[i][j]], 5, num);
}
else{
printf("%*s |", 5, num);
}
}
putchar('\n');
putchar('|');
for(k=0;k<=3;k++){
if (box[i][k]){
printf("\x1B[7m\x1B[%sm%*zd \x1B[0m|", str_list[box[i][k]], 5, key[box[i][k]]);
}
else{
printf("%*s |", 5, num);
}
}
putchar('\n');
putchar('|');
for(m=0;m<=3;m++){
if(box[i][m]){
printf("\x1B[7m\x1B[%sm%*s \x1B[0m|", str_list[box[i][m]], 5, num);
}
else{
printf("%*s |", 5, num);
}
}
putchar('\n');
puts("+------+------+------+------+");
}
}

unsigned int check_repeat(int random_num){
int i;
int j;
int m;
int re;
int total=0;

for(i=0;i<4;i++){
for(j=0;j<4;j++){
m = i*4+j;
if(random_num==m){
if(box[i][j]==0){
return 1;
}
else{
re = 0;
}
}
if(box[i][j]!=0){
total++;
}
}
}
if(total==16){
re = 2;
}
return re;
}

void randomly_add(){
int random_num1;
int random_num2;
unsigned int num;
unsigned int key;
int j;
int i;
int m;

srand(time(0));
while(1){
random_num1 = rand() % 16;
key = check_repeat(random_num1);
if(key == 2){
return;
}
if(key == 1){
break;
}
}

for(i=0;i<4;i++){
for(j=0;j<4;j++){
m=i*4+j;
if(m==random_num1){
random_num2 = rand();
if(random_num2 == (random_num2/10)*10){
num=2;
}
else{
num=1;
}
box[i][j]=num;
return;
}
}
}
}

void table_init(){
int i;
int j;

for(i=0;i<=3;i++){
for(j=0;j<=0;j++){
box[i][j]=0;
}
}
score=0;
extra=0;
win=0;
lose=0;
randomly_add();
randomly_add();
table_show();
}

void table_check(int extra_temp){
int i;
int j;

extra = extra_temp/2;
score += extra_temp/2;
lose = 1;

for(i=0;i<=3;++i){
for(j=0;j<=3;++j){
if(key[box[i][j]]==2048){
win = 1;
}
}
}
}

void move_left(){
int i;
int j;
int k;

for(i=0;i<4;i++){
for(j=1,k=0;j<4;j++){
if(box[i][j]>0){
if(box[i][k]==box[i][j]){
box[i][k]++;
box[i][j]=0;
table_check(key[box[i][k]]);
k++;
}
else if(box[i][k]==0){
box[i][k]=box[i][j];
box[i][j]=0;
}
else{
box[i][++k]=box[i][j];
if(j!=k){
box[i][j]=0;
}
}
}
}
}
}

void move_right(){
int i;
int j;
int k;

for(i=0;i<4;i++){
for(j=2,k=3;j>=0;j--){
if(box[i][j]>0){
if(box[i][k]==box[i][j]){
box[i][k]++;
box[i][j]=0;
table_check(key[box[i][k]]);
k--;
}
else if(box[i][k]==0){
box[i][k]=box[i][j];
box[i][j]=0;
}
else{
box[i][--k]=box[i][j];
if(j!=k){
box[i][j]=0;
}
}
}
}
}
}

void move_up(){
int i;
int j;
int k;

for(i=0;i<4;i++){
for(j=1,k=0;j<4;++j){
if(box[j][i]>0){
if(box[k][i]==box[j][i]){
box[k][i]++;
box[j][i]=0;
table_check(key[box[k][i]]);
k++;
}
else if(box[k][i]==0){
box[k][i]=box[j][i];
box[j][i]=0;
}
else{
box[++k][i]=box[j][i];
if(j!=k){
box[j][i]=0;
}
}
}
}
}
}

void move_down(){
int i;
int j;
int k;

for(i=0;i<4;i++){
for(j=2,k=3;j>=0;--j){
if(box[j][i]>0){
if(box[k][i]==box[j][i]){
box[k][i]++;
box[j][i]=0;
table_check(key[box[k][i]]);
k--;
}
else if(box[k][i]==0){
box[k][i]=box[j][i];
box[j][i]=0;
}
else{
box[--k][i]=box[j][i];
if(j!=k){
box[j][i]=0;
}
}
}
}
}
}

void game(){
int input;
table_init();

while(1){
extra = 0;
input = 0;
input = getchar();
randomly_add();

if(input == 'w'){
move_up();
}
else if(input == 's'){
move_down();
}
else if(input == 'a'){
move_left();
}
else if(input == 'd'){
move_right();
}
table_show();

if(win==1){
printf("You are win~~,Thanks for your playing~~~\n");
getchar();
return;
}
}
}

int main(){
tcgetattr(0,&old_config);
new_config = old_config;
new_config.c_lflag &= ~ICANON;
new_config.c_lflag &= ~ECHO;
new_config.c_cc[VMIN] = 1;
new_config.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_config);
game();

return 0;
}
  • 关于几个 move 的操作参考了网上的代码
  • 而关于 game loss 的判断则没有写(IDA 太乱了)

运行效果如下: