diff --git a/Chapitre 4 - Graphes/C08_labyrinthe.py b/Chapitre 4 - Graphes/C08_labyrinthe.py index 5bb1f04..34c3602 100644 --- a/Chapitre 4 - Graphes/C08_labyrinthe.py +++ b/Chapitre 4 - Graphes/C08_labyrinthe.py @@ -222,4 +222,60 @@ def sortie_tremaux(l: Labyrinthe): return nodes print(labyrinthe_complexe_1.montrer()) -print(sortie_tremaux(labyrinthe_complexe_1)) \ No newline at end of file +print(sortie_tremaux(labyrinthe_complexe_1)) + +def genere_labirinthe_aleatoire(nb_salles, max_aretes): + salles = [ [Salle() for x in range(nb_salles)] for y in range(nb_salles) ] + entree = (randint(0, nb_salles-1), randint(0, nb_salles-1)) + sortie = (randint(0, nb_salles-1), randint(0, nb_salles-1)) + l = Labyrinthe(salles, entree, sortie) + l.salle(entree).mur = False + l.salle(entree).entree = True + l.salle(sortie).mur = False + l.salle(sortie).sortie = True + + visited = [entree] + current = entree + while current != sortie: + voisins = [] + for voisin in [coord for coord in l.voisins(current) if coord not in visited]: + voisins_of_voisin = [coord for coord in l.voisins(voisin) if coord != current] + for coord in voisins_of_voisin: + if coord in visited: + break + else: + voisins.append(voisin) + + if not voisins: + for x in range(nb_salles): + for y in range(nb_salles): + l.salle((x, y)).mur = True + visited = [entree] + current = entree + continue + + current = choice(voisins) + visited.append(current) + if current != sortie: + l.salle(current).mur = False + + for i in range(nb_salles): + for j in range(nb_salles): + for voisin in l.voisins((i, j)): + if not l.salle(voisin).mur: + break + else: + current = (i, j) + a = [current] + l.salle((i, j)).mur = False + while current not in visited: + current = choice(l.voisins(current)) + l.salle(current).mur = False + a.append(current) + for x in a: + visited.append(x) + + return l + +print(genere_labirinthe_aleatoire(10, 10).montrer()) +