From 2f8c7910570b048f58631c7e0ab7de3f9102fb38 Mon Sep 17 00:00:00 2001 From: David Husicka Date: Tue, 28 Dec 2021 14:06:40 +0100 Subject: [PATCH] directions move --- .vscode/launch.json | 2 +- src/2048.c | 92 ++++++++++++++++++++++++++++++++++++++------- src/gui.c | 4 +- 3 files changed, 82 insertions(+), 16 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 9f94d61..2a42fa0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/builddir/src/2048", - "args": ["--x", "4", "--y", "5"], + "args": ["--x", "8", "--y", "12"], "cwd": "${workspaceFolder}" } ] diff --git a/src/2048.c b/src/2048.c index 88ea990..9abcbda 100644 --- a/src/2048.c +++ b/src/2048.c @@ -63,30 +63,96 @@ void game_move(Game *game, Direction direction) { // the fuck? // I have few chained for loops that do a lot of similar stuff so I call them in a for loop for better clarity and readability for (int u = 0; u < 3; u++) { - for (int i = 0; i < game->field_size_x; i++) { + for (int i = 0; i < max(game->field_size_x, game->field_size_y); i++) { uint16_t **field = game->field; // move everything on one side - for (int j = 0; j < game->field_size_y - 1; j++) { + for (int j = 0; j < max(game->field_size_x, game->field_size_y); j++) { // the similar stuff + int helper; switch (u) { case 0: case 2: - if (field[i][j + 1] == 0) { - field[i][j + 1] = field[i][j]; - field[i][j] = 0; + switch (direction) { + case Down: + if (j >= game->field_size_y - 1) + break; + if (i >= game->field_size_x) + break; + if (field[i][j + 1] == 0) { + field[i][j + 1] = field[i][j]; + field[i][j] = 0; + } + break; + case Right: + if (i >= game->field_size_y) + break; + if (j >= game->field_size_x - 1) + break; + if (field[j + 1][i] == 0) { + field[j + 1][i] = field[j][i]; + field[j][i] = 0; + } + break; + case Up: + helper = game->field_size_y - j; + if (helper >= game->field_size_y || helper <= 0) + break; + if (i >= game->field_size_x) + break; + if (field[i][helper - 1] == 0) { + field[i][helper - 1] = field[i][helper]; + field[i][helper] = 0; + } + break; + case Left: + helper = game->field_size_x - i; + if (helper >= game->field_size_y) + break; + if (i >= game->field_size_x || helper <= 0) + break; + if (field[helper - 1][j] == 0) { + field[helper - 1][j] = field[helper][j]; + field[helper][j] = 0; + } + break; + case NoDirection: // get rid of compiler warnings + break; } break; case 1: // TODO: fix 444 <- 48, should be 84 - if (field[i][j + 1] == field[i][j]) { - field[i][j + 1] = field[i][j] * 2; - game->score += field[i][j + 1]; - if (field[i][j + 1] == 2048) { - game->won = true; + switch (direction) { + case Down: + if (j >= game->field_size_y - 1) + break; + if (i >= game->field_size_x) + break; + if (field[i][j + 1] == field[i][j]) { + field[i][j + 1] = field[i][j] * 2; + game->score += field[i][j + 1]; + if (field[i][j + 1] == 2048) { + game->won = true; + } + field[i][j] = 0; + j++; + } + break; + case Right: + if (j >= game->field_size_y) + break; + if (i >= game->field_size_x - 1) + break; + if (field[i + 1][j] == field[i][j]) { + field[i + 1][j] = field[i][j] * 2; + game->score += field[i + 1][j]; + if (field[i + 1][j] == 2048) { + game->won = true; + } + field[i][j] = 0; + j++; + } + break; } - field[i][j] = 0; - j++; - } break; } } diff --git a/src/gui.c b/src/gui.c index 597e29e..301f8f3 100644 --- a/src/gui.c +++ b/src/gui.c @@ -4,8 +4,8 @@ #include #include -const SCREEN_WIDTH = 800; -const SCREEN_HEIGHT = 920; +const int SCREEN_WIDTH = 800; +const int SCREEN_HEIGHT = 920; // these are meant to be immutable like in Rust // unfortunately, C does not allow that