Advertisement
ecco7777

CC Pathfinder Turtle

Jan 25th, 2017
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. --
  2. facings={"north","east","south","west"}
  3. currentFacing=3
  4. map={}
  5. mapFile="map"
  6.  
  7. function readMap(mapFile)
  8. fp=fs.open(mapFile,"r")
  9. y=1
  10. line=1
  11. local myPos={{}}
  12. while line~=nil do
  13. line=fp.readLine()
  14. x=1
  15. if line~=nil then
  16. for x=1,#line do
  17. myPos[x]={}
  18. if string.sub(line,x,x)==" " then
  19. myPos[x][y]=0
  20. elseif string.sub(line,x,x)=="0" then
  21. myPos[x][y]=1
  22. elseif string.sub(line,x,x)=="d" then
  23. myPos[x][y]=2
  24. else
  25. myPos[x][y]=-1
  26. end
  27. --print("myPos["..tostring(x).."]["..tostring(y).."]="..tostring(myPos[x][y]))
  28. end
  29. end
  30. y=y+1
  31. end
  32. return myPos
  33. end
  34.  
  35. function turn(direction) -- 1=right  -1=left
  36. currentFacing=currentFacing+direction
  37. if currentFacing==5 then
  38. currentFacing=1
  39. elseif currentFacing==-1 then
  40. currentFacing=4
  41. end
  42. return currentFacing
  43. end
  44.  
  45. function goForward()
  46. success=true
  47. if currentFacing==1 then success=map[myXPos][myYPos+1]~=1 if success then myYPos=myYPos+1 end
  48. elseif currentFacing==2 then success=map[myXPos+1][myYPos]~=1 if success then myXPos=myXPos+1 end
  49. elseif currentFacing==3 then success=map[myXPos][myYPos-1]~=1 if success then myYPos=myYPos-1 end
  50. elseif currentFacing==4 then success=map[myXPos-1][myYPos]~=1 if success then myXPos=myXPos-1 end
  51. end
  52. return success
  53. end
  54.  
  55. function searchWay(mycXPos,mycYPos)
  56. for i1=1,1 do
  57. trys={}
  58. myXPos=mycXPos
  59. myYPos=mycYPos
  60. while map[myXPos][myYPos]~=2 do
  61. try="0"
  62. myXPos=mycXPos
  63. myYPos=mycYPos
  64. while goForward() do
  65. rand=math.random(-1,1)
  66. print(rand)
  67. attempt=rand
  68. while trys[try]==attempt do
  69. rand=math.random(-1,1)
  70. print(rand)
  71. attempt=rand
  72. print("fail1")
  73. end
  74. turn(attempt)
  75. try=try..tostring(attempt)
  76. end
  77.  
  78. end
  79. print(try)
  80. end
  81. end
  82.  
  83.  
  84. map=readMap(mapFile)
  85. searchWay(3,3)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement