Advertisement
oliverbabish

2048 bot hack

Dec 17th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.08 KB | None | 0 0
  1. // 1. Open a web browser and go to: http://2048game.com/ or https://play2048.co/
  2. // 2. Press "F12" and go to "Console"
  3. // 3. Copy and Pase the following text, then press enter.
  4. setInterval(function(){
  5. var grid = getGridInfo();
  6. var bestMove = getBestNextMove(grid);
  7.  
  8. if (bestMove == "left"){
  9. pressKey(37);
  10. } else if (bestMove == "up"){
  11. pressKey(38);
  12. } else if (bestMove == "right"){
  13. pressKey(39);
  14. } else if (bestMove == "down"){
  15. pressKey(40);
  16. }
  17. }, 250);
  18.  
  19.  
  20. function calcVert(grid){
  21. // Calculate the vertical score (for movement up or down)
  22. var vSum = 0;
  23. for (var i = 0; i < 4; i++) {
  24. if (grid[0][i] != 0){
  25. if (grid[1][i] == grid[0][i]){
  26. vSum += grid[0][i];
  27. } else if (grid[1][i] == 0 && grid[2][i] == grid[0][i]){
  28. vSum += grid[0][i];
  29. } else if (grid[1][i] == 0 && grid[2][i] == 0 && grid[3][i] == grid[0][i]){
  30. vSum += grid[0][i];
  31. }
  32. }
  33.  
  34. if (grid[1][i] != 0){
  35. if (grid[2][i] == grid[1][i]){
  36. vSum += grid[1][i];
  37. } else if (grid[2][i] == 0 && grid[3][i] == grid[1][i]){
  38. vSum += grid[1][i];
  39. }
  40. }
  41.  
  42. if (grid[2][i] != 0){
  43. if (grid[3][i] == grid[2][i]){
  44. vSum += grid[2][i];
  45. }
  46. }
  47. }
  48. return(vSum);
  49. }
  50.  
  51.  
  52. function calcHori(grid){
  53. // Calculate the horizontal score (for movement left or right)
  54. var hSum = 0;
  55. for (var i = 0; i < 4; i++) {
  56. if (grid[i][0] != 0){
  57. if (grid[i][1] == grid[i][0]){
  58. hSum += grid[i][0];
  59. } else if (grid[i][1] == 0 && grid[i][2] == grid[i][0]){
  60. hSum += grid[i][0];
  61. } else if (grid[i][1] == 0 && grid[i][2] == 0 && grid[i][3] == grid[i][0]){
  62. hSum += grid[i][0];
  63. }
  64. }
  65.  
  66. if (grid[i][1] != 0){
  67. if (grid[i][2] == grid[i][1]){
  68. hSum += grid[i][1];
  69. } else if (grid[i][2] == 0 && grid[i][3] == grid[i][1]){
  70. hSum += grid[i][1];
  71. }
  72. }
  73.  
  74. if (grid[i][2] != 0){
  75. if (grid[i][3] == grid[i][2]){
  76. hSum += grid[i][2];
  77. }
  78. }
  79. }
  80. return(hSum);
  81. }
  82.  
  83.  
  84. function getBestNextMove(grid) {
  85. var lastWasUp = false; // to make sure that the subsequent move after going up is down
  86. var scoreFor = {}; // dictionary of directions, with each value the score/quality of the move
  87.  
  88. isPossible = {} // dictionary of directions, where the value is a boolean on whether the move is possible
  89. isPossible["down"] = isDifferentGrid(grid, moveDown(grid));
  90. isPossible["left"] = isDifferentGrid(grid, moveLeft(grid));
  91. isPossible["right"] = isDifferentGrid(grid, moveRight(grid));
  92. isPossible["up"] = isDifferentGrid(grid, moveUp(grid));
  93.  
  94. // calculate scores if the move is left
  95. if (isPossible["left"]){
  96. predictedGrid = moveLeft(grid);
  97. scoreFor["left"] = Math.max(calcHori(predictedGrid), calcVert(predictedGrid)) + calcHori(grid);
  98. } else {
  99. scoreFor["left"] = -1;
  100. }
  101.  
  102. // calculate scores if the move is right
  103. if (isPossible["right"]) {
  104. predictedGrid = moveRight(grid);
  105. scoreFor["right"] = Math.max(calcHori(predictedGrid), calcVert(predictedGrid)) + calcHori(grid);
  106. } else {
  107. scoreFor["right"] = -1;
  108. }
  109.  
  110. // calculate scores if the move is down
  111. if (isPossible["down"]){
  112. predictedGrid = moveDown(grid);
  113. scoreFor["down"] = Math.max(calcHori(predictedGrid), calcVert(predictedGrid)) + calcVert(grid);
  114. } else {
  115. scoreFor["down"] = -1;
  116. }
  117.  
  118. // scoreFor["up"] is not caclulated as we try to avoid this move at all costs, and only use it if there are no choice
  119.  
  120.  
  121. // If the last move was up, then try to move down as soon as possible. This is because our strategy is to stack all the
  122. // high value blocks at the bottom, and if the last move was up this strategy was ruined.
  123. if (lastWasUp && isPossible["down"]){
  124. lastWasUp = false;
  125. return "down";
  126. // If the only possible move is up
  127. } else if (!isPossible["down"] && !isPossible["right"] && !isPossible["left"] && isPossible["up"]){
  128. return "up";
  129.  
  130. } else if (scoreFor["down"] >= scoreFor["right"] && scoreFor["down"] >= scoreFor["left"] && isPossible["down"]){
  131. return "down";
  132.  
  133. } else if (scoreFor["right"] == scoreFor["left"] && isPossible["left"] && isPossible["right"]){
  134. // If the score for moving left and right is equal, then pick a direction randomly
  135. if (Math.floor(Math.random() * 2) == 0){
  136. return "right";
  137. } else {
  138. return "left";
  139. }
  140.  
  141. } else if (scoreFor["right"] == scoreFor["left"] && isPossible["left"]){
  142. return "left";
  143.  
  144. } else if (scoreFor["right"] >= scoreFor["left"] && isPossible["right"]) {
  145. return "right";
  146.  
  147. } else if (isPossible["left"]){
  148. return "left";
  149.  
  150. }
  151. }
  152.  
  153.  
  154. function isDifferentGrid(grid1, grid2){
  155. // Compare the given grids, and return true if they are different
  156. for (var i = 0; i < 4; i++) {
  157. for (var j = 0; j < 4; j++) {
  158. if (grid1[i][j] != grid2[i][j]){
  159. return true;
  160. }
  161. }
  162. }
  163. return false;
  164. }
  165.  
  166.  
  167. function moveLeft(grid){
  168. // Predict what the grid and score will look like if you move left in the game
  169. // Returns an array with grid and score
  170.  
  171. // Create a deep copy of the grid
  172. var newGrid = [];
  173. var score = 0;
  174. for (var i = 0; i < 4; i++)
  175. newGrid[i] = grid[i].slice();
  176.  
  177.  
  178. // Calculate what the board will look like when moving left
  179. for (var h = 0; h < 3; h++){
  180. for (var i = 0; i < 4; i++){
  181. for (var j = 0; j < 3; j++){
  182. // Shift tile over into empty space
  183. if (newGrid[i][j] == 0){
  184. newGrid[i][j] = newGrid[i][j+1]
  185. newGrid[i][j+1] = 0;
  186. }
  187.  
  188. // Combine two tiles
  189. if (h == 2 && newGrid[i][j] == newGrid[i][j+1]){
  190. newGrid[i][j] *= 2;
  191. //score ++;//= newGrid[i][j]; // Increase score by value of tile
  192. newGrid[i][j+1] = 0;
  193. }
  194. }
  195. }
  196. }
  197.  
  198. return newGrid;
  199. }
  200.  
  201.  
  202. function moveRight(grid){
  203. // Predict what the grid and score will look like if you move right in the game
  204. // Returns an array with grid and score
  205.  
  206. // Create a deep copy of the grid
  207. var newGrid = [];
  208. var score = 0;
  209. for (var i = 0; i < 4; i++)
  210. newGrid[i] = grid[i].slice();
  211.  
  212. // Calculate what the board will look like when moving right
  213. for (var h = 0; h < 3; h++){
  214. for (var i = 0; i < 4; i++){
  215. for (var j = 3; j > 0; j--){
  216. // Shift tile over into empty space
  217. if (newGrid[i][j] == 0){
  218. newGrid[i][j] = newGrid[i][j-1]
  219. newGrid[i][j-1] = 0;
  220. }
  221.  
  222. // Combine two tiles
  223. if (h == 2 && newGrid[i][j] == newGrid[i][j-1]){
  224. newGrid[i][j] *= 2;
  225. score ++;//= newGrid[i][j]; // Increase score by value of tile
  226. newGrid[i][j-1] = 0;
  227. }
  228. }
  229. }
  230. }
  231. return newGrid;
  232. }
  233.  
  234.  
  235. function moveUp(grid){
  236. // Predict what the grid and score will look like if you move up in the game
  237. // Returns an array with grid and score
  238.  
  239. // Create a deep copy of the grid
  240. var newGrid = [];
  241. var score = 0;
  242. for (var i = 0; i < 4; i++)
  243. newGrid[i] = grid[i].slice();
  244.  
  245. // Calculate what the board will look like when moving up
  246. for (var h = 0; h < 3; h++){
  247. for (var i = 0; i < 4; i++){
  248. for (var j = 0; j < 3; j++){
  249. // shift tile over into empty space
  250. if (newGrid[j][i] == 0){
  251. newGrid[j][i] = newGrid[j+1][i]
  252. newGrid[j+1][i] = 0;
  253. }
  254.  
  255. // Combine two tiles
  256. if (h == 2 && newGrid[j][i] == newGrid[j+1][i]){
  257. newGrid[j][i] *= 2;
  258. score ++;//= newGrid[j][i]; // Increase score by value of tile
  259. newGrid[j+1][i] = 0;
  260. }
  261. }
  262. }
  263. }
  264.  
  265. return newGrid;
  266. }
  267.  
  268.  
  269. function moveDown(grid){
  270. // Predict what the grid and score will look like if you move down in the game
  271. // Returns an array with grid and score
  272.  
  273. // Create a deep copy of the grid
  274. var newGrid = [];
  275. var score = 0;
  276. for (var i = 0; i < 4; i++)
  277. newGrid[i] = grid[i].slice();
  278.  
  279. // Calculate what the board will look like when moving down
  280. for (var h = 0; h < 3; h++){
  281. for (var i = 0; i < 4; i++){
  282. for (var j = 3; j > 0; j--){
  283. // shift tile over into empty space
  284. if (newGrid[j][i] == 0){
  285. newGrid[j][i] = newGrid[j-1][i]
  286. newGrid[j-1][i] = 0;
  287. }
  288.  
  289. // Combine two tiles
  290. if (h == 2 && newGrid[j][i] == newGrid[j-1][i]){
  291. newGrid[j][i] *= 2;
  292. score ++;//= newGrid[j][i]; // Increase score by value of tile
  293. newGrid[j-1][i] = 0;
  294. }
  295. }
  296. }
  297. }
  298.  
  299. return newGrid;
  300. }
  301.  
  302.  
  303. function getGridInfo(){
  304. // Return a 2D array size 4x4 containing the tile values
  305. return([[getTileInfo(1,1),getTileInfo(2,1),getTileInfo(3,1),getTileInfo(4,1)],
  306. [getTileInfo(1,2),getTileInfo(2,2),getTileInfo(3,2),getTileInfo(4,2)],
  307. [getTileInfo(1,3),getTileInfo(2,3),getTileInfo(3,3),getTileInfo(4,3)],
  308. [getTileInfo(1,4),getTileInfo(2,4),getTileInfo(3,4),getTileInfo(4,4)]
  309. ]);
  310. }
  311.  
  312.  
  313. function getTileInfo (x,y){
  314. // Get the value of the tile in the specified (x, y) position
  315. var location = "tile-position-" + x + "-" + y;
  316. var tile = document.body.getElementsByClassName("container")[0].getElementsByClassName("game-container")[0].getElementsByClassName("tile-container")[0];
  317.  
  318. // If the tile does not exist, consider the value of the spot 0
  319. try{
  320. return(parseInt(tile.getElementsByClassName(location + " tile-merged")[0].getElementsByClassName("tile-inner")[0].innerText));
  321. }
  322. catch(err){
  323. // The reason for a try-catch inside a try-catch, is because not every tile is actually called the same name.
  324. // If the tile has been merged (so 4 and above), it will have a difference name from the starter tiles (2 or 4).
  325. try{
  326. return(parseInt(tile.getElementsByClassName(location)[0].getElementsByClassName("tile-inner")[0].innerText));
  327. }
  328. catch(err){
  329. return(0);
  330. }
  331. }
  332. }
  333.  
  334.  
  335. function pressKey(codeKey){
  336. // Simulate a key press (only used on the four arrow keys in this program)
  337. Podium = {};
  338.  
  339. var oEvent = document.createEvent('KeyboardEvent');
  340.  
  341. // Chromium Hack
  342. Object.defineProperty(oEvent, 'keyCode', {
  343. get : function() {
  344. return this.keyCodeVal;
  345. }
  346. });
  347. Object.defineProperty(oEvent, 'which', {
  348. get : function() {
  349. return this.keyCodeVal;
  350. }
  351. });
  352.  
  353. if (oEvent.initKeyboardEvent) {
  354. oEvent.initKeyboardEvent("keydown", true, true, document.defaultView, codeKey, codeKey, "", "", false, "");
  355. } else {
  356. oEvent.initKeyEvent("keydown", true, true, document.defaultView, false, false, false, false, codeKey, 0);
  357. }
  358.  
  359. oEvent.keyCodeVal = codeKey;
  360.  
  361. document.body.dispatchEvent(oEvent);
  362. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement