cau/rules.py
author Eugen Sawin <sawine@me73.com>
Fri, 31 Dec 2010 02:56:40 +0100
changeset 8 0db344245ac2
parent 7 95ea605276a3
child 11 a131769728f1
permissions -rw-r--r--
Streamlined program interface.
sawine@7
     1
import copy
sawine@7
     2
from grid import Grid
sawine@7
     3
sawine@8
     4
class Rule1(object):
sawine@7
     5
sawine@7
     6
	def iterate(self, oldgrid):
sawine@7
     7
		grid = copy.deepcopy(oldgrid)
sawine@7
     8
		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
sawine@7
     9
			for y in xrange(oldgrid.miny - 1, oldgrid.maxy + 2):
sawine@7
    10
				#print x, y, self.neighbours(oldgrid, x, y)
sawine@7
    11
				n = self.neighbours(oldgrid, (x, y))
sawine@7
    12
				if n > 2:
sawine@7
    13
					grid.set((x, y))					
sawine@7
    14
		return grid
sawine@7
    15
sawine@7
    16
	def neighbours(self, grid, (testx, testy)):
sawine@7
    17
		n = 0
sawine@7
    18
		#print "testing ", testx, testy
sawine@7
    19
		for x in range(testx - 1, testx + 2):
sawine@7
    20
			for y in range(testy - 1, testy + 2):
sawine@7
    21
				if (x, y) in grid.cells:
sawine@7
    22
					n += 1
sawine@7
    23
				#print x, y, n
sawine@7
    24
		return n 
sawine@7
    25
	
sawine@7
    26
sawine@7
    27
class Rule2(object):
sawine@7
    28
sawine@7
    29
	def iterate(self, oldgrid):
sawine@7
    30
		grid = copy.deepcopy(oldgrid)
sawine@7
    31
		for x in xrange(oldgrid.minx - 1, oldgrid.maxx + 2):
sawine@7
    32
			for y in xrange(oldgrid.miny, oldgrid.maxy + 2):
sawine@7
    33
				#print "testing ", x, y,
sawine@7
    34
				if (x+1, y) in oldgrid.cells or (x, y-1) in oldgrid.cells:
sawine@7
    35
					grid.set((x, y), 1)								
sawine@7
    36
		return grid
sawine@7
    37
sawine@7
    38
import random
sawine@7
    39
import marshal
sawine@7
    40
sawine@7
    41
class PotentialGrowth(object):
sawine@7
    42
		
sawine@7
    43
	def __init__(self):
sawine@7
    44
		random.seed()
sawine@8
    45
		self.potential = 4 # values 4-8 are viable
sawine@7
    46
sawine@7
    47
	def iterate(self, oldgrid):
sawine@7
    48
		grid = oldgrid
sawine@7
    49
		value_pos = {}
sawine@8
    50
		for i in range(2, self.potential+1):
sawine@7
    51
			if i in oldgrid.valuemap:
sawine@7
    52
				value_pos[i] = copy.deepcopy(oldgrid.valuemap[i])
sawine@8
    53
		for value, cells in value_pos.iteritems():
sawine@8
    54
			for pos in cells:
sawine@8
    55
				new_pos, new_value = self.grow(grid, pos)
sawine@8
    56
				grid.set(new_pos, new_value)
sawine@8
    57
				grid.set(pos, value - 1)				
sawine@7
    58
		return grid
sawine@7
    59
sawine@7
    60
	def grow(self, grid, (x, y)):
sawine@7
    61
		n1 = [(x-1, y), (x, y+1), (x+1, y), (x, y-1)]
sawine@7
    62
		n2 = [(x+1, y+1), (x+1, y-1), (x-1, y-1), (x-1, y+1)]
sawine@7
    63
		c0 = (n1, )
sawine@7
    64
		c1 = (n1, n2)
sawine@7
    65
		c2 = (n1, n1, n2)
sawine@7
    66
		neighbours = random.choice(random.choice((c0, c1, c2)))
sawine@7
    67
		neighbours = [n for n in neighbours if n not in grid.cells]
sawine@7
    68
		if len(neighbours):
sawine@7
    69
			pos = random.choice(neighbours)	
sawine@8
    70
			value = self.potential
sawine@7
    71
		else:
sawine@7
    72
			pos = (x, y)
sawine@7
    73
			value = grid.cells[pos]
sawine@7
    74
		return pos, value
sawine@8
    75
sawine@8
    76