pvjc24/a2/snake.c

143 lines
4.1 KiB
C
Raw Normal View History

2024-04-18 13:43:15 +00:00
#include <stdio.h>
2024-04-18 13:46:41 +00:00
#include <stdlib.h>
2024-04-18 13:43:15 +00:00
#include <time.h>
2024-04-18 15:07:21 +00:00
#define FOOD_COUNT 3
#define MAX_SNAKE_LENGTH 100
// Define the direction constants
#define UP 0
#define LEFT 1
#define DOWN 2
#define RIGHT 3
struct state {
int width;
int height;
int snake_length;
int snake_x[MAX_SNAKE_LENGTH];
int snake_y[MAX_SNAKE_LENGTH];
int foodx[FOOD_COUNT];
int foody[FOOD_COUNT];
int sx;
int sy;
};
// Function prototypes
void init_state(struct state* state, int width, int height);
void draw_state(struct state* state);
void process_input(struct state* state);
int step_state(struct state* state);
void generate_food(struct state* state);
int is_snake(struct state* state, int x, int y);
void free_snake(struct state* state);
// Add a new segment to the snake
void add_snake(struct state* state, int x, int y) {
state->snake_x[state->snake_length] = x;
state->snake_y[state->snake_length] = y;
state->snake_length++;
}
// Check if the given position is occupied by the snake
int is_snake(struct state* state, int x, int y) {
for (int i = 0; i < state->snake_length; i++) {
if (state->snake_x[i] == x && state->snake_y[i] == y) {
return 1;
}
}
return 0;
}
// Free the memory allocated for the snake
void free_snake(struct state* state) {
// No dynamic memory allocation for the snake in this implementation
}
// Move the snake by one step
int step_state(struct state* state) {
// Move the snake body
for (int i = state->snake_length - 1; i > 0; i--) {
state->snake_x[i] = state->snake_x[i - 1];
state->snake_y[i] = state->snake_y[i - 1];
}
// Move the snake head
state->snake_x[0] += state->sx;
state->snake_y[0] += state->sy;
// Check for collisions with walls
if (state->snake_x[0] < 0 || state->snake_x[0] >= state->width ||
state->snake_y[0] < 0 || state->snake_y[0] >= state->height) {
return 1; // Collision with wall
}
// Check for collisions with itself
for (int i = 1; i < state->snake_length; i++) {
if (state->snake_x[i] == state->snake_x[0] && state->snake_y[i] == state->snake_y[0]) {
return 2; // Collision with itself
}
}
// Check for collisions with food
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->snake_x[0] == state->foodx[i] && state->snake_y[0] == state->foody[i]) {
// Increase the snake length
state->snake_length++;
if (state->snake_length >= MAX_SNAKE_LENGTH) {
return 3; // Snake length exceeds the maximum
}
// Move the food to a new random position
state->foodx[i] = rand() % state->width;
state->foody[i] = rand() % state->height;
return 0; // Food eaten
}
}
return 0; // No collision
}
2024-04-18 13:43:15 +00:00
void init_state(struct state* state, int width, int height) {
state->width = width;
state->height = height;
2024-04-18 15:07:21 +00:00
state->snake_length = 1;
state->snake_x[0] = width / 2;
state->snake_y[0] = height / 2;
2024-04-18 13:43:15 +00:00
state->sx = 0;
2024-04-18 15:07:21 +00:00
state->sy = -1; // Start moving upward
for (int i = 0; i < FOOD_COUNT; i++) {
2024-04-18 13:43:15 +00:00
state->foodx[i] = rand() % width;
state->foody[i] = rand() % height;
2024-04-15 20:31:53 +00:00
}
2024-04-11 12:06:05 +00:00
}
2024-04-18 13:43:15 +00:00
void generate_food(struct state* state) {
2024-04-18 15:07:21 +00:00
for (int i = 0; i < FOOD_COUNT; i++) {
state->foodx[i] = rand() % state->width;
state->foody[i] = rand() % state->height;
2024-04-11 12:06:05 +00:00
}
2024-04-11 12:09:32 +00:00
}
2024-04-11 12:06:05 +00:00
2024-04-18 13:46:41 +00:00
void draw_state(struct state* state) {
2024-04-18 15:07:21 +00:00
for (int y = 0; y < state->height; y++) {
for (int x = 0; x < state->width; x++) {
if (x == state->snake_x[0] && y == state->snake_y[0]) {
printf("H"); // Snake head
} else if (is_snake(state, x, y)) {
printf("S"); // Snake body
2024-04-18 13:46:41 +00:00
} else {
int is_food = 0;
2024-04-18 15:07:21 +00:00
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == x && state->foody[i] == y) {
2024-04-18 13:46:41 +00:00
is_food = 1;
break;
}
}
printf(is_food ? "F" : ".");
}
}
printf("\n");
}
}