mirror of
https://codeberg.org/hkzlab/TK2048.git
synced 2026-01-09 03:33:18 +11:00
Add board randomizer in game logic
This commit is contained in:
parent
2d0b26241c
commit
8ff5b1ca97
2 changed files with 22 additions and 2 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
#include "game_logic.h"
|
#include "game_logic.h"
|
||||||
|
|
||||||
|
#include "utility.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
static uint8_t game_grid_alpha[GRID_SIDE * GRID_SIDE];
|
static uint8_t game_grid_alpha[GRID_SIDE * GRID_SIDE];
|
||||||
|
|
@ -21,6 +23,23 @@ uint8_t *get_front_grid(void) {
|
||||||
return front_grid;
|
return front_grid;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 tile_placed = 0;
|
||||||
|
|
||||||
|
for (int8_t offset = 0; offset < GRID_SIDE * GRID_SIDE; offset++) {
|
||||||
|
if (!front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)]) {
|
||||||
|
front_grid[(offset + rand_offset)%(GRID_SIDE * GRID_SIDE)] = tile_val;
|
||||||
|
tile_placed = 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return tile_placed; // Return 0 if we were not able to place the tile
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t *step_game(step_direction dir) {
|
uint8_t *step_game(step_direction dir) {
|
||||||
uint8_t start_offset;
|
uint8_t start_offset;
|
||||||
int8_t column_step;
|
int8_t column_step;
|
||||||
|
|
@ -60,8 +79,8 @@ uint8_t *step_game(step_direction dir) {
|
||||||
// Clear the back grid
|
// Clear the back grid
|
||||||
memset(back_grid, 0, GRID_SIDE * GRID_SIDE);
|
memset(back_grid, 0, GRID_SIDE * GRID_SIDE);
|
||||||
|
|
||||||
for (int8_t row = 0; row < GRID_SIDE; row++) {
|
for (uint8_t row = 0; row < GRID_SIDE; row++) {
|
||||||
for(int8_t col = 0; col < GRID_SIDE; col++) {
|
for(uint8_t col = 0; col < GRID_SIDE; col++) {
|
||||||
uint8_t current_offset = start_offset + (col * column_step) + (row * row_step);
|
uint8_t current_offset = start_offset + (col * column_step) + (row * row_step);
|
||||||
uint8_t sub_col;
|
uint8_t sub_col;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,5 +14,6 @@ typedef enum {
|
||||||
|
|
||||||
uint8_t *get_front_grid(void);
|
uint8_t *get_front_grid(void);
|
||||||
uint8_t *step_game(step_direction dir);
|
uint8_t *step_game(step_direction dir);
|
||||||
|
uint8_t add_random_tile(void);
|
||||||
|
|
||||||
#endif /* _GAME_LOGIC_HEADER_ */
|
#endif /* _GAME_LOGIC_HEADER_ */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue