Advertisement
CaptainSpaceCat

2048 - Lua Edition

Jul 14th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.69 KB | None | 0 0
  1. local maxNum = 2
  2. local score = 0
  3. local won, showWin = false, true
  4. local w, h = term.getSize()
  5. local midW, midH = math.floor(w/2), math.floor(h/2)
  6. local numtab = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}
  7. local colTab = {
  8.     [0] = colors.black,
  9.     [2] = colors.white,
  10.     [4] = colors.white,
  11.     [8] = colors.orange,
  12.     [16] = colors.orange,
  13.     [32] = colors.red,
  14.     [64] = colors.red,
  15.     [128] = colors.yellow,
  16.     [256] = colors.yellow,
  17.     [512] = colors.yellow,
  18.     [1024] = colors.yellow,
  19.     [2048] = colors.yellow,
  20.     [4096] = colors.green,
  21.     [8192] = colors.purple
  22. }
  23. local glitchWorkaround = {
  24.     [0] = " ",
  25.     [2] = "2",
  26.     [4] = "4",
  27.     [8] = "8",
  28.     [16] = "16",
  29.     [32] = "32",
  30.     [64] = "64",
  31.     [128] = "128",
  32.     [256] = "256",
  33.     [512] = "512",
  34.     [1024] = "1024",
  35.     [2048] = "2048",
  36.     [4096] = "4096",
  37.     [8192] = "8192"
  38. }
  39.  
  40. local function initialTiles()
  41.     local y, x
  42.     for i = 1, 2 do
  43.         repeat
  44.             x = math.random(1, 4)
  45.             y = math.random(1, 4)
  46.         until numtab[y][x] == 0
  47.         numtab[y][x] = 2
  48.     end
  49. end
  50.  
  51. local function spawnTile(tileNum)
  52.     local availableSpots = {}
  53.     for y = 1, 4 do
  54.         for x = 1, 4 do
  55.             if numtab[y][x] == 0 then
  56.                 availableSpots[#availableSpots + 1] = {y, x}
  57.             end
  58.         end
  59.     end
  60.     local choice = availableSpots[math.random(1, #availableSpots)]
  61.     numtab[choice[1]][choice[2]] = tileNum
  62. end
  63.  
  64. local function eventHandler(events)
  65.     if events[1] == "key" then
  66.         local changed = false
  67.         if events[2] == keys.up then
  68.             for x = 1, 4 do
  69.                 local moveMode = false
  70.                 for y = 2, 4 do
  71.                     for i = 0, y - 2 do
  72.                         if numtab[y - i][x] == 0 then
  73.                         elseif numtab[y - 1 - i][x] == 0 then
  74.                             numtab[y - 1 - i][x] = numtab[y - i][x]
  75.                             numtab[y - i][x] = 0
  76.                             changed = true
  77.                         elseif not moveMode and numtab[y - 1 - i][x] == numtab[y - i][x] then
  78.                             numtab[y - 1 - i][x] = numtab[y - i][x] * 2
  79.                             score = score + numtab[y - i][x] * 2
  80.                             numtab[y - i][x] = 0
  81.                             changed = true
  82.                             moveMode = true
  83.                             break
  84.                         else moveMode = false; break end
  85.                     end
  86.                 end
  87.             end
  88.         elseif events[2] == keys.right then
  89.             for y = 1, 4 do
  90.                 local moveMode = false
  91.                 for x = 3, 1, -1 do
  92.                     for i = 0, 3 - x do
  93.                         if numtab[y][x + i] == 0 then
  94.                         elseif numtab[y][x + 1 + i] == 0 then
  95.                             numtab[y][x + 1 + i] = numtab[y][x + i]
  96.                             numtab[y][x + i] = 0
  97.                             changed = true
  98.                         elseif not moveMode and numtab[y][x + 1 + i] == numtab[y][x + i] then
  99.                             numtab[y][x + 1 + i] = numtab[y][x + i] * 2
  100.                             score = score + numtab[y][x + i] * 2
  101.                             numtab[y][x + i] = 0
  102.                             changed = true
  103.                             moveMode = true
  104.                             break
  105.                         else moveMode = false; break end
  106.                     end
  107.                 end
  108.             end
  109.         elseif events[2] == keys.down then
  110.             for x = 1, 4 do
  111.                 local moveMode = false
  112.                 for y = 3, 1, -1 do
  113.                     for i = 0, 3 - y do
  114.                         if numtab[y + i][x] == 0 then
  115.                         elseif numtab[y + 1 + i][x] == 0 then
  116.                             numtab[y + 1 + i][x] = numtab[y + i][x]
  117.                             numtab[y + i][x] = 0
  118.                             changed = true
  119.                         elseif not moveMode and numtab[y + 1 + i][x] == numtab[y + i][x] then
  120.                             numtab[y + 1 + i][x] = numtab[y + i][x] * 2
  121.                             score = score + numtab[y + i][x] * 2
  122.                             numtab[y + i][x] = 0
  123.                             changed = true
  124.                             moveMode = true
  125.                             break
  126.                         else moveMode = false; break end
  127.                     end
  128.                 end
  129.             end
  130.         elseif events[2] == keys.left then
  131.             for y = 1, 4 do
  132.                 local moveMode = false
  133.                 for x = 2, 4 do
  134.                     for i = 0, x - 2 do
  135.                         if numtab[y][x - i] == 0 then
  136.                         elseif numtab[y][x - 1 - i] == 0 then
  137.                             numtab[y][x - 1 - i] = numtab[y][x - i]
  138.                             numtab[y][x - i] = 0
  139.                             changed = true
  140.                         elseif not moveMode and numtab[y][x - 1 - i] == numtab[y][x - i] then
  141.                             numtab[y][x - 1 - i] = numtab[y][x - i] * 2
  142.                             score = score + numtab[y][x - i] * 2
  143.                             numtab[y][x - i] = 0
  144.                             changed = true
  145.                             moveMode = true
  146.                             break
  147.                         else moveMode = false; break end
  148.                     end
  149.                 end
  150.             end
  151.         end
  152.         if changed then
  153.             local c = math.random(1, 10)
  154.             if c == 10 then spawnTile(4) else spawnTile(2) end
  155.         end
  156.     end
  157. end
  158.  
  159. local function checkState()
  160.     for y = 1, 4 do
  161.         for x = 1, 4 do
  162.             if numtab[y][x] > maxNum then maxNum = numtab[y][x] end
  163.             if numtab[y][x] == 2048 then won = true end
  164.         end
  165.     end
  166.     local flag = false
  167.     for y = 1, 4 do
  168.         for x = 1, 4 do
  169.             if numtab[y][x] == 0 then
  170.                 flag = true
  171.             elseif y > 1 and numtab[y - 1][x] == numtab[y][x] then
  172.                 flag = true
  173.             elseif y < 4 and numtab[y + 1][x] == numtab[y][x] then
  174.                 flag = true
  175.             elseif x > 1 and numtab[y][x - 1] == numtab[y][x] then
  176.                 flag = true
  177.             elseif x < 4 and numtab[y][x + 1] == numtab[y][x] then
  178.                 flag = true
  179.             end
  180.             if flag then break end
  181.         end
  182.     end
  183.     if not flag then return false end
  184.     return true
  185. end
  186.  
  187. local function printTiles()
  188.     if maxNum == 16384 then
  189.         term.setBackgroundColor(colors.black)
  190.         term.clear()
  191.         term.setCursorPos(1, 1)
  192.         error("ERROR: User is too f*cking good at this... failure to parse awesomeness level...")
  193.     end
  194.     term.setBackgroundColor(colTab[maxNum])
  195.     term.clear()
  196.     paintutils.drawFilledBox(midW - 7, midH - 8, midW + 10, midH + 14, colors.gray)
  197.     for y = 1, 4 do
  198.         for x = 1, 4 do
  199.             paintutils.drawFilledBox(midW - 10 + x*4, midH - 11 + y*4, midW - 11 + (x+1)*4, midH - 12 + (y+1)*4, colTab[numtab[y][x]])
  200.             term.setCursorPos(midW - 10 + x*4, midH - 10 + y*4)
  201.             if numtab[y][x] == 2 or numtab[y][x] == 4 then
  202.                 term.setTextColor(colors.black)
  203.             else term.setTextColor(colors.white) end
  204.             term.write(glitchWorkaround[numtab[y][x]])
  205.         end
  206.     end
  207.     term.setBackgroundColor(colTab[maxNum])
  208.     term.setTextColor(colTab[maxNum] == colors.white and colors.black or colors.white)
  209.     term.setCursorPos(w - 4, 1)
  210.     term.write("Score")
  211.     term.setCursorPos(w - #tostring(score) + 1, 2)
  212.     term.write(score)
  213. end
  214.  
  215. initialTiles()
  216. local gameState = true
  217. while gameState do
  218.     if won and showWin then
  219.         printTiles()
  220.         term.setBackgroundColor(colors.yellow)
  221.         term.setTextColor(colors.white)
  222.         for i = 0, 1 do
  223.             term.setCursorPos(1, midH + i)
  224.             term.clearLine()
  225.         end
  226.         term.setCursorPos(midW, midH)
  227.         term.write("2048")
  228.         term.setCursorPos(midW - 2, midH + 1)
  229.         term.write("Victory!")
  230.         sleep(.5)
  231.         coroutine.yield("key")
  232.         showWin = false
  233.     end
  234.     printTiles()
  235.     local events = {os.pullEvent()}
  236.     eventHandler(events)
  237.     gameState = checkState()
  238. end
  239. printTiles()
  240. term.setBackgroundColor(colors.red)
  241. term.setTextColor(colors.white)
  242. for i = 0, 1 do
  243.     term.setCursorPos(1, midH + i)
  244.     term.clearLine()
  245. end
  246. term.setCursorPos(midW - #tostring(score)/2 + 2, midH)
  247. term.write(score)
  248. term.setCursorPos(midW - 2, midH + 1)
  249. term.write("You Lose")
  250. sleep(.5)
  251. coroutine.yield("key")
  252. term.setBackgroundColor(colors.black)
  253. term.clear()
  254. term.setCursorPos(1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement