73 lines
1.6 KiB
C
73 lines
1.6 KiB
C
#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;
|
|
}
|
|
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);
|
|
|
|
|
|
if (new_head != NULL && new_head->x == 3 && new_head->y == 3) {
|
|
printf("Test passed: new head coordinates are correct.\n");
|
|
} else {
|
|
printf("Test failed: new head coordinates are incorrect.\n");
|
|
}
|
|
|
|
|
|
free(new_head);
|
|
free(tail);
|
|
}
|
|
|
|
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;
|
|
}
|