Advertisement
Guest User

pac4

a guest
Jul 5th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.43 KB | None | 0 0
  1. --a shortcut for string.format
  2. sf = string.format
  3.  
  4. --[[
  5. helper function
  6. split a line into a table
  7. --]]
  8.  
  9. function split(source, delimiters)
  10.         local elements = {}
  11.         local pattern = '([^'..delimiters..']+)'
  12.         string.gsub(source, pattern, function(value) elements[#elements + 1] =     value;  end);
  13.         return elements
  14.   end
  15. --[[
  16. helper function to ignore comments in data file
  17. returns true if String starts with Start, false otherwise
  18. --]]
  19. function startsWith(String,Start)
  20.    return string.sub(String,1,string.len(Start))==Start
  21. end
  22. --[[
  23. helper function to determine
  24. from the row number which way
  25. the turtle is going and which
  26. way to turn when done
  27. it illustrates some advanced
  28. functional features of lua
  29. like returning a sequence and
  30. returning a function as a value
  31. end
  32. --]]
  33.  
  34. function leftEnd()
  35.   turtle.turnLeft()
  36.   turtle.back()
  37.   turtle.turnLeft()
  38. end
  39.  
  40. function rightEnd()
  41.   turtle.turnRight()
  42.   turtle.back()
  43.   turtle.turnRight()
  44. end
  45.  
  46. function stripes(i,length)
  47. if i % 2 == 0 then  
  48.   return 1,length, 1, leftEnd
  49. end
  50.   return length,1,rightEnd
  51. end
  52. -- assumes turtle is on the spot
  53. -- being acted upon
  54. action = {
  55.  [0] = function() turtle.back() end,
  56.  [2] = function()
  57.    turtle.select(2)  
  58.    turle.placeUp()
  59.    turtle.select(1)
  60.    turtle.back()
  61.  end,
  62.  
  63.  [1] = function()
  64.    turtle.select(1)
  65.    turtle.back()  
  66.    turtle.place()
  67.  end,
  68.  [3] = function()
  69.    turtle.select(3)
  70.    turtle.placeUp()
  71.    turtle.select(1)
  72.    turtle.back()
  73.  end
  74.  }
  75.  
  76. --[[ ----- begin the main work -------------------------
  77. --]]
  78. io.input("maze.dat")
  79. io.output("debug.txt")
  80. local lines = {}
  81. -- read the file into table 'lines'
  82. -- split by lines and each line by commas
  83. -- ignore comment lines in the line that begin with "#"
  84. for line in io.lines() do
  85.    if not startsWith(line, "#") then
  86.        row = split(line,",")
  87.        table.insert(lines, row)
  88.     end
  89. end
  90. io.input():close()
  91. -- build the maze
  92. --remove the maze sizes from the list
  93. rows = lines[1][1]
  94. table.remove(lines,1)
  95. io.write(sf("%d\n",rows))
  96.  
  97. columns = lines[1][1]
  98. table.remove(lines,1)
  99. io.write(sf("%d\n",columns))
  100. for i, l in ipairs(lines) do
  101.    jstart,jend,jstep,turn = stripe(i,table.getn(l))
  102.    for j = jstart,jend, jstep do
  103.       actionIndex = l[j]
  104.       io.write(sf("(%d %d)",j,actionIndex))
  105.        --action[actionIndex]()
  106.     end
  107.     --turn()
  108.     io.write("\n")
  109. --    return
  110. end
  111. io.output():close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement