Colors and double merge blocks

Ok, this was much easier than I thought it would be to implement
This commit is contained in:
2026-03-05 22:52:25 +01:00
parent fac6318f1f
commit 9c7697371f
2 changed files with 29 additions and 5 deletions
+1
View File
@@ -0,0 +1 @@
main
+28 -5
View File
@@ -7,16 +7,34 @@
#define BOARD_HEIGHT 4 #define BOARD_HEIGHT 4
#define STARTING_TILES 3 #define STARTING_TILES 3
const char* tile_colors[] = {
"\033[0m", // 0 - Reset/default (empty tile)
"\033[48;5;230m", // 2 - Light beige
"\033[48;5;223m", // 4 - Tan
"\033[48;5;216m", // 8 - Orange-ish
"\033[48;5;209m", // 16 - Light orange
"\033[48;5;208m", // 32 - Orange
"\033[48;5;202m", // 64 - Dark orange
"\033[48;5;226m", // 128 - Yellow
"\033[48;5;220m", // 256 - Gold
"\033[48;5;214m", // 512 - Orange-gold
"\033[48;5;208m", // 1024 - Bright orange
"\033[48;5;202m", // 2048 - Red-orange
"\033[48;5;196m", // 4096 - Red
"\033[48;5;160m", // 8192 - Dark red
};
typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION; typedef enum { UP = 0, DOWN, LEFT, RIGHT } MOVE_DIRECTION;
static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0}; static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0};
void print_board() { void print_board() {
for (int y = 0; y < BOARD_HEIGHT; y++) { for (int y = 0; y < BOARD_HEIGHT; y++) {
for (int x = 0; x < BOARD_WIDTH; x++) { for (int x = 0; x < BOARD_WIDTH; x++) {
printf("%d ", board[y][x]); printf("%s%d ", tile_colors[board[y][x]], board[y][x]);
} }
printf("\n"); printf("\n");
} }
printf("\033[0m"); // Reset color
} }
// Depending on the direction, move the tiles and check if we can merge with the // Depending on the direction, move the tiles and check if we can merge with the
@@ -24,6 +42,7 @@ void print_board() {
// TODO: Add a guard agains double merge! // TODO: Add a guard agains double merge!
// 2 2 0 4 => 8 0 0 0 which is incorrect and should be 4 4 0 0 // 2 2 0 4 => 8 0 0 0 which is incorrect and should be 4 4 0 0
void move(MOVE_DIRECTION direction) { void move(MOVE_DIRECTION direction) {
bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0};
switch (direction) { switch (direction) {
case UP: { case UP: {
for (int y = 1; y < BOARD_HEIGHT; y++) { for (int y = 1; y < BOARD_HEIGHT; y++) {
@@ -39,7 +58,7 @@ void move(MOVE_DIRECTION direction) {
for (int py = y - 1; py >= 0; py--) { // Go from the tile's current position up to the top of the board for (int py = y - 1; py >= 0; py--) { // Go from the tile's current position up to the top of the board
if (board[py][x] == 0) { if (board[py][x] == 0) {
target_y = py; target_y = py;
} else if (board[py][x] == board[y][x]) { } else if (board[py][x] == board[y][x] && !merged[py][x]) {
target_y = py; target_y = py;
break; break;
} else { } else {
@@ -57,6 +76,7 @@ void move(MOVE_DIRECTION direction) {
board[target_y][x] = board[y][x]; board[target_y][x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[target_y][x] == board[y][x]) { } else if (board[target_y][x] == board[y][x]) {
merged[target_y][x] = true;
board[target_y][x] *= 2; board[target_y][x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }
@@ -74,7 +94,7 @@ void move(MOVE_DIRECTION direction) {
for (int py = y + 1; py < BOARD_HEIGHT; py++) { for (int py = y + 1; py < BOARD_HEIGHT; py++) {
if (board[py][x] == 0) { if (board[py][x] == 0) {
target_y = py; target_y = py;
} else if (board[py][x] == board[y][x]) { } else if (board[py][x] == board[y][x] && !merged[py][x]) {
target_y = py; target_y = py;
break; break;
} else { } else {
@@ -89,6 +109,7 @@ void move(MOVE_DIRECTION direction) {
board[target_y][x] = board[y][x]; board[target_y][x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[target_y][x] == board[y][x]) { } else if (board[target_y][x] == board[y][x]) {
merged[target_y][x] = true;
board[target_y][x] *= 2; board[target_y][x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }
@@ -106,7 +127,7 @@ void move(MOVE_DIRECTION direction) {
for (int px = x - 1; px >= 0; px--) { for (int px = x - 1; px >= 0; px--) {
if (board[y][px] == 0) { if (board[y][px] == 0) {
target_x = px; target_x = px;
} else if (board[y][px] == board[y][x]) { } else if (board[y][px] == board[y][x] && !merged[y][px]) {
target_x = px; target_x = px;
break; break;
} else { } else {
@@ -121,6 +142,7 @@ void move(MOVE_DIRECTION direction) {
board[y][target_x] = board[y][x]; board[y][target_x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[y][target_x] == board[y][x]) { } else if (board[y][target_x] == board[y][x]) {
merged[y][target_x] = true;
board[y][target_x] *= 2; board[y][target_x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }
@@ -138,7 +160,7 @@ void move(MOVE_DIRECTION direction) {
for (int px = x + 1; px < BOARD_WIDTH; px++) { for (int px = x + 1; px < BOARD_WIDTH; px++) {
if (board[y][px] == 0) { if (board[y][px] == 0) {
target_x = px; target_x = px;
} else if (board[y][px] == board[y][x]) { } else if (board[y][px] == board[y][x] && !merged[y][px]) {
target_x = px; target_x = px;
break; break;
} else { } else {
@@ -153,6 +175,7 @@ void move(MOVE_DIRECTION direction) {
board[y][target_x] = board[y][x]; board[y][target_x] = board[y][x];
board[y][x] = 0; board[y][x] = 0;
} else if (board[y][target_x] == board[y][x]) { } else if (board[y][target_x] == board[y][x]) {
merged[y][target_x] = true;
board[y][target_x] *= 2; board[y][target_x] *= 2;
board[y][x] = 0; board[y][x] = 0;
} }