fromrandomimportrandintfromturtleimport*fromtimeimportsleepclassCellule:def__init__(self,murNord,murEst,murSud,murOuest):self.murs={'N':murNord,'E':murEst,'S':murSud,'O':murOuest}# création du serialiseur (méthode spéciale pour convertir en texte)
def__str__(self):return"("+str(self.murs['N'])+","+str(self.murs['E'])+","+str(self.murs['S'])+","+str(self.murs['O'])+")"classLabyrinthe:def__init__(self,hauteur,longueur):self.grille=[]foriinrange(hauteur):ligne=[]forjinrange(longueur):cellule=Cellule(True,True,True,True)ligne.append(cellule)self.grille.append(ligne)defcreer_passage(self,c1_lig,c1_col,c2_lig,c2_col):cellule1=self.grille[c1_lig][c1_col]cellule2=self.grille[c2_lig][c2_col]# cellule2 au Nord de cellule1
ifc1_lig-c2_lig==1andc1_col==c2_col:cellule1.murs['N']=Falsecellule2.murs['S']=False# cellule2 à l’Ouest de cellule1
elifc1_lig==c2_ligandc1_col-1==c2_col:cellule1.murs['O']=Falsecellule2.murs['E']=False# cellule2 au Sud de cellule1
elifc1_lig==c2_lig-1andc1_col==c2_col:cellule1.murs['S']=Falsecellule2.murs['N']=False# cellule2 à l’Est de cellule1
elifc1_lig==c2_ligandc1_col==c2_col-1:cellule1.murs['E']=Falsecellule2.murs['O']=Falsedefcreer_labyrinthe(self,lg,col,haut,long):ifhaut==1:# Cas de base
forkinrange(col,col+long-1):self.creer_passage(lg,k,lg,k+1)eliflong==1:# Cas de base
forkinrange(lg,lg+haut-1):self.creer_passage(k,col,k+1,col)else:# Appels récursifs
ifhaut>=long:haut1=haut//2lg2,haut2=lg+haut1,haut-haut1self.creer_labyrinthe(lg,col,haut1,long)self.creer_labyrinthe(lg2,col,haut2,long)p=col+randint(0,long-1)self.creer_passage(lg2-1,p,lg2,p)else:long1=long//2col2,long2=col+long1,long-long1self.creer_labyrinthe(lg,col,haut,long1)self.creer_labyrinthe(lg,col2,haut,long2)p=lg+randint(0,haut-1)self.creer_passage(p,col2-1,p,col2)# création du serialiseur (méthode spéciale pour convertir en texte)
def__str__(self):s=""forlginrange(len(self.grille)):forcolinrange(len(self.grille[0])):s=s+str(self.grille[lg][col])s=s+"\n"returns# création d'une méthode pour afficher le labyrinthe dans une fenêtre
defaffiche(self):iflen(self.grille)>0:clear()forlginrange(len(self.grille)):forcolinrange(len(self.grille[0])):c=self.grille[lg][col]x,y=col*10,-lg*10m={'N':[x,y,0],'S':[x,y-10,0],'E':[x+10,y,270],'O':[x,y,270]}fordinm.keys():ifc.murs[d]:up()goto(m[d][0],m[d][1])down()setheading(m[d][2])forward(10)sleep(10)
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.