pvjc24/cv9/snake.c
2024-04-08 15:18:12 +02:00

65 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#define FOOD_COUNT 5
struct snake {
int x;
int y;
struct snake* next;
};
struct state {
struct snake* snake;
int foodx[FOOD_COUNT];
int foody[FOOD_COUNT];
int sx;
int sy;
int width;
int height;
};
struct snake* add_snake(struct snake* snake, int x, int y) {
struct snake* head = (struct snake*)malloc(sizeof(struct snake));
if (head == NULL) {
return NULL;
}
head->x = x;
head->y = y;
head->next = snake;
return head;
}
struct snake* remove_snake(struct snake* snake) {
if (snake == NULL) {
return NULL;
}
struct snake* next = snake->next;
free(snake);
return next;
}
int is_snake(struct snake* snake, int x, int y) {
while (snake != NULL) {
if (snake->x == x && snake->y == y) {
return 1;
}
snake = snake->next;
}
return 0;
}
void free_snake(struct snake* snake) {
while (snake != NULL) {
struct snake* next = snake->next;
free(snake);
snake = next;
}
}
int main() {
// Place your testing code here
return 0;
}