Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/annotate/749533cdfc67/Automata.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 22:53:07 2014
Кодировка:
cca: Automata.py annotate

cca

annotate Automata.py @ 22:749533cdfc67

State: readable comments
author darkhan
date Sat, 04 Dec 2010 23:39:27 +0300
parents 38f357feb56e
children c74a9885bf04
rev   line source
darkhan@20 1 from State import *
darkhan@20 2
Ilia@1 3 class Automata(object):
darkhan@11 4 #field[][] - ?????? ?????? ?????????
Ilia@1 5 #states[]
darkhan@11 6 #symbols = {} - ??????: ?????_?_states
darkhan@11 7
darkhan@20 8 def __init__(self, width, height, states=None):
darkhan@11 9 self.width = width
darkhan@11 10 self.height = height
darkhan@20 11 if states == None:
darkhan@20 12 self.states = [State("Dead", ' ', white, [5]),
darkhan@20 13 State("Alive", '+', black, [1] + range(4, 9))]
darkhan@20 14 else:
darkhan@20 15 self.states = states
darkhan@11 16 self.symbols = {}
darkhan@11 17 for num, st in enumerate(self.states):
darkhan@11 18 self.symbols[st.symbol] = num
darkhan@11 19 self.field = []
darkhan@11 20 for row in range(height):
darkhan@11 21 self.field.append([])
darkhan@11 22 for col in range(width):
darkhan@11 23 self.field[row].append(states[0].symbol)
darkhan@11 24
darkhan@17 25 def next_step(self):
darkhan@11 26 new_state = []
darkhan@11 27 for row in range(self.height):
darkhan@11 28 new_state.append([])
darkhan@11 29 for col in range(self.width):
darkhan@11 30 symbol = field[row][col]
darkhan@11 31 num = 0
darkhan@11 32 for vert_long in range(row + self.height - 1,
darkhan@11 33 row + self.height + 2):
darkhan@11 34 for horiz_long in range(col + self.width - 1,
darkhan@11 35 col + self.width + 2):
darkhan@11 36 vert = vert_long % self.height
darkhan@11 37 horiz = horiz_long % self.width
darkhan@11 38 if (vert == row) & (horiz = col): continue
darkhan@11 39 if self.field[vert][horiz] == symbol:
darkhan@11 40 num += 1
darkhan@11 41 new_state[row].append(
darkhan@11 42 self.states[self.symbols[symbol]].next_state(num))
darkhan@11 43
darkhan@11 44 for row in range(self.height):
darkhan@11 45 for col in range(self.width):
darkhan@11 46 if new_state[row][col]:
darkhan@11 47 self.field[row][col] = self.states[(self.symbols[symbol]
darkhan@11 48 + 1) % len(states)].symbol
darkhan@11 49
darkhan@17 50 def change_size(self, value, side):
darkhan@17 51 "0-up, 1-right, 2-down, 3-left"
darkhan@17 52 new_field = []
darkhan@20 53
darkhan@17 54 if side == 0:
darkhan@17 55 self.height += value
darkhan@17 56 for row in range(value):
darkhan@17 57 new_field.append([])
darkhan@17 58 for col in range(self.width):
darkhan@17 59 new_field[row].append(states[0].symbol)
darkhan@20 60 init = value
darkhan@20 61 if value < 0:
darkhan@20 62 init = 0
darkhan@20 63 for row in range(init, self.height):
darkhan@17 64 new_field.append([])
darkhan@17 65 for col in range(self.width):
darkhan@17 66 new_field[row].append(self.field[row - value][col])
darkhan@20 67
darkhan@17 68 if side == 2:
darkhan@17 69 self.height += value
darkhan@20 70 term = -value
darkhan@20 71 if value > 0:
darkhan@20 72 term = 0
darkhan@20 73 for row in range(self.height - term):
darkhan@17 74 new_field.append([])
darkhan@17 75 for col in range(self.width):
darkhan@17 76 new_field[row].append(self.field[row][col])
darkhan@17 77 for row in range(self.height, self.height + value):
darkhan@17 78 new_field.append([])
darkhan@17 79 for col in range(self.width):
darkhan@20 80 new_field[row].append(states[0].symbol)
darkhan@20 81
darkhan@20 82 if side == 1:
darkhan@20 83 self.width += value
darkhan@20 84 term = -value
darkhan@20 85 if value > 0:
darkhan@20 86 term = 0
darkhan@20 87 for row in range(self.height):
darkhan@20 88 new_field.append([])
darkhan@20 89 for col in range(self.width - term):
darkhan@20 90 new_field[row].append(self.field[row][col])
darkhan@20 91 for row in range(self.height):
darkhan@20 92 for col in range(self.width, self.width + value):
darkhan@20 93 new_field[row].append(states[0].symbol)
darkhan@20 94
darkhan@20 95 if side == 3:
darkhan@20 96 self.width += value
darkhan@20 97 for row in range(self.height):
darkhan@20 98 new_field.append([])
darkhan@20 99 for col in range(value):
darkhan@20 100 new_field[row].append(states[0].symbol)
darkhan@20 101 init = value
darkhan@20 102 if value < 0:
darkhan@20 103 init = 0
darkhan@20 104 for row in range(self.height):
darkhan@20 105 for col in range(init, self.width):
darkhan@20 106 new_field[row].append(self.field[row][col - value])
darkhan@20 107 self.field = new_field