| Title: | Grid-Based Number Merge Puzzle Simulation |
|---|---|
| Description: | Provides tools to simulate, analyse, visualise, and benchmark grid-based number merge puzzles. The package implements generic grid mechanics, tile-spawning rules, merge rules, scoring functions, reproducible simulation utilities, and local 'Shiny' and 'WebGL' interfaces for interactive use. It is intended for teaching, algorithmic experimentation, and game-theoretic examples. The autoplay helpers use standard heuristic search and Monte Carlo simulation ideas described in Russell and Norvig (2021, ISBN:9780134610993) and Robert and Casella (2004, ISBN:9780387212395). |
| Authors: | Frederic Bertrand [cre, aut] (ORCID: <https://orcid.org/0000-0002-0837-8281>) |
| Maintainer: | Frederic Bertrand <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.2.0 |
| Built: | 2026-06-24 13:27:58 UTC |
| Source: | https://github.com/cran/mergeGridR |
Provides tools to simulate, analyse, visualise, and benchmark grid-based number merge puzzles. The package implements generic grid mechanics, tile-spawning rules, merge rules, scoring functions, reproducible simulation utilities, and local 'Shiny' and 'WebGL' interfaces for interactive use. It is intended for teaching, algorithmic experimentation, and game-theoretic examples. The autoplay helpers use standard heuristic search and Monte Carlo simulation ideas described in Russell and Norvig (2021, ISBN:9780134610993) and Robert and Casella (2004, ISBN:9780387212395).
Maintainer: Frederic Bertrand [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/fbertran/mergeGridR/issues
A small deterministic benchmark produced by benchmark_autoplay_strategies()
using autoplay_benchmark_settings("fast"), n_games = 5,
max_moves = 50, and seed = 20260609.
autoplay_benchmark_resultsautoplay_benchmark_results
A list with five elements:
Aggregate metrics by autoplay setting.
One row per simulated game and setting.
The benchmark settings grid.
The game configuration.
The top-level benchmark seed.
Autoplay benchmark setting presets
autoplay_benchmark_settings(preset = c("quality", "fast"))autoplay_benchmark_settings(preset = c("quality", "fast"))
preset |
Preset grid to return. |
A data frame with columns setting_id, strategy, depth,
beam_width, simulations, and horizon.
Play a game with autoplay
autoplay_game( state = new_game(), strategy = "lookahead", max_moves = 1000L, keep_states = FALSE, ... )autoplay_game( state = new_game(), strategy = "lookahead", max_moves = 1000L, keep_states = FALSE, ... )
state |
Initial game state. |
strategy |
Strategy passed to |
max_moves |
Maximum number of moves to play. |
keep_states |
If |
... |
Additional arguments passed to |
A list with final_state, history, and states.
autoplay_move() scores the legal columns for a game state and returns the
recommended column without changing the state. The default strategy is a
depth-limited lookahead search. future_mode = "visible" plays fairly from
the visible queue and uses independent planning seeds for unknown future
tiles; future_mode = "rng" allows simulations to use the state's internal
deterministic random-number stream.
autoplay_move( state, strategy = c("lookahead", "growth_lookahead", "monte_carlo", "greedy"), depth = 3L, simulations = 100L, horizon = 30L, beam_width = 10L, future_mode = c("visible", "rng"), seed = NULL )autoplay_move( state, strategy = c("lookahead", "growth_lookahead", "monte_carlo", "greedy"), depth = 3L, simulations = 100L, horizon = 30L, beam_width = 10L, future_mode = c("visible", "rng"), seed = NULL )
state |
A game-state list returned by |
strategy |
Move-selection strategy: |
depth |
Lookahead depth for the |
simulations |
Number of rollout simulations for |
horizon |
Maximum rollout length for |
beam_width |
Number of states retained per lookahead layer. |
future_mode |
Whether planning uses only visible future information or the state's deterministic RNG stream. |
seed |
Optional positive integer seed for visible-future planning. |
A list with column, strategy, future_mode, score_estimate,
and a candidates data frame.
Natural next steps include tabular Q-learning on handcrafted board features, approximate Q-learning using the current heuristic features, Deep Q-Networks with legal-action masks, PPO or actor-critic policy gradients, MCTS/UCT rollouts over the deterministic engine, and evolution strategies for tuning heuristic weights before introducing neural models.
Runs repeated seeded games for each autoplay strategy/settings row and
returns both per-game runs and aggregate summaries. Parallel execution uses
base R PSOCK clusters when workers > 1; sequential execution is the
default for reproducible package checks.
benchmark_autoplay_strategies( config = game_config(), n_games = 50L, max_moves = 1000L, settings = autoplay_benchmark_settings("quality"), future_mode = c("visible", "rng"), seed = NULL, workers = 1L )benchmark_autoplay_strategies( config = game_config(), n_games = 50L, max_moves = 1000L, settings = autoplay_benchmark_settings("quality"), future_mode = c("visible", "rng"), seed = NULL, workers = 1L )
config |
A configuration list created by |
n_games |
Number of games to run for each setting. |
max_moves |
Maximum moves per game. |
settings |
Strategy/settings grid from |
future_mode |
Future-tile handling passed to |
seed |
Optional positive integer seed for deterministic game seeds. If
|
workers |
Number of PSOCK workers. Use |
A list with summary, runs, settings, config, and seed.
Clears the configured number of top rows after a game-over state while preserving score, moves, queue, random-number state, and the largest observed tile used for future spawning.
continue_game(state)continue_game(state)
state |
A game-state list returned by |
The continued game-state list.
Drop the next tile into a column
drop_tile(state, column)drop_tile(state, column)
state |
A game-state list returned by |
column |
One-based column index. |
The updated game-state list.
Copies the packaged standalone browser app to an HTML file. The exported app runs entirely in the browser, including game rules, rendering, preview horizon handling, autoplay-lite controls, and browser-local high scores.
export_static_app(path, overwrite = FALSE)export_static_app(path, overwrite = FALSE)
path |
Output HTML file path. This must be supplied explicitly. If
|
overwrite |
Whether to replace an existing file. |
Invisibly returns the normalized output path.
out <- tempfile(fileext = ".html") export_static_app(out)out <- tempfile(fileext = ".html") export_static_app(out)
Create Merge Grid game configuration
game_config( rows = 7L, cols = 5L, next_count = 1L, spawn_distribution = c("weighted", "uniform", "weighted_high"), spawn_weight_decay = 0.55, max_spawn_value = 256L, max_continues = 3L, continue_clear_rows = 3L )game_config( rows = 7L, cols = 5L, next_count = 1L, spawn_distribution = c("weighted", "uniform", "weighted_high"), spawn_weight_decay = 0.55, max_spawn_value = 256L, max_continues = 3L, continue_clear_rows = 3L )
rows, cols
|
Board size. Row 1 is the bottom row. |
next_count |
Preview horizon: number of upcoming tiles kept in the queue. |
spawn_distribution |
Sampling strategy for newly provided tiles. |
spawn_weight_decay |
Geometric weight decay for |
max_spawn_value |
Maximum tile value that can enter the queue. |
max_continues |
Number of manual continues available after game over. |
continue_clear_rows |
Number of top rows cleared by each continue. |
A list used by new_game().
Read the local high score
get_high_score(preview_horizon = 1L)get_high_score(preview_horizon = 1L)
preview_horizon |
Preview horizon whose local high score should be read. |
A non-negative numeric score. Missing or invalid files return zero.
Start a new Merge Grid game
new_game(config = game_config(), seed = NULL)new_game(config = game_config(), seed = NULL)
config |
A configuration list created by |
seed |
Optional positive integer seed for reproducible tile queues. |
A game-state list containing the board, score, queue, and status.
Reset the local high score
reset_high_score(preview_horizon = 1L)reset_high_score(preview_horizon = 1L)
preview_horizon |
Preview horizon whose local high score should be reset. |
Invisibly returns zero.
Launch the mergeGridR Shiny app
run_drop_number( host = "127.0.0.1", port = NULL, launch.browser = interactive() )run_drop_number( host = "127.0.0.1", port = NULL, launch.browser = interactive() )
host |
Host interface passed to |
port |
Optional port passed to |
launch.browser |
Passed to |
The result of shiny::runApp().