Martmists

2048.lua

Feb 7th, 2016
675
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.21 KB | None | 0 0
  1. --[[
  2.  
  3. 2048 in CC
  4. by Martmists
  5.  
  6.  
  7. Copy this code or use
  8.  
  9. > pastebin get wb6Mn5N0 2048
  10.  
  11.  
  12. NOTE:
  13. This still contains the following bugs:
  14. - Game over when the fiel is full, even if there are still moves left.
  15. - if you have 4,4,8 (or similar, e.g. 2,2,4 or 256,256,512) it becomes 16 automatically. --FIXED
  16. - the colors need to be better (not really a bug)
  17.  
  18. Controls:
  19.  
  20. Arrow keys: Move
  21. Backspace: Quit
  22.  
  23. Field:
  24. [1][1] || [2][1] || [3][1] || [4][1]
  25. =======##========##========##=======
  26. [1][2] || [2][2] || [3][2] || [4][2]
  27. =======##========##========##=======
  28. [1][3] || [2][3] || [3][3] || [4][3]
  29. =======##========##========##=======
  30. [1][4] || [2][4] || [3][4] || [4][4]
  31. --]]
  32.  
  33. function InitField()  
  34.  
  35.     shell.run("clear")
  36.     Field = {}
  37.     for i = 1, 4 do
  38.         Field[i] = {0,0,0,0}
  39.     end
  40.    
  41.     term.setBackgroundColor(colors.gray)
  42.     for J = 2,25 do
  43.         for I = 2,25 do
  44.             term.setCursorPos(J,I)
  45.             print(" ")
  46.         end
  47.     end
  48.     term.setBackgroundColor(colors.lightGray)
  49.     for J = 1,25,6 do
  50.         for I = 1,17 do
  51.             term.setCursorPos(J,I)
  52.             print(" ")
  53.         end
  54.     end
  55.     for J = 1,17,4 do
  56.         for I = 1,25 do
  57.             term.setCursorPos(I,J)
  58.             print(" ")
  59.         end
  60.     end
  61.     term.setBackgroundColor(colors.gray)
  62. end
  63.  
  64. function GetMove() -- Returns what key you pressed
  65.     event, key = os.pullEvent("key")
  66. end
  67.  
  68. function Move() -- Initializes movement
  69.     if key == 200 then
  70.         Direction = "Left"
  71.     elseif key == 208 then
  72.         Direction = "Right"
  73.     elseif key == 205 then
  74.         Direction = "Down"
  75.     elseif key == 203 then
  76.         Direction = "Up"
  77.     elseif key == 14 then
  78.         Quit()
  79.     end
  80.     MoveDir()
  81. end
  82.  
  83.  
  84. function MoveDir() -- Moves
  85.   for X = 1, 4 do
  86.  
  87.     -- Check direction
  88.     if Direction == "Up" then
  89.         D = Field[X][1] -- Initialize variables
  90.         C = Field[X][2]
  91.         B = Field[X][3]
  92.         A = Field[X][4]
  93.     elseif Direction == "Down" then
  94.         A = Field[X][1] -- Initialize variables
  95.         B = Field[X][2]
  96.         C = Field[X][3]
  97.         D = Field[X][4]
  98.     elseif Direction == "Left" then
  99.         D = Field[1][X] -- Initialize variables
  100.         C = Field[2][X]
  101.         B = Field[3][X]
  102.         A = Field[4][X]
  103.     else
  104.         A = Field[1][X] -- Initialize variables
  105.         B = Field[2][X]
  106.         C = Field[3][X]
  107.         D = Field[4][X]
  108.     end
  109.  
  110.     if D ~= 0 then
  111.         if C ~= 0 then
  112.             if D == C then
  113.                 D = 2 * D
  114.                 if A == 0 then 
  115.                     if B == 0 then
  116.                         C = 0
  117.                     else
  118.                         C = B
  119.                         B = 0
  120.                     end
  121.                 else
  122.                     if B == 0 then
  123.                         C = A
  124.                         A = 0
  125.                     else
  126.                         if A == B then
  127.                             C = 2 * B
  128.                             B = 0
  129.                         else
  130.                             C = B
  131.                             B = A
  132.                         end
  133.                         A = 0
  134.                     end
  135.                 end
  136.             else
  137.                 if C == B then
  138.                     C = 2 * C
  139.                     B = 0
  140.                 else
  141.                     if B == A then
  142.                         B = 2 * B
  143.                         A = 0
  144.                     else
  145.                         if B == 0 then
  146.                             B = A
  147.                             A = 0
  148.                         end
  149.                     end
  150.                 end
  151.             end
  152.         else
  153.             if D == B then
  154.                 D = 2 * D
  155.                 C = A
  156.                 B = 0
  157.                 A = 0
  158.             else
  159.                 if A == B then
  160.                     C = 2 * B
  161.                     B = 0
  162.                 else
  163.                     C = B
  164.                     B = A
  165.                 end
  166.                 A = 0
  167.             end
  168.         end
  169.     else
  170.         if C ~= 0 then
  171.             if C == B then
  172.                 D = 2 * C
  173.                 B = 0
  174.                 if A ~= 0 then
  175.                     C = A
  176.                     A = 0
  177.                 end
  178.             else
  179.                 D = C
  180.                 if B == A then
  181.                     C = 2 * B
  182.                     B = 0
  183.                 else
  184.                     C = B
  185.                     B = A
  186.                 end
  187.                 A = 0
  188.             end
  189.         else
  190.             if A == B then
  191.                 D = 2 * B
  192.                 A = 0
  193.                 B = 0
  194.             else
  195.                 D = B
  196.                 C = A
  197.                 B = 0
  198.                 A = 0
  199.             end
  200.         end
  201.     end
  202.     -- Check Direction
  203.     if Direction == "Up" then
  204.         Field[X][1] = D -- Return variables
  205.         Field[X][2] = C
  206.         Field[X][3] = B
  207.         Field[X][4] = A
  208.     elseif Direction == "Down" then
  209.         Field[X][1] = A -- Return variables
  210.         Field[X][2] = B
  211.         Field[X][3] = C
  212.         Field[X][4] = D
  213.     elseif Direction == "Left" then
  214.         Field[1][X] = D -- Return variables
  215.         Field[2][X] = C
  216.         Field[3][X] = B
  217.         Field[4][X] = A
  218.     else
  219.         Field[1][X] = A -- Return variables
  220.         Field[2][X] = B
  221.         Field[3][X] = C
  222.         Field[4][X] = D
  223.     end
  224.   end
  225. end
  226.  
  227. function Quit()
  228.     Loop = false
  229. end
  230.  
  231. function Gen()
  232.  
  233.     RandGen()
  234.     while Field[S][T] ~= 0 do
  235.         RandGen()
  236.         sleep(0.1)
  237.     end
  238.    
  239.     Field[S][T] = 2
  240. end
  241.  
  242. function RandGen()
  243.     S = math.random(1,4)
  244.     T = math.random(1,4)
  245. end
  246.  
  247. function CheckStuck()
  248.  
  249.     StuckCount = 0
  250.    
  251.     for I = 1,4 do
  252.         for J = 1,4 do
  253.             if Field[I][J] ~= 0 then
  254.                 StuckCount = StuckCount + 1
  255.             end
  256.         end
  257.     end
  258.    
  259.     if StuckCount == 16 then
  260.         Loop = false
  261.         GameOver = true
  262.     end
  263.    
  264. end
  265.  
  266. function GetScore()
  267.  
  268.     Score = 0 -- Reset Score
  269.    
  270.     for I = 1,4 do
  271.         for J = 1,4 do
  272.             if Field[I][J] ~= 2 then -- Don't count 2
  273.                 Score = Score + Field[I][J] -- Add every tile to Score
  274.             end
  275.         end
  276.     end
  277.    
  278.     term.setBackgroundColor(colors.white)
  279.     term.setTextColor(colors.black)
  280.     term.setCursorPos(1,1)
  281.     print(Score)
  282.    
  283. end
  284.  
  285.  
  286. function ResetDisp()
  287.  
  288.     term.setBackgroundColor(colors.lightGray)
  289.     for J = 1,25,6 do
  290.         for I = 1,17 do
  291.             term.setCursorPos(J,I)
  292.             print(" ")
  293.         end
  294.     end
  295.     for J = 1,17,4 do
  296.         for I = 1,25 do
  297.             term.setCursorPos(I,J)
  298.             print(" ")
  299.         end
  300.     end
  301.    
  302.     term.setBackgroundColor(colors.gray)
  303.     term.setTextColor(colors.white)
  304.    
  305.     -- Row 1
  306.     X, Y = 4, 3
  307.     printslot(0)
  308.     X, Y = 4, 7
  309.     printslot(0)
  310.     X, Y = 4,11
  311.     printslot(0)
  312.     X, Y = 4,15
  313.     printslot(0)   
  314.    
  315.     -- Row 2
  316.     X, Y = 10,3
  317.     printslot(0)
  318.     X, Y = 10,7
  319.     printslot(0)
  320.     X, Y = 10,11
  321.     printslot(0)
  322.     X, Y = 10,15
  323.     printslot(0)   
  324.    
  325.     -- Row 3
  326.     X, Y = 16,3
  327.     printslot(0)
  328.     X, Y = 16,7
  329.     printslot(0)
  330.     X, Y = 16,11
  331.     printslot(0)
  332.     X, Y = 16,15
  333.     printslot(0)   
  334.    
  335.     -- Row 4
  336.     X, Y = 22,3
  337.     printslot(0)
  338.     X, Y = 22,7
  339.     printslot(0)
  340.     X, Y = 22,11
  341.     printslot(0)
  342.     X, Y = 22,15
  343.     printslot(0)   
  344. end
  345.  
  346. function Display()
  347.  
  348.     -- Row 1
  349.     X, Y = 4,3
  350.     printslot(Field[1][1])
  351.     X, Y = 4,7
  352.     printslot(Field[2][1])
  353.     X, Y = 4,11
  354.     printslot(Field[3][1])
  355.     X, Y = 4,15
  356.     printslot(Field[4][1]) 
  357.    
  358.     -- Row 2
  359.     X, Y = 10,3
  360.     printslot(Field[1][2])
  361.     X, Y = 10,7
  362.     printslot(Field[2][2])
  363.     X, Y = 10,11
  364.     printslot(Field[3][2])
  365.     X, Y = 10,15
  366.     printslot(Field[4][2]) 
  367.    
  368.     -- Row 3
  369.     X, Y = 16,3
  370.     printslot(Field[1][3])
  371.     X, Y = 16,7
  372.     printslot(Field[2][3])
  373.     X, Y = 16,11
  374.     printslot(Field[3][3])
  375.     X, Y = 16,15
  376.     printslot(Field[4][3]) 
  377.    
  378.     -- Row 4
  379.     X, Y = 22,3
  380.     printslot(Field[1][4])
  381.     X, Y = 22,7
  382.     printslot(Field[2][4])
  383.     X, Y = 22,11
  384.     printslot(Field[3][4])
  385.     X, Y = 22,15
  386.     printslot(Field[4][4]) 
  387. end
  388.  
  389. function printslot(slot)
  390.     if slot == 0 then
  391.         term.setBackgroundColor(colors.gray)
  392.     elseif slot == 2 then
  393.         term.setBackgroundColor(colors.yellow)
  394.     elseif slot == 4 then
  395.         term.setBackgroundColor(colors.orange)
  396.     elseif slot == 8 then
  397.         term.setBackgroundColor(colors.red)
  398.     elseif slot == 16 then
  399.         term.setBackgroundColor(colors.purple)
  400.     elseif slot == 32 then
  401.         term.setBackgroundColor(colors.blue)
  402.     else
  403.         term.setBackgroundColor(colors.black)
  404.     end
  405.     for i = -2 , 2 do
  406.         for j = -1, 1 do
  407.             term.setCursorPos(X + i, Y + j)
  408.             print(" ")
  409.         end
  410.     end
  411.     if string.len(slot) >= 3 then
  412.         term.setCursorPos(X-1,Y)
  413.     else
  414.         term.setCursorPos(X,Y)
  415.     end
  416.     print(slot)
  417. end
  418.  
  419.  
  420. function Main()
  421.     term.clear()
  422.     InitField()
  423.     Loop = true
  424.     GameOver = false
  425.     Gen()
  426.     while Loop do
  427.         Gen()
  428.         ResetDisp()
  429.         Display()
  430.         GetScore()
  431.         CheckStuck()
  432.         if Loop then
  433.             GetMove()
  434.             Move()
  435.         end
  436.     end
  437.     term.setBackgroundColor(colors.black)
  438.     term.setTextColor(colors.white)
  439.     term.clear()
  440.     if GameOver then
  441.         print("Game Over!")
  442.     end
  443. end
  444.  
  445. Main()
Advertisement
Add Comment
Please, Sign In to add comment