-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake.c
517 lines (458 loc) · 20.6 KB
/
snake.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
/* ====================================================================
*
* Exercice snake.c du cours
* Programmation Orientee Systeme de M. Chappelier (Sections IN et SC).
*
* Si vous avez ncurses (libcurses-dev), compilez avec
* -DUSE_CURSES et -lncurses ; par exemple :
* gcc -ansi -pedantic -Wall -DUSE_CURSES snake.c -o snake -lncurses
*
* ====================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#ifdef USE_CURSES
#include <curses.h>
#define printf printw
#else
#define printw printf
#endif
/***************************************************************
* Here come the data definitions
***************************************************************/
typedef struct {
int dx;
int dy;
} direction_t;
typedef enum {
EMPTY, WALL, FOOD, SNAKE
} map_cell_t;
typedef struct snake_segment_t_ {
unsigned int x;
unsigned int y;
int size;
direction_t direction;
struct snake_segment_t_ *prev;
} snake_segment_t;
typedef struct {
snake_segment_t* head;
snake_segment_t* tail;
} snake_t;
typedef struct {
snake_t snake;
unsigned int width;
unsigned int height;
map_cell_t* map;
} game_t;
/****************************************************************
* This is the given game map.
****************************************************************/
#define MAP_WIDTH 80
#define MAP_HEIGHT 25
static map_cell_t const header_data[] = {
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,EMPTY,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L,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,
WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL,WALL
};
/****************************************************************
* Here come the function definitions
****************************************************************/
void snake_info(snake_t const* snake) {
snake_segment_t const* seg;
for (seg = snake->tail; seg; seg = seg->prev) {
printf("(%02d,%02d) %d /%-d:%-d\n", seg->x, seg->y, seg->size,
seg->direction.dx, seg->direction.dy);
}
}
void snake_erase_tail(snake_t* snake)
{
if (snake->tail != NULL) {
snake_segment_t* const newtail = snake->tail->prev;
free(snake->tail);
snake->tail = newtail;
}
}
void snake_destroy(snake_t* snake)
{
while (snake->tail != NULL) snake_erase_tail(snake);
}
int snake_add_segment(snake_t* snake, direction_t dir)
{
snake_segment_t* const seg = malloc(sizeof(snake_segment_t));
if (!seg) {
return -2;
}
seg->direction = dir;
seg->prev = NULL;
if (!snake->head) {
snake->tail = seg;
seg->size = 1;
} else {
seg->size = 0;
seg->x = snake->head->x + dir.dx;
seg->y = snake->head->y + dir.dy;
snake->head->prev = seg;
}
snake->head = seg;
return 0;
}
int snake_move(snake_t* snake, direction_t direction)
{
if (snake->head->direction.dx == direction.dx &&
snake->head->direction.dy == direction.dy) {
snake->head->x += direction.dx;
snake->head->y += direction.dy;
} else {
if (snake_add_segment(snake, direction) != 0) {
return -1;
}
}
if (snake->head == snake->tail) {
return 0;
}
++snake->head->size;
--snake->tail->size;
if (snake->tail->size == 0) {
snake_erase_tail(snake);
}
return 0;
}
map_cell_t* cell(game_t* game, unsigned int x, unsigned int y) {
return &(game->map[y* game->width + x]);
}
int game_update(game_t* game, direction_t direction)
{
snake_t* const snake = &game->snake;
unsigned int const tail_x = snake->tail->x - (snake->tail->size-1) * snake->tail->direction.dx;
unsigned int const tail_y = snake->tail->y - (snake->tail->size-1) * snake->tail->direction.dy;
if (snake_move(snake, direction) != 0) {
return -1;
}
switch (*cell(game, snake->head->x, snake->head->y)) {
case WALL:
case SNAKE:
return -1;
case FOOD:
++snake->tail->size;
break;
default:
*cell(game, tail_x, tail_y) = EMPTY;
break;
}
*cell(game, snake->head->x, snake->head->y) = SNAKE;
return 0;
}
int game_init_snake(game_t* game, unsigned int orig_x, unsigned int orig_y)
{
direction_t dir = {0,0};
game->snake.head = NULL;
game->snake.tail = NULL;
if (snake_add_segment(&game->snake, dir) != 0) {
return -1;
}
game->snake.head->x = orig_x;
game->snake.head->y = orig_y;
*cell(game, game->snake.head->x, game->snake.head->y) = SNAKE;
return 0;
}
/****************************************************************/
int game_init_map(game_t* game, const map_cell_t* map, unsigned int width, unsigned int height)
{
unsigned int x, y;
game->width = width;
game->height = height;
game->map = calloc(game->width * game->height, sizeof(map_cell_t));
if (game->map == NULL) {
game->width = game->height = 0;
return -1;
}
for (y = 0; y < height; ++y) {
for (x = 0; x < width; ++x) {
*cell(game, x, y) = map[y*width + x];
}
}
return 0;
}
/****************************************************************/
game_t* game_init(unsigned int orig_x, unsigned int orig_y)
{
game_t* game = malloc(sizeof(game_t));
if (game != NULL) {
if (game_init_map(game, header_data, MAP_WIDTH, MAP_HEIGHT) != 0) {
free(game); game = NULL;
} else if (game_init_snake(game, orig_x, orig_y) != 0) {
free(game->map);
free(game); game = NULL;
}
}
return game;
}
/****************************************************************/
void game_destroy(game_t* game)
{
snake_destroy(&game->snake);
free(game->map);
free(game); game = NULL;
}
/***************************************************************
* The following handles I/O and is not part of the game engine
****************************************************************/
void game_print(game_t* game)
{
unsigned int x, y;
#ifdef USE_CURSES
const int color = has_colors();
clear();
if (color) {
start_color();
init_pair(WALL , COLOR_BLACK, COLOR_YELLOW);
init_pair(SNAKE, COLOR_BLACK, COLOR_GREEN );
init_pair(FOOD , COLOR_BLACK, COLOR_RED );
}
#endif
printw("\n");
for (y = 0; y < game->height; ++y) {
for (x = 0; x < game->width; ++x) {
switch(*cell(game, x, y)) {
case EMPTY:
printw(" ");
break;
case WALL:
#ifdef USE_CURSES
if (color) {
attron(COLOR_PAIR(WALL));
printw(" ");
attroff(COLOR_PAIR(WALL));
} else
#endif
printw("O");
break;
case FOOD:
#ifdef USE_CURSES
if (color) {
attron(COLOR_PAIR(FOOD));
printw(" ");
attroff(COLOR_PAIR(FOOD));
} else
#endif
printw("F");
break;
case SNAKE:
#ifdef USE_CURSES
if (color) {
attron(COLOR_PAIR(SNAKE));
printw(" ");
attroff(COLOR_PAIR(SNAKE));
} else
#endif
printw("S");
break;
default:
printw("?");
break;
}
}
printw("\n");
}
/* For debugging */
snake_info(&game->snake);
#ifdef USE_CURSES
refresh();
#else
getchar();
#endif
}
/* Transforms a keypress to dx and dy coordinates */
void handle_key_press(int key, direction_t* dir)
{
#ifndef USE_CURSES
#define KEY_DOWN 's'
#define KEY_UP 'w'
#define KEY_LEFT 'a'
#define KEY_RIGHT 'd'
#endif
switch (key) {
case KEY_DOWN:
dir->dx = 0; dir->dy = 1;
break;
case KEY_UP:
dir->dx = 0; dir->dy = -1;
break;
case KEY_RIGHT:
dir->dx = 1; dir->dy = 0;
break;
case KEY_LEFT:
dir->dx = -1; dir->dy = 0;
break;
default:
dir->dx = 0; dir->dy = 0;
break;
}
}
void game_loop(game_t* game)
{
direction_t dir;
#ifdef USE_CURSES
/* The user must move the snake manually, it does not move by itself */
#define getkey getch()
#else
/* Change this array to simulates moves.
* An x is a step where no keys are pressed */
char const keys[] = "xsxdxxxxxxxxxxxxxxxxwxxdxxwxxdxsxsxsx";
const char* key = keys;
#define getkey (*key)
#endif
do {
game_print(game);
handle_key_press(getkey, &dir);
if (dir.dx == 0 && dir.dy == 0) {
dir = game->snake.head->direction;
}
} while ((game_update(game, dir) == 0)
#ifndef USE_CURSES
&& (*++key)
#endif
);
printw("Game over\n");
}
int main(void)
{
game_t* game;
#ifdef USE_CURSES
initscr();
raw();
noecho();
keypad(stdscr, TRUE);
#endif
game = game_init(3,3);
if (game) {
game_loop(game);
game_destroy(game);
}
#ifdef USE_CURSES
endwin();
#endif
return 0;
}