Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --
- facings={"north","east","south","west"}
- currentFacing=3
- map={}
- mapFile="map"
- function readMap(mapFile)
- fp=fs.open(mapFile,"r")
- y=1
- line=1
- local myPos={{}}
- while line~=nil do
- line=fp.readLine()
- x=1
- if line~=nil then
- for x=1,#line do
- myPos[x]={}
- if string.sub(line,x,x)==" " then
- myPos[x][y]=0
- elseif string.sub(line,x,x)=="0" then
- myPos[x][y]=1
- elseif string.sub(line,x,x)=="d" then
- myPos[x][y]=2
- else
- myPos[x][y]=-1
- end
- --print("myPos["..tostring(x).."]["..tostring(y).."]="..tostring(myPos[x][y]))
- end
- end
- y=y+1
- end
- return myPos
- end
- function turn(direction) -- 1=right -1=left
- currentFacing=currentFacing+direction
- if currentFacing==5 then
- currentFacing=1
- elseif currentFacing==-1 then
- currentFacing=4
- end
- return currentFacing
- end
- function goForward()
- success=true
- if currentFacing==1 then success=map[myXPos][myYPos+1]~=1 if success then myYPos=myYPos+1 end
- elseif currentFacing==2 then success=map[myXPos+1][myYPos]~=1 if success then myXPos=myXPos+1 end
- elseif currentFacing==3 then success=map[myXPos][myYPos-1]~=1 if success then myYPos=myYPos-1 end
- elseif currentFacing==4 then success=map[myXPos-1][myYPos]~=1 if success then myXPos=myXPos-1 end
- end
- return success
- end
- function searchWay(mycXPos,mycYPos)
- for i1=1,1 do
- trys={}
- myXPos=mycXPos
- myYPos=mycYPos
- while map[myXPos][myYPos]~=2 do
- try="0"
- myXPos=mycXPos
- myYPos=mycYPos
- while goForward() do
- rand=math.random(-1,1)
- print(rand)
- attempt=rand
- while trys[try]==attempt do
- rand=math.random(-1,1)
- print(rand)
- attempt=rand
- print("fail1")
- end
- turn(attempt)
- try=try..tostring(attempt)
- end
- end
- print(try)
- end
- end
- map=readMap(mapFile)
- searchWay(3,3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement