pvjc22/du7/snake.c

103 lines
2.7 KiB
C
Raw Normal View History

2022-04-26 17:40:00 +00:00
#include "snake.h"
#include <stdlib.h>
struct snake* add_snake(struct snake* snake,int x,int y){
2022-04-26 18:33:09 +00:00
struct snake* novyZaciatok = calloc(1, sizeof(struct snake)); //alokujem pamäť smerníka
novyZaciatok->x = x; //nastavím súradnice hada
2022-04-26 17:40:00 +00:00
novyZaciatok->y = y;
2022-04-26 17:44:26 +00:00
2022-04-26 18:33:09 +00:00
if (snake != NULL) { //ak má had telo,
novyZaciatok->next = snake; //tak mu iba pridám hlavu
2022-04-26 17:46:44 +00:00
} else {
2022-04-26 18:33:09 +00:00
novyZaciatok->next = NULL; //inak musím vytvoriť nové telo
2022-04-26 17:44:26 +00:00
}
2022-04-26 17:40:00 +00:00
return novyZaciatok;
}
struct snake* remove_snake(struct snake* snake){
2022-04-27 16:52:02 +00:00
if (snake == NULL) return NULL; //ak nezadám ako vstup hada, tak okamžite vrátim NULL
2022-04-26 18:28:43 +00:00
2022-04-27 16:52:02 +00:00
else if (snake->next == NULL) { //ak had nemá pokračovanie,
free(snake); //tak ho zruším
return NULL; //a vrátim NULL
2022-04-26 19:14:38 +00:00
}
2022-04-26 18:15:29 +00:00
2022-04-27 16:52:02 +00:00
else { //inak prebehnem celého hada od začiatku
2022-04-27 17:22:58 +00:00
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medzipamäť
2022-04-27 16:52:02 +00:00
medzipamat = snake; //adresu hlavy hada uložím do medzipamäte
2022-04-26 19:14:38 +00:00
2022-04-27 16:52:02 +00:00
while (snake->next->next != NULL) snake = snake->next; //hada budem prezerať dovtedy, kým nenájdem jeho koniec
2022-04-26 19:41:30 +00:00
2022-04-27 16:52:02 +00:00
free(snake->next); //koniec hada zmažem
2022-04-26 19:41:30 +00:00
2022-04-27 16:52:02 +00:00
snake->next = NULL; // nastavím nový koniec hada
return medzipamat; //vrátim hlavu hada
2022-04-26 19:14:38 +00:00
}
2022-04-26 17:40:00 +00:00
}
2022-04-27 16:52:02 +00:00
void free_snake(struct snake* sn){ //funkcia odstráni hada z hry
struct snake* medzipamat = calloc(1, sizeof(struct snake)); //alokujem medzipamäť
2022-04-26 17:40:00 +00:00
2022-04-27 16:52:02 +00:00
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
2022-04-26 17:40:00 +00:00
}
2022-04-26 18:28:43 +00:00
2022-04-27 16:52:02 +00:00
free(medzipamat); //nakoniec vyčistím aj medzipamäť
2022-04-26 17:40:00 +00:00
}
int is_snake(struct snake* snake,int x,int y){
2022-04-27 16:55:57 +00:00
if (snake == NULL) return 0;
2022-04-27 17:22:58 +00:00
while (snake != NULL) {
2022-04-27 17:01:10 +00:00
if (snake->x == x || snake->y == y) return 1;
snake = snake->next;
2022-04-27 17:22:58 +00:00
}
2022-04-27 16:52:02 +00:00
return 0;
2022-04-26 17:40:00 +00:00
}
int step_state(struct state* st){
int nx = (st->snake->x + st->sx);
int ny = (st->snake->y + st->sy);
2022-04-27 18:33:02 +00:00
int i, pocetJedal = FOOD_COUNT;
struct snake* had = calloc(1, sizeof(struct snake));
struct snake* hlava = calloc(1, sizeof(struct snake));
had = st->snake;
hlava = st->snake;
while (had != NULL) {
if (snake->x == nx || snake->y == ny) return END_SNAKE;
for (i = 0; i < FOOD_COUNT; i++) {
if (foodx[i] == nx) {
if (foody[i] == ny) {
had = add_snake(had, foodx[i], foody[i]);
foodx[i] = -1;
foody[i] = -1;
pocetJedal--;
}
}
}
if (pocetJedal == 0) return END_FOOD;
}
had = hlava;
free(hlava);
if (nx > 0 && nx < width) {
if (ny > 0 && ny < height) {
had = add_snake(had, nx, ny);
had = remove_snake(had);
}
}
2022-04-26 17:40:00 +00:00
return END_CONTINUE;
}