58 lines
1.1 KiB
C
58 lines
1.1 KiB
C
#include <stdio.h>
|
|
#include "snake.h"
|
|
#include <stdlib.h>
|
|
|
|
struct snake* add_snake (struct snake* snake, int x, int y) {
|
|
struct snake* head = NULL;
|
|
|
|
head = malloc(sizeof(struct snake));
|
|
|
|
if (!head) {
|
|
return snake;
|
|
}
|
|
head->x = x;
|
|
head->y = y;
|
|
|
|
head->next = NULL;
|
|
|
|
if (snake !=NULL) {
|
|
head->next = snake;
|
|
}
|
|
return head;
|
|
}
|
|
|
|
struct snake* remove_snake(struct snake* snake) {
|
|
if(snake == NULL || snake-> next == NULL) {
|
|
free(snake);
|
|
return NULL;
|
|
}
|
|
|
|
struct snake* previous = NULL;
|
|
struct snake* current = NULL;
|
|
while (current->next !=NULL) {
|
|
previous = current;
|
|
current = current->next;
|
|
}
|
|
|
|
if (previous !=NULL) {
|
|
free(previous->next);
|
|
previous->next = NULL;
|
|
}
|
|
|
|
return snake;
|
|
}
|
|
|
|
int is_snake(struct snake* snake, int x, int y) {
|
|
struct snake* current = snake;
|
|
|
|
while () {
|
|
if ((current->x==x) && (current->y==y)) {
|
|
return 1;
|
|
}
|
|
current = current->next;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|