def solve(self, world: World, display=False) -> bool:
     """
     Solve the world provided, using a Breadth-First Search algorithm.
     From the initial configuration of the world, this methods search for a winning configuration with a minimal
     number of moves for Ariane.
     :param world: the world to solve.
     :param display: if set to True, the method will draw each step of its computation. Set it to False if you need
             to quickly compute the result.
     :return: True if there is a solution for this world, False otherwise.
     """
     conf = world.to_configuration()
     to_treat = [(conf, [])]
     self.visited.add(conf)
     while len(to_treat) > 0:
         current, dir_list = to_treat.pop(0)
         world.load_configuration(current)
         if world.game_won():
             self.solution = dir_list
             return True
         if world.game_lost():
             continue
         for direction in Direction:
             if not world.valid_direction(world.ariane, direction):
                 continue
             world.move_all(direction)
             if display:
                 view.display(world)
                 time.sleep(0.05)
             updated = world.to_configuration()
             if updated not in self.visited:
                 self.visited.add(updated)
                 # Constructing a new list containing all the directions that lead to the previous configuration,
                 # and add the direction that leads to the new configuration
                 updated_dir_list = dir_list + [dir_to_string(direction)]
                 to_treat.append((updated, updated_dir_list))
             world.load_configuration(current)
     return False
Exemplo n.º 2
0
 def __solve__(self, world: World, conf: tuple, display=False) -> bool:
     if world.game_won():
         return True
     if world.game_lost():
         return False
     self.visited.add(conf)
     for direction in Direction:
         world.load_configuration(conf)
         if not world.valid_direction(world.ariane, direction):
             continue
         else:
             world.move_all(direction)
             if display:
                 view.display(world)
                 time.sleep(0.05)
             updated_conf = world.to_configuration()
         if updated_conf in self.visited:
             continue
         if self.__solve__(world, updated_conf, display):
             self.solution.append(dir_to_string(direction))
             return True
         else:
             continue
     return False