mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2026-01-24 05:00:52 +11:00
Show score and moves
This commit is contained in:
parent
4ea5c7147e
commit
1a3635fc9a
5 changed files with 55 additions and 35 deletions
|
|
@ -19,14 +19,18 @@ void swap_grids(void) {
|
|||
back_grid = temp;
|
||||
}
|
||||
|
||||
void reset_game(void) {
|
||||
uint8_t reset_game(void) {
|
||||
uint8_t score = 0;
|
||||
|
||||
// Clear the back and front grid
|
||||
memset(back_grid, 0, GRID_SIDE * GRID_SIDE);
|
||||
memset(front_grid, 0, GRID_SIDE * GRID_SIDE);
|
||||
|
||||
// Then add two random tiles
|
||||
add_random_tile();
|
||||
add_random_tile();
|
||||
score += (1 << front_grid[add_random_tile() - 1]);
|
||||
score += (1 << front_grid[add_random_tile() - 1]);
|
||||
|
||||
return score;
|
||||
}
|
||||
|
||||
uint8_t *get_front_grid(void) {
|
||||
|
|
@ -49,12 +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
|
||||
}
|
||||
|
||||
game_state step_game(step_direction dir) {
|
||||
game_state state = {
|
||||
.score = 0,
|
||||
.done = 0
|
||||
};
|
||||
|
||||
uint8_t step_game(step_direction dir) {
|
||||
uint8_t done = 0;
|
||||
uint8_t start_offset;
|
||||
int8_t column_step;
|
||||
int8_t row_step;
|
||||
|
|
@ -104,7 +104,6 @@ game_state step_game(step_direction dir) {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -119,8 +118,7 @@ game_state step_game(step_direction dir) {
|
|||
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;
|
||||
done += back_grid[current_offset] == 11 ? 1 : 0;
|
||||
break;
|
||||
} else if (front_grid[sub_col_offset]) break;
|
||||
}
|
||||
|
|
@ -129,5 +127,15 @@ game_state step_game(step_direction dir) {
|
|||
|
||||
swap_grids();
|
||||
|
||||
return state;
|
||||
return done;
|
||||
}
|
||||
|
||||
uint16_t calculate_score(void) {
|
||||
uint16_t score = 0;
|
||||
|
||||
for(uint8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
|
||||
if(front_grid[offset]) score += ((uint16_t)1 << front_grid[offset]);
|
||||
}
|
||||
|
||||
return score;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue