Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 18.20 KB | None | 0 0
  1. import math
  2. import random
  3. import numpy as np
  4.  
  5.  
  6. ########################################################################################################
  7. #PARAMETRES GENERAUX
  8.  
  9. i = 150  
  10. j = 150
  11. Munitions_max = 20      # Quantité maximale que peut avoir un humain dans son sac
  12. Médicaments_max = 10    # Quantité maximale que peut avoir un humain dans son sac      
  13. Nourriture_max = 20     # Quantité maximale que peut avoir un humain dans son sac
  14. Odorat_Z = 10           # Zone d'odorat des zombies
  15. Odorat_H = 10           # Zone d'alerte des humains
  16.  
  17. ########################################################################################################
  18. #HUMAIN  
  19.  
  20. class Humain:
  21.  
  22.     """
  23.  Classe définissant un humain, qui peux se déplacer, fuir les zombies
  24.  """
  25.    
  26.     num_humain=0
  27.    
  28.     def __init__(self):
  29.         self.nom = Humain.num_humain
  30.         Humain.num_humain += 1
  31.         self.compteur_temps=0
  32.         self.pdv=random.randint(40,100)
  33.         self.faim=100
  34.         self.compteur_faim=0
  35.         self.sac={"Munitions":4,"Médicaments":0,"Nourriture":0}  # Mun max : 30
  36.    
  37.     def __str__(self):
  38.         return "Humain " + str(self.nom)
  39.        
  40.     def __repr__(self):
  41.         return self.__str__()
  42.        
  43.     def sentir_zombie(self,carte):
  44.         """
  45.      Rend les coordonnées du zombie le plus proche
  46.      """
  47.         i_humain,j_humain = np.where(carte == self)      # Donne l'emplacement de l'humain
  48.         i_humain,j_humain = int(i_humain),int(j_humain)
  49.         self.place_matrice = i_humain,j_humain
  50.         liste_zombie = where_classe(Zombie,carte)
  51.         distance = 1000                                  # Distance supposée du zombie le plus proche
  52.         i_zombie = None
  53.         j_zombie = None
  54.         self.Zombie_le_plus_proche = (i_zombie, j_zombie)
  55.         self.liste_zombie = []
  56.         for e in range(len(liste_zombie)):
  57.             i_zombieTempo, j_zombieTempo = liste_zombie[e]
  58.             if i_zombieTempo in range((i_humain-Odorat_H),(i_humain+Odorat_H+1))\
  59.             and j_zombieTempo in range((j_humain-Odorat_H),(j_humain+Odorat_H+1)):         # L'humain est dans un voisinage +/- 10 du zombie
  60.                  self.liste_zombie +=[(i_zombieTempo,j_zombieTempo)]
  61.                  if min(math.sqrt((i_zombieTempo - i_humain)**2+(j_zombieTempo - j_humain)**2),distance)\
  62.                  ==(math.sqrt((i_zombieTempo-i_humain)**2+(j_zombieTempo-j_humain)**2)):                           # Compare la distance H-Z la plus petite trouvée
  63.                     i_zombie = i_zombieTempo                                          # Garde en mémoire les coordonnées du zombie le plus proche
  64.                     j_zombie = j_zombieTempo                                          
  65.                     distance = (math.sqrt((i_zombie - i_humain)**2 + (j_zombie - j_humain)**2))
  66.                     self.Zombie_le_plus_proche = (i_zombieTempo,j_zombieTempo)
  67.  
  68.     def Pdv(self,carte):
  69.         """
  70.      Etat de la vie de l'humain
  71.      """
  72.         i_humain, j_humain = self.place_matrice
  73.         if self.faim == 0:
  74.             if self.compteur_temps < 50:
  75.                 self.compteur_temps += 1
  76.             else:
  77.                 self.pdv -= 1
  78.                 self.compteur_temps = 0
  79.         if self.pdv<=80 and self.sac["Médicaments"]>1:
  80.             self.sac["Médicaments"]-=1
  81.             self.pdv+=20
  82.         if self.pdv==0:
  83.             carte[i_humain,j_humain]=Zombie()
  84.            
  85.     def Faim(self):
  86.         """
  87.      Etat de la faim de l'humain
  88.      """
  89.         if self.sac["Nourriture"]>=1 and self.faim<=80:
  90.                 self.sac["Nourriture"]-=1
  91.                 self.faim+=20
  92.         if self.faim==0      :
  93.             if self.compteur_faim < 20:
  94.                 self.compteur_faim += 1
  95.             else:
  96.                 self.pdv -=1
  97.                 self.compteur_faim=0
  98.  
  99.    
  100.                    
  101.                    
  102.     def shoot(self,carte):
  103.         if len(self.liste_zombie)<=2 and len(self.liste_zombie)>0 :
  104.             if self.sac["Munitions"]>=1:
  105.                 x=random.randint(0,1)
  106.                 print(x)
  107.                 if x==1:
  108.                     carte[self.Zombie_le_plus_proche]=Vide()
  109.                     self.sac["Munitions"]=self.sac["Munitions"]-1
  110.                     print(self.sac)
  111.                     print(carte[self.Zombie_le_plus_proche])
  112.                 else:
  113.                     self.sac["Munitions"]=self.sac["Munitions"]-1
  114.                     print(self.sac)
  115.                     print(carte[self.Zombie_le_plus_proche])
  116.  
  117.                
  118.    
  119.     def ravitaillement(self,carte):
  120.         """
  121.      Permet à l'humain de se ravitailler grâce à une porte si il y en a une près de lui
  122.      """
  123.         i_humain, j_humain = self.place_matrice
  124.         for x in range(-1,2): #Vérifie qu'il y a une porte autour de l'humain
  125.             if isinstance(carte[i_humain+x, j_humain+x], Batiment) and carte[i_humain+x, j_humain+x].type == "Porte":
  126.                 posx_porte = i_humain
  127.                 posy_porte = j_humain
  128.        
  129.                 if not carte[posx_porte,posy_porte].stock_medicaments == 0 :    # Si le stock n'est pas vide
  130.                     if not self.sac["Médicaments"]== Médicaments_max :          # Si le sac n'est pas plein
  131.                         if carte[posx_porte,posy_porte].stock_medicaments >= (Médicaments_max - self.sac["Médicaments"]) : # Si stock >= Place dans le sac
  132.                             carte[posx_porte,posy_porte].stock_medicaments -= (Médicaments_max - self.sac["Médicaments"])  # Vide le stock de la quantité à remplir dans le sac
  133.                             self.sac["Médicaments"] = Médicaments_max                                                      # Rempli au max le sac
  134.                         if carte[posx_porte,posy_porte].stock_medicaments < (Médicaments_max - self.sac["Médicaments"]) :  # Si stock < Place dans le sac
  135.                             self.sac["Médicaments"] += carte[posx_porte,posy_porte].stock_medicaments                      # Rempli le sac avec la taille du stock
  136.                             carte[posx_porte,posy_porte].stock_medicaments -= 0 #Médicaments_max-self.sac["Médicaments"]  # Vide entièrement le stock
  137.                    
  138.                 if not carte[posx_porte,posy_porte].stock_munitions == 0 :  # Même chose mais avec une autre ressource
  139.                     if not self.sac["Munitions"]== Munitions_max :
  140.                         if carte[posx_porte,posy_porte].stock_munitions >= (Munitions_max - self.sac["Munitions"]) :
  141.                             carte[posx_porte,posy_porte].stock_munitions -= (Munitions_max - self.sac["Munitions"])
  142.                             self.sac["Munitions"] = Munitions_max
  143.                         if carte[posx_porte,posy_porte].stock_munitions < (Munitions_max - self.sac["Munitions"]) :
  144.                             self.sac["Munitions"] += carte[posx_porte,posy_porte].stock_munitions
  145.                             carte[posx_porte,posy_porte].stock_munitions = 0     #Probleme: tu remet à 0 le nombres de munitions....
  146.        
  147.                 if not carte[posx_porte,posy_porte].stock_nourriture == 0 :  # Même chose mais avec une autre ressource
  148.                     if not self.sac["Nourriture"]== Nourriture_max :
  149.                         if carte[posx_porte,posy_porte].stock_nourriture >= (Nourriture_max - self.sac["Nourriture"]) :
  150.                             carte[posx_porte,posy_porte].stock_nourriture -= (Nourriture_max - self.sac["Nourriture"])
  151.                             self.sac["Nourriture"] = Nourriture_max
  152.                         if carte[posx_porte,posy_porte].stock_nourriture < (Nourriture_max - self.sac["Nourriture"]) :
  153.                             self.sac["Nourriture"] += carte[posx_porte,posy_porte].stock_nourriture
  154.                             carte[posx_porte,posy_porte].stock_nourriture = 0
  155.        
  156.  
  157.     def move(self,carte,i_destination, j_destination):
  158.         """
  159.      Déplace un humain vers nouvelles coordonnées\
  160.      destinaton_i, destination_j par un chemin possible
  161.      """
  162.        
  163.         i_humain, j_humain = self.place_matrice_temporaire        
  164.         i_prox, j_prox = i_humain, j_humain
  165.         i_boussole = 0                                  # Coordonnées de la direction
  166.         j_boussole = 0
  167.        
  168.         if i_humain < i_destination :                   # Analyse du chemin idéal
  169.             i_prox += 1
  170.             i_boussole = 1
  171.            
  172.         elif i_humain > i_destination :
  173.             i_prox -= 1
  174.             i_boussole = -1
  175.            
  176.         if j_humain < j_destination :
  177.             j_prox += 1
  178.             j_boussole = 1
  179.            
  180.         elif j_humain > j_destination :
  181.             j_prox -= 1
  182.             j_boussole = -1
  183.            
  184.        
  185.         if isinstance((carte[i_prox,j_prox]),Vide):                   # Déplacement prévu
  186.             carte[i_prox,j_prox] = self
  187.             #self.place_matrice=i_prox,j_prox
  188.             carte[i_humain,j_humain] = Vide()
  189.             print(carte[i_prox,j_prox],carte[i_humain,j_humain])
  190.            
  191.         elif isinstance(carte[i_prox - i_boussole ,j_prox],Vide):   # Alternative si chemin obstrué
  192.             carte[i_prox - i_boussole ,j_prox] = self
  193.             #self.place_matrice=i_prox - i_boussole ,j_prox
  194.             carte[i_humain,j_humain] = Vide()
  195.             print(carte[i_prox - i_boussole ,j_prox],carte[i_humain,j_humain])
  196.         elif isinstance(carte[i_prox,j_prox - j_boussole],Vide):
  197.             carte[i_prox ,j_prox - j_boussole] = self
  198.             #self.place_matrice=i_prox ,j_prox - j_boussole
  199.             carte[i_humain,j_humain] = Vide()
  200.             print(carte[i_prox ,j_prox - j_boussole],carte[i_humain,j_humain])
  201.            
  202.        
  203.            
  204.    
  205.     def run(self,carte):
  206.         """
  207.      Lance la capacité de l'humain à se déplacer
  208.      """
  209.         i_humain, j_humain = self.place_matrice
  210.         self.place_matrice_temporaire = self.place_matrice
  211.         if len(self.liste_zombie) == 0:                         # Si il n'y a pas de zombie à l'horizon
  212.             if self.sac["Munitions"] < 1:                        # Déplacement vers une porte quelconque
  213.                 i_porte, j_porte = trouve_batiment("Armurerie",i_humain,j_humain,carte)          # Fonction qui rend toute les portes
  214.                 self.move(carte, i_porte, j_porte)
  215.            
  216.             elif self.faim < 30 and self.sac["Nourriture"] == 0 : # Déplacement vers n'importe quelle porte
  217.                 i_porte, j_porte = trouve_porte(carte)          # Fonction qui rend toute les portes
  218.                 self.move(carte, i_porte, j_porte)
  219.            
  220.             elif self.pdv < 40 and self.sac["Médicaments"]==0:
  221.                 i_porte, j_porte = trouve_batiment("Hôpital",i_humain,j_humain,carte)
  222.                 self.move(carte,i_porte, j_porte)
  223.             else:
  224.                 i_porte, j_porte = trouve_batiment("Maison",i_humain,j_humain,carte)
  225.                 self.move(carte,i_porte, j_porte)
  226.  
  227. ########################################################################################################  
  228. #FONCTIONS AUXILLIARES
  229.  
  230. def where_classe(classe,carte):
  231.     L=[]
  232.     for k in range(0,len(carte)):
  233.         for i in range(0,len(carte)):
  234.             if isinstance(carte[k,i],classe):
  235.                 L+=[(k,i)]
  236.     return L        
  237.    
  238. def trouve_porte(carte):
  239.     """
  240.  Rend la liste des coordonnées de l'ensemble des portes des batiments de la carte
  241.  """
  242.     LR = []
  243.     for ligne in range (i):
  244.         for colonne in range(j):
  245.             if isinstance(carte[ligne,colonne],Batiment) and carte[ligne,colonne].type == "Porte":
  246.                 LR.append((ligne,colonne))
  247.     return LR
  248.  
  249. def liste_porte_Bat(carte,type_batiment):
  250.     """
  251.  Rend la liste des coordonnées de l'ensemble des portes des batiments convoités
  252.  """
  253.     liste_portes_desirees = []
  254.     liste_toutes_portes = trouve_porte(carte)
  255.    
  256.     for i_porte,j_porte in liste_toutes_portes:
  257.         for x in range(-1,2):
  258.             if isinstance(carte[i_porte+x, j_porte+x], Batiment)\
  259.             and carte[i_porte+x, j_porte+x].type == type_batiment:
  260.                 liste_portes_desirees.append((i_porte,j_porte))
  261.     return liste_portes_desirees      
  262.        
  263. def trouve_batiment(type_batiment,i_humain,j_humain,carte):
  264.     """
  265.  Rend les coordonnées de la porte du batiment convoité le plus proche
  266.  """
  267.     L = liste_porte_Bat(carte, type_batiment)
  268.     distance = 1000
  269.     Bat_proche = None,None
  270.     for k in range(len(L)):
  271.         i_porte,j_porte = L[k]
  272.         if min(math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2),distance)\
  273.             ==(math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2)):
  274.                 distance = math.sqrt((i_porte-i_humain)**2+(j_porte-j_humain)**2)
  275.                 Bat_proche = i_porte,j_porte
  276.     return Bat_proche
  277.    
  278.    
  279.            
  280. #####################################################################################################
  281. #ZOMBIE
  282.  
  283. class Zombie:
  284.  
  285.     """
  286.  Classe définissant un zombie, qui peux se déplacer et cherche à tuer les humains
  287.  """
  288.    
  289.     num_zombie=0
  290.    
  291.     def __init__(self):
  292.         self.nom = Zombie.num_zombie                        # Attribue le nom à chaque zombie
  293.         Zombie.num_zombie += 1                              # Compteur pour différencier chaque zombie
  294.        
  295.     def __str__(self):
  296.         return "Zombie " + str(self.nom)
  297.        
  298.     def __repr__(self):
  299.         return self.__str__()                               # Affiche le zombie par son nom dans le tableau
  300.    
  301.     def sentir(self, carte):
  302.         """
  303.      Trouve les coordonnées de l'humain le plus proche
  304.      """
  305.    
  306.         i_zombie,j_zombie = np.where(carte == self)         # Donne l'emplacement du zombie observé
  307.         i_zombie,j_zombie = int(i_zombie), int(j_zombie)
  308.         liste_humain = where_classe(Humain ,carte)          # Arrange cette liste en liste de tuples
  309.         self.place_matrice = i_zombie ,j_zombie
  310.         distance = 1000                                     # Distance supposée du plus proche humain
  311.         i_humain_cible = None                               # Coordonnée i de l'humain le plus proche
  312.         j_humain_cible = None                               # Coordonnée j de l'humain le plus proche
  313.        
  314.         for X in range(len(liste_humain)):                           # Parcours les positions d'humains
  315.             i_humain, j_humain = liste_humain[X]                        
  316.             if i_humain in range((i_zombie-Odorat_Z),(i_zombie+Odorat_Z+1))\
  317.             and j_humain in range((j_zombie-Odorat_Z),(j_zombie+Odorat_Z+1)):
  318.                 if min(math.sqrt((i_humain - i_zombie)**2+(j_humain - j_zombie)**2),distance)\
  319.                 ==(math.sqrt((i_humain-i_zombie)**2+(j_humain-j_zombie)**2)):                        # Compare la distance H-Z avec la plus petite trouvée
  320.                     i_humain_cible = i_humain
  321.                     j_humain_cible = j_humain                          
  322.                     distance = (math.sqrt((i_humain - i_zombie)**2 + (j_humain - j_zombie)**2))
  323.         self.coord_humain=(i_humain_cible ,j_humain_cible)
  324.        
  325.     def move(self, carte):
  326.                  
  327.         if self.coord_humain==(None,None):
  328.             compteur_boucle = 0                                 # Compteur de sortie de boucle
  329.             while compteur_boucle == 0 :
  330.                 x = random.randint(-1,1)                        # Tirage du déplacement vertical
  331.                 y = random.randint(-1,1)                        # Tirage du déplacement horizontal
  332.                 i_zombie, j_zombie = self.place_matrice
  333.                 i1 = i_zombie + x
  334.                 j1 = j_zombie + y
  335.                 if (i1 in range(i)) and (j1 in range(j)):       # Vérifie que le déplacement voulu ne sort pas de la map
  336.                     if isinstance(carte[i1,j1], Vide):                      
  337.                         carte[i1,j1] = self
  338.                         carte[i_zombie,j_zombie] = Vide()
  339.                         self.place_matrice=i1,j1
  340.                         compteur_boucle += 1
  341.         else:
  342.             A1,B1=self.coord_humain
  343.             A,B=self.place_matrice
  344.             A2,B2=0,0
  345.             A1,B1,A2,B2=nouveau_coordonnées(A1,B1,A,B)
  346.             if isinstance(carte[A2,B2], Vide):
  347.                 carte[A2,B2]=self
  348.                 carte[A,B]=Vide()
  349.             if isinstance(carte[A2,B2], Humain):
  350.                 x=random.randint(0,1)
  351.                 if x==1:
  352.                     print("Un humain se fait dévorer!")
  353.                     carte[A2,B2]=self
  354.                     carte[A,B]=Vide()
  355.                 else:
  356.                     print("Un humain devient un zombie!")
  357.                     carte[A2,B2]=self
  358.                     carte[A,B]=Zombie()
  359.                        
  360. def nouveau_coordonnées(H1,H2,Z1,Z2):
  361.     if H1<Z1:
  362.         Z1-=1
  363.     if H1>Z1:
  364.         Z1+=1
  365.     if H2<Z2:
  366.         Z2-=1
  367.     if H2>Z2:
  368.         Z2+=1
  369.     return (H1,H2,Z1,Z2)    
  370.    
  371. ########################################################################################################  
  372. #BATIMENTS
  373.    
  374. class Batiment:
  375.     """
  376.  Création de la classe Bâtiment qui comprend les différents types de batiments
  377.  """
  378.    
  379.     def __init__(self, taille_batiment): # Définit le type des bâtiments selon leur taille
  380.         if taille_batiment == 1:
  381.             self.type = "Porte"        
  382.         elif taille_batiment in range(4,7):
  383.             self.type = "Maison"
  384.         elif taille_batiment == 7 :
  385.             self.type = "Armurerie"
  386.         elif taille_batiment in range(8,12):
  387.             self.type = "Magasin"
  388.         elif taille_batiment == 18:
  389.             self.type = "Hôpital"
  390.        
  391.            
  392.         if self.type == "Porte":        #Stocks des portes
  393.             self.stock_medicaments = 0
  394.             self.stock_nourriture = 0
  395.             self.stock_munitions = 0
  396.            
  397.        
  398.     def __str__(self):                  # Fait en sorte que leur nom soit leur type
  399.         return self.type
  400.        
  401.     def __repr__(self):
  402.         return self.__str__()
  403.        
  404. ########################################################################################################  
  405. #VIDE
  406.        
  407. class Vide:
  408.     def __repr__(self):
  409.         return "Vide"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement