This commit is contained in:
Macko 2024-04-11 14:39:01 +02:00
parent 7c29a6bd1c
commit 2b025d26b6

View File

@ -1,83 +1,118 @@
#include "snake.h" #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <conio.h>
#define FOOD_COUNT 20 #define WIDTH 20
#define HEIGHT 20
#define FOOD_COUNT 3
struct state { enum EndGame { END_WALL, END_SNAKE, END_FOOD, END_CONTINUE };
struct snake* snake;
int sx, sy; struct snake {
int width, height; int x, y;
int foodx[FOOD_COUNT], foody[FOOD_COUNT]; struct snake* next;
}; };
struct snake* add_snake(struct snake* snake, int x, int y) { struct state {
struct snake* new_head = (struct snake*) malloc(sizeof(struct snake)); int width, height;
new_head->x = x; int sx, sy;
new_head->y = y; struct snake* snake;
new_head->next = snake; int foodx[FOOD_COUNT];
return new_head; int foody[FOOD_COUNT];
} };
struct snake* remove_snake(struct snake* snake) {
if (snake == NULL || snake->next == NULL) {
return NULL;
}
struct snake* temp = snake;
while(temp->next->next != NULL) {
temp = temp->next;
}
free(temp->next);
temp->next = NULL;
return snake;
}
int is_snake(struct snake* snake, int x, int y) { void initialize(struct state* state);
while(snake != NULL) { void render(struct state* state);
if(snake->x == x && snake->y == y) { void handle_input(struct state* state);
return 1; void game_loop(struct state* state);
} void cleanup(struct state* state);
snake = snake->next;
} int main() {
struct state game_state;
initialize(&game_state);
game_loop(&game_state);
cleanup(&game_state);
return 0; return 0;
} }
void free_snake(struct snake* snake) { void initialize(struct state* state) {
struct snake* temp; state->width = WIDTH;
while(snake != NULL) { state->height = HEIGHT;
temp = snake; state->sx = 1;
snake = snake->next; state->sy = 0;
free(temp); state->snake = add_snake(NULL, 5, 5);
for (int i = 0; i < FOOD_COUNT; i++) {
state->foodx[i] = rand() % state->width;
state->foody[i] = rand() % state->height;
} }
} }
int step_state(struct state* state) { void render(struct state* state) {
int new_x = state->snake->x + state->sx; system("cls");
int new_y = state->snake->y + state->sy;
if(new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) { for (int y = 0; y < state->height; y++) {
return END_WALL; for (int x = 0; x < state->width; x++) {
} if (is_snake(state->snake, x, y)) {
printf("* ");
if(is_snake(state->snake, new_x, new_y)) { } else {
return END_SNAKE; int food = 0;
} for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == x && state->foody[i] == y) {
for(int i = 0; i < FOOD_COUNT; i++) { printf("o ");
if(state->foodx[i] == new_x && state->foody[i] == new_y) { food = 1;
state->foodx[i] = -1; break;
state->foody[i] = -1;
state->snake = add_snake(state->snake, new_x, new_y);
for(int j = 0; j < FOOD_COUNT; j++) {
if(state->foodx[j] != -1) {
return END_CONTINUE;
} }
} }
return END_FOOD; if (!food) printf(". ");
} }
} }
printf("\n");
state->snake = add_snake(state->snake, new_x, new_y); }
state->snake = remove_snake(state->snake); }
return END_CONTINUE; void handle_input(struct state* state) {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w':
state->sx = 0; state->sy = -1;
break;
case 's':
state->sx = 0; state->sy = 1;
break;
case 'a':
state->sx = -1; state->sy = 0;
break;
case 'd':
state->sx = 1; state->sy = 0;
break;
case 'q':
exit(0);
break;
default:
break;
}
}
}
void game_loop(struct state* state) {
while (1) {
render(state);
handle_input(state);
int result = step_state(state);
if (result == END_WALL || result == END_SNAKE) {
printf("Game Over!\n");
break;
} else if (result == END_FOOD) {
printf("You ate all the food! You win!\n");
break;
}
}
}
void cleanup(struct state* state) {
free_snake(state->snake);
} }