If you have never played the game 2048, try playing it here first!
Please choose your settings and press play.
How does it work?
The AI is written using an algorithm known as Expectimax. The rough overview of how an expecimax algorithm works is that it calculates all possible boards that a player could end up at within a few moves, assigns a score to each board, and it picks the move which has the highest probability of getting a high-scoring board.
The grid weights, empty spot bonus, and smoothness bonus all contribute to what score the algorithm assigns to a board. Grid Weights: The first step is calculating the score of a board is that it adds up all the entires of the board, and it weights each entry according to the weight you input above. So for example, if the top left entry of the board is 16, and the top left weight is 5, then it adds 16*5=80 points to the board's score. By changing the grid weights, you can shape how the AI will try to make the board look (the default weights try to force the biggest tiles to congregate in the bottom right corner).
Empty Spot Bonus: The next step in calculating the score of the board is adding the empty spot bonus for every empty spot on the board. So for example if your empty spot bonus is 20 and there are 7 empty spots on the board, then 20*7=140 points will be added to the board's score.
Smoothness Bonus: The final step in calculating the score of the board is adding a bonus for "smooth" boards. The way it does this is it goes over all pairs of adjacent tiles and for each pair, it adds a score bonus, which ranges from 0 to the number you input, depending on how close together the two adjacent tiles are in value (so neighboring 4 and 1024 tiles will have a very small bonus, but a 1024 next to a 2048 will have a high bonus).
Search Depth: The search depth is how many moves into the future the algorithm looks. A search depth of 3 will likely run at a reasonable speed and hit the 2048 and often 4096 tiles, whereas a search depth of 6 or 7 will take a lot longer to run, but has a chance at hitting the 8192 and 16384 tiles.
The sampling amount is a way to trade speed for ability. For example, if the sampling amount is set to 5 and the board has 10 open spots, then rather than looking into what will happen if a tile is placed in each of the 10 different spots, it will instead pick 5 spots at random to simulate.
These settings just control the probability that a newly generated tile is a 2 or a 4. The original 2048 game has the probability of a 2 showing up at 0.9 and the probability of a 4 showing up at 0.1. To speed up the AI and make it perform better, try setting the probability of a 2 showing up to be exactly 1.
Created by Aaron Kaufer