cau/rules.py
author Eugen Sawin <sawine@me73.com>
Thu, 30 Dec 2010 18:31:19 +0100
changeset 7 95ea605276a3
child 8 0db344245ac2
permissions -rw-r--r--
Moved cau to cau.
sawine@7
     1
import copy
sawine@7
     2
from grid import Grid
sawine@7
     3
sawine@7
     4
class Rule(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@7
    45
sawine@7
    46
	def iterate(self, oldgrid):
sawine@7
    47
		grid = oldgrid
sawine@7
    48
		value_pos = {}
sawine@7
    49
		for i in range(2, 5):
sawine@7
    50
			if i in oldgrid.valuemap:
sawine@7
    51
				value_pos[i] = copy.deepcopy(oldgrid.valuemap[i])
sawine@7
    52
		for i in range(2, 5):
sawine@7
    53
			if i in oldgrid.valuemap:
sawine@7
    54
				for cell in value_pos[i]:
sawine@7
    55
					pos = cell
sawine@7
    56
					value = i
sawine@7
    57
					new_pos, new_value = self.grow(grid, pos)
sawine@7
    58
					grid.set(new_pos, new_value)
sawine@7
    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@7
    72
			value = 4
sawine@7
    73
		else:
sawine@7
    74
			pos = (x, y)
sawine@7
    75
			value = grid.cells[pos]
sawine@7
    76
		return pos, value