-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
309 lines (282 loc) · 8.91 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include <ncurses.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <ctype.h>
/*
* Will define the size of the game board as g_size rows and g_size columns. As it
* stands, the display funcions work best with 4.
*/
int g_size = 4;
struct game_state {
int **grid;
long total_score;
long score_last_move;
int tiles_in_play;
bool game_over;
};
struct tile {
int x;
int y;
};
struct game_state *initGameState();
void draw(struct game_state *);
void moveGrid(struct game_state *, int);
void addRandomTile(struct game_state *);
void getDirectionVector(int vector[], int dir);
void buildTraversals(int *traversals[2], int *vector);
void findFarthestPosition(struct game_state *, int[4], int, int, int[2]);
bool inRange(int);
bool tileMatchesAvailible(struct game_state *);
void initCurses();
bool parseArgs(int, char *[]);
int main(int argc, char *argv[]) {
if (argc > 1) {
if(!parseArgs(argc, argv))
return -1;
}
struct game_state *game = initGameState();
srand((unsigned)time(NULL));
addRandomTile(game);
addRandomTile(game);
initCurses();
draw(game);
draw(game);
int c;
while ((c = getch()) != KEY_F(1) && game->game_over != true) {
moveGrid(game, c - 258);
draw(game);
}
mvprintw(0, 10, "Sorry, you lose! Score: %d", game->total_score);
getch();
endwin();
}
/*
* Initialize game state into a locally allocated game state pointer and return.
*/
struct game_state *initGameState() {
struct game_state *game =
(struct game_state *)malloc(sizeof(struct game_state));
game->grid = (int **)malloc(g_size * sizeof(int *));
for (int i = 0; i < g_size; i++) {
game->grid[i] = (int *)malloc(g_size * sizeof(int));
for (int j = 0; j < g_size; j++) game->grid[i][j] = 0;
}
game->total_score = 0;
game->score_last_move = 0;
game->tiles_in_play = 0;
game->game_over = false;
return game;
}
/*
* Find a random location in the grid, incremenent tiles_in_play.
*/
void addRandomTile(struct game_state *game) {
bool inserted = false;
while (!inserted) {
int i = rand() % g_size;
int j = rand() % g_size;
if (game->grid[i][j] == 0) {
game->grid[i][j] = 2;
inserted = true;
}
}
game->tiles_in_play++;
}
/*
* Draw the g_size by g_size grid onto the screen via ncurses Each grid element is
* its own ncurses WINDOW so that custom colors can be assigned to each value.
*/
void draw(struct game_state *game) {
WINDOW *local_window[g_size][g_size];
for (int i = 0; i < g_size; i++) {
for (int j = 0; j < g_size; j++) {
int y, x;
getmaxyx(stdscr, y, x);
local_window[i][j] = newwin(4, 6, (y - g_size * 4) / 2 + i * 4,
(x - g_size * 6) / 2 + j * 6);
// I could remove box and invert colors instead.
box(local_window[i][j], 0, 0);
char num[4];
sprintf(num, "%d", game->grid[i][j]);
wbkgd(local_window[i][j], COLOR_PAIR(game->grid[i][j] % 256));
mvwprintw(local_window[i][j], 2, 2, num);
wrefresh(local_window[i][j]);
}
}
refresh();
}
/*
* Shift the grid tiles in the direction specified. Determine the "vector" for
* the shift, then build traversal arrays based on those vectors. Iterate
* through the grid according to the travesal arrays and shift accordingly.
* After moving, update the score and check for lose conditions.
*/
void moveGrid(struct game_state *game, int dir) {
mvaddch(0, 0, dir + '0');
bool moved = false;
int vector[2] = {0, 0};
getDirectionVector(vector, dir);
int *traversals[2];
buildTraversals(traversals, vector);
for (int i = 0; i < g_size; i++) {
for (int j = 0; j < g_size; j++) {
int x = traversals[0][i];
int y = traversals[1][j];
int tile = game->grid[x][y];
if (tile != 0) {
int position[4];
findFarthestPosition(game, position, x, y, vector);
int f_x = position[0];
int f_y = position[1];
int next_x = position[2];
int next_y = position[3];
// ONE?
if (inRange(next_x) && inRange(next_y) &&
game->grid[next_x][next_y] == game->grid[x][y]) {
game->grid[next_x][next_y] *= 2;
game->grid[x][y] = 0;
moved = true;
game->score_last_move = game->grid[next_x][next_y];
game->tiles_in_play--;
} else if (inRange(f_x) && inRange(f_y)) {
game->grid[f_x][f_y] = game->grid[x][y];
if (f_x != x || f_y != y) game->grid[x][y] = 0;
moved = true;
}
}
}
}
if (moved) {
addRandomTile(game);
game->total_score += game->score_last_move;
mvprintw(0, 10, "Score %d = (+ %d)", game->total_score,
game->score_last_move);
if ((g_size * g_size - game->tiles_in_play) <= 0 &&
!tileMatchesAvailible(game)) {
game->game_over = true;
}
}
}
/*
* Determine the direction vector for a given direction. A direction vector is
* just an x and a y value that correspond to the proveded direction.
* 0: ( 0, 1) down 1: ( 0, -1) up
* 2: (-1, 0) left 3: ( 1, 0) right
*/
void getDirectionVector(int vector[2], int dir) {
switch (dir) {
case 0: // down
vector[0] = 1;
break;
case 1: // up
vector[0] = -1;
break;
case 2: // left
vector[1] = -1;
break;
case 3: // right
vector[1] = 1;
break;
}
}
void buildTraversals(int *traversals[2], int *vector) {
traversals[0] = (int *)malloc(g_size * sizeof(int));
traversals[1] = (int *)malloc(g_size * sizeof(int));
for (int i = 0; i < g_size; i++) {
if (vector[0] == 1)
traversals[0][i] = g_size - 1 - i;
else
traversals[0][i] = i;
if (vector[1] == 1)
traversals[1][i] = g_size - 1 - i;
else
traversals[1][i] = i;
}
}
void findFarthestPosition(struct game_state *game, int position[4], int x,
int y, int vector[2]) {
int prev_x, prev_y;
do {
prev_x = x;
prev_y = y;
x = prev_x + vector[0];
y = prev_y + vector[1];
} while (inRange(x) && inRange(y) && game->grid[x][y] == 0);
position[0] = prev_x;
position[1] = prev_y;
position[2] = x;
position[3] = y;
}
bool inRange(int i) { return i >= 0 && i < g_size; }
bool tileMatchesAvailible(struct game_state *game) {
for (int i = 0; i < g_size; i++) {
for (int j = 0; j < g_size; j++) {
int tile = game->grid[i][j];
if (tile) {
for (int dir = 0; dir < 4; dir++) {
int vector[2];
getDirectionVector(vector, dir);
if (inRange(i + vector[0]) && inRange(i + vector[1])) {
int new_tile = game->grid[i + vector[0]][j + vector[1]];
if (new_tile == tile) {
return true;
}
}
}
}
}
}
return false;
}
void initCurses() {
initscr();
start_color();
cbreak();
noecho();
keypad(stdscr, TRUE);
init_pair(2, 179, 000);
init_pair(4, 178, 000);
init_pair(8, 172, 000);
init_pair(16, 208, 000);
init_pair(32, 160, 000);
init_pair(64, 196, 000);
init_pair(128, 220, 000);
init_pair(256, 184, 000);
}
bool parseArgs(int argc, char *argv[]) {
int c;
while ((c = getopt(argc, argv, "s:")) != -1) {
switch (c) {
case 'h':
/* TODO: Print help info */
break;
case 'c':
/* TODO: Color Options as a parameter. */
break;
case 's':
if(!atoi(optarg)) {
fprintf(stderr, "%s: Invalid argument parameter -- %s\n", argv[0], optarg);
return false;
}
g_size = atoi(optarg);
break;
case '?':
if (optopt == 's')
fprintf(stderr, "Option -%c requires an argument.\n",
optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n",
optopt);
return false;
default:
abort();
}
}
return true;
}