forked from cuadue/2048_game
/
2048.c
281 lines (238 loc) · 4.82 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
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
/* A version for HP 39gs graphing calculator
*
* see https://github.com/XiangTianxiao/2048
*
*/
/* Clone of the 2048 sliding tile puzzle game. (C) Wes Waugh 2014
*
* This program is free software, licensed under the GPLv3. Check the
* LICENSE file for details.
*/
#include <hpgcc49.h>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define NROWS 4
#define NCOLS NROWS
typedef unsigned int tile_t;
struct game_t
{
int turns, score;
tile_t board[NROWS][NCOLS];
};
int my_random()
{
return random()*10e10;
}
// place_tile() returns 0 if it did place a tile and -1 if there is no open
// space.
int place_tile(struct game_t *game)
{
// lboard is the "linear board" -- no need to distinguish rows/cols
tile_t *lboard = (tile_t *)game->board;
int i, num_zeros = 0;
// Walk the board and count the number of empty tiles
for (i = 0; i < NROWS * NCOLS; i++)
{
num_zeros += lboard[i] ? 0 : 1;
}
if (!num_zeros)
{
return -1;
}
// Choose the insertion point
int loc = my_random() % num_zeros;
// Find the insertion point and place the new tile
for (i = 0; i < NROWS * NCOLS; i++)
{
if (!lboard[i] && !(loc--))
{
lboard[i] = my_random() % 10 ? 1 : 2;
return 0;
}
}
return -1;
}
void print_tile(int tile)
{
if (tile)
{
printf("%4d", 1 << tile);
}
else
{
printf(" .");
}
}
void print_game(struct game_t *game)
{
int r, c;
gotoxy(0, 0);
printf("Score: %6d Turns: %4d", game->score, game->turns);
for (r = 0; r < NROWS; r++)
{
for (c = 0; c < NCOLS; c++)
{
gotoxy(5 * c, r + 2);
print_tile(game->board[r][c]);
}
}
}
int combine_left(struct game_t *game, tile_t row[NCOLS])
{
int c, did_combine = 0;
for (c = 1; c < NCOLS; c++)
{
if (row[c] && row[c - 1] == row[c])
{
row[c - 1]++;
row[c] = 0;
game->score += 1 << (row[c - 1] - 1);
did_combine = 1;
}
}
return did_combine;
}
// deflate_left() returns nonzero if it did deflate, and 0 otherwise
int deflate_left(tile_t row[NCOLS])
{
tile_t buf[NCOLS] = { 0 };
tile_t *out = buf;
int did_deflate = 0;
int in;
for (in = 0; in < NCOLS; in++)
{
if (row[in] != 0)
{
*out++ = row[in];
did_deflate |= buf[in] != row[in];
}
}
memcpy(row, buf, sizeof(buf));
return did_deflate;
}
void rotate_clockwise(struct game_t *game)
{
tile_t buf[NROWS][NCOLS];
memcpy(buf, game->board, sizeof(game->board));
int r, c;
for (r = 0; r < NROWS; r++)
{
for (c = 0; c < NCOLS; c++)
{
game->board[r][c] = buf[NCOLS - c - 1][r];
}
}
}
void move_left(struct game_t *game)
{
int r, ret = 0;
for (r = 0; r < NROWS; r++)
{
tile_t *row = &game->board[r][0];
ret |= deflate_left(row);
ret |= combine_left(game, row);
ret |= deflate_left(row);
}
game->turns += ret;
}
void move_right(struct game_t *game)
{
rotate_clockwise(game);
rotate_clockwise(game);
move_left(game);
rotate_clockwise(game);
rotate_clockwise(game);
}
void move_up(struct game_t *game)
{
rotate_clockwise(game);
rotate_clockwise(game);
rotate_clockwise(game);
move_left(game);
rotate_clockwise(game);
}
void move_down(struct game_t *game)
{
rotate_clockwise(game);
move_left(game);
rotate_clockwise(game);
rotate_clockwise(game);
rotate_clockwise(game);
}
// Pass by value because this function mutates the game
int lose_game(struct game_t test_game)
{
int start_turns = test_game.turns;
move_left(&test_game);
move_up(&test_game);
move_down(&test_game);
move_right(&test_game);
return test_game.turns == start_turns;
}
void init_curses()
{
clear_screen(); // curses init
gotoxy(0, 0);
}
int max_tile(tile_t *lboard)
{
int i, ret = 0;
for (i = 0; i < NROWS * NCOLS; i++)
{
ret = max(ret, lboard[i]);
}
return ret;
}
int main()
{
init_curses();
const char *exit_msg = "";
struct game_t game = { 0 };
int last_turn = game.turns;
place_tile(&game);
place_tile(&game);
gotoxy(0, 7);
printf("Press ENTER to quit.");
int cur_key;
while (1)
{
print_game(&game);
if (lose_game(game))
{
exit_msg = "lost";
goto lose;
}
last_turn = game.turns;
switch (cur_key = keyb_getkey(1))
{
case 22:
move_left(&game);
break;
case 38:
move_down(&game);
break;
case 6:
move_up(&game);
break;
case 54:
move_right(&game);
break;
case 96:
exit_msg = "quit";
goto end;
}
if (last_turn != game.turns)
place_tile(&game);
}
lose:
gotoxy(0, 7);
printf("You lose! Press ENTER to quit.");
while (keyb_getkey(1) != 96);
end:
gotoxy(0, 7);
printf("You %s after scoring %d points in %d turns, "
"with largest tile %d\n",
exit_msg, game.score, game.turns,
1 << max_tile((tile_t *)game.board));
sys_sleep(3000);
return 0;
}