directions move

This commit is contained in:
David Husicka 2021-12-28 14:06:40 +01:00
parent 18b9d4064c
commit 2f8c791057
3 changed files with 82 additions and 16 deletions

2
.vscode/launch.json vendored
View File

@ -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}"
}
]

View File

@ -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;
}
}

View File

@ -4,8 +4,8 @@
#include <SDL_ttf.h>
#include <stdbool.h>
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