Hi
#14
No real thing such as hard. But advanced would involve stuff like tables and coroutines, not just functions.

[lua]virtual_map = {}
virtual_map[1] = {name = "Place 1", ways = {down = 4, right = 2}}
virtual_map[2] = {name = "Place 2", ways = {down = 5, left = 1, right = 3}}
virtual_map[3] = {name = "Place 3", ways = {down = 6, left = 2}}
virtual_map[4] = {name = "Place 4", ways = {up = 1, down = 7, right = 5}}
virtual_map[5] = {name = "Place 5", ways = {up = 2, down = 8, left = 4, right = 6}}
virtual_map[6] = {name = "Place 6", ways = {up = 3, down = 9, left = 5}}
virtual_map[7] = {name = "Place 7", ways = {up = 4, right = 8}}
virtual_map[8] = {name = "Place 8", ways = {up = 5, left = 7, right = 9}}
virtual_map[9] = {name = "Place 9", ways = {up = 6, left = 8}}

function create_searcher(value, way)
local way = way or {}
return {current = value, way = way}
end

function create_path(start, goal)
local open_set = {create_searcher(start)}
local closed_set = {}
while #open_set > 0 do
local new_finder = {}
for index, value in pairs(open_set) do
if virtual_map[value.current] then
local ways = virtual_map[value.current].ways
for key, content in pairs(ways) do
if not closed_set[content] then
closed_set[content] = true
new_way = {}
table.foreach(value.way, function(index, value) table.insert(new_way, value) end)
table.insert(new_way, key)
if content == goal then
return new_way
end
table.insert(new_finder, create_searcher(content, new_way))
end
end
end
end
open_set = new_finder
end
print("Could not find a valid path from ", virtual_map[start].name, " to ", virtual_map[goal].name, ".")
end[/lua]

Not my code, couldn't be bothered fishing out one of my pieces of work.
Reply


Messages In This Thread
Hi - by Eric - 06-24-2011, 07:16 AM

Forum Jump:


Users browsing this thread: 7 Guest(s)