Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --a shortcut for string.format
- sf = string.format
- --[[
- helper function
- split a line into a table
- --]]
- function split(source, delimiters)
- local elements = {}
- local pattern = '([^'..delimiters..']+)'
- string.gsub(source, pattern, function(value) elements[#elements + 1] = value; end);
- return elements
- end
- --[[
- helper function to ignore comments in data file
- returns true if String starts with Start, false otherwise
- --]]
- function startsWith(String,Start)
- return string.sub(String,1,string.len(Start))==Start
- end
- --[[
- helper function to determine
- from the row number which way
- the turtle is going and which
- way to turn when done
- it illustrates some advanced
- functional features of lua
- like returning a sequence and
- returning a function as a value
- end
- --]]
- function leftEnd()
- turtle.turnLeft()
- turtle.back()
- turtle.turnLeft()
- end
- function rightEnd()
- turtle.turnRight()
- turtle.back()
- turtle.turnRight()
- end
- function stripes(i,length)
- if i % 2 == 0 then
- return 1,length, 1, leftEnd
- end
- return length,1,rightEnd
- end
- -- assumes turtle is on the spot
- -- being acted upon
- action = {
- [0] = function() turtle.back() end,
- [2] = function()
- turtle.select(2)
- turle.placeUp()
- turtle.select(1)
- turtle.back()
- end,
- [1] = function()
- turtle.select(1)
- turtle.back()
- turtle.place()
- end,
- [3] = function()
- turtle.select(3)
- turtle.placeUp()
- turtle.select(1)
- turtle.back()
- end
- }
- --[[ ----- begin the main work -------------------------
- --]]
- io.input("maze.dat")
- io.output("debug.txt")
- local lines = {}
- -- read the file into table 'lines'
- -- split by lines and each line by commas
- -- ignore comment lines in the line that begin with "#"
- for line in io.lines() do
- if not startsWith(line, "#") then
- row = split(line,",")
- table.insert(lines, row)
- end
- end
- io.input():close()
- -- build the maze
- --remove the maze sizes from the list
- rows = lines[1][1]
- table.remove(lines,1)
- io.write(sf("%d\n",rows))
- columns = lines[1][1]
- table.remove(lines,1)
- io.write(sf("%d\n",columns))
- for i, l in ipairs(lines) do
- jstart,jend,jstep,turn = stripe(i,table.getn(l))
- for j = jstart,jend, jstep do
- actionIndex = l[j]
- io.write(sf("(%d %d)",j,actionIndex))
- --action[actionIndex]()
- end
- --turn()
- io.write("\n")
- -- return
- end
- io.output():close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement