pvjc24/cv9/snake.c

65 lines
1.1 KiB
C
Raw Normal View History

2024-04-08 13:16:50 +00:00
#include <stdio.h>
2024-04-08 13:14:43 +00:00
#include <stdlib.h>
2024-04-08 13:16:50 +00:00
#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;
};
2024-04-08 13:14:43 +00:00
struct snake* add_snake(struct snake* snake, int x, int y) {
2024-04-08 13:16:50 +00:00
struct snake* head = (struct snake*)malloc(sizeof(struct snake));
if (head == NULL) {
return NULL;
2024-04-08 13:14:43 +00:00
}
2024-04-08 13:16:50 +00:00
head->x = x;
head->y = y;
head->next = snake;
return head;
2024-04-08 13:14:43 +00:00
}
struct snake* remove_snake(struct snake* snake) {
if (snake == NULL) {
2024-04-08 13:16:50 +00:00
return NULL;
2024-04-08 13:14:43 +00:00
}
2024-04-08 13:16:50 +00:00
struct snake* next = snake->next;
2024-04-08 13:14:43 +00:00
free(snake);
2024-04-08 13:16:50 +00:00
return next;
2024-04-08 13:14:43 +00:00
}
int is_snake(struct snake* snake, int x, int y) {
while (snake != NULL) {
if (snake->x == x && snake->y == y) {
2024-04-08 13:16:50 +00:00
return 1;
2024-04-08 13:14:43 +00:00
}
snake = snake->next;
}
return 0;
}
2024-04-08 13:16:50 +00:00
void free_snake(struct snake* snake) {
while (snake != NULL) {
struct snake* next = snake->next;
free(snake);
snake = next;
2024-04-08 13:14:43 +00:00
}
}
2024-04-08 13:16:50 +00:00
int main() {
// Place your testing code here
return 0;
}