-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcarte.py
More file actions
76 lines (70 loc) · 2.92 KB
/
carte.py
File metadata and controls
76 lines (70 loc) · 2.92 KB
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
# -*-coding:Utf-8 -*
"""Ce module contient la classe Carte."""
import os
import random
class Carte:
"""Objet de transition entre un fichier et un labyrinthe."""
def __init__(self, nom, chaine):
self.nom = nom
self.grille = [[str(x) for x in line] for line in chaine.splitlines()] #list 2 dimensions.
self.coord_debut_x, self.coord_debut_y = self.robot_positiont_depart()
@classmethod
def carte_from_file(cls, chemin, nom_carte):
""" initialise la classe a partir d'un fichier """
with open(chemin, "r") as fichier:
contenu = fichier.read()
return cls(nom_carte, contenu)
def __repr__(self):
"affiche la carte par défaut"
return "<Carte {}>".format(self.nom)
def enregistre_partie(self):
"sauvegarde le fichier sur disque"
chemin = os.path.join("cartes", (self.nom +"pre"))
with open(chemin, "w") as fichier:
fichier.write('\n'.join(map(''.join, self.grille)))
def afficher_carte(self):
""" Afficher la carte en cours"""
#print('\n'.join(map(''.join, self.grille))) #si liste a deux dimension
return '\n'.join(map(''.join, self.grille))
#print('\n'.join(''.join(s) for s in grille)) #
#print(*self.grille, sep='\n') # is chaine de caratère
def robot_positiont_depart(self):
""" obtenir les cordonnée de départ"""
for coord_y, line in enumerate(self.grille):
#print(coord_x)
coord_x = [pos for pos, char in enumerate(line) if char == 'X']
if coord_x:
self.coord_debut_y = coord_y
self.coord_debut_x = coord_x[0]
#print("Position du robot au Départ est X {} Y {} ".format(
# self.coord_x, self.coord_y))
break
return self.coord_debut_x, self.coord_debut_y
def clean_robot(self):
for coord_y, line in enumerate(self.grille):
#print(coord_x)
coord_x = [pos for pos, char in enumerate(line) if char == 'X']
if coord_x:
_coord_y = coord_y
_coord_x = coord_x[0]
#print("Position du robot au Départ est X {} Y {} ".format(
# self.coord_x, self.coord_y))
break
#effacer la position indiqué dans la carte
if _coord_y != 0 and _coord_x != 0:
self.grille[_coord_y][_coord_x] = ' '
def robot_random_position(self, symbole):
""" Obtenir un position aléatoir"""
#enlever la position existante dans la carte.
_coord_y = 0
_coord_x = 0
while True:
y = random.randrange(1, len(self.grille), 1)
line = self.grille[y]
x = random.randrange(1, len(line), 1)
if self.grille[y][x] == " ":
break
_coord_y = y
_coord_x = x
self.grille[_coord_y][_coord_x] = symbole
return x, y