it's broken if the playfield is not a square

This commit is contained in:
David Husicka 2021-12-15 00:58:48 +01:00
parent 220a486c9e
commit 7a67890c74
3 changed files with 13 additions and 13 deletions

View File

@ -34,4 +34,4 @@ To run, run:
# Usage
You can pass `--help` as an command line argument to print help message. You can force incomplete TUI mode that exist for a demonstration of portability and independence of SDL. This TUI mode is incomplete only because it's missing few UI elements and not because it is not feasible. You can also force other sizes of the game area.
You can pass `--help` as an command line argument to print help message. You can force incomplete TUI mode that exists for a demonstration of portability and independence of SDL. This TUI mode is incomplete only because it's missing few UI elements and not because it is not feasible. You can also force other sizes of the game area.

View File

@ -6,8 +6,8 @@
void game_add_block(Game *game) {
bool is_full = true;
for (int i = 0; i < game->field_size_y; i++) {
for (int j = 0; j < game->field_size_x; j++) {
for (int i = 0; i < game->field_size_x; i++) {
for (int j = 0; j < game->field_size_y; j++) {
if (game->field[j][i] == 0) {
is_full = false;
break;
@ -25,8 +25,8 @@ void game_add_block(Game *game) {
x = rand() % game->field_size_x;
y = rand() % game->field_size_y;
value = (rand() % 2) + 1;
} while (game->field[x][y] != 0);
game->field[x][y] = value * 2;
} while (game->field[y][x] != 0);
game->field[y][x] = value * 2;
}
Game game_init(size_t field_size_x, size_t field_size_y) {
@ -37,8 +37,8 @@ Game game_init(size_t field_size_x, size_t field_size_y) {
game.field_size_y = field_size_y;
game.score = 0;
game.field = malloc(sizeof(uint16_t *) * field_size_y);
for (size_t i = 0; i < field_size_y; i++) {
game.field[i] = calloc(field_size_x, sizeof(uint16_t) * field_size_x);
for (size_t i = 0; i < field_size_x; i++) {
game.field[i] = calloc(field_size_y, sizeof(uint16_t) * field_size_y);
}
time_t t;
@ -62,10 +62,10 @@ void game_move(Game *game, Direction direction) {
}
// 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_y; i++) {
for (int i = 0; i < game->field_size_x; i++) {
uint16_t **field = game->field;
// move everything on one side
for (int j = 0; j < game->field_size_x - 1; j++) {
for (int j = 0; j < game->field_size_y - 1; j++) {
// a, b is the same as i and j. It is just flipped when
// it does not go horizontally so I can access stuff vertically
int a = i;

View File

@ -144,10 +144,10 @@ void gui_loop(Game *game) {
if (needs_redraw) {
SDL_RenderClear(window_renderer);
SDL_Rect tile_rect = tile_size;
for (int i = 0; i < game->field_size_y; i++) {
tile_rect.x = i * (700 / game->field_size_y) + 50;
for (int j = 0; j < game->field_size_x; j++) {
tile_rect.y = j * (700 / game->field_size_x) + 150;
for (int i = 0; i < game->field_size_x; i++) {
tile_rect.x = i * (700 / game->field_size_x) + 50;
for (int j = 0; j < game->field_size_y; j++) {
tile_rect.y = j * (700 / game->field_size_y) + 150;
// get texture index from value
for (int k = 0; k < 13; k++) {