pvjc24/cv9/snake.c

49 lines
1.0 KiB
C
Raw Normal View History

2024-04-18 06:04:04 +00:00
#include <stdio.h>
#include <stdlib.h>
#include "snake.h"
struct snake* add_snake(struct snake* snake, int x, int y) {
struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
if (new_head == NULL) {
fprintf(stderr, "Memory allocation failed.\n");
exit(EXIT_FAILURE);
}
new_head->x = x;
new_head->y = y;
new_head->next = snake;
return new_head;
}
struct snake* remove_snake(struct snake* snake) {
if (snake == NULL) {
return NULL;
}
struct snake* new_head = snake->next;
free(snake);
return new_head;
}
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* sn) {
struct snake* current = sn;
while (current != NULL) {
struct snake* temp = current;
current = current->next;
free(temp);
}
}
int step_state(struct state* state) {
return END_CONTINUE;
}