pvjc24/cv9/snake.c

65 lines
1.3 KiB
C
Raw Normal View History

2024-04-13 17:12:56 +00:00
#include "snake.h"
#include <stdlib.h>
struct snake* add_snake(struct snake* snake,int x,int y){
struct snake* head = calloc(1,sizeof(struct snake));
head->x = x;
head->y = y;
head->next = snake;
2024-04-13 17:18:36 +00:00
2024-04-13 17:12:56 +00:00
return head;
}
struct snake* remove_snake(struct snake* snake){
if(snake==NULL){
return NULL;
}
struct snake* curent_element = snake;
2024-04-13 17:26:17 +00:00
struct snake* previous_element;
2024-04-13 17:12:56 +00:00
while(curent_element->next != NULL){
2024-04-13 17:26:17 +00:00
previous_element=curent_element;
2024-04-13 17:12:56 +00:00
curent_element = curent_element->next;
}
2024-04-13 17:26:17 +00:00
2024-04-13 17:12:56 +00:00
free(curent_element);
2024-04-13 17:26:17 +00:00
2024-04-13 17:12:56 +00:00
if(curent_element==snake){
return NULL;
}
2024-04-13 17:18:36 +00:00
2024-04-13 17:26:17 +00:00
previous_element->next=NULL;
2024-04-13 17:12:56 +00:00
return snake;
}
void free_snake(struct snake* sn){
struct snake* current=sn;
if(current==NULL){
return;
}
struct snake* next_element;
2024-04-13 17:18:36 +00:00
while(current!=NULL){
2024-04-13 17:12:56 +00:00
next_element=current->next;
free(current);
current=next_element;
}
}
int is_snake(struct snake* snake,int x,int y){
struct snake* current =snake;
while(current!=NULL){
if(x==current->x&&y==current->y){
return 1;
}
current=current->next;
}
return 0;
}
int step_state(struct state* st){
int nx = (st->snake->x + st->sx);
int ny = (st->snake->y + st->sy);
return END_CONTINUE;
}
2024-04-13 17:18:36 +00:00