예제 #1
0
    def parse(location_txt_file):
        text_file = open(location_txt_file, "r")
        locations = []
        passive_locations = []
        paths = []
        name_to_coord = {}

        for line in text_file.readlines():
            line = line.strip()
            if len(line) == 0 or line[0] == '#':
                continue
            tokens = line.split(" ")
            command = tokens[0]

            if command == "location":
                locations.append((tokens[1], int(tokens[2]), int(tokens[3])))
                name_to_coord[tokens[1]] = (int(tokens[2]), int(tokens[3]))

            elif command == "passive_location":
                passive_locations.append(
                    (tokens[1], int(tokens[2]), int(tokens[3])))
                name_to_coord[tokens[1]] = (int(tokens[2]), int(tokens[3]))

            elif command == "path":
                path1 = tokens[1]
                path2 = tokens[2]
                paths.append((name_to_coord[path1], name_to_coord[path2]))

        return MapData(locations=locations,
                       passive_locations=passive_locations,
                       paths=paths,
                       name_to_coord=name_to_coord)
예제 #2
0
from map_data import MapData, map_40
from route_finder import RouteFinder
from space import Space

# Create the test data
map_data = MapData(intersections={
    0: [1, 2],
    1: [2, 3],
    2: [3, 4],
    3: [5, 6]
},
                   roads=[[1, 2], [0, 3], [0, 2], [1, 2]])

# Initialise the space and route finder
space = Space(map_data.intersections, map_data.roads)
finder = RouteFinder(space)

# Find a route between invalid nodes
assert finder.find_route(10, 0) == None
assert finder.find_route(0, 10) == None

# Now for the real deal
space = Space(map_40.intersections, map_40.roads)
finder = RouteFinder(space)

assert finder.find_route(5, 34) == [5, 16, 37, 12, 34]
assert finder.find_route(5, 5) == [5]
assert finder.find_route(8, 24) == [8, 14, 16, 37, 12, 17, 10, 24]