-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini2048.c
249 lines (214 loc) · 5.22 KB
/
mini2048.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
/* See LICENSE for license details. */
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
static int m[4][4] = { 0 }; /* game board */
/* colors for tiles (for ANSI escape sequences \033[38;5;x )*/
static uint8_t bgcolor = 0;
static uint8_t fgcolor[] = {
[0] = 0, /* same as bgcolor, so no not displayed */
[2] = 1,
[4] = 2,
[8] = 3,
[16] = 4,
[32] = 5,
[64] = 6,
[128] = 7,
[256] = 8,
[512] = 9,
[1024] = 10,
[2048] = 11,
};
static void display()
{
int i, j;
for (i = 0; i < 4; ++i) {
for (j = 0; j < 4; ++j) {
printf("\033[48;5;%dm\033[38;5;%dm%5d\033[0m",
bgcolor, fgcolor[m[i][j]], m[i][j]);
if (j < 4)
putchar(' ');
}
putchar('\n');
putchar('\n');
}
}
static int nmoves = 0; /* number of tiles shifted/added in previous move */
static int won = 0; /* game won? */
static int score = 0; /* game score */
static void tile_swap(int *a, int *b) {
/* swap in direction b --> a */
if (!*a && *b) {
*a = *b;
*b = 0;
nmoves++;
}
}
static void tile_sum(int *a, int *b) {
/* sum in direction b --> a */
if (*a && *a == *b) {
*a = *a + *b;
*b = 0;
if (*a == 2048)
won = 1;
nmoves++;
score += *a;
}
}
#define INT(b) ((b)? 1: 0)
#define NEG(b) ((b)? -1: 1)
static void pair_iterator(int dim, int lr, void (*fn)(int*, int*))
{
int i[2], ii[2]; /* 2d loop variables */
int r_offset[2] = { /* index offset for next tile in shift direction */
NEG(lr) * INT(dim == 0),
NEG(lr) * INT(dim == 1)
};
int odim = (dim + 1) % 2;
int *left, *right;
for (i[dim] = 0; i[dim] < 3; ++i[dim]) {
for (i[odim] = 0; i[odim] < 4; ++i[odim]) {
/* transpose iteration on right/down */
ii[0] = lr? 3 - i[0]: i[0];
ii[1] = lr? 3 - i[1]: i[1];
left = &m[ii[0]][ii[1]];
right = &m[ii[0] + r_offset[0]][ii[1] + r_offset[1]];
fn(left, right);
}
}
}
/* shift (swap and add) tiles
dim == 0: x-direction; dim == 1: y-direction
lr == 0: left/up; lr == 1: right/down (depending on "dim") */
static void shift(int dim, int lr)
{
/* 3 rounds of shifting required, sum tiles after second
ensures full shift of all tiles, all possible sums and
full shift of sums. */
pair_iterator(dim, lr, tile_swap);
pair_iterator(dim, lr, tile_swap);
pair_iterator(dim, lr, tile_sum);
pair_iterator(dim, lr, tile_swap);
}
/* number of empty tiles */
static int n_empty_tiles()
{
int i, j, n = 0;
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
if (!m[i][j])
n++;
return n;
}
static int is_sum_possible = 0; /* state variable of sum_possible() */
static void sum_possible_impl(int *a, int *b)
{
if (*a && *a == *b)
is_sum_possible = 1;
}
static int sum_possible()
{
is_sum_possible = 0;
/* need only to examine direct neighbors
as this is only relevant on full boards */
pair_iterator(0, 0, sum_possible_impl);
pair_iterator(1, 0, sum_possible_impl);
return is_sum_possible;
}
static int move_possible()
{
return n_empty_tiles() != 0 || sum_possible();
}
/* uniform random number in [0, ub) */
static int uniform_random(int ub)
{
int lim = RAND_MAX - RAND_MAX % ub;
int r;
do {
r = rand();
} while (r >= lim);
return r % ub;
}
/* randomly add a new empty tile */
static void add_tiles()
{
int i, j;
int r = uniform_random(n_empty_tiles());
for (i = 0; i < 4; ++i)
for (j = 0; j < 4; ++j)
if (!m[i][j] && r-- == 0)
m[i][j] = 2;
}
static struct termios tio; /* termios state */
static void reset_termios()
{
tcsetattr(0, TCSANOW, &tio);
}
static void shandler(int signal)
{
(void) signal;
reset_termios();
exit(2);
}
int main()
{
int c, ret = 0;
struct termios ntio;
/* disable line-based input and echo */
tcgetattr(0, &tio);
ntio = tio;
ntio.c_lflag &= ~ICANON & ~ECHO;
tcsetattr(0, TCSANOW, &ntio);
signal(SIGTERM, shandler);
srand(time(NULL));
/* 2 tiles in the beginning */
add_tiles();
add_tiles();
while (1) {
if (won) {
printf("You win. Final score: %5d\n", score);
goto exit;
} else {
printf("\033[2J\033[H"); /* clear screen */
printf("Score: %5d\n\n", score);
}
if (nmoves)
add_tiles();
nmoves = 0;
display();
if (!move_possible()) {
printf("You lose.\n");
goto exit;
}
c = getchar();
switch (c) {
case 'q':
goto exit;
break;
case 'w':
case '8':
shift(0, 0);
break;
case 'a':
case '4':
shift(1, 0);
break;
case 's':
case '5':
shift(0, 1);
break;
case 'd':
case '6':
shift(1, 1);
break;
default:
break;
}
}
exit:
reset_termios();
return ret;
}