Advertisement
30000legos

Untitled

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