TheKojent

2048 Dummy

Mar 21st, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. --[[
  2.  
  3. 11  12  13  14
  4.  
  5. 21  22  23  24
  6.  
  7. 31  32  33  34
  8.  
  9. 41  42  43  44
  10.  
  11. ]]--
  12.  
  13. local win = false
  14.  
  15. a = {}
  16. for i=1,4 do
  17.     a[i] = {}
  18.     for j=1,4 do
  19.         a[i][j] = 0
  20.     end
  21. end
  22.  
  23. function determinant()
  24.     a[2][1] = a[1][2]
  25.     a[3][1] = a[1][3]
  26.     a[4][1] = a[1][4]
  27.     a[3][2] = a[2][3]
  28.     a[4][2] = a[2][4]
  29.     a[4][3] = a[3][4]
  30. end
  31.  
  32. function update()
  33.     --insert code
  34. end
  35.  
  36. function reset()
  37.     for i = 1,4 do
  38.         for j = 1,4 do
  39.             a[i][j] = 0
  40.         end
  41.     end
  42.     update()
  43. end
  44.  
  45. function newNum()
  46.     local blank = 0
  47.     for i = 1,4 do
  48.         for j = 1,4 do
  49.             if a[i][j] == 0 then
  50.                 blank = blank + 1
  51.             end
  52.         end
  53.     end
  54.     if blank == 0 then
  55.         return false
  56.     else
  57.         while true do
  58.             --insert random selection of x,y
  59.             if a[x][y] == 0 then
  60.                 a[x][y] = 2
  61.                 break
  62.             end
  63.         end
  64.     end
  65. end
  66.  
  67. function score(val)
  68.     --insert code for adding score
  69.     for i = 1,4 do
  70.         for j = 1,4 do
  71.             if a[i][j] = 2048 then
  72.                 win = true
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. function endScreen()
  79.     --insert code here
  80. end
  81.  
  82.  
  83.  
  84. --main body
  85.  
  86. while true do
  87.     update()
  88.     while true do
  89.         event, key = os.pullEvent("key")
  90.         if (key == "w" or key == "a" or key == "s" or key == "d" or key == "r") then
  91.             break
  92.         end
  93.     end
  94.     if key == "r" then
  95.         reset()
  96.     elseif key == "w" or key == "s" then
  97.         determinant()
  98.     end
  99.     if key == "w" or key == "a" then
  100.         for i = 1,4 do
  101.             for j = 1,3 do
  102.                 if a[i][5-j] == a[i][4-j] then
  103.                     a[i][4-j] = a[i][5-j] + a[i][4-j]
  104.                     score(a[i][4-j])
  105.                 elseif a[i][4-j] == 0 then
  106.                     a[i][4-j] = a[i][5-j]
  107.                 end
  108.                 a[i][5-j] = 0
  109.             end
  110.             if a[i][2] == 0 and a[i][3] > 0 then
  111.                 a[i][2] = a[i][3]
  112.                 a[i][3] = 0
  113.             end
  114.         end
  115.     elseif key == "s" or key == "d" then
  116.         for i = 1,4 do
  117.             for j = 1,3 do
  118.                 if a[i][j] == a[i][j+1] then
  119.                     a[i][j+1] = a[i][j] + a[i][j+1]
  120.                     score(a[i][j])
  121.                 elseif a[i][j+1] == 0 then
  122.                     a[i][j+1] = a[i][j]
  123.                 end
  124.                 a[i][j] = 0
  125.             end
  126.             if a[i][3] == 0 and a[i][2] != 0 then
  127.                 a[i][3] = a[i][2]
  128.                 a[i][2] = 0
  129.             end
  130.         end
  131.     end
  132.     if key == "w" or key == "s" then
  133.         determinant()
  134.     end
  135.     if not newNum() then
  136.         local comb = false
  137.         for b = 1,2 do
  138.             for i = 1,4 do
  139.                 for j = 1,3 do
  140.                     if a[i][j] == a[i][j+1] then
  141.                         comb = true
  142.                         break
  143.                     end
  144.                     if comb then break end
  145.                 end
  146.                 if comb then break end
  147.             end
  148.             if comb then break end
  149.             determinant()
  150.         end
  151.         endScreen()
  152.         sleep(1000)
  153.         reset()
  154.     end
  155.     if win == true then
  156.         update()
  157.         sleep(250)
  158.         endScreen()
  159.     end
  160. end
Advertisement
Add Comment
Please, Sign In to add comment