forked from radareorg/radare2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2048.c
205 lines (197 loc) · 4.02 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
#include <r_cons.h>
// TWOK is a C implemnetation of 2048 game
#define ut8 unsigned char
static ut8 twok_buf[4][4];
static int score =0;
static int moves =0;
#define INTERNAL static
INTERNAL void twok_init() {
int i, j;
score = 0;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
twok_buf[i][j] = 0;
}
INTERNAL void twok_add() {
int i, j, min = 1;
int holes = 0;
for (i=0;i<4;i++) {
for (j=0;j<4;j++) {
if (twok_buf[i][j] > 1) {
min = 2;
break;
}
}
}
for (i=0;i<4;i++)
for (j=0;j<4;j++)
if (!twok_buf[i][j]) {
holes = 1;
break;
}
if (holes)
for (;;) {
i = r_num_rand (4);
j = r_num_rand (4);
if (!twok_buf[i][j]) {
twok_buf[i][j] = r_num_rand (min)+1;
break;
}
}
}
INTERNAL int twok_fin() {
int i,j;
for (i=0;i<4;i++)
for (j=0;j<4;j++)
if (!twok_buf[i][j])
return 1;
for (i=0;i<4;i++)
for (j=0;j<3;j++)
if (twok_buf[i][j] == twok_buf[i][j+1])
return 1;
for (i=0;i<3;i++)
for (j=0;j<4;j++)
if (twok_buf[i][j] == twok_buf[i+1][j])
return 1;
return 0;
}
INTERNAL void twok_move(int d) {
int i, j, k;
moves++;
if (d=='a') {
twok_add ();
} else
for (k=0; k<4; k++) {
switch (d) {
case 'h': // left
// for each row
for (i=0; i<4; i++) {
for (j=0; j<3; j++) {
if (twok_buf[i][j] == 0) {
twok_buf[i][j] = twok_buf[i][j+1];
twok_buf[i][j+1] = 0;
} else
if (twok_buf[i][j] == twok_buf[i][j+1]) {
twok_buf[i][j] ++;
score += (1<<twok_buf[i][j]);
twok_buf[i][j+1] = 0;
}
}
}
break;
case 'l': // right
for (i=0; i<4; i++) {
for (j=3; j>0; j--) {
if (twok_buf[i][j] == 0) {
twok_buf[i][j] = twok_buf[i][j-1];
twok_buf[i][j-1] = 0;
} else
if (twok_buf[i][j] == twok_buf[i][j-1]) {
twok_buf[i][j] ++;
score += (1<<twok_buf[i][j]);
twok_buf[i][j-1] = 0;
}
}
}
break;
case 'j': // down
// for each column
for (j=0; j<4; j++) {
for (i=3; i>0; i--) {
if (twok_buf[i][j] == 0) {
twok_buf[i][j] = twok_buf[i-1][j];
twok_buf[i-1][j] = 0;
} else
if (twok_buf[i][j] == twok_buf[i-1][j]) {
twok_buf[i][j] ++;
score += (1<<twok_buf[i][j]);
twok_buf[i-1][j] = 0;
}
}
}
break;
case 'k': // up
// for each column
for (j=0; j<4; j++) {
for (i=0; i<3; i++) {
if (twok_buf[i][j] == 0) {
twok_buf[i][j] = twok_buf[i+1][j];
twok_buf[i+1][j] = 0;
} else
if (twok_buf[i][j] == twok_buf[i+1][j]) {
twok_buf[i][j] ++;
score += (1<<twok_buf[i][j]);
twok_buf[i+1][j] = 0;
}
}
}
break;
}}
}
INTERNAL void twok_print() {
char val0[32];
char val1[32];
char val2[32];
char val3[32];
int i;
#define VAL(x) if (twok_buf[i][x]){\
sprintf(val##x,"%4d",1<<twok_buf[i][x]); \
} else strcpy(val##x, " ");
printf (" +------+------+------+------+\n");
for (i = 0; i<4; i++) {
VAL(0); VAL(1);
VAL(2); VAL(3);
printf (" | | | | |\n");
printf (" | %s | %s | %s | %s |\n",
val0,val1,val2,val3);
printf (" | | | | |\n");
printf (" +------+------+------+------+\n");
}
printf ("Hexboard: 'hjkl' and 'q'uit\n");
for (i = 0; i<4; i++)
printf (" %02x %02x %02x %02x\n",
twok_buf[i][0], twok_buf[i][1],
twok_buf[i][2], twok_buf[i][3]);
}
#if 0
int main() {
char buf[128];
twok_init();
twok_add();
// canonical stdin here
while (twok_fin()) {
twok_print();
if (!fgets (buf, sizeof (buf)-1, stdin))
break;
twok_move (buf[0]);
twok_add ();
}
printf ("score: %d\n", twok_score ());
return 0;
}
#endif
R_API void r_cons_2048() {
int ch;
r_cons_set_raw (1);
twok_init ();
twok_add ();
while (twok_fin()) {
r_cons_clear00();
r_cons_printf ("[r2048] score: %d moves: %d\n",
score, moves);
r_cons_flush ();
twok_print();
ch = r_cons_readchar ();
ch = r_cons_arrow_to_hjkl (ch);
if (ch<1||ch =='q') break;
twok_move (ch);
twok_add ();
}
r_cons_clear00();
r_cons_printf ("[r2048] score: %d\n", score );
r_cons_flush ();
twok_print();
r_cons_printf ("\n [r2048.score] %d\n", score );
r_cons_any_key ();
r_cons_set_raw (0);
}