-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnake.c
592 lines (455 loc) · 14.7 KB
/
cnake.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/select.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define GAMEGRID_ROWS 15
#define GAMEGRID_COLUMNS 30
#define ERROR_EXIT(err_string) \
do { \
system("clear"); \
printf(err_string "\n"); \
exit(1); \
} while(0)
#define SnakeGame_MACRO_PrintAndTerminate(snakegame, ...) \
do { \
system("clear"); \
printf(__VA_ARGS__); \
SnakeGame_Destroy(snakegame); \
exit(1); \
} while (0)
#define ERROR_EXIT_IF_NULL(ptr, err_string) if (ptr == NULL) ERROR_EXIT(err_string)
#define POS_TO_INDEX(columns, column, row) (columns * row + column)
#define SLEEP_MS(ms) usleep(1000 * ms)
#define RANDINT(min, max) ((rand() % (max - min + 1)) + min);
#define GAME_TICK_INTERVAL 100
enum SnakeConstants {
SNAKE_OBJTYPE = 1,
SNAKE_SPRITE = 'S',
SNAKE_DIR_UP,
SNAKE_DIR_DOWN,
SNAKE_DIR_LEFT,
SNAKE_DIR_RIGHT
};
enum FoodConstants {
FOOD_OBJTYPE = 2,
FOOD_SPRITE = 'F'
};
enum MiscConstants {
NONE_OBJTYPE = 0,
NONE_SPRITE = '.'
};
struct GameObject {
char x;
char y;
char type;
char sprite;
};
struct Snake {
struct GameObject** parts;
struct GameObject* head;
struct GameObject* tail;
int length;
char direction;
};
struct Screen {
int rows;
int columns;
char* draw_buffer;
char* print_buffer;
};
struct SnakeGame {
struct Snake* snake;
struct GameObject* food;
struct Screen* screen;
struct InputHandler* inputhandler;
char* grid;
};
struct InputHandler {
struct termios oldt;
struct termios newt;
char input;
};
void
InputHandler_Destroy(struct InputHandler* inputhandler)
{
tcsetattr(STDIN_FILENO, TCSANOW, &inputhandler->oldt);
free(inputhandler);
}
struct InputHandler*
InputHandler_Create(void)
{
struct InputHandler* inputhandler = malloc(sizeof *inputhandler);
tcgetattr(STDIN_FILENO, &inputhandler->oldt);
inputhandler->input = '\0';
inputhandler->newt = inputhandler->oldt;
inputhandler->newt.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &inputhandler->newt);
return inputhandler;
}
int
InputHandler_KeyPressed(void)
{
fd_set fds;
struct timeval tv = { 0L, 0L };
FD_ZERO(&fds);
FD_SET(0, &fds);
return select(1, &fds, NULL, NULL, &tv) > 0;
}
unsigned char
InputHandler_GetChar(void)
{
int r;
unsigned char ch;
if ((r = read(0, &ch, sizeof(ch))) < 0) {
ERROR_EXIT("InputHandler_GetChar(): Invalid Character");
} else {
return ch;
}
}
void
Screen_Destroy(struct Screen* screen)
{
free(screen->draw_buffer);
free(screen->print_buffer);
free(screen);
}
struct Screen*
Screen_Create(int rows,
int columns)
{
struct Screen* screen = malloc(sizeof *screen);
ERROR_EXIT_IF_NULL(screen, "Screen_Create(): screen is NULL");
screen->draw_buffer = calloc((rows * columns), sizeof(char));
ERROR_EXIT_IF_NULL(screen->draw_buffer, "Screen_Create(): screen->draw_buffer is NULL");
screen->print_buffer = calloc((rows * columns) + rows, sizeof(char));
ERROR_EXIT_IF_NULL(screen->print_buffer, "Screen_Create(): screen->print_buffer is NULL");
screen->rows = rows;
screen->columns = columns;
return screen;
}
void
Screen_Render(struct Screen* screen)
{
int print_buffer_index = 0;
int j = 0;
/* Return the cursor to the top left corner */
printf("\033[H");
for (int i = 0; i < screen->columns * screen->rows; ++i) {
++j;
if (screen->draw_buffer[i] == '\0')
screen->print_buffer[print_buffer_index] = NONE_SPRITE;
else
screen->print_buffer[print_buffer_index] = screen->draw_buffer[i];
if (j == screen->columns) {
screen->print_buffer[++print_buffer_index] = '\n';
j = 0;
}
++print_buffer_index;
}
printf("%s", screen->print_buffer);
}
void
Screen_PushToPos(struct Screen* screen,
int column,
int row,
char ch)
{
screen->draw_buffer[POS_TO_INDEX(screen->columns, column, row)] = ch;
}
void
Screen_ClearPos(struct Screen* screen,
int column,
int row)
{
screen->draw_buffer[POS_TO_INDEX(screen->columns, column, row)] = '\0';
}
void
GameObject_Destroy(struct GameObject* gameobject)
{
free(gameobject);
}
struct GameObject*
GameObject_Create(int x,
int y,
int type,
char sprite)
{
struct GameObject* gameobject = malloc(sizeof *gameobject);
ERROR_EXIT_IF_NULL(gameobject, "GameObject_Create(): gameobject is NULL");
gameobject->x = x;
gameobject->y = y;
gameobject->type = type;
gameobject->sprite = sprite;
return gameobject;
}
void
GameObject_SetPos(struct GameObject* gameobject,
int x,
int y)
{
gameobject->x = x;
gameobject->y = y;
}
void
GameObject_AddPos(struct GameObject* gameobject,
int offset_x,
int offset_y)
{
GameObject_SetPos(gameobject, gameobject->x + offset_x, gameobject->y + offset_y);
}
int
GameObject_InBounds(struct GameObject* gameobject,
int max_x,
int max_y)
{
return (gameobject->x < max_x && gameobject->y < max_y
&& gameobject->x > -1 && gameobject->y > -1);
}
void
Snake_Destroy(struct Snake* snake)
{
for (int i = 0; i < snake->length; ++i)
GameObject_Destroy(snake->parts[i]);
free(snake->parts);
free(snake);
}
struct Snake*
Snake_Create(int rows,
int columns)
{
struct Snake* snake = malloc(sizeof *snake);
ERROR_EXIT_IF_NULL(snake, "Snake_Create(): snake is NULL");
snake->parts = malloc(sizeof(*snake->parts) * (rows * columns));
ERROR_EXIT_IF_NULL(snake->parts, "Snake_Create(): snake->parts is NULL");
return snake;
}
/* Initializes the snake */
void
Snake_Init(struct Snake* snake,
int start_x,
int start_y)
{
snake->parts[0] = GameObject_Create(start_x, start_y, SNAKE_OBJTYPE, SNAKE_SPRITE);
snake->head = snake->parts[0];
snake->tail = snake->head;
snake->length = 1;
snake->direction = SNAKE_DIR_DOWN;
}
void
Snake_SetDirection(struct Snake* snake,
char direction)
{
snake->direction = direction;
}
void
Snake_Move(struct Snake* snake,
char direction)
{
for (int i = snake->length - 1; i > 0; --i) {
snake->parts[i]->x = snake->parts[i - 1]->x;
snake->parts[i]->y = snake->parts[i - 1]->y;
}
switch (direction)
{
case SNAKE_DIR_UP:
GameObject_AddPos(snake->head, 0, -1);
break;
case SNAKE_DIR_DOWN:
GameObject_AddPos(snake->head, 0, 1);
break;
case SNAKE_DIR_LEFT:
GameObject_AddPos(snake->head, -1, 0);
break;
case SNAKE_DIR_RIGHT:
GameObject_AddPos(snake->head, 1, 0);
break;
default:
ERROR_EXIT("Snake_Move(): Invalid Direction");
}
}
void
Snake_Grow(struct Snake* snake)
{
snake->parts[snake->length] = GameObject_Create(snake->tail->x, snake->tail->y,
SNAKE_OBJTYPE, SNAKE_SPRITE);
snake->tail = snake->parts[snake->length++];
}
/* De-allocates everything allocated by the game */
void
SnakeGame_Destroy(struct SnakeGame* snakegame)
{
Snake_Destroy(snakegame->snake);
Screen_Destroy(snakegame->screen);
InputHandler_Destroy(snakegame->inputhandler);
GameObject_Destroy(snakegame->food);
free(snakegame->grid);
free(snakegame);
}
void
SnakeGame_DrawGameObject(struct Screen* screen,
struct GameObject* gameobject)
{
Screen_PushToPos(screen, gameobject->x, gameobject->y, gameobject->sprite);
}
/* Creates (allocates memory for) the necessary components of the game */
struct SnakeGame*
SnakeGame_Create(int grid_rows,
int grid_columns)
{
struct SnakeGame* snakegame = malloc(sizeof *snakegame);
ERROR_EXIT_IF_NULL(snakegame, "SnakeGame_Create(): snakegame is NULL");
snakegame->grid = calloc(grid_rows * grid_columns, sizeof(char));
ERROR_EXIT_IF_NULL(snakegame->grid, "SnakeGame_Create(): snakegame->grid is NULL");
/* These all have NULL checks in their respective x_Create functions */
snakegame->snake = Snake_Create(grid_rows, grid_columns);
snakegame->screen = Screen_Create(grid_rows, grid_columns);
snakegame->inputhandler = InputHandler_Create();
snakegame->food = GameObject_Create(0, 0, FOOD_OBJTYPE, FOOD_SPRITE);
return snakegame;
}
/* Returns the GameObject type of whatever is under the snake's head, and NONE_OBJTYPE
if nothing. Must be called *before* writing the snake objtype to the tile */
char
SnakeGame_GetCollision(struct SnakeGame* snakegame)
{
return snakegame->grid[POS_TO_INDEX(snakegame->screen->columns,
snakegame->snake->head->x,
snakegame->snake->head->y)];
}
int
SnakeGame_GetInBounds(struct SnakeGame* snakegame)
{
return GameObject_InBounds(snakegame->snake->head,
snakegame->screen->columns,
snakegame->screen->rows);
}
/* Reads input and sets the direction of the snake accordingly, while
disallowing 180 degree turns (e.g. no going directly from down to up). */
void
SnakeGame_ProcessInput(struct SnakeGame* snakegame)
{
if (InputHandler_KeyPressed()) {
switch (InputHandler_GetChar())
{
case 'w':
if (snakegame->snake->direction != SNAKE_DIR_DOWN)
Snake_SetDirection(snakegame->snake, SNAKE_DIR_UP);
break;
case 'a':
if (snakegame->snake->direction != SNAKE_DIR_RIGHT)
Snake_SetDirection(snakegame->snake, SNAKE_DIR_LEFT);
break;
case 's':
if (snakegame->snake->direction != SNAKE_DIR_UP)
Snake_SetDirection(snakegame->snake, SNAKE_DIR_DOWN);
break;
case 'd':
if (snakegame->snake->direction != SNAKE_DIR_LEFT)
Snake_SetDirection(snakegame->snake, SNAKE_DIR_RIGHT);
break;
}
}
}
void
SnakeGame_SpawnFood(struct SnakeGame* snakegame)
{
int index;
while (1) {
snakegame->food->x = RANDINT(0, snakegame->screen->columns - 1);
snakegame->food->y = RANDINT(0, snakegame->screen->rows - 1);
index = POS_TO_INDEX(snakegame->screen->columns, snakegame->food->x, snakegame->food->y);
if (snakegame->grid[index] == NONE_OBJTYPE) {
snakegame->grid[index] = FOOD_OBJTYPE;
return;
}
}
}
void
SnakeGame_ConsumeFood(struct SnakeGame* snakegame)
{
snakegame->grid[POS_TO_INDEX(snakegame->screen->columns,
snakegame->food->x,
snakegame->food->y)] = NONE_OBJTYPE;
Snake_Grow(snakegame->snake);
}
int
SnakeGame_SnakeIsMaxLength(struct SnakeGame* snakegame)
{
return snakegame->snake->length == (snakegame->screen->columns
* snakegame->screen->rows) - 1;
}
/*
* The game loop, most of the "high level" snake functionality is implemented here
* it's a bit messy, but I've kept it this way since I think it very clearly conveys
* the gameplay logic, and is also the 'core' function of the game, with the job of
* managing all the *other* components, more or less.
*/
void
SnakeGame_GameLoop(struct SnakeGame* snakegame)
{
while (1) {
SLEEP_MS(GAME_TICK_INTERVAL);
if (SnakeGame_SnakeIsMaxLength(snakegame)) {
SnakeGame_MACRO_PrintAndTerminate(snakegame, "Victory, very cool!\n");
}
if (!SnakeGame_GetInBounds(snakegame)) {
SnakeGame_MACRO_PrintAndTerminate(
snakegame, "Unfortunate, the snake was %d tiles long.\n", snakegame->snake->length);
}
switch (SnakeGame_GetCollision(snakegame))
{
case SNAKE_OBJTYPE:
SnakeGame_MACRO_PrintAndTerminate(
snakegame, "Unfortunate, the snake was %d tiles long.\n", snakegame->snake->length);
case FOOD_OBJTYPE:
SnakeGame_ConsumeFood(snakegame);
SnakeGame_SpawnFood(snakegame);
break;
case NONE_OBJTYPE:
break;
default:
ERROR_EXIT("SnakeGame_GameLoop(): Unknown collision type");
}
SnakeGame_ProcessInput(snakegame);
/* Add each part of the snake to the grid and throw it into the render buffer */
for (int i = 0; i < snakegame->snake->length; ++i) {
snakegame->grid[POS_TO_INDEX(snakegame->screen->columns,
snakegame->snake->parts[i]->x,
snakegame->snake->parts[i]->y)] = SNAKE_OBJTYPE;
SnakeGame_DrawGameObject(snakegame->screen, snakegame->snake->parts[i]);
}
SnakeGame_DrawGameObject(snakegame->screen, snakegame->food);
Screen_Render(snakegame->screen);
/* Remove the tail (last part of the snake) from the grid */
snakegame->grid[POS_TO_INDEX(snakegame->screen->columns,
snakegame->snake->tail->x,
snakegame->snake->tail->y)] = NONE_OBJTYPE;
/* ...and from the render buffer */
Screen_ClearPos(snakegame->screen, snakegame->snake->tail->x, snakegame->snake->tail->y);
Snake_Move(snakegame->snake, snakegame->snake->direction);
}
}
/* Initializes the game variables and starts the main gameplay loop. */
void
SnakeGame_BeginPlay(struct SnakeGame* snakegame)
{
srand(time(0));
Snake_Init(snakegame->snake,
snakegame->screen->columns / 2 - 1,
snakegame->screen->rows / 2);
Snake_Grow(snakegame->snake);
Snake_Grow(snakegame->snake);
SnakeGame_SpawnFood(snakegame);
SnakeGame_GameLoop(snakegame);
}
int
main(void)
{
system("clear");
struct SnakeGame* snakegame = SnakeGame_Create(GAMEGRID_ROWS, GAMEGRID_COLUMNS);
SnakeGame_BeginPlay(snakegame);
return 0;
}