Документ взят из кэша поисковой машины. Адрес оригинального документа : http://kodomo.fbb.msu.ru/hg/cca/file/eef739e055f4/Automata.py
Дата изменения: Unknown
Дата индексирования: Fri Feb 28 17:00:15 2014
Кодировка:
cca: eef739e055f4 Automata.py

cca

view Automata.py @ 20:eef739e055f4

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