mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2025-12-25 16:02:15 +11:00
Update game logic to save score
This commit is contained in:
parent
1e66023db8
commit
d634e15eca
2 changed files with 18 additions and 3 deletions
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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_ */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue