From 75c55bd1149c315ef8c758a50530d92920d0b87e Mon Sep 17 00:00:00 2001 From: hkz Date: Fri, 25 Jul 2025 17:00:40 +0200 Subject: [PATCH] Change how random data is generated --- src/game_graphics.c | 18 +++++++++++++++--- src/game_graphics.h | 1 + src/game_logic.c | 18 +++++++++++++----- src/main.c | 11 ++--------- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/game_graphics.c b/src/game_graphics.c index c67cb70..c3c5638 100644 --- a/src/game_graphics.c +++ b/src/game_graphics.c @@ -22,8 +22,8 @@ #define TOP_OFFSET 7 #define LEFT_OFFSET_B 1 // Left is offset by 1 bytes (7 pixels) -static uint8_t *front_buf = (uint8_t*)DISPLAY_PAGE_1; -static uint8_t *back_buf = (uint8_t*)DISPLAY_PAGE_2; +static uint8_t *front_buf; +static uint8_t *back_buf; #define BOX_CONTENT_SIZE 544 @@ -171,7 +171,7 @@ void draw_tiles(void) { #define ENDGAME_BOX_Y_OFFSET 16 void ddraw_endgame_box(int8_t done, uint16_t score, uint16_t hi_score) { // Clear the part of the screen where we'll draw - clear_box(SCREEN_WIDTH_B - (ENDGAME_BOX_X_OFFSET * 2), SCREEN_HEIGHT - (ENDGAME_BOX_Y_OFFSET * 2), ENDGAME_BOX_X_OFFSET, ENDGAME_BOX_Y_OFFSET, front_buf); + clear_box((SCREEN_WIDTH_B - (ENDGAME_BOX_X_OFFSET * 2)) + 1, (SCREEN_HEIGHT - (ENDGAME_BOX_Y_OFFSET * 2)) + CHAR_HEIGHT, ENDGAME_BOX_X_OFFSET, ENDGAME_BOX_Y_OFFSET, front_buf); // Horizontal lines for(uint8_t row = 0; row < CHAR_HEIGHT; row++) { @@ -248,6 +248,18 @@ void swap_display_buffers(void) { PEEK(((uint16_t)front_buf == DISPLAY_PAGE_1) ? IO_DISPLAY_PAGE1 : IO_DISPLAY_PAGE2); } +void clear_display_buffers(void) { + // Clear the buffers + memset((void*)DISPLAY_PAGE_1, 0, DISPLAY_PAGE_SIZE); + memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE); + + PEEK(IO_DISPLAY_PAGE1); // Select the first display page + + // Restore the buffer ordering + front_buf = (uint8_t*)DISPLAY_PAGE_1; + back_buf = (uint8_t*)DISPLAY_PAGE_2; +} + void draw_field_borders_on_buffer(uint8_t brd, uint8_t* buf) { // Horizontal borders for(uint8_t col = 0; col < (GRID_SIDE * (GRID_CELL_SIDE/7)) + 1; col++) { diff --git a/src/game_graphics.h b/src/game_graphics.h index 0ed447d..ae5629d 100644 --- a/src/game_graphics.h +++ b/src/game_graphics.h @@ -26,6 +26,7 @@ void draw_number(uint16_t n, uint8_t len, uint8_t x, uint8_t y); void draw_tiles(void); void ddraw_single_tile(uint8_t offset); void swap_display_buffers(void); +void clear_display_buffers(void); void clear_box(uint8_t w, uint8_t h, uint8_t off_x, uint8_t off_y, uint8_t *disp_buf); void ddraw_direction_arrows(arrow_direction dir); void ddraw_endgame_box(int8_t done, uint16_t score, uint16_t hi_score); diff --git a/src/game_logic.c b/src/game_logic.c index 42fadb3..c98307e 100644 --- a/src/game_logic.c +++ b/src/game_logic.c @@ -40,13 +40,21 @@ 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 rand_offset = rand >> 8; + + uint8_t free_tiles = 0; + uint8_t chosen_tile; 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; + if (!front_grid[offset]) free_tiles++; + } + + if(!free_tiles) return 0; + chosen_tile = (rand >> 8) % free_tiles; + + for (uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) { + if (!front_grid[offset] && !(chosen_tile--)) { + front_grid[offset] = tile_val; + return offset + 1; } } diff --git a/src/main.c b/src/main.c index 003a16e..335f694 100644 --- a/src/main.c +++ b/src/main.c @@ -25,7 +25,6 @@ #define WIN_SCORE_BONUS 10000 void init(void); -void clear_display(void); // Low level initialization void init(void) { @@ -33,15 +32,9 @@ void init(void) { PEEK(IO_ROMSEL); // Make sure the ROM is selected PEEK(IO_DISPLAY_BW); // Disable colors - PEEK(IO_DISPLAY_PAGE1); // Select the first display page // Clear display memory - clear_display(); -} - -void clear_display(void) { - memset((void*)DISPLAY_PAGE_1, 0, DISPLAY_PAGE_SIZE); - memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE); + clear_display_buffers(); } __task int main(void) { @@ -65,7 +58,7 @@ __task int main(void) { lfsr_update(); } BELL1(); - clear_display(); // Clear display again + clear_display_buffers(); // Clear display again // Reset the game, calculate the initial score depending on which tiles we randomly get score = reset_game();