Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/file/930e2f5f65f1/Automata.py
Дата изменения: Unknown
Дата индексирования: Sun Feb 3 09:44:11 2013
Кодировка:
cca: 930e2f5f65f1 Automata.py

cca

view Automata.py @ 66:930e2f5f65f1

correct change,delete and add of states fix #16
author is_rusinov
date Sat, 11 Dec 2010 20:59:16 +0300
parents 94406d1874a2
children c4a108b8ea02
line source
1 from State import *
3 class Automata(object):
4 #field[][] - state symbol
5 #states[]
6 #symbols = {} - symbol: number_in_states
8 def __init__(self, width=50, height=50, states=None):
9 self.width = width
10 self.height = height
11 if states == None:
12 self.states = [State("Dead", '-', "white", [5]),
13 State("Alive", '+', "black", [0, 1] + range(4, 9))]
14 else:
15 self.states = states
16 self.symbols = {}
17 self.st_sym = {}
18 for num, st in enumerate(self.states):
19 self.symbols[st.symbol] = num
20 self.st_sym[st.symbol] = st
21 self.field = []
22 for row in range(height):
23 self.field.append([])
24 for col in range(width):
25 self.field[row].append(self.states[0].symbol)
27 def next_step(self):
28 changed = []
29 for row in range(1, self.height - 1):
30 for col in range(1, self.width - 1):
31 symbol = self.field[row][col]
32 num = 0
33 for vert in range(row - 1, row + 2):
34 for horiz in range(col - 1, col + 2):
35 if self.field[vert][horiz] == symbol:
36 num += 1
37 if self.st_sym[symbol].next_state(num - 1):
38 changed.append((row, col))
40 for row in range(1, self.height - 1):
41 symbol1 = self.field[row][0]
42 symbol2 = self.field[row][self.width - 1]
43 num1 = 0
44 num2 = 0
45 for vert in range(row - 1, row + 2):
46 for horiz in [0, 1, self.width - 1]:
47 if self.field[vert][horiz] == symbol1:
48 num1 += 1
49 for horiz in [self.width - 2, self.width - 1, 0]:
50 if self.field[vert][horiz] == symbol2:
51 num2 += 1
52 if self.st_sym[symbol1].next_state(num1 - 1):
53 changed.append((row, 0))
54 if self.st_sym[symbol2].next_state(num2 - 1):
55 changed.append((row, self.width - 1))
57 for col in range(1, self.width - 1):
58 symbol1 = self.field[0][col]
59 symbol2 = self.field[self.height - 1][col]
60 num1 = 0
61 num2 = 0
62 for horiz in range(col - 1, col + 2):
63 for vert in [0, 1, self.height - 1]:
64 if self.field[vert][horiz] == symbol1:
65 num1 += 1
66 for vert in [self.height - 2, self.height - 1, 0]:
67 if self.field[vert][horiz] == symbol2:
68 num2 += 1
69 if self.st_sym[symbol1].next_state(num1 - 1):
70 changed.append((0, col))
71 if self.st_sym[symbol2].next_state(num2 - 1):
72 changed.append((self.height - 1, col))
74 for row, col in [(0, 0), (self.height - 1, self.width - 1),
75 (0, self.width - 1), (self.height - 1, 0)]:
76 symbol = self.field[row][col]
77 num = 0
78 for vert_long in range(row + self.height - 1,
79 row + self.height + 2):
80 for horiz_long in range(col + self.width - 1,
81 col + self.width + 2):
82 vert = vert_long % self.height
83 horiz = horiz_long % self.width
84 if self.field[vert][horiz] == symbol:
85 num += 1
86 if self.st_sym[symbol].next_state(num - 1):
87 changed.append((row, col))
89 for row, col in changed:
90 index = (self.symbols[self.field[row][col]] +
91 1) % len(self.states)
92 self.field[row][col] = self.states[index].symbol
93 return changed
95 def change_size(self, value, side):
96 "0-up, 1-right, 2-down, 3-left"
97 new_field = []
99 if side == 0:
100 self.height += value
101 for row in range(value):
102 new_field.append([])
103 for col in range(self.width):
104 new_field[row].append(states[0].symbol)
105 init = value
106 if value < 0:
107 init = 0
108 for row in range(init, self.height):
109 new_field.append([])
110 for col in range(self.width):
111 new_field[row].append(self.field[row - value][col])
113 if side == 2:
114 self.height += value
115 term = -value
116 if value > 0:
117 term = 0
118 for row in range(self.height - term):
119 new_field.append([])
120 for col in range(self.width):
121 new_field[row].append(self.field[row][col])
122 for row in range(self.height, self.height + value):
123 new_field.append([])
124 for col in range(self.width):
125 new_field[row].append(states[0].symbol)
127 if side == 1:
128 self.width += value
129 term = -value
130 if value > 0:
131 term = 0
132 for row in range(self.height):
133 new_field.append([])
134 for col in range(self.width - term):
135 new_field[row].append(self.field[row][col])
136 for row in range(self.height):
137 for col in range(self.width, self.width + value):
138 new_field[row].append(states[0].symbol)
140 if side == 3:
141 self.width += value
142 for row in range(self.height):
143 new_field.append([])
144 for col in range(value):
145 new_field[row].append(states[0].symbol)
146 init = value
147 if value < 0:
148 init = 0
149 for row in range(self.height):
150 for col in range(init, self.width):
151 new_field[row].append(self.field[row][col - value])
152 self.field = new_field