pvjc26/du4/snake.c
2026-04-14 15:21:47 +00:00

47 lines
1.2 KiB
C

#include "snake.h"
#include <stdlib.h>
struct snake* add_snake(struct snake* snake,int x,int y){
struct snake* nova_hlava = malloc(sizeof(struct snake)); //vytvorim novu hlavu na ktoru nalinkujem staru hlavu ako telo
nova_hlava -> x = x;
nova_hlava -> y = y; //zapisem suradnice novej hlavy do novej struktury
nova_hlava -> next = snake; //dalsi segment (next) je stara hlava zo vstupu funkcie
return nova_hlava;
}
struct snake* remove_snake(struct snake* snake){
struct snake* clanok = snake;
if (snake -> next == NULL) {
free(snake);
return NULL;
}
while (clanok -> next -> next != NULL) { // posuvam sa po hadovi kym nenajdem koniec chvosta ktory odmazem
clanok = clanok -> next;
}
free (clanok -> next); //po tom co som nasiel kde je koniec, odmazem koniec chvosta a korunujem nasledujuci clanok novym koncom
clanok -> next = NULL;
return snake; //kedze som menil snaka podla referenciam v pamati, mozem vratit noveho stareho hada
}
void free_snake(struct snake* sn){
}
int is_snake(struct snake* snake,int x,int y){
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;
}