smoothretro82

2048 game script

Nov 17th, 2025
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.45 KB | None | 0 0
  1. a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  2. b = []
  3. c = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  4. d = 0
  5. f = 0
  6. g = 0
  7. h = 0
  8. function newgame() {
  9. c = a
  10. a = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
  11. newtile()
  12. newtile()
  13. }
  14. function testtiles() {
  15. a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
  16. }
  17. function swapitems(list, item1, item2) {
  18. b = [];
  19. for (let i = 0; i < list.length; i++) {
  20. if (i == item1) {
  21. b = b.concat([list[item2]])
  22. } else {
  23. if (i == item2) {
  24. b = b.concat([list[item1]])
  25. } else {
  26. b = b.concat([list[i]])
  27. }
  28. }
  29. }
  30. return b;
  31. }
  32. function move() {
  33. for (let j = 0; j < 4; j++) {
  34. if (a[4*j+2] == 0 && a[4*j+3] !== 0) {
  35. a = swapitems(a, 4*j+2, 4*j+3)
  36. }
  37. if (a[4*j+1] == 0 && a[4*j+2] !== 0) {
  38. a = swapitems(a, 4*j+1, 4*j+2)
  39. }
  40. if (a[4*j+0] == 0 && a[4*j+1] !== 0) {
  41. a = swapitems(a, 4*j+0, 4*j+1)
  42. }
  43. if (a[4*j+2] == 0 && a[4*j+3] !== 0) {
  44. a = swapitems(a, 4*j+2, 4*j+3)
  45. }
  46. if (a[4*j+1] == 0 && a[4*j+2] !== 0) {
  47. a = swapitems(a, 4*j+1, 4*j+2)
  48. }
  49. if (a[4*j+2] == 0 && a[4*j+3] !== 0) {
  50. a = swapitems(a, 4*j+2, 4*j+3)
  51. }
  52. }
  53. }
  54. function merge() {
  55. for (let j = 0; j < 4; j++) {
  56. if(a[4*j+0] == a[4*j+1] && a[4*j+0] !== 0) {
  57. a.splice(4*j+0, 1, a[4*j+0]+1)
  58. a.splice(4*j+1, 1, 0)
  59. }
  60. if(a[4*j+2] == a[4*j+3] && a[4*j+2] !== 0) {
  61. a.splice(4*j+2, 1, a[4*j+2]+1)
  62. a.splice(4*j+3, 1, 0)
  63. }
  64. if(a[4*j+1] == a[4*j+2] && a[4*j+1] !== 0) {
  65. a.splice(4*j+1, 1, a[4*j+1]+1)
  66. a.splice(4*j+2, 1, 0)
  67. }
  68. }
  69. }
  70. function rotate() {
  71. a = [].concat(a[12]).concat(a[8]).concat(a[4]).concat(a[0]).concat(a[13]).concat(a[9]).concat(a[5]).concat(a[1]).concat(a[14]).concat(a[10]).concat(a[6]).concat(a[2]).concat(a[15]).concat(a[11]).concat(a[7]).concat(a[3])
  72. }
  73. function newtile() {
  74. if (a.includes(0)) {
  75. d = Math.floor(Math.random()*16)
  76. while (a[d] !== 0) {
  77. d = Math.floor(Math.random()*16)
  78. }
  79. if (Math.random() > 0.1) {a.splice(d, 1, 1)} else {a.splice(d, 1, 2)}
  80. }
  81. }
  82. function moveA() {
  83. c = a
  84. move()
  85. merge()
  86. move()
  87. if (a.includes(0) && a !== c) {
  88. newtile()
  89. }
  90. }
  91. function moveW() {
  92. c = a
  93. rotate()
  94. rotate()
  95. rotate()
  96. move()
  97. merge()
  98. move()
  99. rotate()
  100. if (a.includes(0) && a !== c) {
  101. newtile()
  102. }
  103. }
  104. function moveD() {
  105. c = a
  106. rotate()
  107. rotate()
  108. move()
  109. merge()
  110. move()
  111. rotate()
  112. rotate()
  113. if (a.includes(0) && a !== c) {
  114. newtile()
  115. }
  116. }
  117. function moveS() {
  118. c = a
  119. rotate()
  120. move()
  121. merge()
  122. move()
  123. rotate()
  124. rotate()
  125. rotate()
  126. if (a.includes(0) && a !== c) {
  127. newtile()
  128. }
  129. }
  130. newtile()
  131. newtile()
  132.  
  133. w.on("msg", (data) => {
  134. if (data.msg == "move left") {
  135. moveA()
  136. }
  137. if (data.msg == "move up") {
  138. moveW()
  139. }
  140. if (data.msg == "move right") {
  141. moveD()
  142. }
  143. if (data.msg == "move down") {
  144. moveS()
  145. }
  146. if (data.msg == "new game") {
  147. newgame()
  148. }
  149. });
  150. w.on("pong", (data) => {
  151. f = cursor.x
  152. g = cursor.start
  153. h = cursor.y
  154. for (let i = 0; i < 13; i++) {
  155. for (let j = 0; j < 13; j++) {
  156. cursor.x = j - 6
  157. cursor.y = i + 9
  158. writeChar(".", 0)
  159. }
  160. }
  161. for (let dx = 0; dx < 4; dx++) {
  162. for (let dy = 0; dy < 4; dy++) {
  163. cursor.x = dy*3-5
  164. cursor.y = dx*3+10
  165. writeChar(" 12482222"[a[dx*4+dy]], 0)
  166. }
  167. }
  168. for (let dx = 0; dx < 4; dx++) {
  169. for (let dy = 0; dy < 4; dy++) {
  170. cursor.x = dy*3-4
  171. cursor.y = dx*3+10
  172. writeChar(" 1250001^^^^"[a[dx*4+dy]], 0)
  173. }
  174. }
  175. for (let dx = 0; dx < 4; dx++) {
  176. for (let dy = 0; dy < 4; dy++) {
  177. cursor.x = dy*3-5
  178. cursor.y = dx*3+11
  179. writeChar(" 13625124991111"[a[dx*4+dy]], 0)
  180. }
  181. }
  182. for (let dx = 0; dx < 4; dx++) {
  183. for (let dy = 0; dy < 4; dy++) {
  184. cursor.x = dy*3-4
  185. cursor.y = dx*3+11
  186. writeChar(" 24862486248624567"[a[dx*4+dy]], 0)
  187. }
  188. }
  189. cursor.x = -6
  190. cursor.y = 7
  191. writeChar("2", 1)
  192. writeChar("0", 1)
  193. writeChar("4", 1)
  194. writeChar("8", 1)
  195. cursor.x = 10
  196. cursor.y = 14
  197. writeChar("W", 0)
  198. cursor.x = 8
  199. cursor.y = 15
  200. writeChar("A", 0)
  201. cursor.x = 10
  202. cursor.y = 16
  203. writeChar("S", 0)
  204. cursor.x = 12
  205. cursor.y = 15
  206. writeChar("D", 0)
  207. cursor.x = 10
  208. cursor.y = 18
  209. writeChar("+", 0)
  210. cursor.x = f
  211. cursor.start = g
  212. cursor.y = h
  213. });
  214. w.on("cursor", (data) => {
  215. if (data.l[0] ==10 && data.l[1] == 14) {
  216. moveW()
  217. }
  218. if (data.l[0] == 8 && data.l[1] == 15) {
  219. moveA()
  220. }
  221. if (data.l[0] == 10 && data.l[1] == 16) {
  222. moveS()
  223. }
  224. if (data.l[0] == 12 && data.l[1] == 15) {
  225. moveD()
  226. }
  227. if (data.l[0] == 10 && data.l[1] == 18) {
  228. newgame()
  229. }
  230. });
  231. w.chat.send("2048 bot initiated. say move direction to move tiles. say new game to make a new game. or maybe you can use the canvas keys if you don't wanna chat.", 0)
Advertisement
Add Comment
Please, Sign In to add comment