Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Population:
- """Population of cells in a field
- Attributes (data representation):
- THIS MUST BE DEFINED
- Methods (interface specification):
- Population (__init__): returns a new field of dimensions (xsize, ysize) - by default: (50, 50)
- the field wraps like a sphere if wrap is True - by default: True
- evolution rules provided through a string birth/survival - by default: 3/23
- apply_wrap : makes the field to wrap like a sphere and updates cell bounds according to it
- is_wrap_active : returns True if wrap is active, otherwise it returns False
- load_evolution_rules : sets the evolution rules from a string birth/survival
- get_rules : returns a string birth/survival with the evolution rules
- size : returns a tuple (xsize, ysize) with the dimensions of the field
- is_alive : tells whether the cell in position (i,j) is alive or not
- is_legal : tells whether position (i,j) is within the limits of the field
- add : adds a live cell at position (i,j)
- remove : removes the live cell at position (i,j)
- len : returns the number of live cells
- clear : removes all live cells
- gen_random : produces a random field of live cells
- load : adds to the field all the cells of a list provided by the user
- get_as_sorted_list : returns a list of ordered tuples (i,j) with the positions of live cells
- resize : resizes the field to new dimensions xsize, ysize
- if the field gets smaller, it removes all the cells that fall outside
- step : applies the evolution rules to get the next generation of live cells
- """
- def __init__(self, xsize=50, ysize=50, wrap=True, rules_str="3/23"):
- self.field=set()
- self.x_size = xsize
- self.y_size = ysize
- self.wrap = wrap
- self.load_evolution_rules(rules_str)
- def apply_wrap(self,wrap):
- self.wrap=wrap
- def is_wrap_active(self):
- return self.wrap
- def load_evolution_rules(self,rules_str):
- self.rules=rules_str
- lista=rules_str.split("/")
- self.birth=lista[0]
- self.survival=lista[1]
- def get_rules(self):
- return self.rules
- def size(self):
- return self.x_size , self.y_size
- def is_alive(self,i,j):
- if (i,j) in self.field:
- return True
- else:
- return False
- def is_legal(self,i,j):
- if self.wrap:
- return True
- else:
- if i>self.x_size or j>self.y_size:
- return False
- else:
- return True
- def add(self,i,j):
- self.field.add((i,j))
- def remove(self,i,j):
- self.field.discard((i,j))
- def __len__(self):
- return len(self.field)
- def clear(self):
- self.field.clear()
- def gen_random(self):
- for i in range(self.x_size):
- for j in range(self.y_size):
- a=random.random()
- if a>=0.5:
- self.add(i,j)
- def load(self,live_cells):
- for i in live_cells:
- self.add(i)
- def get_as_sorted_list(self):
- lista=list(self.field)
- lista.sort()
- return lista
- def resize(self,xsize,ysize):
- if xsize<self.x_size or ysize<self.y_size:
- self.x_size=xsize
- self.y_size=ysize
- for i in self.field:
- if not self.is_legal(i[0],i[1]):
- self.remove(i[0],i[1])
- else:
- self.x_size=xsize
- self.y_size=ysize
- def __contar(self,i,j):
- contador=0
- for k in i-1,i,i+1:
- for l in j-1,j,j+1:
- if self.wrap:
- k=k%self.x_size
- l=l%self.y_size
- if self.is_legal(k,l) and k,l!=i,j and self.is_alive(k,l):
- contador+=1
- return contador
- def step(self):
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement