想用演算法來解決問題: 所以,只做知識的搬運工 (圖好像不穩定): What is the optimal algorithm for the game 2048?
I"m the author of the AI program that others have mentioned in this thread. You can view the AI in action or read the source.
Currently, the program achieves about a 90% win rate running in
javascript in the browser on my laptop given about 100 milliseconds of
thinking time per move, so while not perfect (yet!) it performs pretty
well.
Since the game is a discrete state space, perfect information,
turn-based game like chess and checkers, I used the same methods that
have been proven to work on those games, namely minimaxsearch with alpha-beta pruning.
Since there is already a lot of info on that algorithm out there, I"ll
just talk about the two main heuristics that I use in the static evaluation function and which formalize many of the intuitions that other people have expressed here.
Monotonicity
This heuristic tries to ensure that the values of the tiles are all
either increasing or decreasing along both the left/right and up/down
directions. This heuristic alone captures the intuition that many others
have mentioned, that higher valued tiles should be clustered in a
corner. It will typically prevent smaller valued tiles from getting
orphaned and will keep the board very organized, with smaller tiles
cascading in and filling up into the larger tiles.
Here"s a screenshot of a perfectly monotonic grid. I obtained this by
running the algorithm with the eval function set to disregard the other
heuristics and only consider monotonicity.
Smoothness
The above heuristic alone tends to create structures in which
adjacent tiles are decreasing in value, but of course in order to merge,
adjacent tiles need to be the same value. Therefore, the smoothness
heuristic just measures the value difference between neighboring tiles,
trying to minimize this count.
A commenter on Hacker News gave an interesting formalization of this idea in terms of graph theory.
Here"s a screenshot of a perfectly smooth grid, courtesy of this excellent parody fork.
Free Tiles
And finally, there is a penalty for having too few free tiles, since
options can quickly run out when the game board gets too cramped.
And that"s it! Searching through the game space while optimizing
these criteria yields remarkably good performance. One advantage to
using a generalized approach like this rather than an explicitly coded
move strategy is that the algorithm can often find interesting and
unexpected solutions. If you watch it run, it will often make surprising
but effective moves, like suddenly switching which wall or corner it"s
building up against.
Edit:
Here"s a demonstration of the power of this approach. I uncapped the
tile values (so it kept going after reaching 2048) and here is the best
result after eight trials.
Yes, that"s a 4096 alongside a 2048. =) That means it achieved the elusive 2048 tile three times on the same board.
控制台輸入: GameManager.prototype.addRandomTile = function () { if (this.grid.cellsAvailable()) { var value = 1024; var tile = new Tile(this.grid.randomAvailableCell(), value);