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