134 lines
3.0 KiB
C
134 lines
3.0 KiB
C
|
#include "snake.h"
|
||
|
#include <stdlib.h>
|
||
|
struct snake* add_snake(struct snake* snake,int x,int y){
|
||
|
struct snake* first_element = calloc(1,sizeof(struct snake));
|
||
|
first_element->x = x;
|
||
|
first_element->y = y;
|
||
|
|
||
|
first_element->next = snake;
|
||
|
return first_element;
|
||
|
}
|
||
|
int is_snake(struct snake* snake,int x,int y){
|
||
|
if(snake == NULL)
|
||
|
return 0;
|
||
|
struct snake* step_snake = snake;
|
||
|
|
||
|
while (step_snake->next != NULL)
|
||
|
{
|
||
|
if (step_snake->x == x && step_snake->y == y)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
step_snake = step_snake->next;
|
||
|
}
|
||
|
|
||
|
if (step_snake->x == x && step_snake->y == y)
|
||
|
{
|
||
|
return 1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int step_state(struct state* st){
|
||
|
struct snake* main_element = st->snake;
|
||
|
//hlavu
|
||
|
int root_x = st->snake->x;
|
||
|
int nx = (st->snake->x + st->sx);
|
||
|
int root_y = st->snake->y;
|
||
|
int ny = (st->snake->y + st->sy);
|
||
|
// pohybovať hlavou
|
||
|
st->snake->y = ny;
|
||
|
st->snake->x = nx;
|
||
|
|
||
|
st->snake = st->snake->next;
|
||
|
while (st->snake->next != NULL)
|
||
|
{
|
||
|
//zmeniť na hodnotu ďalšieho prvku
|
||
|
int step_prev_x = st->snake->x;
|
||
|
int step_prev_y = st->snake->y;
|
||
|
|
||
|
st->snake->x = root_x;
|
||
|
st->snake->y = root_y;
|
||
|
|
||
|
root_x = step_prev_x;
|
||
|
root_y = step_prev_y;
|
||
|
|
||
|
st->snake = st->snake->next;
|
||
|
}
|
||
|
//zmeniť hodnotu posledného prvku
|
||
|
st->snake->x = root_x;
|
||
|
st->snake->y = root_y;
|
||
|
|
||
|
st->snake = main_element;
|
||
|
|
||
|
root_x = main_element->x;
|
||
|
root_y = main_element->y;
|
||
|
if (main_element->next != NULL){
|
||
|
st->snake = main_element->next;
|
||
|
}
|
||
|
while (st->snake->next != NULL)
|
||
|
{
|
||
|
if (st->snake->x == root_x && st->snake->y == root_y)
|
||
|
{
|
||
|
free_snake(st->snake);
|
||
|
free(main_element);//voľne alokované mamory (Pamätaj)
|
||
|
return END_SNAKE;
|
||
|
}
|
||
|
st->snake = st->snake->next;
|
||
|
}
|
||
|
if (st->snake->x == root_x && st->snake->y == root_y)
|
||
|
{
|
||
|
free_snake(st->snake);
|
||
|
free(main_element);
|
||
|
|
||
|
return END_SNAKE;
|
||
|
}
|
||
|
st->snake = main_element;
|
||
|
int i = 0;
|
||
|
while (i < FOOD_COUNT)
|
||
|
{
|
||
|
if (st->foodx[i] == st->snake->x && st->foody[i] == st->snake->y)
|
||
|
{
|
||
|
st->snake = add_snake(st->snake, st->foodx[i], st->foody[i]);
|
||
|
//koordinovať -1
|
||
|
st->foody[i] = -1;
|
||
|
st->foodx[i] = -1;
|
||
|
main_element = st->snake;
|
||
|
}
|
||
|
i++;
|
||
|
}
|
||
|
int k =0;
|
||
|
while(k < FOOD_COUNT)
|
||
|
{
|
||
|
if (st->foodx[k] != -1 && st->foody[k] != -1)
|
||
|
return END_CONTINUE;
|
||
|
k++;
|
||
|
}
|
||
|
|
||
|
free(main_element);
|
||
|
return END_FOOD;
|
||
|
}
|
||
|
|
||
|
|
||
|
void free_snake(struct snake* sn){
|
||
|
if(sn != NULL){
|
||
|
free(sn);
|
||
|
}
|
||
|
}
|
||
|
struct snake* remove_snake(struct snake* snake){
|
||
|
if(snake == NULL|| snake->next == NULL)
|
||
|
return NULL;
|
||
|
|
||
|
struct snake* main_element = snake;
|
||
|
struct snake* step_snake = snake;
|
||
|
//cheak
|
||
|
while (step_snake->next->next != NULL)
|
||
|
{
|
||
|
step_snake = step_snake->next;
|
||
|
}
|
||
|
step_snake->next = NULL;
|
||
|
step_snake = main_element;
|
||
|
return step_snake;
|
||
|
}
|
||
|
|