This commit is contained in:
Sadchenko 2024-04-18 08:04:04 +02:00
parent 55d738a03e
commit 047b46a41b

View File

@ -1,218 +1,48 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "snake.h" #include "snake.h"
/*typedef struct Snake { struct snake* add_snake(struct snake* snake, int x, int y) {
int x; struct snake* new_head = (struct snake*)malloc(sizeof(struct snake));
int y; if (new_head == NULL) {
struct Snake *next; fprintf(stderr, "Memory allocation failed.\n");
} Snake; exit(EXIT_FAILURE);
}
new_head->x = x;
typedef struct State { new_head->y = y;
Snake *snake; new_head->next = snake;
int food[2]; return new_head;
int foodCount; }
int height;
int width; struct snake* remove_snake(struct snake* snake) {
int speedX; if (snake == NULL) {
int speedY; return NULL;
} State;*/ }
struct snake* new_head = snake->next;
free(snake);
/*enum endgame { return new_head;
END_CONTINUE, }
END_SNAKE,
END_FOOD int is_snake(struct snake* snake, int x, int y) {
};*/ while (snake != NULL) {
if (snake->x == x && snake->y == y) {
return 1;
void calculateNewSnakeHeadPosition(State *state, int *newX, int *newY) { }
*newX = state->snake->x + state->speedX; snake = snake->next;
*newY = state->snake->y + state->speedY; }
} return 0;
}
int isPointInsideSnake(Snake *snake, int x, int y) { void free_snake(struct snake* sn) {
Snake *current = snake; struct snake* current = sn;
while (current != NULL) { while (current != NULL) {
if (current->x == x && current->y == y) { struct snake* temp = current;
return 1; current = current->next;
} free(temp);
current = current->next; }
} }
return 0;
} int step_state(struct state* state) {
return END_CONTINUE;
void removeLastSnakePart(Snake *snake) { }
Snake *current = snake;
Snake *previous = NULL;
while (current->next != NULL) {
previous = current;
current = current->next;
}
free(current);
if (previous != NULL) {
previous->next = NULL;
}
}
void addNewSnakePart(Snake *snake, int x, int y) {
Snake *newPart = (Snake*)malloc(sizeof(Snake));
if (newPart == NULL) {
printf("Error\n");
exit(1);
}
newPart->x = x;
newPart->y = y;
newPart->next = NULL;
Snake *current = snake;
while (current->next != NULL) {
current = current->next;
}
current->next = newPart;
}
/*int step_state(struct state* state) {
int newX, newY;
calculateNewSnakeHeadPosition(state, &newX, &newY);
if (isPointInsideSnake(state->snake, newX, newY)) {
return END_SNAKE;
}
if (newX == state->foodx[0] && newY == state->foody[1]) {
state->foodx[0] = -1;
state->foody[1] = -1;
addNewSnakePart(state->snake, newX, newY);
state->foodCount--;
if (state->foodCount == 0) {
return END_FOOD;
} else {
return END_CONTINUE;
}
}
if (newX >= 0 && newX < state->width && newY >= 0 && newY < state->height) {
addNewSnakePart(state->snake, newX, newY);
removeLastSnakePart(state->snake);
return END_CONTINUE;
} else {
return END_SNAKE;
}
}*/
int step_state(struct state* state) {
// Рассчитываем новые координаты головы змеи
int newHeadX = state->snake->x + state->sx;
int newHeadY = state->snake->y + state->sy;
// Проверяем, находится ли новая позиция головы змеи на змее
struct snake *current = state->snake->next;
while (current != NULL) {
if (current->x == newHeadX && current->y == newHeadY) {
return END_SNAKE; // Игра завершается из-за столкновения со змеей
}
current = current->next;
}
// Проверяем, находится ли новая позиция головы змеи на еде
for (int i = 0; i < FOOD_COUNT; ++i) {
if (newHeadX == state->foodx[i] && newHeadY == state->foody[i]) {
// Помечаем еду как съеденную и добавляем новый элемент змеи
state->foodx[i] = -1;
state->foody[i] = -1;
struct snake *newPart = (struct snake*)malloc(sizeof(struct snake));
if (newPart == NULL) {
printf("Ошибка выделения памяти\n");
exit(1);
}
newPart->x = newHeadX;
newPart->y = newHeadY;
newPart->next = state->snake;
state->snake = newPart;
return (i == FOOD_COUNT - 1) ? END_FOOD : END_CONTINUE;
}
}
// Проверяем, находится ли новая позиция головы змеи в пределах поля
if (newHeadX >= 0 && newHeadX < state->width && newHeadY >= 0 && newHeadY < state->height) {
// Добавляем новую часть змеи и удаляем последнюю
struct snake *newHead = (struct snake*)malloc(sizeof(struct snake));
if (newHead == NULL) {
printf("Ошибка выделения памяти\n");
exit(1);
}
newHead->x = newHeadX;
newHead->y = newHeadY;
newHead->next = state->snake;
state->snake = newHead;
// Удаляем последнюю часть змеи
struct snake *tail = state->snake;
while (tail->next->next != NULL) {
tail = tail->next;
}
free(tail->next);
tail->next = NULL;
return END_CONTINUE; // Игра продолжается после перемещения змеи
} else {
return END_SNAKE; // Игра завершается из-за выхода за пределы поля
}
}
/*int main() {
State *gameState = (State*)malloc(sizeof(State));
if (gameState == NULL) {
printf("Error\n");
return 1;
}
gameState->snake = (Snake*)malloc(sizeof(Snake));
if (gameState->snake == NULL) {
printf("Error\n");
return 1;
}
gameState->snake->x = 0;
gameState->snake->y = 0;
gameState->snake->next = NULL;
gameState->food[0] = 5;
gameState->food[1] = 5;
gameState->foodCount = 1;
gameState->height = 10;
gameState->width = 10;
gameState->speedX = 1;
gameState->speedY = 0;
int endGameReason = step_state(gameState);
switch (endGameReason) {
case END_CONTINUE:
printf("Game continue.\n");
break;
case END_SNAKE:
printf("Game is finished due to colision.\n");
break;
case END_FOOD:
printf("Game is finished due to lack of food.\n");
break;
}
free(gameState->snake);
free(gameState);
return 0;
}*/