domaca_uloha_7
This commit is contained in:
parent
90019a464d
commit
199ef7191c
40
du7/snake.c
40
du7/snake.c
@ -16,39 +16,43 @@ struct snake* add_snake(struct snake* snake,int x,int y){
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake){
|
struct snake* remove_snake(struct snake* snake){
|
||||||
if (snake == NULL) return NULL;
|
if (snake == NULL) return NULL; //ak nezadám ako vstup hada, tak okamžite vrátim NULL
|
||||||
|
|
||||||
else if (snake->next == NULL) {
|
else if (snake->next == NULL) { //ak had nemá pokračovanie,
|
||||||
free(snake);
|
free(snake); //tak ho zruším
|
||||||
return NULL;
|
return NULL; //a vrátim NULL
|
||||||
}
|
}
|
||||||
|
|
||||||
else {
|
else { //inak prebehnem celého hada od začiatku
|
||||||
struct snake* medzipamat = calloc(1, sizeof(struct snake));
|
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medipamäť
|
||||||
medzipamat = snake;
|
medzipamat = snake; //adresu hlavy hada uložím do medzipamäte
|
||||||
|
|
||||||
while (snake->next->next != NULL) snake = snake->next;
|
while (snake->next->next != NULL) snake = snake->next; //hada budem prezerať dovtedy, kým nenájdem jeho koniec
|
||||||
|
|
||||||
free(snake->next);
|
free(snake->next); //koniec hada zmažem
|
||||||
|
|
||||||
snake->next = NULL;
|
snake->next = NULL; // nastavím nový koniec hada
|
||||||
return medzipamat;
|
return medzipamat; //vrátim hlavu hada
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_snake(struct snake* sn){
|
void free_snake(struct snake* sn){ //funkcia odstráni hada z hry
|
||||||
struct snake* nasledujuci = calloc(1, sizeof(struct snake));
|
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medzipamäť
|
||||||
|
|
||||||
while (sn != NULL) {
|
while (sn != NULL) { //v cykle vymažem každý jeden prvok hada
|
||||||
nasledujuci = sn->next;
|
medzipamat = sn->next; //hlavu hada uložím do medzipamäte
|
||||||
free(sn);
|
free(sn); //ak odstránim ju
|
||||||
sn = nasledujuci;
|
sn = medzipamat; //nastavím novú hlavu hada
|
||||||
}
|
}
|
||||||
|
|
||||||
free(nasledujuci);
|
free(medzipamat); //nakoniec vyčistím aj medzipamäť
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_snake(struct snake* snake,int x,int y){
|
int is_snake(struct snake* snake,int x,int y){
|
||||||
|
do {
|
||||||
|
if (snake->x == x || snake->y == y) return 1;
|
||||||
|
} while (snake->next != NULL);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user