From 9c7697371f429d04b9b3c809bc17a2f5105d52c3 Mon Sep 17 00:00:00 2001 From: Piotr Kozak Date: Thu, 5 Mar 2026 22:52:25 +0100 Subject: [PATCH] Colors and double merge blocks Ok, this was much easier than I thought it would be to implement --- .gitignore | 1 + main.c | 33 ++++++++++++++++++++++++++++----- 2 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/main.c b/main.c index 3d22711..1e54c4b 100644 --- a/main.c +++ b/main.c @@ -7,16 +7,34 @@ #define BOARD_HEIGHT 4 #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; static int board[BOARD_HEIGHT][BOARD_WIDTH] = {0}; void print_board() { for (int y = 0; y < BOARD_HEIGHT; y++) { 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("\033[0m"); // Reset color } // 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! // 2 2 0 4 => 8 0 0 0 which is incorrect and should be 4 4 0 0 void move(MOVE_DIRECTION direction) { + bool merged[BOARD_HEIGHT][BOARD_WIDTH] = {0}; switch (direction) { case UP: { 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 if (board[py][x] == 0) { 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; break; } else { @@ -57,6 +76,7 @@ void move(MOVE_DIRECTION direction) { board[target_y][x] = board[y][x]; board[y][x] = 0; } else if (board[target_y][x] == board[y][x]) { + merged[target_y][x] = true; board[target_y][x] *= 2; board[y][x] = 0; } @@ -74,7 +94,7 @@ void move(MOVE_DIRECTION direction) { for (int py = y + 1; py < BOARD_HEIGHT; py++) { if (board[py][x] == 0) { 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; break; } else { @@ -89,6 +109,7 @@ void move(MOVE_DIRECTION direction) { board[target_y][x] = board[y][x]; board[y][x] = 0; } else if (board[target_y][x] == board[y][x]) { + merged[target_y][x] = true; board[target_y][x] *= 2; board[y][x] = 0; } @@ -106,7 +127,7 @@ void move(MOVE_DIRECTION direction) { for (int px = x - 1; px >= 0; px--) { if (board[y][px] == 0) { 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; break; } else { @@ -121,6 +142,7 @@ void move(MOVE_DIRECTION direction) { board[y][target_x] = board[y][x]; board[y][x] = 0; } else if (board[y][target_x] == board[y][x]) { + merged[y][target_x] = true; board[y][target_x] *= 2; board[y][x] = 0; } @@ -138,7 +160,7 @@ void move(MOVE_DIRECTION direction) { for (int px = x + 1; px < BOARD_WIDTH; px++) { if (board[y][px] == 0) { 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; break; } else { @@ -153,6 +175,7 @@ void move(MOVE_DIRECTION direction) { board[y][target_x] = board[y][x]; board[y][x] = 0; } else if (board[y][target_x] == board[y][x]) { + merged[y][target_x] = true; board[y][target_x] *= 2; board[y][x] = 0; }