Tâche 2 terminée
This commit is contained in:
parent
2d21119841
commit
8202ae5e17
|
@ -28,9 +28,9 @@ aventurier = {
|
|||
'vivant': True
|
||||
}
|
||||
|
||||
## Fonctions utilitaires
|
||||
## Gestion de partie
|
||||
|
||||
def créer_salle():
|
||||
def créer_salle_random():
|
||||
"""
|
||||
Créer une salle aléatoire
|
||||
@params void
|
||||
|
@ -75,7 +75,7 @@ def peupler_donjon():
|
|||
for i in range(TAILLE_DONJON):
|
||||
ligne = []
|
||||
for j in range(TAILLE_DONJON):
|
||||
ligne.append(créer_salle())
|
||||
ligne.append(créer_salle_random())
|
||||
|
||||
if random.random() * 100 > 75: # 25% de chance
|
||||
dragons.append(créer_dragon((i,j)))
|
||||
|
@ -161,14 +161,10 @@ def calcul_chemin(donjon, position, dragons, visite):
|
|||
dragons, une liste de dragons
|
||||
@return booléen
|
||||
"""
|
||||
cid = int(random.random() * 100)
|
||||
print("[{} calcul_chemin()] position {} et visite {}".format(cid, position, visite))
|
||||
|
||||
résultats = []
|
||||
|
||||
# Déjà visité
|
||||
if position in visite:
|
||||
print("[{} calcul_chemin()] Déjà visité".format(cid))
|
||||
return None
|
||||
else:
|
||||
visite.add(position)
|
||||
|
@ -176,7 +172,6 @@ def calcul_chemin(donjon, position, dragons, visite):
|
|||
# Cas de base
|
||||
for dragon in dragons:
|
||||
if position == dragon["position"]:
|
||||
print("[{} calcul_chemin()] Dragon trouvé".format(cid))
|
||||
return ([position], dragon["niveau"])
|
||||
|
||||
# Test récursif des potentielles connexions
|
||||
|
@ -196,10 +191,8 @@ def calcul_chemin(donjon, position, dragons, visite):
|
|||
|
||||
# Aucun couple dans résultats
|
||||
if résultats == [None] * len(résultats):
|
||||
print("[{} calcul_chemin()] Aucun dragon trouvé".format(cid))
|
||||
return None
|
||||
else:
|
||||
print("[{} calcul_chemin()] Plusieurs candidats : {}".format(cid, résultats))
|
||||
niveau = 0
|
||||
candidat = (0,0)
|
||||
for couple in résultats:
|
||||
|
@ -208,7 +201,6 @@ def calcul_chemin(donjon, position, dragons, visite):
|
|||
if couple[1] > niveau:
|
||||
niveau = couple[1]
|
||||
candidat = (couple[0], couple[1])
|
||||
print("[{} calcul_chemin()] Meilleur chemin : {}".format(cid, candidat))
|
||||
return ([position] + candidat[0], candidat[1])
|
||||
|
||||
def intention(donjon, position, dragons):
|
||||
|
@ -279,7 +271,87 @@ def fin_partie(aventurier, dragons):
|
|||
return -1
|
||||
else:
|
||||
return 0
|
||||
|
||||
## Gestion des maps
|
||||
|
||||
def créer_salle(forme_salle):
|
||||
"""
|
||||
Créer une salle aléatoire (N,E,S,O)
|
||||
@params void
|
||||
@return une salle, tuple de taille 4
|
||||
"""
|
||||
|
||||
correspondance = {
|
||||
"╬" : (True,)*4,
|
||||
|
||||
"╡" : (False, False, False, True),
|
||||
"╞" : (False, True, False, False),
|
||||
"╥" : (False, False, True, False),
|
||||
"╨" : (True, False, False, False),
|
||||
|
||||
"╔" : (False, True, True, False),
|
||||
"╗" : (False, False, True, True),
|
||||
"╝" : (True, False, False, True),
|
||||
"╚" : (True, True, False, False),
|
||||
|
||||
"║" : (True, False, True, False),
|
||||
"═" : (False, True, False, True),
|
||||
|
||||
"╦" : (False, True, True, True),
|
||||
"╩" : (True, True, False, True),
|
||||
"╠" : (True, True, True, False),
|
||||
"╣" : (True, False, True, True)
|
||||
}
|
||||
|
||||
return correspondance[forme_salle]
|
||||
|
||||
|
||||
def charger(fichier):
|
||||
"""
|
||||
Charge un fichier et peuple la structure donjon
|
||||
@params fichier, chemin d'accès à un fichier
|
||||
@return 1 ou None
|
||||
"""
|
||||
global donjon
|
||||
global dragons
|
||||
|
||||
donjon.clear()
|
||||
dragons.clear()
|
||||
|
||||
# Lecture fichier brut
|
||||
contenu = ""
|
||||
with open(fichier, 'r') as fichier:
|
||||
contenu = fichier.read()
|
||||
|
||||
try:
|
||||
# Séparation entre carte et coordonnées des personnages
|
||||
donjon_map = contenu[:contenu.find("A")-1].split("\n")
|
||||
donjon_declaration = contenu[contenu.find("A"):].split("\n")
|
||||
|
||||
# Création du donjon
|
||||
for i in range(len(donjon_map[0])):
|
||||
ligne = []
|
||||
for j in range(len(donjon_map)):
|
||||
ligne.append(créer_salle(donjon_map[i][j]))
|
||||
donjon.append(ligne)
|
||||
|
||||
# Création des personnages
|
||||
for declaration in donjon_declaration:
|
||||
# Déclaration de l'aventurier
|
||||
if "A" in declaration:
|
||||
cible, x, y = declaration.split(" ")
|
||||
aventurier["position"] = (x,y)
|
||||
aventurier["niveau"] = 1
|
||||
|
||||
# Déclaration d'un dragon
|
||||
elif "D" in declaration:
|
||||
cible, x, y, niveau = declaration.split(" ")
|
||||
dragons.append({ 'position' : (x,y), 'niveau' : niveau })
|
||||
|
||||
return 1
|
||||
except Exception as e:
|
||||
print("Erreur : {}".format(e))
|
||||
return None
|
||||
|
||||
## Fonction principale
|
||||
|
||||
|
|
Loading…
Reference in New Issue