-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2048.c
267 lines (225 loc) · 5.87 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
//
// 2048.c
//
// Copyright (c) 2014 Ben Kogan <http://benkogan.com>
// Gameplay based on 2048 by Gabriele Cirulli <http://gabrielecirulli.com>
//
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <signal.h>
#define GOAL 2048
#define SIZE 4
/*
* boardLt is the canonical board; every other board reflects boardLt in its
* respective direction.
*/
static int *(boardLt[SIZE][SIZE]), // left (canonical)
*(boardRt[SIZE][SIZE]), // right
*(boardUp[SIZE][SIZE]), // up
*(boardDn[SIZE][SIZE]), // down
*lastAdd = 0, // address of last tile added
score = 0,
win = false;
static const int QUIT = -1,
LOSE = 0,
WIN = 1;
/*
* Free boards using boardLt.
*/
void
cleanup() {
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
free(boardLt[r][c]);
}
}
}
/*
* Print outcome and exit gracefully.
*/
int
quit(int op) {
char *msg = op == 1 ? "YOU WIN!": op == 0 ? "GAME OVER." : "QUIT";
printf("\n\n%s\n", msg);
cleanup();
exit(0);
}
/*
* Signal handler function for SIGINT.
*/
void
terminate(int signum) {
quit(QUIT);
}
/*
* Exit with message on error.
*/
static void
die(const char *message) {
perror(message);
exit(1);
}
/*
* Set up boards.
*/
void
initBoard() {
for (int r = 0; r < SIZE; r++) { // row for canonical board
for (int c = 0; c < SIZE; c++) { // column for canonical board
int *tile = (int *) malloc(sizeof(int));
if (tile == NULL) die("malloc returned NULL");
*tile = 0;
// add tile to equivalent location in all directional boards
boardLt[r][c] = tile;
boardRt[r][SIZE-1-c] = boardLt[r][c];
boardUp[SIZE-1-c][r] = boardLt[r][c];
boardDn[c][SIZE-1-r] = boardLt[r][c];
}
}
}
/*
* Print the specified board.
*/
void
printBoard(int *board[SIZE][SIZE]) {
printf("\n2048\n\nSCORE: %d\n\n", score);
for (int r = 0; r < SIZE; r++) {
for (int c = 0; c < SIZE; c++) {
if (*board[r][c]) {
if (lastAdd == board[r][c])
printf("\033[036m%6d\033[0m", *board[r][c]); // with color
else
printf("%6d", *board[r][c]);
} else {
printf("%6s", ".");
}
}
printf("\n\n");
}
printf("\nMOVEMENT:\n w\n a s d ");
}
/*
* Add tile to random open space on board.
* Tile is a 2 or a 4, randomly chosen.
*/
void
addRandom() {
int r, c;
do {
r = rand()%4;
c = rand()%4;
} while (*boardLt[r][c] != 0);
int two_or_four = 2 * rand()%2 + 2;
*boardLt[r][c] = two_or_four;
lastAdd = boardLt[r][c];
}
/*
* Slide all tiles left on the specified board.
*/
bool
slide(int *b[SIZE][SIZE]) {
bool success = 0; // true if something moves
int marker = -1; // marks a cell one past location of a previous merge
for (int r = 0; r < SIZE; r++) { // rows
for (int c = 1; c < SIZE; c++) { // don't need to slide first col
if (!(*b[r][c])) continue; // no tile
// advance to proper merge target in new column (newc)
int newc = c;
while (newc && (!*b[r][newc-1] || *b[r][newc-1] == *b[r][c])) {
newc--;
if (*b[r][newc] == *b[r][c] || marker == newc) break;
}
// merge tile with target tile
if (newc != c) {
if (*b[r][newc]) {
score += *b[r][newc] += *b[r][c];
marker = newc+1;
if (*b[r][newc] == GOAL) win = true;
} else {
*b[r][newc] += *b[r][c];
}
*b[r][c] = 0;
success = true;
}
}
marker = -1; // reset for next row
}
return success;
}
/*
* Get and act on next move from user.
*/
int
move() {
bool success = false;
char direction = getchar();
switch(direction) {
case 119: // 'w' key; up
success = slide(boardUp);
break;
case 97: // 'a' key; left
success = slide(boardLt);
break;
case 115: // 's' key; down
success = slide(boardDn);
break;
case 100: // 'd' key; right
success = slide(boardRt);
break;
case 113: // 'q' key; quit
quit(QUIT);
break;
// default:
// success = false;
}
return success;
}
/*
* Check for possible moves.
*
* Return: true if no moves are possible
* false if a move exists
*/
bool
isFull() {
for (int r = SIZE-1; r >= 0; r--) {
for (int c = SIZE-1; c >= 0; c--) {
// check tile above where there is a row above
if (r &&
(*boardLt[r-1][c] == 0 ||
*boardLt[r-1][c] == *boardLt[r][c]))
return false;
// check tile to left where there is a column to the left
if (c &&
(*boardLt[r][c-1] == 0 ||
*boardLt[r][c-1] == *boardLt[r][c]))
return false;
}
}
return true; // no possible moves found
}
/*
* Main.
*/
int
main(int argc, char **argv){
srand(time(NULL)); // seed random number
signal(SIGINT, terminate); // set up signal to handle ctrl-c
system("stty cbreak"); // read user input immediately
initBoard();
addRandom();
for (;;) {
printf("\e[1;1H\e[2J"); // clear screen
addRandom();
printBoard(boardLt);
if (isFull()) break;
if (win) quit(WIN);
while(!move())
;
}
quit(LOSE); // game over
cleanup(); // not reached
return 0;
}