Advertisement
elcocodrilotito

práctoca 3

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