Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- size=15
- wayLenght=50
- minLenght=20
- trys=0
- avtrys=0
- function newMap(size)
- map={}
- for x=1,size do
- map[x]={}
- for y=1,size do
- map[x][y]=0
- end
- end
- return map
- end
- function getOptions(map,x,y)
- local i = 1
- local option={}
- if x~=nil and y~=nil then
- if x+2<=size and y+2<=size and x+2>0 and y+2>0 and x-2<=size and y-2<=size and x-2>0 and y-2>0 then
- if map[x][y+2]==nil or map[x][y+2]==1 then else map[x][y+2]=1 map[x][y+1]=1 option[i]=1 i=i+1 print("debug1") end
- if map[x][y-2]==nil or map[x][y-2]==1 then else map[x][y-2]=1 map[x][y-1]=1 option[i]=2 i=i+1 print("debug2") end
- if map[x+2][y]==nil or map[x+2][y]==1 then else map[x+2][y]=1 map[x+1][y]=1 option[i]=3 i=i+1 print("debug3") end
- if map[x-2][y]==nil or map[x-2][y]==1 then else map[x-2][y]=1 map[x-1][y]=1 option[i]=4 i=i+1 print("debug4") end
- end
- end
- return option
- end
- function goForward(map,dir)
- if x+2<=size and y+2<=size and x+2>0 and y+2>0 and x-2<=size and y-2<=size and x-2>0 and y-2>0 then
- if dir==1 then
- if map[x][y+2]==nil or map[x][y+2]==1 then return false else map[x][y+2]=1 map[x][y+1]=1 y=y+2 end
- end
- if dir==3 then
- if map[x][y-2]==nil or map[x][y-2]==1 then return false else map[x][y-2]=1 map[x][y-1]=1 y=y-2 end
- end
- if dir==2 then
- if map[x+2][y]==nil or map[x+2][y]==1 then return false else map[x+2][y]=1 map[x+1][y]=1 x=x+2 end
- end
- if dir==4 then
- if map[x-2][y]==nil or map[x-2][y]==1 then return false else map[x-2][y]=1 map[x-1][y]=1 x=x-2 end
- end
- addStack(x,y)
- else
- return false
- end
- end
- function getPathLenght(map)
- wert=0
- for x=1,size do
- for y=1,size do
- wert=wert+map[x][y]
- end
- end
- return wert
- end
- function makeStack()
- stack={}
- stack["current"]=0
- stack["x"]={}
- stack["y"]={}
- end
- function addStack(x,y)
- stack["current"]=stack["current"]+1
- stack["x"][stack["current"]]=x
- stack["y"][stack["current"]]=y
- end
- function rmStack()
- stack["current"]=stack["current"]-1
- end
- function getStack()
- return stack["x"][stack["current"]], stack["y"][stack["current"]]
- end
- function writeMap(map,file)
- shell.run("rm "..file)
- fp=fs.open(file,"w")
- size=#map[1]
- for x=1,size do
- for y=1,size do
- fp.write(tostring(map[x][y]))
- end
- fp.write("\n")
- end
- fp.close()
- end
- makeStack()
- while true do
- way=newMap(size)
- x=math.random(1,size)
- y=math.random(1,size)
- way[x][y]=1
- for i=1,wayLenght do
- --while stack["current"]>1 do
- local option=getOptions(map,x,y)
- print("Option "..tostring(#option))
- term.clear()
- if #option~=0 then
- goForward(way,option[math.random(1,#option)])
- else
- rmStack()
- x,y=getStack()
- end
- end
- if getPathLenght(way)>=minLenght then
- paintutils.drawImage(way,1,1)
- term.setBackgroundColor(colors.black)
- term.setCursorPos(1,1)
- term.write(trys)
- avtrys=(avtrys+trys)/2
- term.setCursorPos(1,2)
- term.write(avtrys)
- sleep(0.5)
- term.clear()
- trys=0
- else trys=trys+1
- end
- end
Add Comment
Please, Sign In to add comment