Create and run agents to play Gabriele Cirulli's 2048!
You'll need Firefox and a Ruby interpreter (ruby, jruby, etc)
git clone https://github.com/rmseng/2048-agent.git
bundle install
ruby twenty_forty_eight.rb --help
ruby twenty_forty_eight.rb -a GreedyAgent -t 2
- Create a file for your new agent in the agents directory
- Implement the agent with:
initialize
with any setup your agent needsnext_move
that takes the gameboard as its parameter and returns your agent's move
Check greedy_agent.rb
or sample_agent.rb
in the agents directory for examples.
3. ruby twenty_forty_eight.rb -a <YourAgent>
4. Have fun!
TwentyFortyEight can help you run parallel trials for your agent, monitor its decisions, and even run it locally for a big speed boost. See your options: ruby twenty_forty_eight.rb --help
TwentyFortyEight::Gameboard comes with many useful methods. Some of these methods return or yield TwentyFortyEight::Gameboard::Tile objects, which have attributes row, column, and value (which use zero-based coordinates.) Here are some of Gameboard's useful methods:
size
self-explanatoryscore
self-explanatoryvalid_moves
returns [:left, :right, :up, :down]move(direction, insert_random = false)
returns a new copy of the gameboard with the move simulated (with or without adding a new random tile)would_tiles_move?
tells you if a move will change the game state==
compares tile values and positions (not scores)board_full?
returns true even if there is a possible combination to be madewon?
self-explanatorylost?
self-explanatorytiles
enumerates each Tilehorizontal_pairs
enumerates horizontal pairs of Tilesvertical_pairs
enumerates vertical pairs of Tilesadjacent_tiles(row, col)
enumerates the 2, 3, or 4 immediately adjacent Tileslargest_tile_value
returns the value of the highest-valued tileto_s
prints a ASCII representation of the gameboardfree_cells
returns an array of Tiles with nil (empty) valuesget_value(row, col)
returns either nil or an integer, depending on the presence and value of a Tile
Starting a new trial means a new agent will be instantiated. If you want to write a learning agent, you could use class variables, but be sure to synchronize them if you're testing them in parallel.
After you think you've got an agent worthy of extensive testing, try using the 'local' option to simulate many games locally (without running Firefox.) TwentyFortyEight will simulate games of 2048 and collect the results for you.
If you find a bug, please submit a pull request - I'll happily confirm and merge it.
01/13/2017: Updated to work with Ruby 2.3.3 and Firefox 45.6.0.