Fix logic

This commit is contained in:
hkz 2025-07-18 09:12:17 +02:00
commit 38a40eb2e4
4 changed files with 37 additions and 13 deletions

View file

@ -35,19 +35,18 @@ uint8_t *get_front_grid(void) {
uint8_t add_random_tile(void) {
uint16_t rand = lfsr_update();
uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // 90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4")
uint8_t tile_val = (rand & 0x000F) > 0x0D ? 2 : 1; // ~90% chance of a tile of type 1 (a "2"), 10% of a type 2 (a "4")
uint8_t rand_offset = rand >> 8;
uint8_t tile_placed = 0;
for (int8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
if (!front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)]) {
front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)] = tile_val;
tile_placed = 1;
break;
for (uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
uint8_t rand_off = (offset + rand_offset)%(GRID_SIDE * GRID_SIDE);
if (!front_grid[rand_off]) {
front_grid[rand_off] = tile_val;
return rand_off + 1;
}
}
return tile_placed; // Return 0 if we were not able to place the tile
return 0; // Return 0 if we were not able to place the tile, else we return (offset + 1) to indicate where the tile was placed
}
game_state step_game(step_direction dir) {
@ -123,7 +122,7 @@ game_state step_game(step_direction dir) {
state.score += ((uint16_t)1) << back_grid[current_offset];
state.done += back_grid[current_offset] == 11 ? 1 : 0;
break;
}
} else if (front_grid[sub_col_offset]) break;
}
}
}