Advertisement
Guest User

Untitled

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