Compare commits
No commits in common. "b2c01620ed6142f3084e53a1b4cfc488dff1b2a4" and "d8cc342c220b10d08902225a85441555b23193bf" have entirely different histories.
b2c01620ed
...
d8cc342c22
61
du4/snake.c
61
du4/snake.c
@ -1,61 +0,0 @@
|
|||||||
#include "snake.h"
|
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
struct snake* add_snake(struct snake* snake, int x, int y) {
|
|
||||||
struct snake* nova_cast = calloc(1, sizeof(struct snake));
|
|
||||||
nova_cast->x = x;
|
|
||||||
nova_cast->y = y;
|
|
||||||
nova_cast->next = snake;
|
|
||||||
return snake;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* hlava) {
|
|
||||||
if (hlava == NULL) return NULL;
|
|
||||||
if (hlava->next == NULL) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
hlava->next = remove_snake(hlava->next);
|
|
||||||
return hlava;
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_snake(struct snake* snake, int x, int y) {
|
|
||||||
for (struct snake* this = snake; this != NULL; this = this->next) {
|
|
||||||
if (this->x = x && this->y == y) return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_snake(struct snake* sn) {
|
|
||||||
if (sn == NULL) return;
|
|
||||||
free_snake(sn->next);
|
|
||||||
}
|
|
||||||
|
|
||||||
int step_state(struct state* st) {
|
|
||||||
int nova_x = st->snake->x + st->sx;
|
|
||||||
int nova_y = st->snake->y + st->sy;
|
|
||||||
|
|
||||||
if (nova_x < 0 || nova_x >= st->width || nova_y < 0 || nova_y >= st->height)
|
|
||||||
return END_WALL;
|
|
||||||
|
|
||||||
if (is_snake(st->snake, nova_x, nova_y))
|
|
||||||
return END_SNAKE;
|
|
||||||
|
|
||||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
|
||||||
if (st->foodx[i] == nova_x && st->foody[i] == nova_y) {
|
|
||||||
st->foodx[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++) {
|
|
||||||
if (st->foodx[j] >= 0) zostatok = 1;
|
|
||||||
}
|
|
||||||
if (!zostatok) return END_FOOD;
|
|
||||||
return END_CONTINUE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
st->snake = add_snake(st->snake, nova_x, nova_y);
|
|
||||||
remove_snake(st->snake);
|
|
||||||
return END_CONTINUE;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue
Block a user