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
3 class Automata(object):
4 def __init__(self, width=150, height=70, states=None):
8 self.states = [State("Dead", '-', "white", '0', [5]),
9 State("Alive", '+', "black", '1', [0, 1] + range(4, 9))]
14 for num, st in enumerate(self.states):
15 self.symbols[st.symbol] = num
16 self.st_sym[st.symbol] = st
18 for row in range(height):
20 for col in range(width):
21 self.field[row].append(self.states[0].symbol)
25 for row in range(1, self.height - 1):
26 for col in range(1, self.width - 1):
27 symbol = self.field[row][col]
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:
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]
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:
45 for horiz in [self.width - 2, self.width - 1, 0]:
46 if self.field[vert][horiz] == symbol2:
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]
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:
62 for vert in [self.height - 2, self.height - 1, 0]:
63 if self.field[vert][horiz] == symbol2:
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]
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:
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]] +
88 self.field[row][col] = self.states[index].symbol
91 def change_size(self, value, side):
92 "0-up, 1-right, 2-down, 3-left"
97 for row in range(value):
99 for col in range(self.width):
100 new_field[row].append(self.states[0].symbol)
104 for row in range(init, self.height):
106 for col in range(self.width):
107 new_field[row].append(self.field[row - value][col])
114 for row in range(self.height - term):
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):
120 for col in range(self.width):
121 new_field[row].append(self.states[0].symbol)
128 for row in range(self.height):
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)
138 for row in range(self.height):
140 for col in range(value):
141 new_field[row].append(self.states[0].symbol)
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