h
This commit is contained in:
parent
55d738a03e
commit
047b46a41b
228
cv9/snake.c
228
cv9/snake.c
@ -2,217 +2,47 @@
|
|||||||
#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;
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct snake* remove_snake(struct snake* snake) {
|
||||||
|
if (snake == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
struct snake* new_head = snake->next;
|
||||||
|
free(snake);
|
||||||
|
return new_head;
|
||||||
|
}
|
||||||
|
|
||||||
int isPointInsideSnake(Snake *snake, int x, int y) {
|
int is_snake(struct snake* snake, int x, int y) {
|
||||||
Snake *current = snake;
|
while (snake != NULL) {
|
||||||
while (current != NULL) {
|
if (snake->x == x && snake->y == y) {
|
||||||
if (current->x == x && current->y == y) {
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
current = current->next;
|
snake = snake->next;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void free_snake(struct snake* sn) {
|
||||||
void removeLastSnakePart(Snake *snake) {
|
struct snake* current = sn;
|
||||||
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) {
|
while (current != NULL) {
|
||||||
if (current->x == newHeadX && current->y == newHeadY) {
|
struct snake* temp = current;
|
||||||
return END_SNAKE; // Игра завершается из-за столкновения со змеей
|
|
||||||
}
|
|
||||||
current = current->next;
|
current = current->next;
|
||||||
}
|
free(temp);
|
||||||
|
|
||||||
// Проверяем, находится ли новая позиция головы змеи на еде
|
|
||||||
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() {
|
int step_state(struct state* state) {
|
||||||
|
|
||||||
State *gameState = (State*)malloc(sizeof(State));
|
return END_CONTINUE;
|
||||||
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;
|
|
||||||
}*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user