This commit is contained in:
Tančáková 2024-04-08 15:23:28 +02:00
parent 9ec5ae06fe
commit cc37d791de

View File

@ -1,46 +1,21 @@
#include "snake.h"
#include <stdlib.h>
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;
struct snake* current = snake;
struct snake* prev = NULL;
while (current->next != NULL) {
prev = current;
current = current->next;
}
return 0;
}
void free_snake(struct snake* snake) {
while (snake != NULL) {
struct snake* next = snake->next;
free(snake);
snake = next;
if (prev != NULL) {
prev->next = NULL;
} else {
// If prev is NULL, it means we are removing the head of the list
// and the new head will be the next node
snake = NULL;
}
}
int step_state(struct state* state) {
// Implement step_state function here
return END_CONTINUE;
free(current);
return snake;
}