sawine@7: import copy sawine@7: from grid import Grid sawine@7: sawine@8: class Rule1(object): sawine@7: sawine@7: def iterate(self, oldgrid): sawine@7: grid = copy.deepcopy(oldgrid) sawine@7: for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2): sawine@7: for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2): sawine@7: #print x, y, self.neighbours(oldgrid, x, y) sawine@7: n = self.neighbours(oldgrid, (x, y)) sawine@7: if n > 2: sawine@7: grid.set((x, y)) sawine@7: return grid sawine@7: sawine@7: def neighbours(self, grid, (testx, testy)): sawine@7: n = 0 sawine@7: #print "testing ", testx, testy sawine@7: for x in range(testx - 1, testx + 2): sawine@7: for y in range(testy - 1, testy + 2): sawine@7: if (x, y) in grid.cells: sawine@7: n += 1 sawine@7: #print x, y, n sawine@7: return n sawine@7: sawine@7: sawine@7: class Rule2(object): sawine@7: sawine@7: def iterate(self, oldgrid): sawine@7: grid = copy.deepcopy(oldgrid) sawine@7: for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2): sawine@7: for y in xrange(oldgrid.miny, oldgrid.maxy + 2): sawine@7: #print "testing ", x, y, sawine@7: if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells: sawine@7: grid.set((x, y), 1) sawine@7: return grid sawine@7: sawine@7: import random sawine@7: import marshal sawine@7: sawine@7: class PotentialGrowth(object): sawine@7: sawine@7: def __init__(self): sawine@7: random.seed() sawine@8: self.potential = 4 # values 4-8 are viable sawine@7: sawine@7: def iterate(self, oldgrid): sawine@7: grid = oldgrid sawine@7: value_pos = {} sawine@8: for i in range(2, self.potential+1): sawine@7: if i in oldgrid.valuemap: sawine@7: value_pos[i] = copy.deepcopy(oldgrid.valuemap[i]) sawine@8: for value, cells in value_pos.iteritems(): sawine@8: for pos in cells: sawine@8: new_pos, new_value = self.grow(grid, pos) sawine@8: grid.set(new_pos, new_value) sawine@8: grid.set(pos, value - 1) sawine@7: return grid sawine@7: sawine@7: def grow(self, grid, (x, y)): sawine@7: n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)] sawine@7: n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)] sawine@7: c0 = (n1, ) sawine@7: c1 = (n1, n2) sawine@7: c2 = (n1, n1, n2) sawine@7: neighbours = random.choice(random.choice((c0, c1, c2))) sawine@7: neighbours = [n for n in neighbours if n not in grid.cells] sawine@7: if len(neighbours): sawine@7: pos = random.choice(neighbours) sawine@8: value = self.potential sawine@7: else: sawine@7: pos = (x, y) sawine@7: value = grid.cells[pos] sawine@7: return pos, value sawine@8: sawine@8: