forked from SarevokAnchev/PowerGridStudent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerrain.py
More file actions
77 lines (68 loc) · 2.24 KB
/
Terrain.py
File metadata and controls
77 lines (68 loc) · 2.24 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
from enum import Enum
class Case(Enum):
VIDE = 0
OBSTACLE = 1
CLIENT = 2
ENTREE = 4
class Terrain:
def __init__(self):
self.largeur = 0
self.hauteur = 0
self.cases = []
def charger(self, fichier):
self.cases.clear()
with open(fichier, "r") as f:
ligne_max = 0
for ligne in f:
ligne = list(ligne)[:-1]
ligne_cases = []
n = 0
for c in ligne:
n += 1
if c == " ":
ligne_cases.append(Case.OBSTACLE)
elif c == "C":
ligne_cases.append(Case.CLIENT)
elif c == "~":
ligne_cases.append(Case.VIDE)
elif c == "E":
ligne_cases.append(Case.ENTREE)
else:
ligne_cases.append(Case.OBSTACLE)
self.cases.append(ligne_cases)
if ligne_max < n:
ligne_max = n
for i, l in enumerate(self.cases):
while len(l) < ligne_max:
self.cases[i].append(Case.OBSTACLE)
self.largeur = ligne_max
self.hauteur = len(self.cases)
def __getitem__(self, l):
return self.cases[l]
def get_clients(self) -> list[tuple[int, int]]:
clients = []
for i, l in enumerate(self.cases):
for j, c in enumerate(l):
if c == Case.CLIENT:
clients.append((i, j))
return clients
def get_entree(self) -> tuple[int, int]:
for i, l in enumerate(self.cases):
for j, c in enumerate(l):
if c == Case.ENTREE:
return (i, j)
return (-1, -1)
def afficher(self):
for l in self.cases:
for c in l:
if c == Case.OBSTACLE:
print("X", end="")
if c == Case.CLIENT:
print("C", end="")
if c == Case.VIDE:
print("~", end="")
if c == Case.ENTREE:
print("E", end="")
else:
print(" ", end="")
print()