alabyrinthe.py

Created by bilouclic

Created on December 16, 2024

3.6 KB


from random import randint
from turtle import *
from time import sleep

class Cellule:
    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']) + ")"

class Labyrinthe:
    def __init__(self, hauteur, longueur):
        self.grille= []
        for i in range(hauteur):
            ligne = []
            for j in range(longueur):
                cellule = Cellule(True, True, True, True)
                ligne.append(cellule)
            self.grille.append(ligne)
    def creer_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
        if c1_lig - c2_lig == 1 and c1_col == c2_col:
            cellule1.murs['N'] = False
            cellule2.murs['S'] = False
        # cellule2 à l’Ouest de cellule1
        elif c1_lig == c2_lig and c1_col - 1 == c2_col:
            cellule1.murs['O'] = False
            cellule2.murs['E'] = False
        # cellule2 au Sud de cellule1
        elif c1_lig == c2_lig - 1 and c1_col == c2_col:
            cellule1.murs['S'] = False
            cellule2.murs['N'] = False
        # cellule2 à l’Est de cellule1
        elif c1_lig == c2_lig and c1_col == c2_col - 1:
            cellule1.murs['E'] = False
            cellule2.murs['O'] = False
    def creer_labyrinthe(self, lg, col, haut, long):
        if haut == 1 : # Cas de base
            for k in range(col,col+long-1):
                self.creer_passage(lg, k, lg, k+1)
        elif long == 1: # Cas de base
            for k in range(lg,lg+haut-1):
                self.creer_passage(k,col,k+1,col)
        else: # Appels récursifs
            if haut >= long :
                haut1 = haut//2
                lg2, haut2 = lg+haut1, haut-haut1
                self.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//2
                col2, long2 = col+long1, long-long1
                self.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 = ""
        for lg in range(len(self.grille)):
            for col in range(len(self.grille[0])):
                s = s + str(self.grille[lg][col])
            s = s + "\n"
        return s
    # création d'une méthode pour afficher le labyrinthe dans une fenêtre
    def affiche(self):
        if len(self.grille) > 0 :
            clear()
            for lg in range(len(self.grille)):
                for col in range(len(self.grille[0])):
                    c = self.grille[lg][col]
                    x, y = col*10, -lg*10
                    m = { 'N' : [x,y,0], 'S' : [x,y-10,0], 'E' : [x+10,y,270],
                    'O' : [x,y,270]}
                    for d in m.keys() :
                        if c.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:

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.