-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame2048.c
291 lines (256 loc) · 8.43 KB
/
game2048.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
/*
2048 Game
Coded by - Elton Fonseca
Supports limited colours through vt100 escape codes. Requires a posix machine for termios.h and unistd.h headers.
Provides simplistic animations when moving and merging blocks.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define D_INVALID -1
#define D_UP 1
#define D_DOWN 2
#define D_RIGHT 3
#define D_LEFT 4
const long values[] = {
0, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
};
const char *colors[] = {
"39", "31", "32", "33", "34", "35", "36", "37", "91", "92", "93", "94"
};
struct gamestate_struct__ {
int grid[4][4];
int have_moved;
long total_score;
long score_last_move;
int blocks_in_play;
} game;
struct termios oldt, newt;
void do_draw(void)
{
printf("\033[2J\033[HScore: %ld", game.total_score);
if (game.score_last_move)
printf(" (+%ld)", game.score_last_move);
printf("\n");
for (int i = 0; i < 25; ++i)
printf("-");
printf("\n");
for (int y = 0; y < 4; ++y) {
printf("|");
for (int x = 0; x < 4; ++x) {
if (game.grid[x][y])
printf("\033[7m\033[%sm%*zd \033[0m|", colors[game.grid[x][y]],
4, values[game.grid[x][y]]);
else
printf("%*s |", 4, "");
}
printf("\n");
}
for (int i = 0; i < 25; ++i) {
printf("-");
}
printf("\n");
}
void do_merge(int d)
{
/* These macros look pretty scary, but mainly demonstrate some space saving */
#define MERGE_DIRECTION(_v1, _v2, _xs, _xc, _xi, _ys, _yc, _yi, _x, _y) \
do { \
for (int _v1 = _xs; _v1 _xc; _v1 += _xi) { \
for (int _v2 = _ys; _v2 _yc; _v2 += _yi) { \
if (game.grid[x][y] && (game.grid[x][y] == \
game.grid[x + _x][y + _y])) { \
game.grid[x][y] += (game.have_moved = 1); \
game.grid[x + _x][y + _y] = (0 * game.blocks_in_play--);\
game.score_last_move += values[game.grid[x][y]]; \
game.total_score += values[game.grid[x][y]]; \
} \
} \
} \
} while (0)
game.score_last_move = 0;
switch (d) {
case D_LEFT:
MERGE_DIRECTION(x, y, 0, < 3, 1, 0, < 4, 1, 1, 0);
break;
case D_RIGHT:
MERGE_DIRECTION(x, y, 3, > 0, -1, 0, < 4, 1, -1, 0);
break;
case D_DOWN:
MERGE_DIRECTION(y, x, 3, > 0, -1, 0, < 4, 1, 0, -1);
break;
case D_UP:
MERGE_DIRECTION(y, x, 0, < 3, 1, 0, < 4, 1, 0, 1);
break;
}
#undef MERGE_DIRECTION
}
void do_gravity(int d)
{
#define GRAVITATE_DIRECTION(_v1, _v2, _xs, _xc, _xi, _ys, _yc, _yi, _x, _y) \
do { \
int break_cond = 0; \
while (!break_cond) { \
break_cond = 1; \
for (int _v1 = _xs; _v1 _xc; _v1 += _xi) { \
for (int _v2 = _ys; _v2 _yc; _v2 += _yi) { \
if (!game.grid[x][y] && game.grid[x + _x][y + _y]) { \
game.grid[x][y] = game.grid[x + _x][y + _y]; \
game.grid[x + _x][y + _y] = break_cond = 0; \
game.have_moved = 1; \
} \
} \
} \
do_draw(); usleep(40000); \
} \
} while (0)
switch (d) {
case D_LEFT:
GRAVITATE_DIRECTION(x, y, 0, < 3, 1, 0, < 4, 1, 1, 0);
break;
case D_RIGHT:
GRAVITATE_DIRECTION(x, y, 3, > 0, -1, 0, < 4, 1, -1, 0);
break;
case D_DOWN:
GRAVITATE_DIRECTION(y, x, 3, > 0, -1, 0, < 4, 1, 0, -1);
break;
case D_UP:
GRAVITATE_DIRECTION(y, x, 0, < 3, 1, 0, < 4, 1, 0, 1);
break;
}
#undef GRAVITATE_DIRECTION
}
int do_check_end_condition(void)
{
int ret = -1;
for (int x = 0; x < 4; ++x) {
for (int y = 0; y < 4; ++y) {
if (values[game.grid[x][y]] == 2048)
return 1;
if (!game.grid[x][y] ||
((x + 1 < 4) && (game.grid[x][y] == game.grid[x + 1][y])) ||
((y + 1 < 4) && (game.grid[x][y] == game.grid[x][y + 1])))
ret = 0;
}
}
return ret;
}
int do_tick(int d)
{
game.have_moved = 0;
do_gravity(d);
do_merge(d);
do_gravity(d);
return game.have_moved;
}
void do_newblock(void) {
if (game.blocks_in_play >= 16) return;
int bn = rand() % (16 - game.blocks_in_play);
int pn = 0;
for (int x = 0; x < 4; ++x) {
for (int y = 0; y < 4; ++y) {
if (game.grid[x][y])
continue;
if (pn == bn){
game.grid[x][y] = rand() % 10 ? 1 : 2;
game.blocks_in_play += 1;
return;
}
else {
++pn;
}
}
}
}
int main(void)
{
/* Initialize terminal settings */
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
srand(time(NULL));
memset(&game, sizeof(game), 0);
do_newblock();
do_newblock();
do_draw();
while (1) {
int found_valid_key, direction, value;
do {
found_valid_key = 1;
direction = D_INVALID;
value = getchar();
switch (value) {
case 'h': case 'a':
direction = D_LEFT;
break;
case 'l': case 'd':
direction = D_RIGHT;
break;
case 'j': case 's':
direction = D_DOWN;
break;
case 'k': case 'w':
direction = D_UP;
break;
case 'q':
goto game_quit;
break;
case 27:
if (getchar() == 91) {
value = getchar();
switch (value) {
case 65:
direction = D_UP;
break;
case 66:
direction = D_DOWN;
break;
case 67:
direction = D_RIGHT;
break;
case 68:
direction = D_LEFT;
break;
default:
found_valid_key = 0;
break;
}
}
break;
default:
found_valid_key = 0;
break;
}
} while (!found_valid_key);
do_tick(direction);
if (game.have_moved != 0){
do_newblock();
}
do_draw();
switch (do_check_end_condition()) {
case -1:
goto game_lose;
case 1:
goto game_win;
case 0:
break;
}
}
if (0)
game_lose:
printf("You lose!\n");
goto game_quit;
if (0)
game_win:
printf("You win!\n");
goto game_quit;
if (0)
game_quit:
/* Restore terminal settings */
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
return 0;
}