Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/diff/08b9cff3aa10/automata.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 09:55:47 2013
Кодировка:
cca: automata.py diff

cca

diff automata.py @ 89:08b9cff3aa10

Names of modules automata and state changed for PEP-8
author darkhan
date Mon, 13 Dec 2010 22:32:10 +0300
parents Automata.py@20bf0586f8ca
children ef8839e99f34
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/automata.py	Mon Dec 13 22:32:10 2010 +0300
     1.3 @@ -0,0 +1,148 @@
     1.4 +from state import *
     1.5 +
     1.6 +class Automata(object):
     1.7 +	def __init__(self, width=150, height=70, states=None):
     1.8 +		self.width = width
     1.9 +		self.height = height
    1.10 +		if states == None:
    1.11 +			self.states = [State("Dead", '-', "white", '0', [5]),
    1.12 +					State("Alive", '+', "black", '1', [0, 1] + range(4, 9))]
    1.13 +		else:
    1.14 +			self.states = states
    1.15 +		self.symbols = {}
    1.16 +		self.st_sym = {}
    1.17 +		for num, st in enumerate(self.states):
    1.18 +			self.symbols[st.symbol] = num
    1.19 +			self.st_sym[st.symbol] = st
    1.20 +		self.field = []
    1.21 +		for row in range(height):
    1.22 +			self.field.append([])
    1.23 +			for col in range(width):
    1.24 +				self.field[row].append(self.states[0].symbol)
    1.25 +
    1.26 +	def next_step(self):
    1.27 +		changed = []
    1.28 +		for row in range(1, self.height - 1):
    1.29 +			for col in range(1, self.width - 1):
    1.30 +				symbol = self.field[row][col]
    1.31 +				num = 0
    1.32 +				for vert in range(row - 1, row + 2):
    1.33 +					for horiz in range(col - 1, col + 2):
    1.34 +						if self.field[vert][horiz] == symbol:
    1.35 +							num += 1
    1.36 +				if self.st_sym[symbol].next_state(num - 1):
    1.37 +					changed.append((row, col))
    1.38 +					
    1.39 +		for row in range(1, self.height - 1):
    1.40 +			symbol1 = self.field[row][0]
    1.41 +			symbol2 = self.field[row][self.width - 1]
    1.42 +			num1 = 0
    1.43 +			num2 = 0
    1.44 +			for vert in range(row - 1, row + 2):
    1.45 +				for horiz in [0, 1, self.width - 1]:
    1.46 +					if self.field[vert][horiz] == symbol1:
    1.47 +						num1 += 1
    1.48 +				for horiz in [self.width - 2, self.width - 1, 0]:
    1.49 +					if self.field[vert][horiz] == symbol2:
    1.50 +						num2 += 1
    1.51 +			if self.st_sym[symbol1].next_state(num1 - 1):
    1.52 +				changed.append((row, 0))
    1.53 +			if self.st_sym[symbol2].next_state(num2 - 1):
    1.54 +				changed.append((row, self.width - 1))
    1.55 +				
    1.56 +		for col in range(1, self.width - 1):
    1.57 +			symbol1 = self.field[0][col]
    1.58 +			symbol2 = self.field[self.height - 1][col]
    1.59 +			num1 = 0
    1.60 +			num2 = 0
    1.61 +			for horiz in range(col - 1, col + 2):
    1.62 +				for vert in [0, 1, self.height - 1]:
    1.63 +					if self.field[vert][horiz] == symbol1:
    1.64 +						num1 += 1
    1.65 +				for vert in [self.height - 2, self.height - 1, 0]:
    1.66 +					if self.field[vert][horiz] == symbol2:
    1.67 +						num2 += 1
    1.68 +			if self.st_sym[symbol1].next_state(num1 - 1):
    1.69 +				changed.append((0, col))
    1.70 +			if self.st_sym[symbol2].next_state(num2 - 1):
    1.71 +				changed.append((self.height - 1, col))
    1.72 +				
    1.73 +		for row, col in [(0, 0), (self.height - 1, self.width - 1),
    1.74 +						(0, self.width - 1), (self.height - 1, 0)]:
    1.75 +			symbol = self.field[row][col]
    1.76 +			num = 0
    1.77 +			for vert_long in range(row + self.height - 1, 
    1.78 +									row + self.height + 2):
    1.79 +				for horiz_long in range(col + self.width - 1, 
    1.80 +										col + self.width + 2):
    1.81 +					vert = vert_long % self.height
    1.82 +					horiz = horiz_long % self.width
    1.83 +					if self.field[vert][horiz] == symbol:
    1.84 +						num += 1
    1.85 +			if self.st_sym[symbol].next_state(num - 1):
    1.86 +				changed.append((row, col))
    1.87 +						
    1.88 +		for row, col in changed:
    1.89 +			index = (self.symbols[self.field[row][col]] + 
    1.90 +														1) %  len(self.states)
    1.91 +			self.field[row][col] = self.states[index].symbol
    1.92 +		return changed
    1.93 +
    1.94 +	def change_size(self, value, side):
    1.95 +		"0-up, 1-right, 2-down, 3-left"
    1.96 +		new_field = []
    1.97 +		
    1.98 +		if side == 0:
    1.99 +			self.height += value
   1.100 +			for row in range(value):
   1.101 +				new_field.append([])
   1.102 +				for col in range(self.width):
   1.103 +					new_field[row].append(self.states[0].symbol)
   1.104 +			init = value
   1.105 +			if value < 0:
   1.106 +				init = 0
   1.107 +			for row in range(init, self.height):
   1.108 +				new_field.append([])
   1.109 +				for col in range(self.width):
   1.110 +					new_field[row].append(self.field[row - value][col])
   1.111 +					
   1.112 +		if side == 2:
   1.113 +			self.height += value
   1.114 +			term = value
   1.115 +			if value < 0:
   1.116 +				term = 0
   1.117 +			for row in range(self.height - term):
   1.118 +				new_field.append([])
   1.119 +				for col in range(self.width):
   1.120 +					new_field[row].append(self.field[row][col])
   1.121 +			for row in range(self.height - term, self.height):
   1.122 +				new_field.append([])
   1.123 +				for col in range(self.width):
   1.124 +					new_field[row].append(self.states[0].symbol)
   1.125 +					
   1.126 +		if side == 1:
   1.127 +			self.width += value
   1.128 +			term = value
   1.129 +			if value < 0:
   1.130 +				term = 0
   1.131 +			for row in range(self.height):
   1.132 +				new_field.append([])
   1.133 +				for col in range(self.width - term):
   1.134 +					new_field[row].append(self.field[row][col])
   1.135 +			for row in range(self.height):
   1.136 +				for col in range(self.width - term, self.width):
   1.137 +					new_field[row].append(self.states[0].symbol)
   1.138 +					
   1.139 +		if side == 3:
   1.140 +			self.width += value
   1.141 +			for row in range(self.height):
   1.142 +				new_field.append([])
   1.143 +				for col in range(value):
   1.144 +					new_field[row].append(self.states[0].symbol)
   1.145 +			init = value
   1.146 +			if value < 0:
   1.147 +				init = 0
   1.148 +			for row in range(self.height):
   1.149 +				for col in range(init, self.width):
   1.150 +					new_field[row].append(self.field[row][col - value])
   1.151 +		self.field = new_field
   1.152 \ No newline at end of file