Skip to content

g2048 environment: new tile logic is bugged (different from the original) #491

@bbbbbbbbba

Description

@bbbbbbbbba

In the c_step function, the function place_tile_at_random_cell is called after move, but before update_stats_and_get_heuristic_rewards. This is a problem because place_tile_at_random_cell relies on game->empty_count, which is not updated after a move merges some tiles, only in update_stats_and_get_heuristic_rewards.

void c_step(Game* game) {
float reward = 0.0f;
float score_add = 0.0f;
unsigned char prev_max_tile = game->max_tile;
bool did_move = move(game, game->actions[0] + 1, &reward, &score_add);
game->tick++;
if (did_move) {
game->moves_made++;
place_tile_at_random_cell(game, get_new_tile());
game->score += score_add;
// Add heuristic rewards/penalties and update grid stats
reward += update_stats_and_get_heuristic_rewards(game);
reward *= game->reward_scaler;

static inline void place_tile_at_random_cell(Game* game, unsigned char tile) {
if (game->empty_count == 0) return;
int target = rand() % game->empty_count;
int pos = 0;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (game->grid[i][j] == EMPTY) {
if (pos == target) {
game->grid[i][j] = tile;
game->empty_count--;
return;
}
pos++;
}
}
}
}

In update_stats_and_get_heuristic_rewards:

game->empty_count = empty_count;

The consequences are:

  1. When the grid was full before a valid move, the move will necessarily have merged some tiles, but place_tile_at_random_cell still sees that game->empty_count is 0 and returns immediately. Therefore the game does not spawn a tile for that move.
  2. When the grid was not full before a move merged more tiles, place_tile_at_random_cell will see a smaller value of game->empty_count than the actual empty count, and thus will never spawn a tile in the last empty cell(s).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions