-
Notifications
You must be signed in to change notification settings - Fork 3
/
snake.h
37 lines (27 loc) · 977 Bytes
/
snake.h
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
// SPDX-License-Identifier: GPL-3.0-only
// Copyright © 2024 Mario D'Andrea https://ormai.dev
#ifndef SNAKE_H
#define SNAKE_H
#include <stdbool.h>
#include <stddef.h>
enum direction { UP, RIGHT, DOWN, LEFT };
// Coordinates from the top left corner of the map
struct point {
int x, y;
};
struct snake {
size_t length; // Also the score
struct point old_tail; // Previous position of the tail
bool growing;
enum direction direction;
struct point head; // would be body[length - 1], for easy access
struct point *body;
};
struct snake *snake_create(const struct point head, const size_t size);
void snake_destroy(struct snake *self);
// Move the snake one cell forward in the current direction
void advance(struct snake *self);
void change_direction(struct snake *self, const enum direction direction);
// Check whether the snake's head overlaps with any other point of its body
bool self_collision(const struct snake *snake);
#endif // SNAKE_H