#!/usr/bin/env python3 # TP1 info601 # NOM : Alan Turing # groupe L3-info-00101101 import re import sys def find_path_fixed(M): """cherche une solution de durée minimale pour sortir du labyrinthe `M` `M` est un tableau à 2 dimension, et `M[i][j]` donne le j-ème caractère de la i-ème ligne. Par exemple, pour #@##### #...x.# #.....# #####$# on a `M[0][3]` donne le caractère `x`. ATTENTION : l'entrée et la sortie ne sont pas dans le tableau ! La fonction doit renvoyer un tableau avec les positions du personnage aux temps 0, 1, ..., t """ ... return [] def find_path(M): """cherche une solution de durée minimale pour sortir du labyrinthe `M` `M` est un tableau à 2 dimension, et `M[i][j]` donne le j-ème caractère de la i-ème ligne. Par exemple, pour #@##### #...x.# #.....# #####$# on a `M[0][3]` donne le caractère `x`. ATTENTION : l'entrée et la sortie ne sont pas dans le tableau ! La fonction doit renvoyer un tableau avec les positions du personnage aux temps 0, 1, ..., t """ ... return [] def show_path(M, C): """affiche toutes les étapes de la solution `C` pour le labyrinthe `M`""" """Attention, la version fournie affiche la solution, sans mettre le labyrinthe à jours.""" for i in range(len(C)): print(f"t = {i}") print_maze(M, pos=C[i], strict=False) print() def main_test(*args): """fonction de test appelée depuis la ligne de commande avec l'option -T / --test `args[0]`, `args[1]`, etc. contiennent les chaines données sur la ligne de commande. Par exemple, $ python3 obstacle-ALAN.py -T pomme poire appellera cette fonction avec `args=["pomme", "poire"]`. """ # test de la fonction print_maze: on récupère un labyrinthe dans le # fichier args[0], et les coordonnées d'une case dans args[1] et args[2] M = read_maze(args[0]) i = int(args[1]) j = int(args[2]) print_maze(M, pos=(i, j), strict=True) ######################################### # NE MODIFIEZ PAS LA SUITE DU PROGRAMME # ######################################### def random_maze(width, height, obstacle_probability=0.8, obstacles="<>v^x"): import random M = [] for _ in range(height): L = [] for _ in range(width): if random.uniform(0, 1) > obstacle_probability: L.append('.') else: L.append(random.choice(obstacles)) M.append(L) print_maze(M) def error(*args, **kwargs): kwargs["file"] = sys.stderr print(*args, **kwargs) def read_maze(filename): """read the maze from given file into a 2 dimensional array M M[i][j] gives the j-th character from the i-th line in the maze. Walls '#' are stripped, so that only the inner cells are in M. """ if filename != '-': f = open(filename) else: f = sys.stdin i = 1 line = f.readline() if not re.match("^#[ @]#*$", line): error(f"Line {i} from {filename} is invalid: '{line}'.") sys.exit(1) width = len(line) - 2 M = [] for line in f: i += 1 # check for last line of maze if re.match("^#*[S $]#$", line): if len(line) != width + 2: error(f"Line {i} from {filename} is invalid: '{line}'.") error(f"Expected {width+2} characters, got {len(line)}.") sys.exit(1) break # check validity of the (inner) line if not re.match("^#[.>v^x": error(f"OBSTACLES argument can only contain < > v ^ x, got '{c}'") exit(3) random_maze(width, height, obstacle_proba, obstacles) else: if len(args) > 1: error(f"expected a single file argument, got {len(args)}") help(argv[0]) exit(3) if len(args) == 0: filename = "-" # stdin else: filename = args[0] M = read_maze(filename) if fixed: C = find_path_fixed(M) else: C = find_path(M) t = len(C) - 1 if t < 0: print("*** NO SOLUTION FOUND!") return assert t > 0 and C if show_sol: show_path(M, C) else: print(f">>> solution found in {t} steps") if __name__ == "__main__": main(sys.argv) # vim: textwidth=100 foldmethod=indent