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){
|
||||
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) {
|
||||
free(snake);
|
||||
return NULL;
|
||||
else if (snake->next == NULL) { //ak had nemá pokračovanie,
|
||||
free(snake); //tak ho zruším
|
||||
return NULL; //a vrátim NULL
|
||||
}
|
||||
|
||||
else {
|
||||
struct snake* medzipamat = calloc(1, sizeof(struct snake));
|
||||
medzipamat = snake;
|
||||
else { //inak prebehnem celého hada od začiatku
|
||||
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medipamäť
|
||||
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;
|
||||
return medzipamat;
|
||||
snake->next = NULL; // nastavím nový koniec hada
|
||||
return medzipamat; //vrátim hlavu hada
|
||||
}
|
||||
}
|
||||
|
||||
void free_snake(struct snake* sn){
|
||||
struct snake* nasledujuci = calloc(1, sizeof(struct snake));
|
||||
void free_snake(struct snake* sn){ //funkcia odstráni hada z hry
|
||||
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medzipamäť
|
||||
|
||||
while (sn != NULL) {
|
||||
nasledujuci = sn->next;
|
||||
free(sn);
|
||||
sn = nasledujuci;
|
||||
while (sn != NULL) { //v cykle vymažem každý jeden prvok hada
|
||||
medzipamat = sn->next; //hlavu hada uložím do medzipamäte
|
||||
free(sn); //ak odstránim ju
|
||||
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){
|
||||
do {
|
||||
if (snake->x == x || snake->y == y) return 1;
|
||||
} while (snake->next != NULL);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user