{"id":5243,"date":"2014-04-04T14:05:11","date_gmt":"2014-04-04T18:05:11","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=5243"},"modified":"2014-04-04T14:09:42","modified_gmt":"2014-04-04T18:09:42","slug":"submit-your-algorithms-to-solve-2048","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/","title":{"rendered":"Submit your algorithms to solve 2048!"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\">Jiro<\/a>'s pick this week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46124-2048-matlab-edition\">2048 MATLAB<\/a>.\r\n   <\/p>\r\n   <p><b>\"2048\"<\/b><\/p>\r\n   <p>Need I say more? I'm certain that many of you know what I'm talking about. <a href=\"http:\/\/gabrielecirulli.github.io\/2048\/\">2048<\/a> is an online and mobile game created by Gabriele Cirulli recently. All I can say is that it is an addictive game that has\r\n      already resulted in a few <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=2048+game\">MATLAB implementations<\/a>.\r\n   <\/p>\r\n   <p>A couple of weekends ago, I also spent a day implementing a MATLAB version. I probably spent more time than I needed to on\r\n      trying to make it look as close as possible to the original version, but it was a lot of fun.\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048MATLAB\/2048Video.gif\"> <\/p>\r\n   <p>But then, one of my colleagues, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/2708336\">Seth<\/a> (who was also just <a href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/03\/28\/solve-it-and-solve-it-right\/\">featured<\/a> in this blog last week), suggested including a mechanism for feeding in different algorithms to solve the game. There must\r\n      be an AI algorithm that would solve the game or produce a high score consistently.\r\n   <\/p>\r\n   <p>In the app, I've included the ability to select a MATLAB function as the solving algorithm. Then you can see the algorithm\r\n      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\r\n      the game board (NaN for empty spots) and returns a character array representing the direction to move ('up', 'down', 'right',\r\n      or 'left'). Here's an example of a simple algorithm that randomly chooses a direction.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #0000FF\">function<\/span> direction = myAI(board)\r\n  d = {<span style=\"color: #A020F0\">'up'<\/span>, <span style=\"color: #A020F0\">'down'<\/span>, <span style=\"color: #A020F0\">'right'<\/span>, <span style=\"color: #A020F0\">'left'<\/span>};\r\n  direction = d{randi(4)};<\/pre><p>I've also included a command line simulator, which you can use to do a montecarlo simulation of a particular algorithm.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">game = Game2048Simulator(@myAI);\r\nsimulate(game, 1000)   <span style=\"color: #228B22\">% 1000 simulations<\/span>\r\nviewResult(game, 25)   <span style=\"color: #228B22\">% 25 histogram bins<\/span><\/pre><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048MATLAB\/result_histogram.png\"> <\/p>\r\n   <p>We can display the highest score and compute what percentages of the simulation resulted in each of the highest block.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Display highest score<\/span>\r\ndisp([<span style=\"color: #A020F0\">'Highest score: '<\/span>, num2str(max(game.Result.Score))])\r\ndisp(<span style=\"color: #A020F0\">' '<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Get the unique values for high blocks<\/span>\r\nhighblocks = game.Result.HighestBlock;\r\nblocks = unique(highblocks);\r\n\r\n<span style=\"color: #228B22\">% Compute the percentage of occurrance for each high block value<\/span>\r\nnSims = length(highblocks);\r\n<span style=\"color: #0000FF\">for<\/span> id = 1:length(blocks)\r\n    percentage = nnz(highblocks == blocks(id)) \/ nSims;\r\n    disp([num2str(blocks(id)), <span style=\"color: #A020F0\">'   '<\/span>, num2str(percentage*100), <span style=\"color: #A020F0\">'%'<\/span>])\r\n<span style=\"color: #0000FF\">end<\/span><\/pre><pre style=\"font-style:oblique\">Highest score: 3172\r\n \r\n16   0.2%\r\n32   6.8%\r\n64   34.5%\r\n128   49.7%\r\n256   8.8%\r\n<\/pre><p><b>Submit your algorithm!<\/b><\/p>\r\n   <p>So here's a challenge for you. <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5243#respond\">Submit<\/a> your algorithm and win some MathWorks swag! I will give prizes for each of the following criteria:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Highest score - In the example above, <b>3172<\/b>.\r\n         <\/li>\r\n         <li>Highest block OR in the case of a tie for \"Highest block\", the one with the highest percentage for the highest block. In the\r\n            example above, <b>8.8% for 256<\/b>.\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p><i>It was brought to my attention that since the simulator I've included with the File Exchange entry would stop the game once\r\n         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\r\n         continue until no more blocks can move.<\/i><\/p>\r\n   <p>If your algorithm is fairly short, please submit the algorithm through the <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=5243#respond\">comments<\/a> for this blog post. Be sure to use code formatting. For lengthy algorithms or multi-function algorithms, feel free to <a href=\"mailto:jdoke@mathworks.com?subject=2048%20Algorithm\">email<\/a> them to me (as ASCII text).\r\n   <\/p>\r\n   <p>In the next blog post I write, I will go over some of the winning algorithms you have submitted.<\/p>\r\n   <p>Enjoy!!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6d6624e9a5424f519d2c184aa64e0134() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='6d6624e9a5424f519d2c184aa64e0134 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6d6624e9a5424f519d2c184aa64e0134';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = '';\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_6d6624e9a5424f519d2c184aa64e0134()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p>\r\n<\/div>\r\n<!--\r\n6d6624e9a5424f519d2c184aa64e0134 ##### SOURCE BEGIN #####\r\n%%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/15007\r\n% Jiro>'s pick this week is\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/46124-2048-matlab-edition 2048 MATLAB>.\r\n%\r\n% *\"2048\"*\r\n%\r\n% Need I say more? I'm certain that many of you know what I'm talking\r\n% about. <http:\/\/gabrielecirulli.github.io\/2048\/ 2048> is an online and\r\n% mobile game created by Gabriele Cirulli recently. All I can say is that\r\n% it is an addictive game that has already resulted in a few\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/index?term=2048+game\r\n% MATLAB implementations>.\r\n%\r\n% A couple of weekends ago, I also spent a day implementing a MATLAB\r\n% version. I probably spent more time than I needed to on trying to make it\r\n% look as close as possible to the original version, but it was a lot of\r\n% fun.\r\n%\r\n% <<2048Video.gif>>\r\n%\r\n% But then, one of my colleagues,\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/answers\/contributors\/2708336\r\n% Seth> (who was also just\r\n% <https:\/\/blogs.mathworks.com\/pick\/2014\/03\/28\/solve-it-and-solve-it-right\/\r\n% featured> in this blog last week), suggested including a mechanism for\r\n% feeding in different algorithms to solve the game. There must be an AI\r\n% algorithm that would solve the game or produce a high score consistently.\r\n%\r\n% In the app, I've included the ability to select a MATLAB function as the\r\n% solving algorithm. Then you can see the algorithm in action as it tries\r\n% to solve the game. The algorithm must be defined as a MATLAB function\r\n% that takes in a 4x4 matrix representing the game board (NaN for empty\r\n% spots) and returns a character array representing the direction to move\r\n% ('up', 'down', 'right', or 'left'). Here's an example of a simple\r\n% algorithm that randomly chooses a direction.\r\n%\r\n%   function direction = myAI(board)\r\n%     d = {'up', 'down', 'right', 'left'};\r\n%     direction = d{randi(4)};\r\n%\r\n% I've also included a command line simulator, which you can use to do a\r\n% montecarlo simulation of a particular algorithm.\r\n%\r\n%   game = Game2048Simulator(@myAI);\r\n%   simulate(game, 1000)   % 1000 simulations\r\n%   viewResult(game, 25)   % 25 histogram bins\r\n%\r\n% <<result_histogram.png>>\r\n%\r\n% We can display the highest score and compute what percentages of the\r\n% simulation resulted in each of the highest block.\r\n\r\n% Display highest score\r\ndisp(['Highest score: ', num2str(max(game.Result.Score))])\r\ndisp(' ')\r\n\r\n% Get the unique values for high blocks\r\nhighblocks = game.Result.HighestBlock;\r\nblocks = unique(highblocks);\r\n\r\n% Compute the percentage of occurrance for each high block value\r\nnSims = length(highblocks);\r\nfor id = 1:length(blocks)\r\n    percentage = nnz(highblocks == blocks(id)) \/ nSims;\r\n    disp([num2str(blocks(id)), '   ', num2str(percentage*100), '%'])\r\nend\r\n\r\n%%\r\n% *Submit your algorithm!*\r\n%\r\n% So here's a challenge for you.\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=5243#respond Submit> your algorithm\r\n% and win some MathWorks swag! I will give prizes for each of the following\r\n% criteria:\r\n%\r\n% * Highest score - In the example above, *3172*.\r\n% * Highest block OR in the case of a tie for \"Highest block\", the one with\r\n% the highest percentage for the highest block. In the example above, *8.8%\r\n% for 256*.\r\n%\r\n% _It was brought to my attention that since the simulator I've included\r\n% with the File Exchange entry would stop the game once you reach 2048, I\r\n% am limiting the highest score you could get. For my next update, I will\r\n% change it so that the game will continue until no more blocks can move._\r\n%\r\n% If your algorithm is fairly short, please submit the algorithm through\r\n% the <https:\/\/blogs.mathworks.com\/pick\/?p=5243#respond comments> for this\r\n% blog post. Be sure to use code formatting. For lengthy algorithms or\r\n% multi-function algorithms, feel free to\r\n% <mailto:jdoke@mathworks.com?subject=2048%20Algorithm email> them to me\r\n% (as ASCII text).\r\n%\r\n% In the next blog post I write, I will go over some of the winning\r\n% algorithms you have submitted.\r\n%\r\n% Enjoy!!\r\n\r\n##### SOURCE END ##### 6d6624e9a5424f519d2c184aa64e0134\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/pick\/jiro\/potw_2048MATLAB\/2048Video.gif\" onError=\"this.style.display ='none';\" \/><\/div><p>\r\n   Jiro's pick this week is 2048 MATLAB.\r\n   \r\n   \"2048\"\r\n   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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2014\/04\/04\/submit-your-algorithms-to-solve-2048\/\">read more >><\/a><\/p>","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5243"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=5243"}],"version-history":[{"count":7,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5243\/revisions"}],"predecessor-version":[{"id":5250,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/5243\/revisions\/5250"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=5243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=5243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=5243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}