def play_game(self, world: World) -> None: """ This method should be called after self.solve() has been called. Emulate the game by making Ariane play all the moves found in the self.solution list. :param world: the world in which the solver should play the game :return: None """ for direction in self.solution: input_dir = get_dir_from_string(direction) world.move_ariane(input_dir) world.move_thesee() view.display(world) if world.game_lost() or world.game_won(): return world.move_minos(view) time.sleep(0.3)
def player_game(lab_path: str) -> None: """ Start a new game where the player controls Ariane. The world is loaded using the file whose path has been provided. :param lab_path: path of the file to parse to create the world :return: None """ canceled_last_move = False world = create_world_from_file(lab_path) world.save_game_state() if world.load_save(): print("Save has been loaded!") view.create_window(world) view.display(world) while True: # If the player has no more chance of winning, the game is stopped if not still_remaining_options(world): break ev = upemtk.attend_ev() tev = upemtk.type_ev(ev) if tev == 'Quitte': break if tev == 'Touche': key = touche(ev) if key == 'c': if not canceled_last_move: world.cancel_move(view) canceled_last_move = True continue else: canceled_last_move = False if key == 's': world.create_save() direction = get_dir_from_string(key) if not world.move_ariane(direction): continue world.move_thesee() view.display(world) if world.game_won(): display_result(world) break world.move_minos(view) if world.game_lost(): display_result(world) break world.save_game_state() upemtk.ferme_fenetre()
def solver_game(lab_path: str, game_type: str, display: bool) -> None: """ Start a new game where one of the solver tries to solve the game, and then demonstrate its solution if it can find one. :param lab_path: path of the file to parse to create the world. :param game_type: type of solver that the user wants to use (naive or minimal) :param display: set to True, the user will visualize all the computations steps made by the solver to find a solution. :return: None """ if game_type == "naive": solver = NaiveSolver() elif game_type == "minimal": solver = MinimalSolver() else: return usage() world = create_world_from_file(lab_path) initial_config = world.to_configuration() view.create_window(world) view.display(world) if solver.solve(world, display): print("Solution found (", len(solver.solution), "moves) !") solver.display_solution() world.load_configuration(initial_config) upemtk.texte(HEIGHT // 2, WIDTH // 2, "CLICK TO SEE SOLUTION", couleur='black', ancrage='center', taille=40, tag="click_to_display") upemtk.attend_clic_gauche() upemtk.efface("click_to_display") view.display(world) solver.play_game(world) display_result(world) else: print("No solution found...") upemtk.ferme_fenetre()
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
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