Exemplo n.º 1
0
def a_star(start, end):
    location = Location.get_instance()

    open_set = {start}
    came_from = dict()

    gscore = {coord: 100000 for coord in location.get_valid_for_ghosts_coords()}
    fscore = dict(gscore)  # copy of this dict

    gscore[start] = 0
    fscore[start] = h(start, end)

    while len(open_set) > 0:
        # get the coordinate with the lowest value
        current = min(open_set, key=lambda x: fscore[x])

        if current == end:
            return get_first_step(start, came_from, end)

        open_set.remove(current)
        for neighbour in [coord for coord in current.get_neighbours()]:
            tentative_gScore = gscore[current] + 1

            if tentative_gScore < gscore[neighbour]:
                came_from[neighbour] = current
                gscore[neighbour] = tentative_gScore
                fscore[neighbour] = gscore[neighbour] + h(neighbour, end)
                if neighbour not in open_set:
                    open_set.add(neighbour)
Exemplo n.º 2
0
    def move(self, movement):
        locations = Location.get_instance()

        next_coord = Coordinate(self.location.x, self.location.y)
        next_coord.go_in_direction(movement)

        if not Movement.inverses(
                self.movement,
                movement) and locations.is_valid_for_ghosts(next_coord):
            self.movement = movement
        super().move(self.movement)