Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("generate maze instantly? y/n")
- instants = read()
- print("play the maze like a game? y/n")
- games = read()
- if games == "y" then games = true else games = false end
- if instants == "y" then instants = true else instants = false end
- w,h = term.getSize()
- curx = 1
- cury = 1
- stack = {}
- visited = {}
- mazex = (h - 1) / 2
- mazey = (w - 1) / 2
- showstack = true
- --non color conversions
- textcolors = {
- [colors.black] = " ",
- [colors.blue] = "X",
- [colors.lime] = "@",
- [colors.red] = "F"
- }
- canvas = {}
- for xx = 1,w do
- canvas[xx] = {}
- for yy = 1,h do
- canvas[xx][yy] = colors.blue
- end
- end
- --first thing is first draw the maze
- term.clear()
- function draw()
- if term.isColor() then
- for i = 1,w do
- for j = 1,h do
- term.setCursorPos(i,j)
- term.setBackgroundColor(canvas[i][j])
- write(" ")
- end
- end
- term.setBackgroundColor(colors.black)
- else
- for i = 1,w do
- for j = 1,h do
- term.setCursorPos(i,j)
- write(textcolors[canvas[i][j]])
- end
- end
- end
- end
- draw()
- function movecell(x,y)
- -- removing wall and x at cells
- canvas[curx * 2][cury * 2] = colors.black
- canvas[x * 2][y * 2] = colors.black
- if x > curx then canvas[(x * 2) - 1][y * 2] = colors.black end
- if y > cury then canvas[x * 2][(y * 2) - 1] = colors.black end
- if x < curx then canvas[(x * 2) + 1][y * 2] = colors.black end
- if y < cury then canvas[x * 2][(y * 2) + 1] = colors.black end
- canvas[curx * 2][cury * 2] = colors.black
- curx = x
- cury = y
- canvas[curx * 2][cury * 2] = colors.lime
- if not instants then draw() end---------------------------------------------------
- end
- function checkvisited(y,x)
- if x <= 0 or y <= 0 or x > mazex or y > mazey then return true end
- local v
- for _,v in pairs(visited) do
- if v[1] == y and v[2] == x then
- return true
- end
- end
- return false
- end
- function addvisited(x,y)
- table.insert(visited,{x,y})
- end
- addvisited(curx,cury)
- function addstack(x,y)
- table.insert(stack,{x,y})
- end
- function popstack()
- local val = stack[#stack]
- table.remove(stack,#stack);
- return val[1],val[2]
- end
- repeat
- --find unvisited cells next to current one
- local posiblecells = {}
- if checkvisited(curx-1,cury) == false then
- table.insert(posiblecells,{curx - 1,cury})
- end
- if checkvisited(curx,cury - 1) == false then
- table.insert(posiblecells,{curx,cury - 1})
- end
- if checkvisited(curx + 1,cury) == false then
- table.insert(posiblecells,{curx + 1,cury})
- end
- if checkvisited(curx,cury + 1) == false then
- table.insert(posiblecells,{curx,cury + 1})
- end
- if #posiblecells > 0 then
- addstack(curx,cury)
- local targetx, targety
- local i = math.random(1,#posiblecells)
- targetx = posiblecells[i][1]
- targety = posiblecells[i][2]
- addvisited(targetx,targety)
- movecell(targetx,targety)
- else
- local targetx, targety
- targetx, targety = popstack()
- movecell(targetx,targety)
- end
- --sleep(0.05)
- os.queueEvent("randomevent")
- os.pullEvent()
- until #stack == 0
- if games then
- canvas[w-1][h-1] = colors.red
- draw()
- running = true
- px = 2
- py = 2
- while running do
- changed = false
- lpx = px
- lpy = py
- event,p1 = os.pullEvent("key")
- if p1 == 200 then
- if canvas[px][py-1] == colors.black or canvas[px][py-1] == colors.red then
- py = py - 1
- changed = true
- end
- elseif p1 == 208 then
- if canvas[px][py+1] == colors.black or canvas[px][py+1] == colors.red then
- py = py + 1
- changed = true
- end
- elseif p1 == 203 then
- if canvas[px-1][py] == colors.black or canvas[px-1][py] == colors.red then
- px = px - 1
- changed = true
- end
- elseif p1 == 205 then
- if canvas[px+1][py] == colors.black or canvas[px+1][py] == colors.red then
- px = px + 1
- changed = true
- end
- elseif p1 == 16 then
- running = false
- end
- if px == w-1 and py == h - 1 then
- running = false
- end
- if changed then
- canvas[px][py] = colors.lime
- canvas[lpx][lpy] = colors.black
- draw()
- end
- end
- else
- draw()
- os.pullEvent()
- end
Advertisement
Add Comment
Please, Sign In to add comment