Guest User

2048

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