2024-04-18 06:04:04 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "snake.h"
|
|
|
|
|
|
|
|
struct snake* add_snake(struct snake* snake, int x, int y) {
|
|
|
|
struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
|
|
|
|
if (new_head == NULL) {
|
|
|
|
fprintf(stderr, "Memory allocation failed.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
new_head->x = x;
|
|
|
|
new_head->y = y;
|
|
|
|
new_head->next = snake;
|
|
|
|
return new_head;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct snake* remove_snake(struct snake* snake) {
|
|
|
|
if (snake == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
struct snake* new_head = snake->next;
|
|
|
|
free(snake);
|
|
|
|
return new_head;
|
|
|
|
}
|
2024-04-18 06:16:00 +00:00
|
|
|
void test_remove_snake_non_empty() {
|
|
|
|
|
|
|
|
struct snake* tail = calloc(1, sizeof(struct snake));
|
|
|
|
tail->x = 3;
|
|
|
|
tail->y = 3;
|
|
|
|
struct snake* head = calloc(1, sizeof(struct snake));
|
|
|
|
head->x = 2;
|
|
|
|
head->y = 2;
|
|
|
|
head->next = tail;
|
|
|
|
|
|
|
|
|
|
|
|
struct snake* new_head = remove_snake(head);
|
|
|
|
|
|
|
|
|
2024-04-18 06:28:07 +00:00
|
|
|
if (new_head != NULL && new_head->x == 2 && new_head->y == 2 && new_head->y == 2) {
|
2024-04-18 06:16:00 +00:00
|
|
|
printf("Test passed: new head coordinates are correct.\n");
|
|
|
|
} else {
|
|
|
|
printf("Test failed: new head coordinates are incorrect.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
free(new_head);
|
|
|
|
}
|
2024-04-18 06:04:04 +00:00
|
|
|
|
|
|
|
int is_snake(struct snake* snake, int x, int y) {
|
|
|
|
while (snake != NULL) {
|
|
|
|
if (snake->x == x && snake->y == y) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
snake = snake->next;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_snake(struct snake* sn) {
|
|
|
|
struct snake* current = sn;
|
|
|
|
while (current != NULL) {
|
|
|
|
struct snake* temp = current;
|
|
|
|
current = current->next;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int step_state(struct state* state) {
|
|
|
|
|
|
|
|
return END_CONTINUE;
|
|
|
|
}
|