Files
2048/game.c
T

221 lines
6.4 KiB
C

#include "game.h"
#include <stdlib.h>
#include <string.h>
// Depending on the direction, move the tiles and check if we can merge with the
// tiles in that direction. We merge the tiles only if they are of the same value
void move(GameState_t *state, move_direction_t direction) {
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
bool moved = false;
switch (direction) {
case UP: {
for (int y = 1; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
if (state->board[y][x] == 0)
continue;
// Hard keeping track of this so I'm commenting as much as I can
// Check if we can move up any space, so loop until we find a non-empty tile or the top of the board
// py is the potential target y that we will settle for and move the tile into
int target_y = y;
for (int py = y - 1; py >= 0; py--) { // Go from the tile's current position up to the top of the board
if (state->board[py][x] == 0) {
target_y = py;
} else if (state->board[py][x] == state->board[y][x] && !merged[py][x]) {
target_y = py;
break;
} else {
break;
}
}
// Check if we are moving anything
if (target_y == y)
continue;
// If the target tile is empty, move the current tile there
// If the target tile has the same value, merge them
if (state->board[target_y][x] == 0) {
state->board[target_y][x] = state->board[y][x];
state->board[y][x] = 0;
} else if (state->board[target_y][x] == state->board[y][x]) {
merged[target_y][x] = true;
state->board[target_y][x] *= 2;
state->board[y][x] = 0;
state->score += state->board[target_y][x];
}
moved = true;
}
}
break;
}
case DOWN: {
for (int y = BOARD_HEIGHT - 1; y >= 0; y--) {
for (int x = 0; x < BOARD_WIDTH; x++) {
if (state->board[y][x] == 0)
continue;
int target_y = y;
for (int py = y + 1; py < BOARD_HEIGHT; py++) {
if (state->board[py][x] == 0) {
target_y = py;
} else if (state->board[py][x] == state->board[y][x] && !merged[py][x]) {
target_y = py;
break;
} else {
break;
}
}
if (target_y == y)
continue;
if (state->board[target_y][x] == 0) {
state->board[target_y][x] = state->board[y][x];
state->board[y][x] = 0;
} else if (state->board[target_y][x] == state->board[y][x]) {
merged[target_y][x] = true;
state->board[target_y][x] *= 2;
state->board[y][x] = 0;
state->score += state->board[target_y][x];
}
moved = true;
}
}
break;
}
case LEFT: {
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 1; x < BOARD_WIDTH; x++) {
if (state->board[y][x] == 0)
continue;
int target_x = x;
for (int px = x - 1; px >= 0; px--) {
if (state->board[y][px] == 0) {
target_x = px;
} else if (state->board[y][px] == state->board[y][x] && !merged[y][px]) {
target_x = px;
break;
} else {
break;
}
}
if (target_x == x)
continue;
if (state->board[y][target_x] == 0) {
state->board[y][target_x] = state->board[y][x];
state->board[y][x] = 0;
} else if (state->board[y][target_x] == state->board[y][x]) {
merged[y][target_x] = true;
state->board[y][target_x] *= 2;
state->board[y][x] = 0;
state->score += state->board[y][target_x];
}
moved = true;
}
}
break;
}
case RIGHT: {
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = BOARD_WIDTH - 1; x >= 0; x--) {
if (state->board[y][x] == 0)
continue;
int target_x = x;
for (int px = x + 1; px < BOARD_WIDTH; px++) {
if (state->board[y][px] == 0) {
target_x = px;
} else if (state->board[y][px] == state->board[y][x] && !merged[y][px]) {
target_x = px;
break;
} else {
break;
}
}
if (target_x == x)
continue;
if (state->board[y][target_x] == 0) {
state->board[y][target_x] = state->board[y][x];
state->board[y][x] = 0;
} else if (state->board[y][target_x] == state->board[y][x]) {
merged[y][target_x] = true;
state->board[y][target_x] *= 2;
state->board[y][x] = 0;
state->score += state->board[y][target_x];
}
moved = true;
}
}
break;
}
}
if (moved)
state->moves_made++;
}
void reset_board(GameState_t *state) {
memset(state->board, 0, sizeof(state->board));
state->score = 0;
state->moves_made = 0;
state->status = PLAYING;
int tile_count = 0;
while (tile_count != STARTING_TILES) {
int y = rand() % BOARD_HEIGHT;
int x = rand() % BOARD_WIDTH;
if (state->board[y][x] != 0)
continue;
state->board[y][x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
tile_count++;
}
}
bool spawn_tile(GameState_t *state) {
int empty_tiles[BOARD_WIDTH * BOARD_HEIGHT][2];
int empty_count = 0;
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
if (state->board[y][x] == 0) {
empty_tiles[empty_count][0] = y;
empty_tiles[empty_count][1] = x;
empty_count++;
}
}
}
if (empty_count == 0)
return false;
int random_index = rand() % empty_count;
int spawn_y = empty_tiles[random_index][0];
int spawn_x = empty_tiles[random_index][1];
state->board[spawn_y][spawn_x] = (rand() % 10 == 0) ? 4 : 2; // 10% chance to spawn a 4
return true;
}
// This function will only run, if every tile is filled (checked by `spawn_tile`). We can take advantage of that and
// just check every neighboring cell for equal values for merging
bool can_move(GameState_t *state) {
for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) {
if (state->board[y][x] == 0)
return true;
if (x < BOARD_WIDTH - 1 && state->board[y][x] == state->board[y][x + 1])
return true;
if (y < BOARD_HEIGHT - 1 && state->board[y][x] == state->board[y + 1][x])
return true;
}
}
return false;
}