Update game logic to save score

This commit is contained in:
hkz 2025-07-16 22:27:49 +02:00
commit d634e15eca
2 changed files with 18 additions and 3 deletions

View file

@ -50,7 +50,12 @@ uint8_t add_random_tile(void) {
return tile_placed; // Return 0 if we were not able to place the tile
}
uint8_t *step_game(step_direction dir) {
game_state step_game(step_direction dir) {
game_state state = {
.score = 0,
.done = 0
};
uint8_t start_offset;
int8_t column_step;
int8_t row_step;
@ -100,6 +105,8 @@ uint8_t *step_game(step_direction dir) {
if(front_grid[sub_col_offset]) {
back_grid[current_offset] = front_grid[sub_col_offset];
front_grid[sub_col_offset] = 0;
state.score += ((uint16_t)1) << back_grid[current_offset];
break;
}
}
@ -113,6 +120,9 @@ uint8_t *step_game(step_direction dir) {
if(front_grid[sub_col_offset] == back_grid[current_offset]) {
back_grid[current_offset]++; // Merge them (by increasing the value of the current square and removing the merged one)
front_grid[sub_col_offset] = 0;
state.score += ((uint16_t)1) << back_grid[current_offset];
state.done += back_grid[current_offset] == 11 ? 1 : 0;
break;
}
}
@ -121,5 +131,5 @@ uint8_t *step_game(step_direction dir) {
swap_grids();
return front_grid;
return state;
}

View file

@ -12,9 +12,14 @@ typedef enum {
RIGHT
} step_direction;
typedef struct {
uint16_t score;
uint8_t done;
} game_state;
void reset_game(void);
uint8_t *get_front_grid(void);
uint8_t *step_game(step_direction dir);
game_state step_game(step_direction dir);
uint8_t add_random_tile(void);
#endif /* _GAME_LOGIC_HEADER_ */