Aktualizovat du5/snake.c

This commit is contained in:
Denys Sanchuk 2025-04-08 12:38:08 +00:00
parent 5953bc57eb
commit 203181507e

View File

@ -1,48 +1,45 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.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) {
return NULL;
}
struct snake* add_snake(struct snake* snake, int x, int y) {
struct snake* new_head = malloc(sizeof(struct snake));
if (!new_head) return NULL;
new_head->x = x;
new_head->y = y;
new_head->next = snake;
new_head->next = snake;
return new_head;
}
struct snake *remove_snake(struct snake *snake) {
if (!snake) {
return NULL;
}
if (!snake->next) {
free(snake);
struct snake* remove_snake(struct snake* snake) {
if (!snake || !snake->next) {
free(snake);
return NULL;
}
struct snake *current = snake;
while (current->next && current->next->next) {
struct snake* current = snake;
while (current->next->next) {
current = current->next;
}
free(current->next);
current->next = NULL;
free(current->next);
current->next = NULL;
return snake;
}
int is_snake(struct snake *snake, int x, int y) {
struct snake *current = snake;
while (current) {
if (current->x == x && current->y == y) {
return 1;
}
current = current->next;
int is_snake(struct snake* snake, int x, int y) {
while (snake) {
if (snake->x == x && snake->y == y)
return 1;
snake = snake->next;
}
return 0;
return 0;
}
void free_snake(struct snake* sn) {
while (sn) {
struct snake* next = sn->next;
free(sn);
sn = next;
}
}