1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
import matplotlib.pyplot as plt
import numpy as np
import random as rd[ATTACH=CONFIG]636683[/ATTACH]
########################################################
########################################################
def init_Labyrinthe(n) :
"""Initialisation du Labyrinthe : pose des murs exterieurs"""
Laby = np.zeros((n, n), dtype = int)
for i in range (n):
Laby[0,i] = 1
Laby[n-1,i] = 1
Laby[i,0] = 1
Laby[i,n-1] = 1
return Laby
########################################################
def matrice_Trou (n) :
"""Création de la Matrice receuillant l'emplacement des Trous"""
Matrou = np.zeros((n, n), dtype = int)
return Matrou
########################################################
def entree_Sortie (n, Matrou, Laby) :
"""Definition de l'entrée et la sortie"""
Debut = rd.randint(1, n-2)
Laby[Debut,0] = 0
Matrou[Debut,1] , Matrou[Debut,0] = 1,1
Arrivee = rd.randint(1, n-2)
Laby[Arrivee,n-1] = 0
Matrou[Arrivee,n-2],Matrou[Arrivee,n-1] = 1,1
########################################################
def labyrinthe_Final (n,fps) :
"""Creation du Labyrinthe de taille n"""
Matrou = matrice_Trou(n)
Laby = init_Labyrinthe(n)
entree_Sortie(n, Matrou, Laby)
img = mur((1,1), (n-2,n-2), Laby, Matrou)
afficher(Laby, Matrou)
########################################################
def afficher (labyrinthe, Matrou):
"""Permet l'affichage de la matrice sous forme de labyrinthe
et aussi de l'affichage de la matrice de trou"""
# plt.subplot(2,1,2)
plt.imshow(labyrinthe, cmap='GnBu')
plt.axis('off')
# plt.subplot(2,1,1)
# plt.imshow(Matrou, cmap='Greys')
# plt.imshow(labyrinthe, cmap='GnBu', alpha=0.6)
# plt.axis('off')
plt.show()
########################################################
def mur_Horizontal(GH, DB, Laby, Matrou):
"""Création de mur horizontal
GH = (ligne, colonne)"""
rdLigne=0
while Matrou[rdLigne, GH[1]] == 1 or Matrou[rdLigne, DB[1]] == 1 or rdLigne == 0:
rdLigne = rd.randint(GH[0]+1,DB[0]-1)
rdTrou = rd.randint(GH[1],DB[1])
Laby[rdLigne,GH[1]:DB[1]+1] = 1
Laby[rdLigne,rdTrou] = 0
Matrou[rdLigne-1:rdLigne+1 +1, rdTrou] = 1
return rdLigne
########################################################
def mur_Vertical(GH, DB, Laby, Matrou):
"""Création de mur vertical
GH = (ligne, colonne)"""
rdColonne=0
while Matrou[GH[0], rdColonne] == 1 or Matrou[DB[0], rdColonne] == 1 or rdColonne == 0:
rdColonne = rd.randint(GH[1]+1,DB[1]-1)
rdTrou = rd.randint(GH[0],DB[0])
Laby[GH[0]:DB[0]+1,rdColonne] = 1
Laby[rdTrou,rdColonne] = 0
Matrou[rdTrou, rdColonne-1:rdColonne+1 +1] = 1
return rdColonne
########################################################
def mur (GHinit, DBinit, Laby, Matrou):
n=len(Matrou[0])
attente = []
attente.append ((GHinit , DBinit))
while attente != [] :
#afficher2(Laby, attente, Matrou)
(GH,DB) = attente.pop()
if not( DB[0]-GH[0] >= 2 and DB[1]-GH[1] >= 2 and not(Matrou[GH[0]+1 : DB[0]-1 +1, GH[1] ] + Matrou[GH[0]+1 : DB[0]-1 +1, DB[1] ] >= 1).all() ) :
pass
elif DB[1]-GH[1] >= DB[0]-GH[0] :
colonne = mur_Vertical(GH, DB, Laby, Matrou)
attente.append((GH, (DB[0], colonne-1)))
attente.append(((GH[0], colonne+1), DB))
else :
ligne = mur_Horizontal(GH, DB, Laby, Matrou)
attente.append((GH, (ligne-1, DB[1])))
attente.append(((ligne+1, GH[1]), DB))
labyrinthe_Final(20,3) |
Partager