Advertisement
RobotBubble

2048

Oct 24th, 2017
199
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- 2048
  2. -- Clone by Richard (Rich73)
  3. -- Hacked together 10/04/2014
  4.  
  5. --Improved slightly by civilwargeeky
  6. --Found here: http://www.computercraft.info/forums2/index.php?/topic/18017-2048-for-pocket-computers/
  7.  
  8. g = { }
  9. tiles = { }
  10. tiles[1] = {" ", 1}
  11. tiles[2] = {" 2 ", 2}
  12. tiles[3] = {" 4 ", 4}
  13. tiles[4] = {" 8 ", 8}
  14. tiles[5] = {" 16 ", 16}
  15. tiles[6] = {" 32 ", 32}
  16. tiles[7] = {" 64 ", 64}
  17. tiles[8] = {"128 ", 128}
  18. tiles[9] = {"256 ", 256}
  19. tiles[10] = {"512 ", 512}
  20. tiles[11] = {"1024", 1024}
  21. tiles[12] = {"2048", 2048}
  22. bg = "icon"
  23. size = 4
  24. saveFile = "2048Save"
  25. score = 0
  26. hiscore = 0
  27.  
  28. if fs.exists(saveFile) then
  29. local a = fs.open(saveFile, "r")
  30. hiscore = tonumber(a.readAll())
  31. a.close()
  32. end
  33.  
  34. function createBoard()
  35. for i=1, size do
  36. g[i] = { }
  37. for j=1, size do
  38. g[i][j] = 0
  39. end
  40. end
  41. for j=1, 2 do
  42. local x, y = findEmptyPos()
  43. g[y][x] = 1
  44. end
  45. end
  46.  
  47. function getRandomPos()
  48. return math.random(size), math.random(size)
  49. end
  50.  
  51. function findEmptyPos()
  52. while true do
  53. x, y = getRandomPos()
  54. if g[y][x] == 0 then return x, y end
  55. end
  56. end
  57.  
  58. function isFull()
  59. local full = true
  60. for i=1, size do
  61. for j=1, size do
  62. if g[i][j] == 0 then full = false end
  63. end
  64. end
  65. return full
  66. end
  67.  
  68. function canMove()
  69. if not isFull() then return true end
  70. local pr = nil
  71. for i=1, size do
  72. local k = 1
  73. while k <= size do
  74. if k~=size and g[i][k] == g[i][k+1] or false then break end
  75. if pr and g[i][k] == pr[k] or false then break end
  76. k = k + 1
  77. end
  78. if k ~= size+1 then return true end
  79. pr = g[i]
  80. end
  81. return false
  82. end
  83.  
  84. function moveLeft()
  85. for i=1, size do
  86. for j=2, size do
  87. local k = j
  88. while k > 1 do
  89. if g[i][k] == 0 then break
  90. elseif g[i][k] == g[i][k-1] then
  91. g[i][k-1] = g[i][k] + 1
  92. g[i][k] = 0
  93. score = score + math.pow(2,g[i][k-1])
  94. k = k-1
  95. elseif g[i][k-1] == 0 then
  96. g[i][k-1] = g[i][k]
  97. g[i][k] = 0
  98. else break end
  99. k = k-1
  100. end
  101. end
  102. end
  103. end
  104.  
  105. function moveUp()
  106. for j=1, size do
  107. for i=2, size do
  108. local k = i
  109. while k > 1 do
  110. if g[k][j] == 0 then break
  111. elseif g[k][j] == g[k-1][j] then
  112. g[k-1][j] = g[k][j] + 1
  113. g[k][j] = 0
  114. score = score + math.pow(2,g[k-1][j])
  115. k = k-1
  116. elseif g[k-1][j] == 0 then
  117. g[k-1][j] = g[k][j]
  118. g[k][j] = 0
  119. else break end
  120. k = k-1
  121. end
  122. end
  123. end
  124. end
  125.  
  126. function moveRight()
  127. flipX()
  128. moveLeft()
  129. flipX()
  130. end
  131.  
  132. function moveDown()
  133. flipY()
  134. moveUp()
  135. flipY()
  136. end
  137.  
  138. function drawBoard()
  139. term.setBackgroundColor(colors.black)
  140. term.clear()
  141. term.setTextColor(colors.white)
  142. for x=1, size do
  143. for y=1, size do
  144. term.setCursorPos(x*5-1,y*4-2)
  145. term.setBackgroundColor(tiles[g[y][x]+1][2])
  146. term.write(" ")
  147. term.setCursorPos(x*5-1,y*4-1)
  148. term.write(tiles[g[y][x]+1][1])
  149. term.setCursorPos(x*5-1,y*4)
  150. term.write(" ")
  151. end
  152. end
  153. term.setCursorPos(4,18)
  154. term.setBackgroundColor(colors.black)
  155. term.write("Score: "..score)
  156. drawScores()
  157. end
  158.  
  159. function drawScores()
  160. term.setCursorPos(4,18)
  161. term.setBackgroundColor(colors.black)
  162. term.write(" Score: "..score)
  163. term.setCursorPos(4,19)
  164. term.write("HiScore: "..hiscore)
  165. term.setCursorPos(4,20)
  166. term.setTextColor(colors.lightGray)
  167. term.write("Press q to quit")
  168. end
  169.  
  170. function drawHome()
  171. if not fs.exists(bg) then shell.run("pastebin get AWKwMWXW "..bg) end
  172. term.clear()
  173. im = paintutils.loadImage(bg)
  174. paintutils.drawImage(im,2,2)
  175. end
  176.  
  177. function table.reverse(tab)
  178. local newtab = { }
  179. for i=1, #tab do
  180. newtab[#tab+1-i] = tab[i]
  181. end
  182. return newtab
  183. end
  184.  
  185. function flipX()
  186. for i=1, size do
  187. g[i] = table.reverse(g[i])
  188. end
  189. end
  190.  
  191. function flipY()
  192. g = table.reverse(g)
  193. end
  194.  
  195. function update()
  196. drawBoard()
  197. if not isFull() then
  198. local x, y = findEmptyPos()
  199. g[y][x] = 1
  200. end
  201. os.sleep(0.075)
  202. end
  203.  
  204. function saveScore()
  205. handle = fs.open(saveFile, "w")
  206. handle.write(tostring(hiscore))
  207. handle.close()
  208. end
  209.  
  210.  
  211. function newGame()
  212. if score > hiscore then
  213. hiscore = score
  214. saveScore()
  215. end
  216. score = 0
  217. term.setCursorPos(9,18)
  218. term.setBackgroundColor(colors.white)
  219. term.setTextColor(colors.black)
  220. term.clearLine()
  221. term.write("GAME OVER!")
  222. term.setCursorPos(8,19)
  223. term.clearLine()
  224. sleep(3)
  225. os.reboot()
  226. end
  227.  
  228. drawHome()
  229.  
  230. os.sleep(2)
  231. while true do
  232. createBoard()
  233. while canMove() do
  234. drawBoard()
  235. event, key = os.pullEvent("key")
  236. if event == "key" then
  237. if key == keys.left then moveLeft()
  238. elseif key == keys.right then
  239. moveRight()
  240. update()
  241. elseif key == keys.up then
  242. moveUp()
  243. update()
  244. elseif key == keys.down then
  245. moveDown()
  246. update()
  247. elseif key == keys.q then
  248. break
  249. end
  250. end
  251. end
  252. drawBoard()
  253. if not newGame() then
  254. term.setBackgroundColor(colors.black)
  255. term.setTextColor(colors.white)
  256. term.clear()
  257. term.setCursorPos(1,1)
  258. break
  259. end
  260. end
Advertisement
RAW Paste Data Copied
Advertisement