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

cca

view automata.py @ 102:a308291203ea

Extracted examples from .zip file zip gives: - smaller overall size - more work to add new examples - more work for end user - inability for VCS to track each example separately Mercurial itself does compress it's history, so, by adding one large binary file you may have even increased the repository size, not decreased it.
author Daniil Alexeyevsky <me.dendik@gmail.com>
date Wed, 15 Dec 2010 16:50:19 +0300
parents 20bf0586f8ca
children ef8839e99f34
line source
1 from state import *
3 class Automata(object):
4 def __init__(self, width=150, height=70, states=None):
5 self.width = width
6 self.height = height
7 if states == None:
8 self.states = [State("Dead", '-', "white", '0', [5]),
9 State("Alive", '+', "black", '1', [0, 1] + range(4, 9))]
10 else:
11 self.states = states
12 self.symbols = {}
13 self.st_sym = {}
14 for num, st in enumerate(self.states):
15 self.symbols[st.symbol] = num
16 self.st_sym[st.symbol] = st
17 self.field = []
18 for row in range(height):
19 self.field.append([])
20 for col in range(width):
21 self.field[row].append(self.states[0].symbol)
23 def next_step(self):
24 changed = []
25 for row in range(1, self.height - 1):
26 for col in range(1, self.width - 1):
27 symbol = self.field[row][col]
28 num = 0
29 for vert in range(row - 1, row + 2):
30 for horiz in range(col - 1, col + 2):
31 if self.field[vert][horiz] == symbol:
32 num += 1
33 if self.st_sym[symbol].next_state(num - 1):
34 changed.append((row, col))
36 for row in range(1, self.height - 1):
37 symbol1 = self.field[row][0]
38 symbol2 = self.field[row][self.width - 1]
39 num1 = 0
40 num2 = 0
41 for vert in range(row - 1, row + 2):
42 for horiz in [0, 1, self.width - 1]:
43 if self.field[vert][horiz] == symbol1:
44 num1 += 1
45 for horiz in [self.width - 2, self.width - 1, 0]:
46 if self.field[vert][horiz] == symbol2:
47 num2 += 1
48 if self.st_sym[symbol1].next_state(num1 - 1):
49 changed.append((row, 0))
50 if self.st_sym[symbol2].next_state(num2 - 1):
51 changed.append((row, self.width - 1))
53 for col in range(1, self.width - 1):
54 symbol1 = self.field[0][col]
55 symbol2 = self.field[self.height - 1][col]
56 num1 = 0
57 num2 = 0
58 for horiz in range(col - 1, col + 2):
59 for vert in [0, 1, self.height - 1]:
60 if self.field[vert][horiz] == symbol1:
61 num1 += 1
62 for vert in [self.height - 2, self.height - 1, 0]:
63 if self.field[vert][horiz] == symbol2:
64 num2 += 1
65 if self.st_sym[symbol1].next_state(num1 - 1):
66 changed.append((0, col))
67 if self.st_sym[symbol2].next_state(num2 - 1):
68 changed.append((self.height - 1, col))
70 for row, col in [(0, 0), (self.height - 1, self.width - 1),
71 (0, self.width - 1), (self.height - 1, 0)]:
72 symbol = self.field[row][col]
73 num = 0
74 for vert_long in range(row + self.height - 1,
75 row + self.height + 2):
76 for horiz_long in range(col + self.width - 1,
77 col + self.width + 2):
78 vert = vert_long % self.height
79 horiz = horiz_long % self.width
80 if self.field[vert][horiz] == symbol:
81 num += 1
82 if self.st_sym[symbol].next_state(num - 1):
83 changed.append((row, col))
85 for row, col in changed:
86 index = (self.symbols[self.field[row][col]] +
87 1) % len(self.states)
88 self.field[row][col] = self.states[index].symbol
89 return changed
91 def change_size(self, value, side):
92 "0-up, 1-right, 2-down, 3-left"
93 new_field = []
95 if side == 0:
96 self.height += value
97 for row in range(value):
98 new_field.append([])
99 for col in range(self.width):
100 new_field[row].append(self.states[0].symbol)
101 init = value
102 if value < 0:
103 init = 0
104 for row in range(init, self.height):
105 new_field.append([])
106 for col in range(self.width):
107 new_field[row].append(self.field[row - value][col])
109 if side == 2:
110 self.height += value
111 term = value
112 if value < 0:
113 term = 0
114 for row in range(self.height - term):
115 new_field.append([])
116 for col in range(self.width):
117 new_field[row].append(self.field[row][col])
118 for row in range(self.height - term, self.height):
119 new_field.append([])
120 for col in range(self.width):
121 new_field[row].append(self.states[0].symbol)
123 if side == 1:
124 self.width += value
125 term = value
126 if value < 0:
127 term = 0
128 for row in range(self.height):
129 new_field.append([])
130 for col in range(self.width - term):
131 new_field[row].append(self.field[row][col])
132 for row in range(self.height):
133 for col in range(self.width - term, self.width):
134 new_field[row].append(self.states[0].symbol)
136 if side == 3:
137 self.width += value
138 for row in range(self.height):
139 new_field.append([])
140 for col in range(value):
141 new_field[row].append(self.states[0].symbol)
142 init = value
143 if value < 0:
144 init = 0
145 for row in range(self.height):
146 for col in range(init, self.width):
147 new_field[row].append(self.field[row][col - value])
148 self.field = new_field