Advertisement
guitarplayer616

gridlew

Jun 19th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. --local sel.Bcol
  2. --local sel.Tcol
  3. local over = false
  4. --init
  5. local solved = 0
  6. local console
  7. local w,h = term.getSize()
  8. local tBuffer
  9. local sel = {}
  10. local bases = {}
  11. local preX,preY
  12. local defback = colors.white
  13. local defcol = colors.black
  14. local conX,conY
  15. term.setBackgroundColor(defback)
  16. term.clear()
  17.  
  18.  
  19. --children
  20.  
  21. function initBuffer()
  22.   tBuffer = {}
  23.   for x = 1,w do
  24.     tBuffer[x] = {}
  25.     for y = 1,h do
  26.         tBuffer[x][y] = nil
  27.     end
  28.   end
  29. end
  30.  
  31. function displayBuffer()
  32.   for x,v in pairs(tBuffer) do
  33.     for y,v2 in pairs(v) do
  34.       if v2 then
  35.         term.setBackgroundColor(v2)
  36.         term.setCursorPos(x,y)
  37.         term.write(" ")
  38.       end
  39.     end
  40.   end
  41. end
  42.  
  43. function draw(x,y,back)
  44.     local words = " "
  45.     back = back or colors.black
  46.     if x and y then
  47.       term.setCursorPos(x,y)
  48.     end
  49.     term.setBackgroundColor(back)
  50.     write(words)
  51. end
  52.  
  53. function base(x,y,col)
  54.     tBuffer[x][y] = col
  55.     draw(x,y,col)
  56.     bases[x] = y
  57. end
  58.  
  59. function checkNearby(x,y,col)
  60.   if preX == x or preY == y then
  61.     if preX < x then
  62.       if tBuffer[x-2][y] == col then
  63.         tBuffer[x][y] = col
  64.         draw(x,y,col)
  65.         tBuffer[x-1][y] = col
  66.         draw(x-1,y,col)
  67.       end
  68.     elseif preX > x then
  69.       if tBuffer[x+2][y] == col then
  70.         tBuffer[x][y] = col
  71.         draw(x,y,col)
  72.         tBuffer[x+1][y] = col
  73.         draw(x+1,y,col)
  74.       end
  75.     elseif preY < y then
  76.       if tBuffer[x][y-2] == col then
  77.         tBuffer[x][y] = col
  78.         draw(x,y,col)
  79.         tBuffer[x][y-1] = col
  80.         draw(x,y-1,col)
  81.       end
  82.     elseif preY > y then
  83.       if tBuffer[x][y+2] == col then
  84.         tBuffer[x][y] = col
  85.         draw(x,y,col)
  86.         tBuffer[x][y+1] = col
  87.         draw(x,y+1,col)
  88.       end
  89.     end
  90.   end
  91. end
  92.  
  93. function checkNearbyFill(x,y,col)
  94.     for _,v in ipairs{2,-2} do
  95.       if tBuffer[x+v][y] == col then
  96.         tBuffer[x][y] = col
  97.         draw(x,y,col)
  98.         tBuffer[x+v/2][y] = col
  99.         draw(x+v/2,y,col)
  100.       elseif tBuffer[x][y+v] == col then
  101.         tBuffer[x][y] = col
  102.         draw(x,y,col)
  103.         tBuffer[x][y+v/2] = col
  104.         draw(x,y+v/2,col)
  105.       end
  106.     end
  107. end
  108.  
  109. function checkSolved(x,y,col)
  110.     --activated on base
  111.     local currX,currY = x,y
  112.     local last
  113.     local moves = 0
  114.     while not over do
  115.       if tBuffer[x-1][y] == col and last ~= "x + 1" then
  116.         x = x - 1
  117.         last = "x - 1"
  118.         moves = moves + 1
  119.       elseif tBuffer[x+1][y] == col and last ~= "x - 1" then
  120.         x = x + 1
  121.         last = "x + 1"
  122.         moves = moves + 1
  123.       elseif tBuffer[x][y-1] == col and last ~= "y + 1" then
  124.         y = y - 1
  125.         last = "y - 1"
  126.         moves = moves + 1
  127.       elseif tBuffer[x][y+1] == col and last ~= "y - 1" then
  128.         y = y + 1
  129.         last = "y + 1"
  130.         moves = moves + 1
  131.       else
  132.         over = true
  133.       end
  134.     end
  135.    
  136.     print(x,y)
  137.     if bases[x] == y then
  138.       log("solved")
  139.       solved = solved + 1
  140.     end
  141. end
  142.      
  143. function clearPath(x,y,col)    
  144.     local currX,currY = x,y
  145.     local last
  146.        
  147.     local function clearVal(x,y)
  148.        
  149.        
  150.         if (x-1)%2 == 0 and (y-1)%2 == 0 then
  151.           draw(x,y,defcol)
  152.           tBuffer[x][y] = defcol
  153.         else
  154.           draw(x,y,defback)
  155.           tBuffer[x][y] = nil
  156.         end
  157.     end
  158.    
  159.     while not over do
  160.       if tBuffer[x-1][y] == col and last ~= "x + 1" and bases[x-1] ~= y then
  161.         x = x - 1
  162.         last = "x - 1"
  163.         clearVal(x,y)
  164.         log("left")
  165.       elseif tBuffer[x+1][y] == col and last ~= "x - 1" and bases[x+1] ~= y then
  166.         x = x + 1
  167.         last = "x + 1"
  168.         clearVal(x,y)
  169.         log("write")
  170.       elseif tBuffer[x][y-1] == col and last ~= "y + 1" and bases[x] ~= y-1 then
  171.         y = y - 1
  172.         last = "y - 1"
  173.         clearVal(x,y)
  174.         log("up")
  175.       elseif tBuffer[x][y+1] == col and last ~= "y - 1" and bases[x] ~= y+1 then
  176.         y = y + 1
  177.         last = "y + 1"
  178.         clearVal(x,y)
  179.         log("down")
  180.       else
  181.         over = true
  182.       end
  183.     end
  184.    
  185. end
  186.  
  187. function drawConsole(x,y,x2,y2)
  188.     console = {x,y,x2,y2}
  189.     paintutils.drawFilledBox(x,y,x2,y2,colors.black)
  190.     term.setCursorPos(x+1,y)
  191.     term.setTextColor(colors.yellow)
  192.     conX,conY = term.getCursorPos()
  193.     write("Console")
  194.     conY = conY + 1
  195. end
  196.  
  197. local consoleText = {}
  198.  
  199.  
  200.  
  201. function log(words,color,bWrite)
  202.     words = tostring(words)
  203.     if conX and conY then
  204.         color = color or colors.white
  205.         term.setBackgroundColor(colors.black)
  206.         term.setTextColor(color)
  207.         term.setCursorPos(conX,conY)
  208.         if words:len() + 1 + conX > w then
  209.             conX = console[1]+1
  210.             conY = conY + 1
  211.             term.setCursorPos(conX,conY)
  212.         end
  213.         if bWrite then
  214.             consoleText[conY] = consoleText[conY] or ""..words
  215.             conX,conY = term.getCursorPos()
  216.         else
  217.             conX,conY = term.getCursorPos()
  218.             consoleText[conY] = consoleText[conY] or ""..words
  219.             conY = conY + 1
  220.         end
  221.     end
  222. end
  223.  
  224.  
  225.  
  226. --parent
  227. function Main()
  228.   initBuffer()
  229.  
  230.   for i = 1,w,2 do
  231.     for v = 1,h,2 do
  232.       tBuffer[i][v] = colors.black
  233.     end
  234.   end
  235.  
  236.   displayBuffer()
  237.   drawConsole(27,1,w,11)
  238. end
  239.  
  240. Main()
  241.  
  242. base(7,7,colors.red)
  243. base(3,5,colors.red)
  244. base(11,15,colors.yellow)
  245. base(5,13,colors.yellow)
  246. base(9,11,colors.orange)
  247. base(17,19,colors.orange)
  248.  
  249.  
  250.  
  251.  
  252. while true do
  253.   local e = {coroutine.yield("mouse_click")}
  254.  
  255.   if tBuffer[e[3]][e[4]] and e[2] == 1 then
  256.     if bases[e[3]] == e[4] then
  257.       sel.x = e[3]
  258.       sel.y = e[4]
  259.       sel.Bcol = tBuffer[e[3]][e[4]]
  260.       checkNearby(sel.x,sel.y,sel.Bcol)
  261.       preX,preY = sel.x,sel.y
  262.       --clear path
  263.     elseif sel.x and sel.y and sel.Bcol then
  264.       checkNearby(e[3],e[4],sel.Bcol)
  265.       preX,preY = e[3],e[4]
  266.     end
  267.   end
  268.  
  269.   if e[2] == 3 then
  270.     for i,v in pairs(bases) do
  271.       local col = tBuffer[i][v]
  272.       checkSolved(i,v,col)
  273.     end
  274.     textutils.slowWrite(solved)
  275.     if solved == 6 then textutils.slowWrite("SOLVED!!!!!!...") end
  276.   end
  277.  
  278.   if e[2] == 2 then
  279.     log("color = "..tBuffer[e[3]][e[4]] or "null")
  280.     clearPath(e[3],e[4],tBuffer[e[3]][e[4]])
  281.   end
  282.  
  283. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement