replaced x and y positions with coordinate

This commit is contained in:
2025-10-30 12:07:27 +01:00
parent 299612634b
commit fb7856b69e
8 changed files with 78 additions and 85 deletions

View File

@@ -132,9 +132,9 @@ static void generate_food(void) {
for (int x = 0; x < MAP_WIDTH; x++) {
for (int y = 0; y < MAP_HEIGHT; y++) {
if (grid_fetch(GRID, x, y) == CELL_EMPTY) {
const coordinate new_spot = {x, y};
empty_spots[available_spots] = new_spot;
const coordinate location = {x, y};
if (grid_fetch(GRID, location) == CELL_EMPTY) {
empty_spots[available_spots] = location;
available_spots++;
}
}
@@ -142,7 +142,7 @@ static void generate_food(void) {
const coordinate food_location = empty_spots[modulo(rand(), available_spots)];
grid_put(GRID, food_location.x, food_location.y, CELL_FOOD);
grid_put(GRID, CELL_FOOD, food_location);
}
/*
@@ -176,7 +176,7 @@ static void initialize(void) {
MESSAGE_LOCATION = (coordinate){.x = RENDER_AT.x, .y = RENDER_AT.y + grid_height(GRID) + 2};
// Create the first body part and spawn the first piece of food.
grid_put(GRID, SNAKE_HEAD.x, SNAKE_HEAD.y, get_body_part(CURRENT_DIRECTION));
grid_put(GRID, get_body_part(CURRENT_DIRECTION), SNAKE_HEAD);
generate_food();
pthread_mutex_init(&SNAKE_MUTEX, NULL);
@@ -220,7 +220,7 @@ static snake_action collision_check(const char c) {
* Moving to a location the snake can't reach is undefined behaviour.
*/
static void update_snake(const coordinate new_location) {
const snake_action action = collision_check(grid_fetch(GRID, new_location.x, new_location.y));
const snake_action action = collision_check(grid_fetch(GRID, new_location));
if (action == SNAKE_DIE) {
grid_put_state(GRID, STATE_VERLOREN);
@@ -230,7 +230,7 @@ static void update_snake(const coordinate new_location) {
if (action == SNAKE_MOVE) {
coordinate new_tail = SNAKE_TAIL;
switch (get_body_part_direction(grid_fetch(GRID, SNAKE_TAIL.x, SNAKE_TAIL.y))) {
switch (get_body_part_direction(grid_fetch(GRID, SNAKE_TAIL))) {
case DIRECTION_UP:
new_tail.y--;
break;
@@ -245,12 +245,12 @@ static void update_snake(const coordinate new_location) {
break;
}
grid_put(GRID, SNAKE_TAIL.x, SNAKE_TAIL.y, CELL_EMPTY);
grid_put(GRID, CELL_EMPTY, SNAKE_TAIL);
SNAKE_TAIL = new_tail;
}
// New head placed after tail moves. It can occupy the empty space created by the tail moving.
grid_put(GRID, new_location.x, new_location.y, get_body_part(CURRENT_DIRECTION));
grid_put(GRID, get_body_part(CURRENT_DIRECTION), new_location);
SNAKE_HEAD = new_location;
PREVIOUS_DIRECTION = CURRENT_DIRECTION;
@@ -300,7 +300,7 @@ static void *snake_move(void *arg) {
}
update_snake(new_location);
show_grid_on_offset(GRID, OFFSET_X, OFFSET_Y);
show_grid_on_offset(GRID, RENDER_AT);
pthread_mutex_unlock(&SNAKE_MUTEX);
}
return NULL;
@@ -324,9 +324,9 @@ static void turn_snake(const direction dir) {
&& !same_coordinate(SNAKE_HEAD, SNAKE_TAIL)) {
return;
}
grid_put(GRID, SNAKE_HEAD.x, SNAKE_HEAD.y, get_body_part(dir));
grid_put(GRID, get_body_part(dir), SNAKE_HEAD);
CURRENT_DIRECTION = dir;
show_grid_on_offset(GRID, OFFSET_X, OFFSET_Y);
show_grid_on_offset(GRID, RENDER_AT);
}
/*
@@ -426,7 +426,7 @@ void snake(void) {
// Show game.
erase();
show_grid_on_offset(GRID, OFFSET_X, OFFSET_Y);
show_grid_on_offset(GRID, RENDER_AT);
wait_for_start();