Fin exercice 14
This commit is contained in:
parent
efc4cdedfa
commit
54a04af93c
|
@ -2,7 +2,7 @@
|
|||
# Chapitre 8 - Exercice Initié 14 - Sortie de labyrinthe
|
||||
#
|
||||
|
||||
from random import randint
|
||||
from random import randint, choice
|
||||
from math import sqrt, ceil, floor
|
||||
|
||||
# Changements de directions possibles depuis les
|
||||
|
@ -51,7 +51,7 @@ class Labyrinthe:
|
|||
|
||||
return f"Labyrinthe de {len(self.salles)} x {len(self.salles[0])} : {self.coord_entree} vers {self.coord_sortie}"
|
||||
|
||||
def salle(self, coords):
|
||||
def salle(self, coords) -> Salle:
|
||||
""" Renvoie la Salle correspondant aux `coords`
|
||||
passées en argument, si elle existe. """
|
||||
|
||||
|
@ -207,3 +207,19 @@ def sortie_main_droite(l: Labyrinthe):
|
|||
|
||||
print(labyrinthe_simple.montrer())
|
||||
print(sortie_main_droite(labyrinthe_simple))
|
||||
|
||||
def sortie_tremaux(l: Labyrinthe):
|
||||
nodes = [l.coord_entree]
|
||||
l.salle(nodes[-1]).visites += 1
|
||||
while nodes[-1] != l.coord_sortie:
|
||||
voisins = [coord for coord in l.voisins(nodes[-1]) if not l.salle(coord).mur]
|
||||
voisins_non_visite = [coord for coord in voisins if l.salle(coord).visites == 0]
|
||||
if voisins_non_visite:
|
||||
nodes.append(choice(voisins_non_visite))
|
||||
l.salle(nodes[-1]).visites += 1
|
||||
else:
|
||||
nodes.pop()
|
||||
return nodes
|
||||
|
||||
print(labyrinthe_complexe_1.montrer())
|
||||
print(sortie_tremaux(labyrinthe_complexe_1))
|
Loading…
Reference in a new issue