2024-04-10 21:12:35 +00:00
|
|
|
#include "snake.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
2024-04-10 21:20:20 +00:00
|
|
|
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
|
2024-04-10 21:12:35 +00:00
|
|
|
new_head->x = x;
|
|
|
|
new_head->y = y;
|
|
|
|
new_head->next = snake;
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
2024-04-10 21:25:55 +00:00
|
|
|
/*struct snake* remove_snake(struct snake* snake) {
|
|
|
|
struct snake* temp = snake;
|
|
|
|
while(temp->next->next != NULL) {
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
free(temp->next);
|
|
|
|
temp->next = NULL;
|
|
|
|
return snake;
|
|
|
|
}*/
|
|
|
|
|
2024-04-10 21:29:11 +00:00
|
|
|
/*struct snake* remove_snake(struct snake* snake) {
|
2024-04-10 21:25:55 +00:00
|
|
|
if (snake == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2024-04-10 21:20:20 +00:00
|
|
|
struct snake* temp = snake;
|
|
|
|
while(temp->next->next != NULL) {
|
|
|
|
temp = temp->next;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
2024-04-10 21:20:20 +00:00
|
|
|
free(temp->next);
|
|
|
|
temp->next = NULL;
|
|
|
|
return snake;
|
2024-04-10 21:29:11 +00:00
|
|
|
}*/
|
|
|
|
|
|
|
|
struct snake* remove_snake(struct snake* snake) {
|
|
|
|
if (snake == NULL || snake->next == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct snake* temp = snake;
|
|
|
|
while(temp->next->next != NULL) {
|
|
|
|
temp = temp->next;
|
|
|
|
}
|
|
|
|
free(temp->next);
|
|
|
|
temp->next = NULL;
|
|
|
|
return snake;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 21:25:55 +00:00
|
|
|
|
2024-04-10 21:29:11 +00:00
|
|
|
|
2024-04-10 21:12:35 +00:00
|
|
|
int is_snake(struct snake* snake, int x, int y) {
|
2024-04-10 21:20:20 +00:00
|
|
|
while(snake != NULL) {
|
|
|
|
if(snake->x == x && snake->y == y) {
|
|
|
|
return 1;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
|
|
|
snake = snake->next;
|
|
|
|
}
|
2024-04-10 21:20:20 +00:00
|
|
|
return 0;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 21:20:20 +00:00
|
|
|
void free_snake(struct snake* snake) {
|
|
|
|
struct snake* temp;
|
|
|
|
while(snake != NULL) {
|
|
|
|
temp = snake;
|
|
|
|
snake = snake->next;
|
2024-04-10 21:12:35 +00:00
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int step_state(struct state* state) {
|
|
|
|
int new_x = state->snake->x + state->sx;
|
|
|
|
int new_y = state->snake->y + state->sy;
|
|
|
|
|
2024-04-10 21:20:20 +00:00
|
|
|
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
|
2024-04-10 21:12:35 +00:00
|
|
|
return END_WALL;
|
|
|
|
}
|
|
|
|
|
2024-04-10 21:20:20 +00:00
|
|
|
if(is_snake(state->snake, new_x, new_y)) {
|
2024-04-10 21:12:35 +00:00
|
|
|
return END_SNAKE;
|
|
|
|
}
|
|
|
|
|
2024-04-10 21:20:20 +00:00
|
|
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
|
|
|
if(state->foodx[i] == new_x && state->foody[i] == new_y) {
|
2024-04-10 21:12:35 +00:00
|
|
|
state->foodx[i] = -1;
|
|
|
|
state->foody[i] = -1;
|
|
|
|
state->snake = add_snake(state->snake, new_x, new_y);
|
2024-04-10 21:20:20 +00:00
|
|
|
for(int j = 0; j < FOOD_COUNT; j++) {
|
|
|
|
if(state->foodx[j] != -1) {
|
|
|
|
return END_CONTINUE;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-10 21:20:20 +00:00
|
|
|
return END_FOOD;
|
2024-04-10 21:12:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state->snake = add_snake(state->snake, new_x, new_y);
|
2024-04-10 21:20:20 +00:00
|
|
|
state->snake = remove_snake(state->snake);
|
|
|
|
|
2024-04-10 21:12:35 +00:00
|
|
|
return END_CONTINUE;
|
|
|
|
}
|