1111
This commit is contained in:
parent
91ff2ad931
commit
304844b7e1
65
cv9/snake.c
65
cv9/snake.c
@ -3,10 +3,6 @@
|
|||||||
|
|
||||||
struct snake* add_snake(struct snake* snake, int x, int y) {
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
||||||
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
|
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
|
||||||
if (!new_head) {
|
|
||||||
// Memory allocation failed
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
new_head->x = x;
|
new_head->x = x;
|
||||||
new_head->y = y;
|
new_head->y = y;
|
||||||
new_head->next = snake;
|
new_head->next = snake;
|
||||||
@ -14,81 +10,62 @@ struct snake* add_snake(struct snake* snake, int x, int y) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct snake* remove_snake(struct snake* snake) {
|
struct snake* remove_snake(struct snake* snake) {
|
||||||
if (!snake) {
|
struct snake* temp = snake;
|
||||||
// Snake is empty
|
while(temp->next->next != NULL) {
|
||||||
return NULL;
|
temp = temp->next;
|
||||||
}
|
}
|
||||||
struct snake* next_part = snake->next;
|
free(temp->next);
|
||||||
free(snake);
|
temp->next = NULL;
|
||||||
return next_part;
|
return snake;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
int is_snake(struct snake* snake, int x, int y) {
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
while (snake) {
|
while(snake != NULL) {
|
||||||
if(snake->x == x && snake->y == y) {
|
if(snake->x == x && snake->y == y) {
|
||||||
return 1; // Found snake part at given coordinates
|
return 1;
|
||||||
}
|
}
|
||||||
snake = snake->next;
|
snake = snake->next;
|
||||||
}
|
}
|
||||||
return 0; // Snake part not found
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void free_snake(struct snake* sn) {
|
void free_snake(struct snake* snake) {
|
||||||
while (sn) {
|
struct snake* temp;
|
||||||
struct snake* temp = sn;
|
while(snake != NULL) {
|
||||||
sn = sn->next;
|
temp = snake;
|
||||||
|
snake = snake->next;
|
||||||
free(temp);
|
free(temp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int step_state(struct state* state) {
|
int step_state(struct state* state) {
|
||||||
// Calculate new position of the snake head
|
|
||||||
int new_x = state->snake->x + state->sx;
|
int new_x = state->snake->x + state->sx;
|
||||||
int new_y = state->snake->y + state->sy;
|
int new_y = state->snake->y + state->sy;
|
||||||
|
|
||||||
// Check if snake hits a wall
|
|
||||||
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
|
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
|
||||||
return END_WALL;
|
return END_WALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake hits itself
|
if(is_snake(state->snake, new_x, new_y)) {
|
||||||
if (is_snake(state->snake->next, new_x, new_y)) {
|
|
||||||
return END_SNAKE;
|
return END_SNAKE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if snake eats food
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
||||||
int i;
|
|
||||||
for (i = 0; i < FOOD_COUNT; i++) {
|
|
||||||
if(state->foodx[i] == new_x && state->foody[i] == new_y) {
|
if(state->foodx[i] == new_x && state->foody[i] == new_y) {
|
||||||
// Food eaten, mark it as negative
|
|
||||||
state->foodx[i] = -1;
|
state->foodx[i] = -1;
|
||||||
state->foody[i] = -1;
|
state->foody[i] = -1;
|
||||||
// Add new snake part at the food position
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
if (state->snake == NULL) {
|
|
||||||
// Memory allocation failed
|
|
||||||
return END_USER;
|
|
||||||
}
|
|
||||||
// Check if no food left
|
|
||||||
int food_left = 0;
|
|
||||||
for(int j = 0; j < FOOD_COUNT; j++) {
|
for(int j = 0; j < FOOD_COUNT; j++) {
|
||||||
if (state->foodx[j] >= 0 && state->foody[j] >= 0) {
|
if(state->foodx[j] != -1) {
|
||||||
food_left = 1;
|
return END_CONTINUE;
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!food_left) {
|
return END_FOOD;
|
||||||
return END_FOOD; // No food left
|
|
||||||
}
|
|
||||||
return END_CONTINUE; // Food eaten, continue the game
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add new snake part on the new position and remove the last part of the snake
|
|
||||||
state->snake = add_snake(state->snake, new_x, new_y);
|
state->snake = add_snake(state->snake, new_x, new_y);
|
||||||
state->snake = remove_snake(state->snake->next);
|
state->snake = remove_snake(state->snake);
|
||||||
|
|
||||||
return END_CONTINUE;
|
return END_CONTINUE;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user