From f18b23b08aa96909e6e6b05a67ea2af8300cae8e Mon Sep 17 00:00:00 2001 From: hkz Date: Thu, 24 Jul 2025 18:05:51 +0200 Subject: [PATCH] Update the game logic to take note of high scores --- src/game_logic.c | 4 ++-- src/game_logic.h | 2 +- src/loading_screen.c | 1 - src/main.c | 43 +++++++++++++++++++++++++++++-------------- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/game_logic.c b/src/game_logic.c index d23e8d9..42fadb3 100644 --- a/src/game_logic.c +++ b/src/game_logic.c @@ -53,8 +53,8 @@ uint8_t add_random_tile(void) { 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 } -uint8_t step_game(step_direction dir) { - uint8_t done = 0; +int8_t step_game(step_direction dir) { + int8_t done = 0; uint8_t start_offset; int8_t column_step; int8_t row_step; diff --git a/src/game_logic.h b/src/game_logic.h index b8f7266..19d7939 100644 --- a/src/game_logic.h +++ b/src/game_logic.h @@ -14,7 +14,7 @@ typedef enum { uint8_t reset_game(void); uint8_t *get_front_grid(void); -uint8_t step_game(step_direction dir); +int8_t step_game(step_direction dir); uint16_t calculate_score(void); uint8_t add_random_tile(void); diff --git a/src/loading_screen.c b/src/loading_screen.c index 6d3594f..06c0243 100644 --- a/src/loading_screen.c +++ b/src/loading_screen.c @@ -207,5 +207,4 @@ __attribute__((section("loadscreen"))) const uint8_t __loading_screen[] = { 0x00, 0x00, 0x18, 0x00, 0x3C, 0x00, 0x3E, 0x00, 0x08, 0x00, 0x02, 0x00, 0x00, 0x00, 0x70, 0x01, 0x00, 0x0C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47, 0x7F, 0x7F, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x60, 0x18, 0x0E, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, 0x00, 0x4E, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -#pragma require __loading_screen diff --git a/src/main.c b/src/main.c index 3d371ce..033c7a6 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,19 @@ #include "game_graphics.h" #include "monitor_subroutines.h" +// Make sure the loading screen is included +#pragma require __loading_screen + +#define MOVES_TEXT_X 32 +#define MOVES_TEXT_Y 61 +#define MOVES_TEXT_WIDTH 5 + +#define SCORE_TEXT_X 32 +#define SCORE_TEXT_Y 29 +#define SCORE_TEXT_WIDTH 5 + +#define WIN_SCORE_BONUS 10000 + void init(void); // Low level initialization @@ -26,25 +39,21 @@ void init(void) { memset((void*)DISPLAY_PAGE_2, 0, DISPLAY_PAGE_SIZE); } -#define MOVES_TEXT_X 32 -#define MOVES_TEXT_Y 61 -#define MOVES_TEXT_WIDTH 5 - -#define SCORE_TEXT_X 32 -#define SCORE_TEXT_Y 29 -#define SCORE_TEXT_WIDTH 5 -#pragma require __loading_screen - __task int main(void) { uint16_t moves_count; - uint16_t score; - uint8_t done; + uint16_t score = 0; + uint16_t hi_score = 0; + int8_t done = 0; init(); while(1){ // Outer loop moves_count = 0; + + // Check if we have a new high-score + if (score > hi_score) hi_score = score; + // Reset the game, calculate the initial score depending on which tiles we randomly get score = reset_game(); // Draw the initial state of the game @@ -88,8 +97,11 @@ __task int main(void) { moves_count++; // If we have won, break out of this loop - if(done) break; - + if(done) { + score += WIN_SCORE_BONUS; + break; + } + // Draw the number of moves draw_number(moves_count, MOVES_TEXT_WIDTH, MOVES_TEXT_X, MOVES_TEXT_Y); @@ -98,7 +110,10 @@ __task int main(void) { // Unable to add a tile: we ran out of space and lost!!! uint8_t random_tile_off = add_random_tile(); - if(!random_tile_off) break; + if(!random_tile_off) { + done = -1; // Lost the game + break; + } score = calculate_score();