Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --local sel.Bcol
- --local sel.Tcol
- local over = false
- --init
- local solved = 0
- local console
- local w,h = term.getSize()
- local tBuffer
- local sel = {}
- local bases = {}
- local preX,preY
- local defback = colors.white
- local defcol = colors.black
- local conX,conY
- term.setBackgroundColor(defback)
- term.clear()
- --children
- function initBuffer()
- tBuffer = {}
- for x = 1,w do
- tBuffer[x] = {}
- for y = 1,h do
- tBuffer[x][y] = nil
- end
- end
- end
- function displayBuffer()
- for x,v in pairs(tBuffer) do
- for y,v2 in pairs(v) do
- if v2 then
- term.setBackgroundColor(v2)
- term.setCursorPos(x,y)
- term.write(" ")
- end
- end
- end
- end
- function draw(x,y,back)
- local words = " "
- back = back or colors.black
- if x and y then
- term.setCursorPos(x,y)
- end
- term.setBackgroundColor(back)
- write(words)
- end
- function base(x,y,col)
- tBuffer[x][y] = col
- draw(x,y,col)
- bases[x] = y
- end
- function checkNearby(x,y,col)
- if preX == x or preY == y then
- if preX < x then
- if tBuffer[x-2][y] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x-1][y] = col
- draw(x-1,y,col)
- end
- elseif preX > x then
- if tBuffer[x+2][y] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x+1][y] = col
- draw(x+1,y,col)
- end
- elseif preY < y then
- if tBuffer[x][y-2] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x][y-1] = col
- draw(x,y-1,col)
- end
- elseif preY > y then
- if tBuffer[x][y+2] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x][y+1] = col
- draw(x,y+1,col)
- end
- end
- end
- end
- function checkNearbyFill(x,y,col)
- for _,v in ipairs{2,-2} do
- if tBuffer[x+v][y] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x+v/2][y] = col
- draw(x+v/2,y,col)
- elseif tBuffer[x][y+v] == col then
- tBuffer[x][y] = col
- draw(x,y,col)
- tBuffer[x][y+v/2] = col
- draw(x,y+v/2,col)
- end
- end
- end
- function checkSolved(x,y,col)
- --activated on base
- local currX,currY = x,y
- local last
- local moves = 0
- while not over do
- if tBuffer[x-1][y] == col and last ~= "x + 1" then
- x = x - 1
- last = "x - 1"
- moves = moves + 1
- elseif tBuffer[x+1][y] == col and last ~= "x - 1" then
- x = x + 1
- last = "x + 1"
- moves = moves + 1
- elseif tBuffer[x][y-1] == col and last ~= "y + 1" then
- y = y - 1
- last = "y - 1"
- moves = moves + 1
- elseif tBuffer[x][y+1] == col and last ~= "y - 1" then
- y = y + 1
- last = "y + 1"
- moves = moves + 1
- else
- over = true
- end
- end
- print(x,y)
- if bases[x] == y then
- log("solved")
- solved = solved + 1
- end
- end
- function clearPath(x,y,col)
- local currX,currY = x,y
- local last
- local function clearVal(x,y)
- if (x-1)%2 == 0 and (y-1)%2 == 0 then
- draw(x,y,defcol)
- tBuffer[x][y] = defcol
- else
- draw(x,y,defback)
- tBuffer[x][y] = nil
- end
- end
- while not over do
- if tBuffer[x-1][y] == col and last ~= "x + 1" and bases[x-1] ~= y then
- x = x - 1
- last = "x - 1"
- clearVal(x,y)
- log("left")
- elseif tBuffer[x+1][y] == col and last ~= "x - 1" and bases[x+1] ~= y then
- x = x + 1
- last = "x + 1"
- clearVal(x,y)
- log("write")
- elseif tBuffer[x][y-1] == col and last ~= "y + 1" and bases[x] ~= y-1 then
- y = y - 1
- last = "y - 1"
- clearVal(x,y)
- log("up")
- elseif tBuffer[x][y+1] == col and last ~= "y - 1" and bases[x] ~= y+1 then
- y = y + 1
- last = "y + 1"
- clearVal(x,y)
- log("down")
- else
- over = true
- end
- end
- end
- function drawConsole(x,y,x2,y2)
- console = {x,y,x2,y2}
- paintutils.drawFilledBox(x,y,x2,y2,colors.black)
- term.setCursorPos(x+1,y)
- term.setTextColor(colors.yellow)
- conX,conY = term.getCursorPos()
- write("Console")
- conY = conY + 1
- end
- local consoleText = {}
- function log(words,color,bWrite)
- words = tostring(words)
- if conX and conY then
- color = color or colors.white
- term.setBackgroundColor(colors.black)
- term.setTextColor(color)
- term.setCursorPos(conX,conY)
- if words:len() + 1 + conX > w then
- conX = console[1]+1
- conY = conY + 1
- term.setCursorPos(conX,conY)
- end
- if bWrite then
- consoleText[conY] = consoleText[conY] or ""..words
- conX,conY = term.getCursorPos()
- else
- conX,conY = term.getCursorPos()
- consoleText[conY] = consoleText[conY] or ""..words
- conY = conY + 1
- end
- end
- end
- --parent
- function Main()
- initBuffer()
- for i = 1,w,2 do
- for v = 1,h,2 do
- tBuffer[i][v] = colors.black
- end
- end
- displayBuffer()
- drawConsole(27,1,w,11)
- end
- Main()
- base(7,7,colors.red)
- base(3,5,colors.red)
- base(11,15,colors.yellow)
- base(5,13,colors.yellow)
- base(9,11,colors.orange)
- base(17,19,colors.orange)
- while true do
- local e = {coroutine.yield("mouse_click")}
- if tBuffer[e[3]][e[4]] and e[2] == 1 then
- if bases[e[3]] == e[4] then
- sel.x = e[3]
- sel.y = e[4]
- sel.Bcol = tBuffer[e[3]][e[4]]
- checkNearby(sel.x,sel.y,sel.Bcol)
- preX,preY = sel.x,sel.y
- --clear path
- elseif sel.x and sel.y and sel.Bcol then
- checkNearby(e[3],e[4],sel.Bcol)
- preX,preY = e[3],e[4]
- end
- end
- if e[2] == 3 then
- for i,v in pairs(bases) do
- local col = tBuffer[i][v]
- checkSolved(i,v,col)
- end
- textutils.slowWrite(solved)
- if solved == 6 then textutils.slowWrite("SOLVED!!!!!!...") end
- end
- if e[2] == 2 then
- log("color = "..tBuffer[e[3]][e[4]] or "null")
- clearPath(e[3],e[4],tBuffer[e[3]][e[4]])
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement