-
Notifications
You must be signed in to change notification settings - Fork 0
/
2048.c
159 lines (154 loc) · 3.5 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
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#define ddd printf("%d\n",__LINE__)
struct Block{
int num;
struct Block*up;
struct Block*down;
struct Block*left;
struct Block*right;
};
struct Block**gridGen(){
struct Block**list = (struct Block**)malloc(16*sizeof(struct Block*));
for(int i=0;i<16;i++){
struct Block*nb = (struct Block*)malloc(sizeof(struct Block));
nb->num=0;
list[i]=nb;
if(!(i+1)%4)
nb->right=NULL;
if(i<4)
nb->up = NULL;
else{
nb->up = list[i-4];
list[i-4]->down = nb;
}
if(i>11)
nb->down = NULL;
if((i+1)%4==1)
nb->left = NULL;
else{
nb->left = list[i-1];
list[i-1]->right = nb;
}
}
return list;
}
void printGrid(struct Block**grid){
printf("----------------------------------\n");
for(int i=0;i<16;i++){
printf("|");
if(grid[i]->num)
printf("%d\t",grid[i]->num);
else printf("\t");
if(((i+1)%4))
printf("|");
else printf("|\n----------------------------------\n");
}
}
#define CONT(d) MOV##d
#define MOVup down
#define MOVdown up
#define MOVright left
#define MOVleft right
#define MOVE(dir) \
void move##dir(struct Block*b){\
if(!b->dir){\
move##dir(b->CONT(dir));\
return;\
}\
struct Block*itr;\
for(itr=b;itr->dir&&!itr->dir->num;itr=itr->dir);\
itr->num=b->num;\
if(itr!=b)b->num=0;\
if(itr->dir&&itr->dir->num==itr->num){\
itr->dir->num+=itr->num;\
itr->num=0;\
}\
if(b->CONT(dir))move##dir(b->CONT(dir));\
}
MOVE(up)
MOVE(down)
MOVE(right)
MOVE(left)
void up(struct Block**grid){
moveup(grid[0]);
moveup(grid[1]);
moveup(grid[2]);
moveup(grid[3]);
}
void down(struct Block**grid){
movedown(grid[15]);
movedown(grid[14]);
movedown(grid[13]);
movedown(grid[12]);
}
void left(struct Block**grid){
moveleft(grid[0]);
moveleft(grid[4]);
moveleft(grid[8]);
moveleft(grid[12]);
}
void right(struct Block**grid){
moveright(grid[3]);
moveright(grid[7]);
moveright(grid[11]);
moveright(grid[15]);
}
void randGenN(struct Block**grid){
size_t x=0;
while(1){
x = rand()%16;
if(!grid[x]->num)
break;
}
int y = rand()%2;
grid[x]->num = y?2:4;
}
void clearScreen(){
for(int i=0;i<=9;i++)
printf("\033[1A");
for(int i=0;i<=9;i++)
printf(" \n");
for(int i=0;i<=9;i++)
printf("\033[1A");
}
char getChar(){
system("/bin/stty raw");
char res=getchar();
system("/bin/stty cooked");
return res;
}
int main(){
struct Block**x = gridGen();
randGenN(x);
printf("\033[2J\033[1;1H");
clearScreen();
printGrid(x);
char a=' ';
while(a!='q'){
a=getChar();
if(a==27&& getChar()==91){
a=getChar();
switch(a){
case 'A':
up(x);
break;
case 'B':
down(x);
break;
case 'C':
right(x);
break;
case 'D':
left(x);
break;
default:break;
}
randGenN(x);
}
clearScreen();
printGrid(x);
}
return 0;
}