Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.cmm.msu.ru/trac/cca/browser/Automata.py?rev=23%3Ac74a9885bf04&format=txt
Дата изменения: Sat Dec 4 23:44:37 2010
Дата индексирования: Fri Feb 11 19:54:05 2011
Кодировка:
from State import *

class Automata(object):
#field[][] - state symbol
#states[]
#symbols = {} - symbol: number_in_states

def __init__(self, width, height, states=None):
self.width = width
self.height = height
if states == None:
self.states = [State("Dead", ' ', white, [5]),
State("Alive", '+', black, [1] + range(4, 9))]
else:
self.states = states
self.symbols = {}
for num, st in enumerate(self.states):
self.symbols[st.symbol] = num
self.field = []
for row in range(height):
self.field.append([])
for col in range(width):
self.field[row].append(states[0].symbol)

def next_step(self):
new_state = []
for row in range(self.height):
new_state.append([])
for col in range(self.width):
symbol = field[row][col]
num = 0
for vert_long in range(row + self.height - 1,
row + self.height + 2):
for horiz_long in range(col + self.width - 1,
col + self.width + 2):
vert = vert_long % self.height
horiz = horiz_long % self.width
if (vert == row) & (horiz = col): continue
if self.field[vert][horiz] == symbol:
num += 1
new_state[row].append(
self.states[self.symbols[symbol]].next_state(num))

for row in range(self.height):
for col in range(self.width):
if new_state[row][col]:
self.field[row][col] = self.states[(self.symbols[symbol]
+ 1) % len(states)].symbol

def change_size(self, value, side):
"0-up, 1-right, 2-down, 3-left"
new_field = []

if side == 0:
self.height += value
for row in range(value):
new_field.append([])
for col in range(self.width):
new_field[row].append(states[0].symbol)
init = value
if value < 0:
init = 0
for row in range(init, self.height):
new_field.append([])
for col in range(self.width):
new_field[row].append(self.field[row - value][col])

if side == 2:
self.height += value
term = -value
if value > 0:
term = 0
for row in range(self.height - term):
new_field.append([])
for col in range(self.width):
new_field[row].append(self.field[row][col])
for row in range(self.height, self.height + value):
new_field.append([])
for col in range(self.width):
new_field[row].append(states[0].symbol)

if side == 1:
self.width += value
term = -value
if value > 0:
term = 0
for row in range(self.height):
new_field.append([])
for col in range(self.width - term):
new_field[row].append(self.field[row][col])
for row in range(self.height):
for col in range(self.width, self.width + value):
new_field[row].append(states[0].symbol)

if side == 3:
self.width += value
for row in range(self.height):
new_field.append([])
for col in range(value):
new_field[row].append(states[0].symbol)
init = value
if value < 0:
init = 0
for row in range(self.height):
for col in range(init, self.width):
new_field[row].append(self.field[row][col - value])
self.field = new_field