Advertisement
Redxone

[Computercraft] 2048

May 25th, 2016
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. local running = true
  2. local cx,cy = 0, 0
  3. local drawx = 11
  4. local drawy = -2
  5. local xscale = 5
  6. local yscale = 4
  7. local cubexsize = 6
  8. local cubeysize = 4
  9. local score = 2
  10. local function calcdragpos(sx,sy,ex,ey,dx,dy)
  11.     if(ex < sx-dx)then return -1,0
  12.     elseif(ex > sx+dx)then return 1,0
  13.     elseif(ey < sy-dy)then return 0,-1
  14.     elseif(ey > sy+dy)then return 0,1 end
  15. end
  16.  
  17. local map = {
  18.     {2,0,0,0},
  19.     {0,0,0,0},
  20.     {0,0,0,0},
  21.     {0,0,0,0},
  22. }
  23. local numcols = {
  24.     ['2'] = "white",
  25.     ['4'] = "lightGray",
  26.     ['8'] = "brown",
  27.     ['16'] = "orange",
  28.     ['32'] = "pink",
  29.     ['64'] = "red",
  30.     ['128'] = "yellow",
  31.     ['256'] = "yellow",
  32.     ['512'] = "yellow",
  33.     ['1024'] = "yellow",
  34.     ['2048'] = "yellow",
  35.     ['4096'] = "magenta",
  36.     ['8192'] = "purple",
  37. }
  38. local function drawtile(x,y)
  39.         local sx = (drawx)+(x)*xscale
  40.         local sy = (drawy)+(y)*yscale
  41.         local ex = (drawx+x*xscale)+cubexsize
  42.         local ey = (drawy+y*yscale)+cubeysize
  43.         if(map[y][x] ~= 0)then
  44.             -- drawing code form empty
  45.             bcol = numcols[tostring(map[y][x])]
  46.             if(bcol == nil)then col = colors.lightBlue end
  47.             paintutils.drawFilledBox(sx+1,sy+1,ex-(cubexsize/2)+1,ey-(cubeysize/2)+1,colors[bcol])
  48.             term.setTextColor(colors.black)
  49.             term.setCursorPos((sx+3) - #tostring(map[y][x])/2 ,sy+2)
  50.             write(map[y][x])
  51.         else
  52.             paintutils.drawFilledBox(sx+1,sy+1,ex-(cubexsize/2)+1,ey-(cubeysize/2)+1,colors.black)
  53.         end
  54. end
  55. local function drawtiles()
  56.     term.current().setVisible(false)
  57.     --paintutils.drawBox(drawx,drawy,drawx+(#map[1]*xscale),drawy+(#map*yscale),colors.gray)
  58.     for y = 1, #map do
  59.        for x = 1, #map[y] do
  60.             local tile = map[y][x]
  61.                 local sx = (drawx)+(x)*xscale
  62.                 local sy = (drawy)+(y)*yscale
  63.                 local ex = (drawx+x*xscale)+cubexsize
  64.                 local ey = (drawy+y*yscale)+cubeysize
  65.                 paintutils.drawBox(sx,sy,ex-1,ey,colors.gray)
  66.                 drawtile(x,y)
  67.        end
  68.     end
  69.     term.current().setVisible(true)
  70. end
  71. local function setTile(x,y,n)
  72.     if(map[y][x] ~= 0 and n ~= 0)then
  73.         score = score + (n-map[y][x])
  74.     elseif(map[y][x] ~= 0 and n == 0)then
  75.         score = score - map[y][x]
  76.     else
  77.         score = score + n
  78.     end
  79.     map[y][x] = n
  80.     drawtile(x,y)
  81. end
  82.  
  83. local function getRandTile()
  84.     local rx = math.ceil(math.random(1,#map[1]))
  85.     local ry = math.ceil(math.random(1,#map))
  86.     local rn = math.ceil(math.random(1,10))
  87.     if(rn == 10)then
  88.         rn = 4
  89.     else
  90.         rn = 2
  91.     end
  92.     return rx,ry,rn
  93. end
  94.  
  95. local function slidetiles(xd,yd)
  96.     -- 1, UP: 2, DOWN, 3: LEFT, 4: RIGHT
  97.     local rna = math.ceil(math.random(1,2))
  98.     local moved=false
  99.     local cancombine = true
  100.     for li = 1, #map[1] do
  101.        for y = 1, #map do
  102.           for x = 1, #map[y] do
  103.                 local to = nil
  104.                 if(y+yd < 0 or x+xd < 0)then break end
  105.                 if(map[y+yd] ~= nil)then
  106.                     if(map[y+yd][x+xd] ~= nil)then
  107.                         to = map[y+yd][x+xd]
  108.                     end
  109.                 end
  110.  
  111.                 if(to == 0)then
  112.                     setTile(x+xd,y+yd,map[y][x])
  113.                     setTile(x,y,0)
  114.                     moved = true
  115.                 elseif(to == map[y][x] and cancombine and to ~= 8192)then
  116.                     setTile(x+xd,y+yd,map[y+yd][x+xd]+map[y][x])
  117.                     setTile(x,y,0)
  118.                     cancombine = false
  119.                 end
  120.            end
  121.        end
  122.     end
  123.     local rx,ry,rn = getRandTile()
  124.     if(map[ry][rx] == 0 and moved)then
  125.         setTile(rx,ry,rn)
  126.     end
  127.     term.setCursorPos(1,1)
  128.     term.setBackgroundColor(colors.white)
  129.     term.setTextColor(colors.black)
  130.     term.clearLine()
  131.     print(score)
  132. end
  133. term.clear()
  134. drawtiles()
  135. while running do
  136.  
  137.     local e = {os.pullEvent()} 
  138.     if(e[1] == "mouse_click")then
  139.         cx = e[3]
  140.         cy = e[4]
  141.     end
  142.     if(e[1] == "mouse_drag" and cx ~= 0 and cy ~= 0)then
  143.         local xdir,ydir = calcdragpos(cx,cy,e[3],e[4],2,1)
  144.        if(type(xdir) == "number" and type(ydir) == "number")then
  145.           slidetiles(tonumber(xdir),tonumber(ydir))
  146.           cx = 0
  147.           cy = 0
  148.        end
  149.     end
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement