Advertisement
elcocodrilotito

práctica3 (definitiva)(creo)

May 8th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.55 KB | None | 0 0
  1. #Athenea Beltrán y Daniel Bedialauneta
  2. import random
  3.  
  4. class Population:
  5.     """Population of cells in a field
  6.  
  7.   Attributes (data representation):
  8.  
  9.       THIS MUST BE DEFINED
  10.  
  11.   Methods (interface specification):
  12.  
  13.       Population (__init__): returns a new field of dimensions (xsize, ysize) - by default: (50, 50)
  14.                              the field wraps like a sphere if wrap is True - by default: True
  15.                              evolution rules provided through a string birth/survival - by default: 3/23
  16.  
  17.       apply_wrap : makes the field to wrap like a sphere and updates cell bounds according to it
  18.  
  19.       is_wrap_active : returns True if wrap is active, otherwise it returns False
  20.  
  21.       load_evolution_rules : sets the evolution rules from a string birth/survival
  22.  
  23.       get_rules : returns a string birth/survival with the evolution rules
  24.  
  25.       size : returns a tuple (xsize, ysize) with the dimensions of the field
  26.  
  27.       is_alive : tells whether the cell in position (i,j) is alive or not
  28.  
  29.       is_legal : tells whether position (i,j) is within the limits of the field
  30.  
  31.       add : adds a live cell at position (i,j)
  32.  
  33.       remove : removes the live cell at position (i,j)
  34.  
  35.       len : returns the number of live cells
  36.  
  37.       clear : removes all live cells
  38.  
  39.       gen_random : produces a random field of live cells
  40.  
  41.       load : adds to the field all the cells of a list provided by the user
  42.  
  43.       get_as_sorted_list : returns a list of ordered tuples (i,j) with the positions of live cells
  44.  
  45.       resize : resizes the field to new dimensions xsize, ysize
  46.                if the field gets smaller, it removes all the cells that fall outside
  47.  
  48.       step : applies the evolution rules to get the next generation of live cells
  49.  
  50.   """
  51.  
  52.     def __init__(self, xsize=50, ysize=50, wrap=True, rules_str="3/23"):
  53.         self.field=set()
  54.         self.x_size = xsize
  55.         self.y_size = ysize
  56.         self.wrap = wrap
  57.         self.load_evolution_rules(rules_str)
  58.    
  59.     def apply_wrap(self,wrap):
  60.         self.wrap=wrap
  61.  
  62.     def is_wrap_active(self):
  63.         return self.wrap
  64.  
  65.     def load_evolution_rules(self,rules_str):
  66.         self.rules=rules_str
  67.         lista=rules_str.split("/")
  68.         self.birth=lista[0]
  69.         self.survival=lista[1]
  70.  
  71.     def get_rules(self):
  72.         return self.rules
  73.  
  74.     def size(self):
  75.         return self.x_size , self.y_size
  76.  
  77.     def is_alive(self,i,j):
  78.         if (i,j) in self.field:
  79.             return True
  80.         else:
  81.             return False
  82.  
  83.     def is_legal(self,i,j):
  84.         if self.wrap:
  85.             return True
  86.         else:
  87.             if i>self.x_size or j>self.y_size:
  88.                 return False
  89.             else:
  90.                 return True
  91.            
  92.     def add(self,i,j):
  93.         self.field.add((i,j))
  94.  
  95.     def remove(self,i,j):
  96.         self.field.discard((i,j))
  97.  
  98.     def __len__(self):
  99.         return len(self.field)
  100.  
  101.     def clear(self):
  102.         self.field.clear()
  103.  
  104.     def gen_random(self):
  105.         for i in range(self.x_size):
  106.             for j in range(self.y_size):
  107.                 a=random.random()
  108.                 if a>=0.5:
  109.                     self.add(i,j)
  110.  
  111.     def load(self,live_cells):
  112.         for i in live_cells:
  113.             self.add(i[0],i[1])
  114.  
  115.     def get_as_sorted_list(self):
  116.         lista=list(self.field)
  117.         lista.sort()
  118.         return lista
  119.  
  120.     def resize(self,xsize,ysize):
  121.         if xsize<self.x_size or ysize<self.y_size:
  122.             self.x_size=xsize
  123.             self.y_size=ysize
  124.             for i in self.field:
  125.                 if not self.is_legal(i[0],i[1]):
  126.                     self.remove(i[0],i[1])
  127.         else:
  128.             self.x_size=xsize
  129.             self.y_size=ysize
  130.  
  131.    
  132.     def __contar(self,i,j):
  133.         contador=0
  134.         for k in i-1,i,i+1:
  135.             for l in j-1,j,j+1:
  136.                 if self.wrap:
  137.                     k=k%self.x_size
  138.                     l=l%self.y_size
  139.                 if self.is_legal(k,l) and (k,l)!=(i,j) and self.is_alive(k,l):
  140.                     contador+=1
  141.         return contador
  142.  
  143.     def step(self):
  144.         new_field=set()
  145.         for i in range(self.x_size):
  146.             for j in range(self.y_size):
  147.                 n=self.__contar(i,j)
  148.                 if self.is_alive(i,j) and str(n) in self.survival:
  149.                     new_field.add((i,j))
  150.                 elif not self.is_alive(i,j) and str(n) in self.birth:
  151.                     new_field.add((i,j))
  152.         self.field=new_field
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement