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
| def robot(self) :
"""Place le robot sur la carte, de manière aléatoire"""
if len(self.labyrinthe) > 1 :
limit_maxi = len(self.labyrinthe)-1
x_aleat = randint(1,limit_maxi)
y_aleat = randint(1,limit_maxi)
position_aleat = self.labyrinthe[x_aleat][y_aleat]
while "X" not in self.labyrinthe :
if position_aleat == " " :
position_aleat = "X"
else :
x_aleat = randint(1,limit_maxi)
y_aleat = randint(1,limit_maxi)
def chargement(self) :
"""Récupère la carte choisie par l'utilisateur, et la transfère dans
une liste des lignes de la carte"""
with open(self.path +'/'+ self.nom,'r') as fichier :
self.labyrinthe = []
while 1 :
ligne = fichier.readline()
if ligne == '' :
break
else :
ligne = list(ligne)
self.labyrinthe.append(ligne)
# j'ajoute le robot :
self.robot()
def generation_aleatoire(self) :
"""Génère automatiquement des cartes aléatoires, sans garantir que le
labyrinthe sera 'faisable'"""
elts_laby = [' ','O','.',' ']
self.labyrinthe = []
# création de 15 listes de symboles aléatoires
for _ in range(15):
line = []
for _ in range(15):
line.append(choice(elts_laby))
self.labyrinthe.append(line)
for ligne in self.labyrinthe :
ligne.append('\n')
# ensuite remplacement des symboles entourant le labyrinthe par
# des 'o' pour qu'il soit "fermé"
self.labyrinthe[0] = ['O']*15
self.labyrinthe[0].append('\n')
self.labyrinthe[14] = ['O']*15
self.labyrinthe[14].append('\n')
i = 0
while i <= 14 :
self.labyrinthe[i][14] = 'O'
self.labyrinthe[i][0] = 'O'
i += 1
# + la sortie + le robot :
self.labyrinthe[12][14] = 'U'
self.robot() |
Partager