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 <stdlib.h>
#include "snake.h"
/*typedef struct Snake {
int x;
int y;
struct Snake *next;
} Snake;
typedef struct State {
Snake *snake;
int food[2];
int foodCount;
int height;
int width;
int speedX;
int speedY;
} State;*/
/*enum endgame {
END_CONTINUE,
END_SNAKE,
END_FOOD
};*/
void calculateNewSnakeHeadPosition(State *state, int *newX, int *newY) {
*newX = state->snake->x + state->speedX;
*newY = state->snake->y + state->speedY;
}
int isPointInsideSnake(Snake *snake, int x, int y) {
Snake *current = snake;
while (current != NULL) {
if (current->x == x && current->y == y) {
return 1;
}
current = current->next;
}
return 0;
}
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;
}*/
#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;
}
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;
}