This commit is contained in:
Tančáková 2024-04-08 15:26:13 +02:00
parent cc37d791de
commit 8e2c855d18

View File

@ -1,3 +1,21 @@
#include "snake.h"
#include <stddef.h>
#include <stdlib.h>
struct snake* add_snake(struct snake* snake,int x,int y) {
// Allocate memory for the new snake part
struct snake* new_snake_part = calloc(1, sizeof(struct snake));
if (new_snake_part == NULL) {
return NULL; // Memory allocation failed
}
// Set coordinates of the new snake part
new_snake_part->x = x;
new_snake_part->y = y;
// The new snake part becomes the head of the snake
new_snake_part->next = snake;
return new_snake_part;
}
struct snake* remove_snake(struct snake* snake) { struct snake* remove_snake(struct snake* snake) {
if (snake == NULL) { if (snake == NULL) {
return NULL; return NULL;
@ -19,3 +37,30 @@ struct snake* remove_snake(struct snake* snake) {
return snake; return snake;
} }
int is_snake(struct snake* snake,int x, int y) {
// Traverse the snake linked list
struct snake* current = snake;
while (current != NULL) {
// Check if current snake part has the given coordinates
if (current->x == x && current->y == y) {
return 1; // Coordinates found in the snake
}
current = current->next;
}
return 0; // Coordinates not found in the snake
}
void free_snake(struct snake* sn) {
struct snake* current = sn;
while (current != NULL) {
struct snake* next = current->next;
free(current);
current = next;
}
}
int step_state(struct state* state) {
// Implementation of this function is left to you
return END_CONTINUE;
}