Ejemplo n.º 1
0
    def get_shortest_path(self, start, end, avoid):
        if start == end: return end
        if self.is_wall(start) or self.is_wall(end): return None

        queue = PriorityQueue()

        queue.add(start, 0)

        inverted_tree = {}
        movement_costs = {}

        inverted_tree[start] = None
        movement_costs[start] = 0

        while not queue.is_empty():
            current = queue.poll()

            neighbours = self.get_neighbours(current)
            for direction in Direction.ORDERED_DIRECTIONS:
                neighbour = neighbours[direction]
                if self.is_wall(neighbour) or (avoid and (neighbour in avoid)):
                    continue
                cost = movement_costs[current] + 1
                if (neighbour not in movement_costs) or (
                        cost < movement_costs[neighbour]):
                    movement_costs[neighbour] = cost
                    queue.add(
                        neighbour, cost +
                        mod_taxi_cab_distance(neighbour, end, self.get_width(),
                                              self.get_height()))
                    inverted_tree[neighbour] = current

            if current == end:
                path = []
                cursor = end
                peek_cursor = inverted_tree[cursor]
                while peek_cursor:
                    path.append(cursor)
                    cursor = peek_cursor
                    peek_cursor = inverted_tree[cursor]
                path.reverse()
                return path

        return None
Ejemplo n.º 2
0
 def get_taxicab_distance(self, start, end):
     return mod_taxi_cab_distance(start, end, self.width, self.height)