/2048-bot

A bot that plays the 2048 game for you

Primary LanguageJavaScript

2048 Bot

Bot that plays the popular 2048 game for you.

The strategy is as follows: we calculate a score for every direction except up; this is the score that you would get moving left/right/down added to the highest scoring direction from your new position. We exclude the upwards direction in order to force all the high value blocks to remain on the bottom row. We then compare these scores and move in the direction which has the highest score. In the case where moving down has the same score as left/right, we move down to ensure that high value blocks remain on the bottom row. In the case of a tie going left and right, we simply choose a direction at random. In the case where the only move is up, we make sure to move down the very next turn.

It has beat the game before, though not consistantly.

GameWin

How To Use

  1. Open a web browser and go to: http://2048game.com/ or https://play2048.co/
  2. Press "F12" and go to "Console"
  3. Copy and Pase the following text, then press enter.
function calcVert(e){for(var t=0,i=0;i<4;i++)0!=e[0][i]&&(e[1][i]==e[0][i]?t+=e[0][i]:0==e[1][i]&&e[2][i]==e[0][i]?t+=e[0][i]:0==e[1][i]&&0==e[2][i]&&e[3][i]==e[0][i]&&(t+=e[0][i])),0!=e[1][i]&&(e[2][i]==e[1][i]?t+=e[1][i]:0==e[2][i]&&e[3][i]==e[1][i]&&(t+=e[1][i])),0!=e[2][i]&&e[3][i]==e[2][i]&&(t+=e[2][i]);return t}function calcHori(e){for(var t=0,i=0;i<4;i++)0!=e[i][0]&&(e[i][1]==e[i][0]?t+=e[i][0]:0==e[i][1]&&e[i][2]==e[i][0]?t+=e[i][0]:0==e[i][1]&&0==e[i][2]&&e[i][3]==e[i][0]&&(t+=e[i][0])),0!=e[i][1]&&(e[i][2]==e[i][1]?t+=e[i][1]:0==e[i][2]&&e[i][3]==e[i][1]&&(t+=e[i][1])),0!=e[i][2]&&e[i][3]==e[i][2]&&(t+=e[i][2]);return t}function getBestNextMove(e){var t=!1,i={};return isPossible={},isPossible.down=isDifferentGrid(e,moveDown(e)),isPossible.left=isDifferentGrid(e,moveLeft(e)),isPossible.right=isDifferentGrid(e,moveRight(e)),isPossible.up=isDifferentGrid(e,moveUp(e)),isPossible.left?(predictedGrid=moveLeft(e),i.left=Math.max(calcHori(predictedGrid),calcVert(predictedGrid))+calcHori(e)):i.left=-1,isPossible.right?(predictedGrid=moveRight(e),i.right=Math.max(calcHori(predictedGrid),calcVert(predictedGrid))+calcHori(e)):i.right=-1,isPossible.down?(predictedGrid=moveDown(e),i.down=Math.max(calcHori(predictedGrid),calcVert(predictedGrid))+calcVert(e)):i.down=-1,t&&isPossible.down?(t=!1,"down"):isPossible.down||isPossible.right||isPossible.left||!isPossible.up?i.down>=i.right&&i.down>=i.left&&isPossible.down?"down":i.right==i.left&&isPossible.left&&isPossible.right?0==Math.floor(2*Math.random())?"right":"left":i.right==i.left&&isPossible.left?"left":i.right>=i.left&&isPossible.right?"right":isPossible.left?"left":void 0:"up"}function isDifferentGrid(e,t){for(var i=0;i<4;i++)for(var r=0;r<4;r++)if(e[i][r]!=t[i][r])return!0;return!1}function moveLeft(e){for(var t=[],i=0;i<4;i++)t[i]=e[i].slice();for(var r=0;r<3;r++)for(i=0;i<4;i++)for(var o=0;o<3;o++)0==t[i][o]&&(t[i][o]=t[i][o+1],t[i][o+1]=0),2==r&&t[i][o]==t[i][o+1]&&(t[i][o]*=2,t[i][o+1]=0);return t}function moveRight(e){for(var t=[],i=0;i<4;i++)t[i]=e[i].slice();for(var r=0;r<3;r++)for(i=0;i<4;i++)for(var o=3;o>0;o--)0==t[i][o]&&(t[i][o]=t[i][o-1],t[i][o-1]=0),2==r&&t[i][o]==t[i][o-1]&&(t[i][o]*=2,0,t[i][o-1]=0);return t}function moveUp(e){for(var t=[],i=0;i<4;i++)t[i]=e[i].slice();for(var r=0;r<3;r++)for(i=0;i<4;i++)for(var o=0;o<3;o++)0==t[o][i]&&(t[o][i]=t[o+1][i],t[o+1][i]=0),2==r&&t[o][i]==t[o+1][i]&&(t[o][i]*=2,0,t[o+1][i]=0);return t}function moveDown(e){for(var t=[],i=0;i<4;i++)t[i]=e[i].slice();for(var r=0;r<3;r++)for(i=0;i<4;i++)for(var o=3;o>0;o--)0==t[o][i]&&(t[o][i]=t[o-1][i],t[o-1][i]=0),2==r&&t[o][i]==t[o-1][i]&&(t[o][i]*=2,0,t[o-1][i]=0);return t}function getGridInfo(){return[[getTileInfo(1,1),getTileInfo(2,1),getTileInfo(3,1),getTileInfo(4,1)],[getTileInfo(1,2),getTileInfo(2,2),getTileInfo(3,2),getTileInfo(4,2)],[getTileInfo(1,3),getTileInfo(2,3),getTileInfo(3,3),getTileInfo(4,3)],[getTileInfo(1,4),getTileInfo(2,4),getTileInfo(3,4),getTileInfo(4,4)]]}function getTileInfo(e,t){var i="tile-position-"+e+"-"+t,r=document.body.getElementsByClassName("container")[0].getElementsByClassName("game-container")[0].getElementsByClassName("tile-container")[0];try{return parseInt(r.getElementsByClassName(i+" tile-merged")[0].getElementsByClassName("tile-inner")[0].innerText)}catch(e){try{return parseInt(r.getElementsByClassName(i)[0].getElementsByClassName("tile-inner")[0].innerText)}catch(e){return 0}}}function pressKey(e){Podium={};var t=document.createEvent("KeyboardEvent");Object.defineProperty(t,"keyCode",{get:function(){return this.keyCodeVal}}),Object.defineProperty(t,"which",{get:function(){return this.keyCodeVal}}),t.initKeyboardEvent?t.initKeyboardEvent("keydown",!0,!0,document.defaultView,e,e,"","",!1,""):t.initKeyEvent("keydown",!0,!0,document.defaultView,!1,!1,!1,!1,e,0),t.keyCodeVal=e,document.body.dispatchEvent(t)}setInterval(function(){var e=getBestNextMove(getGridInfo());"left"==e?pressKey(37):"up"==e?pressKey(38):"right"==e?pressKey(39):"down"==e&&pressKey(40)},250);

Created October 2017 by Daniel Apushkinsky (Last Updated December 2019)