101 lines
3.1 KiB
C
101 lines
3.1 KiB
C
#include "snake.h"
|
|
#include <stdlib.h>
|
|
|
|
// Adds a new part to the snake at the given (x, y) position, making it the new head
|
|
struct snake* add_snake(struct snake* snake, int x, int y){
|
|
struct snake* new_head = malloc(sizeof(struct snake));
|
|
if(!new_head){
|
|
return NULL;
|
|
}
|
|
new_head->x = x;
|
|
new_head->y = y;
|
|
new_head->next = snake;
|
|
return new_head;
|
|
}
|
|
|
|
// Removes the last part of the snake (tail)
|
|
struct snake* remove_snake(struct snake* snake){
|
|
if(!snake || !snake->next){
|
|
free(snake);
|
|
return NULL;
|
|
}
|
|
|
|
struct snake* current = snake;
|
|
while (current->next && current->next->next) {
|
|
current = current->next; // Move to the second last part
|
|
}
|
|
free(current->next); // Free the last part (tail)
|
|
current->next = NULL; // Set the second last part's next pointer to NULL
|
|
return snake;
|
|
}
|
|
|
|
// Checks if there is a part of the snake at the given (x, y) coordinates
|
|
int is_snake(struct snake* snake, int x, int y){
|
|
while(snake){
|
|
if(snake->x == x && snake->y == y){
|
|
return 1;
|
|
}
|
|
snake = snake->next;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
// Frees the memory allocated for all parts of the snake
|
|
void free_snake(struct snake* sn){
|
|
while(sn){
|
|
struct snake* next = sn->next;
|
|
free(sn);
|
|
sn = next;
|
|
}
|
|
}
|
|
|
|
// Main function that updates the game state based on the snake's movement
|
|
// Returns the game status according to the reason to end the game (enum endgame)
|
|
int step_state(struct state* state){
|
|
if (!state || !state->snake) return END_USER;
|
|
|
|
int new_x = state->snake->x + state->sx;
|
|
int new_y = state->snake->y + state->sy;
|
|
|
|
// Check for collision with wall (end the game if collided)
|
|
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height){
|
|
return END_WALL;
|
|
}
|
|
|
|
// Check for collision with itself (end the game if the snake runs into itself)
|
|
if(is_snake(state->snake, new_x, new_y)){
|
|
return END_SNAKE;
|
|
}
|
|
|
|
// Check if the snake ate any food
|
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
|
if(state->foodx[i] == new_x && state->foody[i] == new_y){
|
|
// Mark the food as eaten
|
|
state->foodx[i] = -1;
|
|
state->foody[i] = -1;
|
|
// Add a new snake part at the food's location (new head)
|
|
state->snake = add_snake(state->snake, new_x, new_y);
|
|
|
|
int food_placed = 0;
|
|
while(!food_placed){
|
|
int food_x = rand() % (state->width - 2) + 1;
|
|
int food_y = rand() % (state->height - 2) + 1;
|
|
|
|
if(!is_snake(state->snake, food_x, food_y)){
|
|
state->foodx[i] = food_x;
|
|
state->foody[i] = food_y;
|
|
food_placed = 1; // Successfully placed food
|
|
}
|
|
}
|
|
|
|
return END_CONTINUE;
|
|
}
|
|
}
|
|
|
|
// Regular snake movement: move the snake by adding a new head and removing the tail
|
|
state->snake = add_snake(state->snake, new_x, new_y);
|
|
state->snake = remove_snake(state->snake);
|
|
|
|
return END_CONTINUE; // Continue the game if no other conditions are met
|
|
}
|