push
This commit is contained in:
parent
48c91660f1
commit
b2c01620ed
71
du4/snake.c
71
du4/snake.c
@ -2,71 +2,60 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
struct snake* add_snake(struct snake* snake, int x, int y) {
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
||||||
struct snake* nova_hlava = malloc(sizeof(struct snake));
|
struct snake* nova_cast = calloc(1, sizeof(struct snake));
|
||||||
nova_hlava->x = x;
|
nova_cast->x = x;
|
||||||
nova_hlava->y = y;
|
nova_cast->y = y;
|
||||||
nova_hlava->next = snake;
|
nova_cast->next = snake;
|
||||||
return nova_hlava;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake) {
|
|
||||||
if (snake == NULL) return NULL;
|
|
||||||
if (snake->next == NULL) {
|
|
||||||
free(snake);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
struct snake* aktualny = snake;
|
|
||||||
while (aktualny->next->next != NULL) {
|
|
||||||
aktualny = aktualny->next;
|
|
||||||
}
|
|
||||||
free(aktualny->next);
|
|
||||||
aktualny->next = NULL;
|
|
||||||
return snake;
|
return snake;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_snake(struct snake* sn) {
|
struct snake* remove_snake(struct snake* hlava) {
|
||||||
while (sn != NULL) {
|
if (hlava == NULL) return NULL;
|
||||||
struct snake* dalsi = sn->next;
|
if (hlava->next == NULL) {
|
||||||
free(sn);
|
return NULL;
|
||||||
sn = dalsi;
|
|
||||||
}
|
}
|
||||||
|
hlava->next = remove_snake(hlava->next);
|
||||||
|
return hlava;
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_snake(struct snake* snake, int x, int y) {
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
struct snake* aktualny = snake;
|
for (struct snake* this = snake; this != NULL; this = this->next) {
|
||||||
while (aktualny != NULL) {
|
if (this->x = x && this->y == y) return 1;
|
||||||
if (aktualny->x == x && aktualny->y == y) return 1;
|
|
||||||
aktualny = aktualny->next;
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_snake(struct snake* sn) {
|
||||||
|
if (sn == NULL) return;
|
||||||
|
free_snake(sn->next);
|
||||||
|
}
|
||||||
|
|
||||||
int step_state(struct state* st) {
|
int step_state(struct state* st) {
|
||||||
int nx = (st->snake->x + st->sx);
|
int nova_x = st->snake->x + st->sx;
|
||||||
int ny = (st->snake->y + st->sy);
|
int nova_y = st->snake->y + st->sy;
|
||||||
|
|
||||||
if (nx < 0 || nx >= st->width ||
|
if (nova_x < 0 || nova_x >= st->width || nova_y < 0 || nova_y >= st->height)
|
||||||
ny < 0 || ny >= st->height) {
|
|
||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
|
||||||
|
|
||||||
if (is_snake(st->snake, nx, ny)) {
|
if (is_snake(st->snake, nova_x, nova_y))
|
||||||
return END_SNAKE;
|
return END_SNAKE;
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||||
if (st->foodx[i] == nx && st->foody[i] == ny) {
|
if (st->foodx[i] == nova_x && st->foody[i] == nova_y) {
|
||||||
st->snake = add_snake(st->snake, nx, ny);
|
|
||||||
st->foodx[i] = -1;
|
st->foodx[i] = -1;
|
||||||
st->foody[i] = -1;
|
st->foody[i] = -1;
|
||||||
|
st->snake = add_snake(st->snake, nova_x, nova_y);
|
||||||
|
|
||||||
|
int zostatok = 0;
|
||||||
for (int j = 0; j < FOOD_COUNT; j++) {
|
for (int j = 0; j < FOOD_COUNT; j++) {
|
||||||
if (st->foodx[j] >= 0) return END_CONTINUE;
|
if (st->foodx[j] >= 0) zostatok = 1;
|
||||||
}
|
}
|
||||||
return END_FOOD;
|
if (!zostatok) return END_FOOD;
|
||||||
|
return END_CONTINUE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
st->snake = add_snake(st->snake, nx, ny);
|
st->snake = add_snake(st->snake, nova_x, nova_y);
|
||||||
st->snake = remove_snake(st->snake);
|
remove_snake(st->snake);
|
||||||
return END_CONTINUE;
|
return END_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user