File Exchange Pick of the Week

Our best user submissions

Submit your algorithms to solve 2048!

Jiro's pick this week is 2048 MATLAB.

"2048"

Need I say more? I'm certain that many of you know what I'm talking about. 2048 is an online and mobile game created by Gabriele Cirulli recently. All I can say is that it is an addictive game that has already resulted in a few MATLAB implementations.

A couple of weekends ago, I also spent a day implementing a MATLAB version. I probably spent more time than I needed to on trying to make it look as close as possible to the original version, but it was a lot of fun.

But then, one of my colleagues, Seth (who was also just featured in this blog last week), suggested including a mechanism for feeding in different algorithms to solve the game. There must be an AI algorithm that would solve the game or produce a high score consistently.

In the app, I've included the ability to select a MATLAB function as the solving algorithm. Then you can see the algorithm in action as it tries to solve the game. The algorithm must be defined as a MATLAB function that takes in a 4x4 matrix representing the game board (NaN for empty spots) and returns a character array representing the direction to move ('up', 'down', 'right', or 'left'). Here's an example of a simple algorithm that randomly chooses a direction.

function direction = myAI(board)
  d = {'up', 'down', 'right', 'left'};
  direction = d{randi(4)};

I've also included a command line simulator, which you can use to do a montecarlo simulation of a particular algorithm.

game = Game2048Simulator(@myAI);
simulate(game, 1000)   % 1000 simulations
viewResult(game, 25)   % 25 histogram bins

We can display the highest score and compute what percentages of the simulation resulted in each of the highest block.

% Display highest score
disp(['Highest score: ', num2str(max(game.Result.Score))])
disp(' ')

% Get the unique values for high blocks
highblocks = game.Result.HighestBlock;
blocks = unique(highblocks);

% Compute the percentage of occurrance for each high block value
nSims = length(highblocks);
for id = 1:length(blocks)
    percentage = nnz(highblocks == blocks(id)) / nSims;
    disp([num2str(blocks(id)), '   ', num2str(percentage*100), '%'])
end
Highest score: 3172
 
16   0.2%
32   6.8%
64   34.5%
128   49.7%
256   8.8%

Submit your algorithm!

So here's a challenge for you. Submit your algorithm and win some MathWorks swag! I will give prizes for each of the following criteria:

  • Highest score - In the example above, 3172.
  • Highest block OR in the case of a tie for "Highest block", the one with the highest percentage for the highest block. In the example above, 8.8% for 256.

It was brought to my attention that since the simulator I've included with the File Exchange entry would stop the game once you reach 2048, I am limiting the highest score you could get. For my next update, I will change it so that the game will continue until no more blocks can move.

If your algorithm is fairly short, please submit the algorithm through the comments for this blog post. Be sure to use code formatting. For lengthy algorithms or multi-function algorithms, feel free to email them to me (as ASCII text).

In the next blog post I write, I will go over some of the winning algorithms you have submitted.

Enjoy!!




Published with MATLAB® R2014a

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.