finally finished

This commit is contained in:
David Husicka 2021-12-28 21:34:06 +01:00
parent 2f8c791057
commit 0ea91127bc
2 changed files with 41 additions and 7 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"request": "launch",
"name": "Debug",
"program": "${workspaceFolder}/builddir/src/2048",
"args": ["--x", "8", "--y", "12"],
"args": ["--y", "4", "--x", "4"],
"cwd": "${workspaceFolder}"
}
]

View File

@ -62,7 +62,7 @@ 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 u = 0; u < max(game->field_size_x, game->field_size_y); u++) {
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
@ -72,6 +72,7 @@ void game_move(Game *game, Direction direction) {
switch (u) {
case 0:
case 2:
default:
switch (direction) {
case Down:
if (j >= game->field_size_y - 1)
@ -106,9 +107,9 @@ void game_move(Game *game, Direction direction) {
break;
case Left:
helper = game->field_size_x - i;
if (helper >= game->field_size_y)
if (helper >= game->field_size_x || helper <= 0)
break;
if (i >= game->field_size_x || helper <= 0)
if (j >= game->field_size_y)
break;
if (field[helper - 1][j] == 0) {
field[helper - 1][j] = field[helper][j];
@ -120,9 +121,8 @@ void game_move(Game *game, Direction direction) {
}
break;
case 1:
// TODO: fix 444 <- 48, should be 84
switch (direction) {
case Down:
case Up:
if (j >= game->field_size_y - 1)
break;
if (i >= game->field_size_x)
@ -137,7 +137,7 @@ void game_move(Game *game, Direction direction) {
j++;
}
break;
case Right:
case Left:
if (j >= game->field_size_y)
break;
if (i >= game->field_size_x - 1)
@ -152,6 +152,40 @@ void game_move(Game *game, Direction direction) {
j++;
}
break;
case Down:
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] == field[i][helper]) {
field[i][helper - 1] = field[i][helper] * 2;
game->score += field[i][helper - 1];
if (field[i][helper - 1] == 2048) {
game->won = true;
}
field[i][helper] = 0;
j++;
}
break;
case Right:
helper = game->field_size_x - i;
if (helper >= game->field_size_x || helper <= 0)
break;
if (j >= game->field_size_y)
break;
if (field[helper - 1][j] == field[helper][j]) {
field[helper - 1][j] = field[helper][j] * 2;
game->score += field[helper - 1][j];
if (field[helper - 1][j] == 2048) {
game->won = true;
}
field[helper][j] = 0;
j++;
}
break;
case NoDirection:
break;
}
break;
}